1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
"""Geometry factories based on the geo interface."""
import numpy as np
from shapely.errors import GeometryTypeError
from shapely.geometry.collection import GeometryCollection
from shapely.geometry.linestring import LineString
from shapely.geometry.multilinestring import MultiLineString
from shapely.geometry.multipoint import MultiPoint
from shapely.geometry.multipolygon import MultiPolygon
from shapely.geometry.point import Point
from shapely.geometry.polygon import LinearRing, Polygon
def _is_coordinates_empty(coordinates):
"""Identify if coordinates or subset of coordinates are empty."""
if coordinates is None:
return True
if isinstance(coordinates, (list, tuple, np.ndarray)):
if len(coordinates) == 0:
return True
return all(map(_is_coordinates_empty, coordinates))
else:
return False
def _empty_shape_for_no_coordinates(geom_type):
"""Return empty counterpart for geom_type."""
if geom_type == "point":
return Point()
elif geom_type == "multipoint":
return MultiPoint()
elif geom_type == "linestring":
return LineString()
elif geom_type == "multilinestring":
return MultiLineString()
elif geom_type == "polygon":
return Polygon()
elif geom_type == "multipolygon":
return MultiPolygon()
else:
raise GeometryTypeError(f"Unknown geometry type: {geom_type!r}")
def box(minx, miny, maxx, maxy, ccw=True):
"""Return a rectangular polygon with configurable normal vector."""
coords = [(maxx, miny), (maxx, maxy), (minx, maxy), (minx, miny)]
if not ccw:
coords = coords[::-1]
return Polygon(coords)
def shape(context):
"""Return a new, independent geometry with coordinates copied from the context.
Changes to the original context will not be reflected in the geometry
object.
Parameters
----------
context :
a GeoJSON-like dict, which provides a "type" member describing the type
of the geometry and "coordinates" member providing a list of coordinates,
or an object which implements __geo_interface__.
Returns
-------
Geometry object
Examples
--------
Create a Point from GeoJSON, and then create a copy using __geo_interface__.
>>> from shapely.geometry import shape
>>> context = {'type': 'Point', 'coordinates': [0, 1]}
>>> geom = shape(context)
>>> geom.geom_type == 'Point'
True
>>> geom.wkt
'POINT (0 1)'
>>> geom2 = shape(geom)
>>> geom == geom2
True
"""
if hasattr(context, "__geo_interface__"):
ob = context.__geo_interface__
else:
ob = context
geom_type = ob.get("type").lower()
if geom_type == "feature":
# GeoJSON features must have a 'geometry' field.
ob = ob["geometry"]
geom_type = ob.get("type").lower()
if "coordinates" in ob and _is_coordinates_empty(ob["coordinates"]):
return _empty_shape_for_no_coordinates(geom_type)
elif geom_type == "point":
return Point(ob["coordinates"])
elif geom_type == "linestring":
return LineString(ob["coordinates"])
elif geom_type == "linearring":
return LinearRing(ob["coordinates"])
elif geom_type == "polygon":
return Polygon(ob["coordinates"][0], ob["coordinates"][1:])
elif geom_type == "multipoint":
return MultiPoint(ob["coordinates"])
elif geom_type == "multilinestring":
return MultiLineString(ob["coordinates"])
elif geom_type == "multipolygon":
return MultiPolygon([[c[0], c[1:]] for c in ob["coordinates"]])
elif geom_type == "geometrycollection":
geoms = [shape(g) for g in ob.get("geometries", [])]
return GeometryCollection(geoms)
else:
raise GeometryTypeError(f"Unknown geometry type: {geom_type!r}")
def mapping(ob):
"""Return a GeoJSON-like mapping.
Input should be a Geometry or an object which implements __geo_interface__.
Parameters
----------
ob : geometry or object
An object which implements __geo_interface__.
Returns
-------
dict
Examples
--------
>>> from shapely.geometry import mapping, Point
>>> pt = Point(0, 0)
>>> mapping(pt)
{'type': 'Point', 'coordinates': (0.0, 0.0)}
"""
return ob.__geo_interface__
|