Encapsulate an OGR geometry.
| 63 | # |
| 64 | # The OGR_G_* routines are relevant here. |
| 65 | class OGRGeometry(GDALBase): |
| 66 | """Encapsulate an OGR geometry.""" |
| 67 | |
| 68 | destructor = capi.destroy_geom |
| 69 | geos_support = True |
| 70 | |
| 71 | def __init__(self, geom_input, srs=None): |
| 72 | """Initialize Geometry on either WKT or an OGR pointer as input.""" |
| 73 | str_instance = isinstance(geom_input, str) |
| 74 | |
| 75 | # If HEX, unpack input to a binary buffer. |
| 76 | if str_instance and hex_regex.match(geom_input): |
| 77 | geom_input = memoryview(bytes.fromhex(geom_input)) |
| 78 | str_instance = False |
| 79 | |
| 80 | # Constructing the geometry, |
| 81 | if str_instance: |
| 82 | wkt_m = wkt_regex.match(geom_input) |
| 83 | json_m = json_regex.match(geom_input) |
| 84 | if wkt_m: |
| 85 | if wkt_m["srid"]: |
| 86 | # If there's EWKT, set the SRS w/value of the SRID. |
| 87 | srs = int(wkt_m["srid"]) |
| 88 | if wkt_m["type"].upper() == "LINEARRING": |
| 89 | # OGR_G_CreateFromWkt doesn't work with LINEARRING WKT. |
| 90 | # See https://trac.osgeo.org/gdal/ticket/1992. |
| 91 | g = capi.create_geom(OGRGeomType(wkt_m["type"]).num) |
| 92 | capi.import_wkt(g, byref(c_char_p(wkt_m["wkt"].encode()))) |
| 93 | else: |
| 94 | g = capi.from_wkt( |
| 95 | byref(c_char_p(wkt_m["wkt"].encode())), None, byref(c_void_p()) |
| 96 | ) |
| 97 | elif json_m: |
| 98 | g = self._from_json(geom_input.encode()) |
| 99 | else: |
| 100 | # Seeing if the input is a valid short-hand string |
| 101 | # (e.g., 'Point', 'POLYGON'). |
| 102 | OGRGeomType(geom_input) |
| 103 | g = capi.create_geom(OGRGeomType(geom_input).num) |
| 104 | elif isinstance(geom_input, memoryview): |
| 105 | # WKB was passed in |
| 106 | g = self._from_wkb(geom_input) |
| 107 | elif isinstance(geom_input, OGRGeomType): |
| 108 | # OGRGeomType was passed in, an empty geometry will be created. |
| 109 | g = capi.create_geom(geom_input.num) |
| 110 | elif isinstance(geom_input, self.ptr_type): |
| 111 | # OGR pointer (c_void_p) was the input. |
| 112 | g = geom_input |
| 113 | else: |
| 114 | raise GDALException( |
| 115 | "Invalid input type for OGR Geometry construction: %s" |
| 116 | % type(geom_input) |
| 117 | ) |
| 118 | |
| 119 | # Now checking the Geometry pointer before finishing initialization |
| 120 | # by setting the pointer for the object. |
| 121 | if not g: |
| 122 | raise GDALException( |
no outgoing calls