Exemple #1
0
def wrap_polylines(polylines, lon0=0, tesselate_degrees=1):

    data = {"type": "FeatureCollection"}
    data["features"] = []
    for polyline in polylines:

        if lon0 is not None:
            wrapper = pygplates.DateLineWrapper(lon0)
            geometries = wrapper.wrap(polyline.get_geometry(),
                                      tesselate_degrees)
        else:
            geometries = polyline.get_geometries()

        for geometry in geometries:
            feature = {"type": "Feature"}
            feature["geometry"] = {}
            feature["geometry"]["type"] = "MultiLineString"
            point_list = []
            for point in geometry.get_points():
                point_list.append(
                    (point.to_lat_lon()[1], point.to_lat_lon()[0]))
            feature["geometry"]["coordinates"] = [point_list]
            data["features"].append(feature)

    return data
Exemple #2
0
def wrap_plate_boundaries(shared_boundary_sections,
                          lon0=0,
                          tesselate_degrees=1):

    data = {"type": "FeatureCollection"}
    data["features"] = []
    for shared_boundary_section in shared_boundary_sections:
        for shared_sub_segment in shared_boundary_section.get_shared_sub_segments(
        ):

            if lon0 is not None:
                wrapper = pygplates.DateLineWrapper(lon0)
                geometries = wrapper.wrap(shared_sub_segment.get_geometry(),
                                          tesselate_degrees)
            else:
                geometries = shared_sub_segment.get_geometries()

            for geometry in geometries:
                feature = {"type": "Feature"}
                feature["geometry"] = {}
                feature["geometry"]["type"] = "MultiLineString"
                point_list = []
                for point in geometry.get_points():
                    point_list.append(
                        (point.to_lat_lon()[1], point.to_lat_lon()[0]))
                feature["geometry"]["coordinates"] = [point_list]
                feature["feature_type"] = str(
                    shared_sub_segment.get_feature().get_feature_type())
                feature["Length"] = float(
                    shared_sub_segment.get_geometry().get_arc_length())
                data["features"].append(feature)

    return data
Exemple #3
0
def wrap_reconstructed_polygons(reconstructed_polygons,
                                lon0=0,
                                tesselate_degrees=1):

    data = {"type": "FeatureCollection"}
    data["features"] = []
    for reconstructed_polygon in reconstructed_polygons:
        rev = False
        print(reconstructed_polygon.get_reconstructed_geometry().
              get_orientation())
        if reconstructed_polygon.get_reconstructed_geometry().get_orientation(
        ) == pygplates.PolygonOnSphere.Orientation.counter_clockwise:
            print('hello')
            rev = True

        if lon0 is not None:
            wrapper = pygplates.DateLineWrapper(lon0)
            geometries = wrapper.wrap(
                reconstructed_polygon.get_reconstructed_geometry(),
                tesselate_degrees)

            for geometry in geometries:
                feature = {"type": "Feature"}
                feature["geometry"] = {}
                feature["geometry"]["type"] = "Polygon"
                point_list = []
                for point in geometry.get_exterior_points():
                    point_list.append(
                        (point.to_lat_lon()[1], point.to_lat_lon()[0]))
                if rev:
                    point_list.reverse()
                feature["geometry"]["coordinates"] = [point_list]
                data["features"].append(feature)

        else:
            geometry = reconstructed_polygon.get_reconstructed_geometry()
            feature = {"type": "Feature"}
            feature["geometry"] = {}
            feature["geometry"]["type"] = "Polygon"
            point_list = []
            for point in geometry.get_points():
                point_list.append(
                    (point.to_lat_lon()[1], point.to_lat_lon()[0]))
            if rev:
                point_list.reverse()
            feature["geometry"]["coordinates"] = [point_list]
            data["features"].append(feature)

    return data
