MCPcopy
hub / github.com/django/django / GEOSGeometry

Class GEOSGeometry

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

A class that, generally, encapsulates a GEOS geometry.

Source from the content-addressed store, hash-verified

737
738@deconstructible
739class GEOSGeometry(GEOSGeometryBase, ListMixin):
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.

Callers 15

get_geomsMethod · 0.90
geosMethod · 0.90
readMethod · 0.90
readMethod · 0.90
from_bboxMethod · 0.90
_get_single_externalMethod · 0.90
fromfileFunction · 0.90
fromstrFunction · 0.90
_get_single_externalMethod · 0.90
to_pythonMethod · 0.90
deserializeMethod · 0.90
convert_extentMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_sridMethod · 0.72
test_geom_typeMethod · 0.72
test_to_pythonMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
deserializeMethod · 0.72
setUpMethod · 0.72