MCPcopy
hub / github.com/django/django / __init__

Method __init__

django/contrib/gis/geos/geometry.py:742–799  ·  view source on GitHub ↗

The base constructor for GEOS geometry objects. It may take the following inputs: * strings: - WKT - HEXEWKB (a PostGIS-specific canonical form) - GeoJSON (requires GDAL) * memoryview: - WKB The `srid` keywo

(self, geo_input, srid=None)

Source from the content-addressed store, hash-verified

740 "A class that, generally, encapsulates a GEOS geometry."
741
742 def __init__(self, geo_input, srid=None):
743 """
744 The base constructor for GEOS geometry objects. It may take the
745 following inputs:
746
747 * strings:
748 - WKT
749 - HEXEWKB (a PostGIS-specific canonical form)
750 - GeoJSON (requires GDAL)
751 * memoryview:
752 - WKB
753
754 The `srid` keyword specifies the Source Reference Identifier (SRID)
755 number for this Geometry. If not provided, it defaults to None.
756 """
757 input_srid = None
758 if isinstance(geo_input, bytes):
759 geo_input = force_str(geo_input)
760 if isinstance(geo_input, str):
761 wkt_m = wkt_regex.match(geo_input)
762 if wkt_m:
763 # Handle WKT input.
764 if wkt_m["srid"]:
765 input_srid = int(wkt_m["srid"])
766 g = self._from_wkt(force_bytes(wkt_m["wkt"]))
767 elif hex_regex.match(geo_input):
768 # Handle HEXEWKB input.
769 g = wkb_r().read(force_bytes(geo_input))
770 elif json_regex.match(geo_input):
771 # Handle GeoJSON input.
772 ogr = gdal.OGRGeometry.from_json(geo_input)
773 g = ogr._geos_ptr()
774 input_srid = ogr.srid
775 else:
776 raise ValueError("String input unrecognized as WKT EWKT, and HEXEWKB.")
777 elif isinstance(geo_input, GEOM_PTR):
778 # When the input is a pointer to a geometry (GEOM_PTR).
779 g = geo_input
780 elif isinstance(geo_input, memoryview):
781 # When the input is a memoryview (WKB).
782 g = wkb_r().read(geo_input)
783 elif isinstance(geo_input, GEOSGeometry):
784 g = capi.geom_clone(geo_input.ptr)
785 else:
786 raise TypeError("Improper geometry input type: %s" % type(geo_input))
787
788 if not g:
789 raise GEOSException("Could not initialize GEOS Geometry with given input.")
790
791 input_srid = input_srid or capi.geos_get_srid(g) or None
792 if input_srid and srid and input_srid != srid:
793 raise ValueError("Input geometry already has SRID: %d." % input_srid)
794
795 super().__init__(g, None)
796 # Set the SRID, if given.
797 srid = input_srid or srid
798 if srid and isinstance(srid, int):
799 self.srid = srid

Callers

nothing calls this directly

Calls 9

force_strFunction · 0.90
force_bytesFunction · 0.90
wkb_rFunction · 0.90
GEOSExceptionClass · 0.90
_from_wktMethod · 0.80
from_jsonMethod · 0.80
matchMethod · 0.45
readMethod · 0.45
_geos_ptrMethod · 0.45

Tested by

no test coverage detected