How to use the shapely.geometry.mapping function in shapely

To help you get started, we’ve selected a few shapely examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opendatacube / datacube-core / utils / s2prepare.py View on Github external
geom = shape.convex_hull

    # buffer by 1 pixel
    geom = geom.buffer(1, join_style=3, cap_style=3)

    # simplify with 1 pixel radius
    geom = geom.simplify(1)

    # intersect with image bounding box
    geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))

    # transform from pixel space into CRS space
    geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,
                                                    transform.e, transform.xoff, transform.yoff))

    output = shapely.geometry.mapping(geom)

    return geom
    # output['coordinates'] = _to_lists(output['coordinates'])
github agile-geoscience / geotransect / sgy2shp.py View on Github external
x = x / 100.0
                            y = y / 100.0
                        else:
                            log.info('Found weird coords: dividing by 10')
                            x = x / 10.0
                            y = y / 10.0
                    else:
                        pass

                    if convert:
                        log.info("Converting from NAD27 to NAD83")
                        x, y = pp.transform(utm_nad27, utm_nad83, x, y)

                    p = Point(x, y)
                    points.append(p)
                    trace_out.write({'geometry': mapping(p),
                                     'properties': {'line': filebase,
                                                    'segyfile': path,
                                                    'trace': i}
                                     })

            # We need this to plot seismic lines on the map.
            linestring = LineString(points)
            line_out.write({'geometry': mapping(linestring),
                            'properties': {'segyfile': path,
                                           'line': filebase}
                            })
github MPAS-Dev / geometric_features / fix_features_at_prime_meridian.py View on Github external
(-180., 90.),
                                         (-180., -90.)])

    print "This feature crosses the prime merdian."
    print "  bounds before split:", featureShape.bounds

    westShape = featureShape.difference(eastMask)
    print "  bounds of western half:", westShape.bounds
    eastShape = featureShape.difference(westMask)
    print "  bounds of eastern half:", eastShape.bounds

    outShape = shapely.geometry.MultiPolygon([westShape,eastShape])

    print "  bounds after split:", outShape.bounds

    return shapely.geometry.mapping(outShape)
github opendatacube / datacube-core / utils / USGS_precollection_oldscripts / usgslsprepare.py View on Github external
geom = shape.convex_hull

    # buffer by 1 pixel
    geom = geom.buffer(1, join_style=3, cap_style=3)

    # simplify with 1 pixel radius
    geom = geom.simplify(1)

    # intersect with image bounding box
    geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))

    # transform from pixel space into CRS space
    geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,
                                                    transform.e, transform.xoff, transform.yoff))

    output = shapely.geometry.mapping(geom)
    output['coordinates'] = _to_lists(output['coordinates'])
    return output
github ekansa / open-context-py / opencontext_py / libs / clustergeojson.py View on Github external
def get_feature_centroid(self, feature_geometry):
        """Gets the centroid from a feature's geometry"""
        if feature_geometry['type'].lower() == 'point':
            return feature_geometry['coordinates']
        else:
            geom = shape(feature_geometry)
            g_centroid = geom.centroid
            centroid = mapping(g_centroid)
            return centroid['coordinates']
github jaantollander / crowddynamics / crowddynamics / io.py View on Github external
def geometry_mapping(geom):
    from shapely.geometry import mapping

    if isinstance(geom, Iterable):
        return [mapping(geo) for geo in geom]
    elif isinstance(geom, Mapping):
        return {name: mapping(geo) for name, geo in geom.items()}
    else:
        return mapping(geom) if geom else geom
github earthlab / earthpy / earthpy / spatial.py View on Github external
for the extent
        A GeoJSON style dictionary of corner coordinates representing
        the spatial extent of the provided spatial object.

    Example
    -------
    >>> import geopandas as gpd
    >>> import earthpy.spatial as es
    >>> from earthpy.io import path_to_example
    >>> rmnp = gpd.read_file(path_to_example('rmnp.shp'))
    >>> es.extent_to_json(rmnp)
    {'type': 'Polygon', 'coordinates': (((-105.4935937, 40.1580827), ...),)}
    """

    if type(ext_obj) == gpd.geodataframe.GeoDataFrame:
        extent_json = mapping(box(*ext_obj.total_bounds))
    elif type(ext_obj) == list:
        assert ext_obj[0] <= ext_obj[2], "xmin must be <= xmax"
        assert ext_obj[1] <= ext_obj[3], "ymin must be <= ymax"
        extent_json = mapping(box(*ext_obj))
    else:
        raise ValueError("Please provide a GeoDataFrame or a list of values.")

    return extent_json
github e2nIEE / pandapower / pandapower / io_utils.py View on Github external
def json_linestring(obj):
        logger.debug("shapely linestring")
        json_string = shapely.geometry.mapping(obj)
        d = with_signature(obj, json_string, obj_module="shapely")
        return d
github nismod / digital_comms / scripts / fixed_cluster_input_files.py View on Github external
print('generate_exchange_area - Process removed islands')
        idx_exchange_areas = index.Index()
        for idx, exchange_area in enumerate(exchange_areas):
            idx_exchange_areas.insert(idx, shape(exchange_area['geometry']).bounds, exchange_area)
        for island in removed_islands:
            intersections = [n for n in idx_exchange_areas.intersection((island.bounds), objects=True)]

            if len(intersections) > 0:
                for idx, intersection in enumerate(intersections):
                    if idx == 0:
                        merge_with = intersection
                    elif shape(intersection.object['geometry']).intersection(island).length > shape(merge_with.object['geometry']).intersection(island).length:
                        merge_with = intersection

                merged_geom = merge_with.object
                merged_geom['geometry'] = mapping(shape(merged_geom['geometry']).union(island))
                idx_exchange_areas.delete(merge_with.id, shape(merge_with.object['geometry']).bounds)
                idx_exchange_areas.insert(merge_with.id, shape(merged_geom['geometry']).bounds, merged_geom)

        exchange_areas = [n.object for n in idx_exchange_areas.intersection(idx_exchange_areas.bounds, objects=True)]

    return exchange_areas