Exemple #4
0
def plot_velocities_and_topologies(pmap,
                                   topology_features,
                                   rotation_model,
                                   time,
                                   delta_time=1,
                                   res=10,
                                   scale=2000,
                                   lon0=0,
                                   clip_path=None,
                                   alpha=0.2):

    Xnodes = np.arange(-180, 180, res)
    Ynodes = np.arange(-90, 90, res)
    Xg, Yg = np.meshgrid(Xnodes, Ynodes)

    velocity_domain_features = make_GPML_velocity_feature(
        Xg.flatten(), Yg.flatten())

    # Call the function we created above to get the velocities
    all_velocities = get_plate_velocities(velocity_domain_features,
                                          topology_features, rotation_model,
                                          time, delta_time, 'vector_comp')

    # The rest of the cell is for plotting, including rendering resolved topological boundaries to the map
    pt_vel_n = []
    pt_vel_e = []
    for vel in all_velocities:
        pt_vel_e.append(vel.get_y())
        pt_vel_n.append(vel.get_x())

    u = np.asarray(pt_vel_e).reshape((Ynodes.shape[0], Xnodes.shape[0]))
    v = np.asarray(pt_vel_n).reshape((Ynodes.shape[0], Xnodes.shape[0]))

    # Resolve our topological plate polygons (and deforming networks) to the current 'time'.
    # We generate both the resolved topology boundaries and the boundary sections between them.
    resolved_topologies = []
    shared_boundary_sections = []
    pygplates.resolve_topologies(topology_features, rotation_model,
                                 resolved_topologies, time,
                                 shared_boundary_sections)

    # create a dateline wrapper object
    wrapper = pygplates.DateLineWrapper(lon0)

    # Iterate over the shared boundary sections.
    for shared_boundary_section in shared_boundary_sections:

        # The shared sub-segments contribute either to the ridges or to the subduction zones.
        if shared_boundary_section.get_feature().get_feature_type(
        ) == pygplates.FeatureType.create_gpml('MidOceanRidge'):
            # Ignore zero length segments - they don't have a direction.
            for shared_sub_segment in shared_boundary_section.get_shared_sub_segments(
            ):
                split_geometry = wrapper.wrap(
                    shared_sub_segment.get_geometry())
                for geometry in split_geometry:
                    X = []
                    Y = []
                    for point in geometry.get_points():
                        X.append(point.get_longitude()), Y.append(
                            point.get_latitude())
                    x, y = pmap(X, Y)
                    pmap.plot(x,
                              y,
                              'darkturquoise',
                              clip_path=clip_path,
                              linewidth=2,
                              alpha=0.5,
                              zorder=1)

        elif shared_boundary_section.get_feature().get_feature_type(
        ) == pygplates.FeatureType.create_gpml('SubductionZone'):
            for shared_sub_segment in shared_boundary_section.get_shared_sub_segments(
            ):
                split_geometry = wrapper.wrap(
                    shared_sub_segment.get_geometry())
                for geometry in split_geometry:
                    X = []
                    Y = []
                    for point in geometry.get_points():
                        X.append(point.get_longitude()), Y.append(
                            point.get_latitude())
                    x, y = pmap(X, Y)
                pmap.plot(x,
                          y,
                          'k',
                          clip_path=clip_path,
                          linewidth=2,
                          alpha=alpha,
                          zorder=1)

        else:  #shared_boundary_section.get_feature().get_feature_type() == pygplates.FeatureType.create_gpml('FractureZone'):
            for shared_sub_segment in shared_boundary_section.get_shared_sub_segments(
            ):
                split_geometry = wrapper.wrap(
                    shared_sub_segment.get_geometry())
                for geometry in split_geometry:
                    X = []
                    Y = []
                    for point in geometry.get_points():
                        X.append(point.get_longitude()), Y.append(
                            point.get_latitude())
                    x, y = pmap(X, Y)
                pmap.plot(x,
                          y,
                          'darkorange',
                          clip_path=clip_path,
                          linewidth=2,
                          alpha=0.6,
                          zorder=1)

    lons, lats = np.meshgrid(Xnodes, Ynodes)
    # compute native x,y coordinates of grid.
    x, y = pmap(lons, lats)
    # define parallels and meridians to draw.

    uproj,vproj,xx,yy = \
    pmap.transform_vector(u,v,Xnodes,Ynodes,54,26,returnxy=True,masked=True)
    # now plot.
    Q = pmap.quiver(xx,
                    yy,
                    uproj,
                    vproj,
                    scale=scale,
                    clip_path=clip_path,
                    alpha=alpha)
