def ogr(self): "Returns the OGR Geometry for this Geometry." if self.srid: try: return gdal.OGRGeometry(self.wkb, self.srid) except gdal.SRSException: pass return gdal.OGRGeometry(self.wkb)
def ogr(self): "Returns the OGR Geometry for this Geometry." if not gdal.HAS_GDAL: raise GEOSException('GDAL required to convert to an OGRGeometry.') if self.srid: try: return gdal.OGRGeometry(self.wkb, self.srid) except gdal.SRSException: pass return gdal.OGRGeometry(self.wkb)
def __init__(self, geo_input, srid=None): """ The base constructor for GEOS geometry objects, and may take the following inputs: * strings: - WKT - HEXEWKB (a PostGIS-specific canonical form) - GeoJSON (requires GDAL) * buffer: - WKB The `srid` keyword is used to specify the Source Reference Identifier (SRID) number for this Geometry. If not set, the SRID will be None. """ input_srid = None if isinstance(geo_input, bytes): geo_input = force_text(geo_input) if isinstance(geo_input, str): wkt_m = wkt_regex.match(geo_input) if wkt_m: # Handling WKT input. if wkt_m.group('srid'): input_srid = int(wkt_m.group('srid')) g = wkt_r().read(force_bytes(wkt_m.group('wkt'))) elif hex_regex.match(geo_input): # Handling HEXEWKB input. g = wkb_r().read(force_bytes(geo_input)) elif json_regex.match(geo_input): # Handling GeoJSON input. g = wkb_r().read(gdal.OGRGeometry(geo_input).wkb) else: raise ValueError( 'String input unrecognized as WKT EWKT, and HEXEWKB.') elif isinstance(geo_input, GEOM_PTR): # When the input is a pointer to a geometry (GEOM_PTR). g = geo_input elif isinstance(geo_input, memoryview): # When the input is a buffer (WKB). g = wkb_r().read(geo_input) elif isinstance(geo_input, GEOSGeometry): g = capi.geom_clone(geo_input.ptr) else: # Invalid geometry type. raise TypeError('Improper geometry input type: %s' % type(geo_input)) if not g: raise GEOSException( 'Could not initialize GEOS Geometry with given input.') input_srid = input_srid or capi.geos_get_srid(g) or None if input_srid and srid and input_srid != srid: raise ValueError('Input geometry already has SRID: %d.' % input_srid) # Setting the pointer object with a valid pointer. self.ptr = g # Post-initialization setup. self._post_init(input_srid or srid)
def convert_from_gk25(north, east): ps = "POINT (%f %f)" % (east, north) g = gdal.OGRGeometry(ps, GK25_SRS) if coord_transform: g.transform(coord_transform) return g pnt = Point(east, north, srid=GK25_SRID) if PROJECTION_SRID == GK25_SRID: return pnt pnt.transform(coord_transform) return pnt
def transform(self, ct, clone=False): """ Requires GDAL. Transforms the geometry according to the given transformation object, which may be an integer SRID, and WKT or PROJ.4 string. By default, the geometry is transformed in-place and nothing is returned. However if the `clone` keyword is set, then this geometry will not be modified and a transformed clone will be returned instead. """ srid = self.srid if ct == srid: # short-circuit where source & dest SRIDs match if clone: return self.clone() else: return if not gdal.HAS_GDAL: raise GEOSException( "GDAL library is not available to transform() geometry.") if isinstance(ct, gdal.CoordTransform): # We don't care about SRID because CoordTransform presupposes # source SRS. srid = None elif srid is None or srid < 0: raise GEOSException( "Calling transform() with no SRID set is not supported") # Creating an OGR Geometry, which is then transformed. g = gdal.OGRGeometry(self.wkb, srid) g.transform(ct) # Getting a new GEOS pointer ptr = wkb_r().read(g.wkb) if clone: # User wants a cloned transformed geometry returned. return GEOSGeometry(ptr, srid=g.srid) if ptr: # Reassigning pointer, and performing post-initialization setup # again due to the reassignment. capi.destroy_geom(self.ptr) self.ptr = ptr self._post_init(g.srid) else: raise GEOSException('Transformed WKB was invalid.')
def to_python(self, value): if value in self.empty_values: return None # Work with a single GeoJSON geometry or a Feature. Avoid parsing # overhead unless we have a true "Feature". if '"Feature",' in value: d = json.loads(value) value = json.dumps(d.get('geometry')) elif isinstance(value, collections.Mapping): value = json.dumps(value.get('geometry') or value) # Handle a comma delimited extent. elif list(value).count(',') == 3: value = Envelope(value.split(',')).polygon.ExportToWkt() try: geom = gdal.OGRGeometry(value) except (gdal.OGRException, TypeError, ValueError): raise forms.ValidationError(self.error_messages['invalid_geom']) if not geom.srs: geom.srid = self.srid or self.widget.map_srid return geom
def to_python(self, value): if value in self.empty_values: return None sref = None # Work with a single GeoJSON geometry or a Feature. value = json.loads(value) if '"Feature",' in value else value if isinstance(value, collections.Mapping): feat = sc.as_feature(value) value = json.dumps(feat.get('geometry') or value) sref = feat.srs # Handle a comma delimited extent. elif list(value).count(',') == 3: value = Envelope(value.split(',')).polygon.ExportToWkt() try: geom = gdal.OGRGeometry(value, srs=getattr(sref, 'wkt', None)) except (gdal.OGRException, TypeError, ValueError): raise forms.ValidationError(self.error_messages['invalid_geom']) if not geom.srs: geom.srid = self.srid or self.widget.map_srid return geom
def ogr(self): "Return the OGR Geometry for this Geometry." return gdal.OGRGeometry(self._ogr_ptr(), self.srs)