Exemple #5
0
def resolve_topologies(rotation_model, topological_features,
                       reconstruction_time, output_filename_prefix,
                       output_filename_extension, anchor_plate_id):

    # FIXME: Temporary fix to avoid getting OGR GMT/Shapefile error "Mismatch in field names..." and
    # missing geometries when saving resolved topologies/sections to GMT/Shapefile.
    # It's caused by the OGR writer inside pyglates trying to write out features with different
    # shapefiles attribute field (key) names to the same file. We get around this by removing
    # all shapefile attributes.
    topological_features = pygplates.FeaturesFunctionArgument(
        topological_features).get_features()
    for topological_feature in topological_features:
        topological_feature.remove(
            pygplates.PropertyName.gpml_shapefile_attributes)

    # Resolve our topological plate polygons (and deforming networks) to the current 'reconstruction_time'.
    # We generate both the resolved topology boundaries and the boundary sections between them.
    resolved_topologies = []
    shared_boundary_sections = []
    pygplates.resolve_topologies(topological_features, rotation_model,
                                 resolved_topologies, reconstruction_time,
                                 shared_boundary_sections, anchor_plate_id)

    # We'll create a feature for each boundary polygon feature and each type of
    # resolved topological section feature we find.
    resolved_topology_features = []
    ridge_transform_boundary_section_features = []
    subduction_boundary_section_features = []
    left_subduction_boundary_section_features = []
    right_subduction_boundary_section_features = []

    wrapper = pygplates.DateLineWrapper(120.)

    # Iterate over the resolved topologies.
    for resolved_topology in resolved_topologies:
        split_geometry = wrapper.wrap(
            resolved_topology.get_resolved_feature().get_geometry())
        for geometry in split_geometry:
            feature = pygplates.Feature()
            feature.set_geometry(geometry)
            resolved_topology_features.append(feature)

    # Iterate over the shared boundary sections.
    for shared_boundary_section in shared_boundary_sections:

        # Get all the geometries of the current boundary section.
        boundary_section_features = [
            shared_sub_segment.get_resolved_feature() for shared_sub_segment in
            shared_boundary_section.get_shared_sub_segments()
        ]

        # Add the feature to the correct list depending on feature type, etc.
        if shared_boundary_section.get_feature().get_feature_type(
        ) == pygplates.FeatureType.create_gpml('SubductionZone'):

            # Put all subduction zones in one collection/file.
            subduction_boundary_section_features.extend(
                boundary_section_features)

            # Also put subduction zones in left/right collection/file.
            polarity_property = shared_boundary_section.get_feature().get(
                pygplates.PropertyName.create_gpml('subductionPolarity'))
            if polarity_property:
                polarity = polarity_property.get_value().get_content()
                if polarity == 'Left':
                    left_subduction_boundary_section_features.extend(
                        boundary_section_features)
                elif polarity == 'Right':
                    right_subduction_boundary_section_features.extend(
                        boundary_section_features)
        else:
            # Put all ridges in one collection/file.
            ridge_transform_boundary_section_features.extend(
                boundary_section_features)

    if resolved_topology_features:
        # Put the features in a feature collection so we can write them to a file.
        resolved_topology_feature_collection = pygplates.FeatureCollection(
            resolved_topology_features)
        resolved_topology_features_filename = '{0}boundary_polygons_{1:0.2f}Ma.{2}'.format(
            output_filename_prefix, reconstruction_time,
            output_filename_extension)
        resolved_topology_feature_collection.write(
            resolved_topology_features_filename)

    if ridge_transform_boundary_section_features:
        # Put the features in a feature collection so we can write them to a file.
        ridge_transform_boundary_section_feature_collection = pygplates.FeatureCollection(
            ridge_transform_boundary_section_features)
        ridge_transform_boundary_section_features_filename = '{0}ridge_transform_boundaries_{1:0.2f}Ma.{2}'.format(
            output_filename_prefix, reconstruction_time,
            output_filename_extension)
        ridge_transform_boundary_section_feature_collection.write(
            ridge_transform_boundary_section_features_filename)

    if subduction_boundary_section_features:
        # Put the features in a feature collection so we can write them to a file.
        subduction_boundary_section_feature_collection = pygplates.FeatureCollection(
            subduction_boundary_section_features)
        subduction_boundary_section_features_filename = '{0}subduction_boundaries_{1:0.2f}Ma.{2}'.format(
            output_filename_prefix, reconstruction_time,
            output_filename_extension)
        subduction_boundary_section_feature_collection.write(
            subduction_boundary_section_features_filename)

    if left_subduction_boundary_section_features:
        # Put the features in a feature collection so we can write them to a file.
        left_subduction_boundary_section_feature_collection = pygplates.FeatureCollection(
            left_subduction_boundary_section_features)
        left_subduction_boundary_section_features_filename = '{0}subduction_boundaries_sL_{1:0.2f}Ma.{2}'.format(
            output_filename_prefix, reconstruction_time,
            output_filename_extension)
        left_subduction_boundary_section_feature_collection.write(
            left_subduction_boundary_section_features_filename)

    if right_subduction_boundary_section_features:
        # Put the features in a feature collection so we can write them to a file.
        right_subduction_boundary_section_feature_collection = pygplates.FeatureCollection(
            right_subduction_boundary_section_features)
        right_subduction_boundary_section_features_filename = '{0}subduction_boundaries_sR_{1:0.2f}Ma.{2}'.format(
            output_filename_prefix, reconstruction_time,
            output_filename_extension)
        right_subduction_boundary_section_feature_collection.write(
            right_subduction_boundary_section_features_filename)
Exemple #6
0
def wrap_resolved_polygons(resolved_polygons,
                           wrap=False,
                           central_meridian=0,
                           avoid_map_boundary=False,
                           tesselate_degrees=2):

    polygons = []
    if wrap:
        wrapped_polygons = []
        date_line_wrapper = pygplates.DateLineWrapper(central_meridian)
        for p in resolved_polygons:
            wrapped_polygons += date_line_wrapper.wrap(p.get_geometry(),
                                                       tesselate_degrees)
        for p in wrapped_polygons:
            lats = [i.get_latitude() for i in p.get_exterior_points()]
            lons = [i.get_longitude() for i in p.get_exterior_points()]
            if pygplates.PolygonOnSphere(list(zip(
                    lats, lons))).get_orientation(
                    ) == pygplates.PolygonOnSphere.Orientation.clockwise:
                polygons.append((lons, lats))
            else:
                polygons.append((lons[::-1], lats[::-1]))
    else:
        for p in resolved_polygons:
            lats, lons = list(zip(*p.get_geometry().to_lat_lon_list()))
            lats = list(lats)
            lons = list(lons)
            if pygplates.PolygonOnSphere(list(zip(
                    lats, lons))).get_orientation(
                    ) == pygplates.PolygonOnSphere.Orientation.clockwise:
                polygons.append((lons, lats))
            else:
                polygons.append((lons[::-1], lats[::-1]))

    data = {"type": "FeatureCollection"}
    data["features"] = []
    for p in polygons:
        feature = {"type": "Feature"}
        feature["geometry"] = {}
        feature["geometry"]["type"] = "Polygon"

        #make sure the coordinates are valid.
        lats = p[1]
        lons = p[0]
        #print lats, lons
        #some map plotting program(such as leaflet) does not like points on the map boundary,
        #for example the longitude 180 and -180.
        #So, move the points slightly inwards.
        if avoid_map_boundary:
            for idx, x in enumerate(lons):
                if x < central_meridian - 179.99:
                    lons[idx] = central_meridian - 179.99
                elif x > central_meridian + 179.99:
                    lons[idx] = central_meridian + 179.99
            for idx, x in enumerate(lats):
                if x < -89.99:
                    lats[idx] = -89.99
                elif x > 89.99:
                    lats[idx] = 89.99

        feature["geometry"]["coordinates"] = [
            list(zip(lons + lons[:1], lats + lats[:1]))
        ]
        data["features"].append(feature)

    return data