o aP]@sdZddlZddlmZddlmZmZmZmZm Z m Z ddl m Z ddl mZmZddlmZmZddlmZdd lmZmZdd lmZmZdd lmZmZm Z dd l!m"Z"Gd dde Z#Gddde#Z$Gddde#Z%Gddde%Z&Gddde#Z'Gddde#Z(Gddde(Z)Gddde(Z*Gddde(Z+de$d e%d!e'd"e)d#e*d$e+d%e(d&e&dej,e$d ej,e%d!ej,e'd"ej,e)d#ej,e*d$ej,e+d%ej,e(iZ-dS)'a The OGRGeometry is a wrapper for using the OGR Geometry class (see https://gdal.org/api/ogrgeometry_cpp.html#_CPPv411OGRGeometry). OGRGeometry may be instantiated when reading geometries from OGR Data Sources (e.g. SHP files), or when given OGC WKT (a string). While the 'full' API is not present yet, the API is "pythonic" unlike the traditional and "next-generation" OGR Python bindings. One major advantage OGR Geometries have over their GEOS counterparts is support for spatial reference systems and their transformation. Example: >>> from django.contrib.gis.gdal import OGRGeometry, OGRGeomType, SpatialReference >>> wkt1, wkt2 = 'POINT(-90 30)', 'POLYGON((0 0, 5 0, 5 5, 0 5)' >>> pnt = OGRGeometry(wkt1) >>> print(pnt) POINT (-90 30) >>> mpnt = OGRGeometry(OGRGeomType('MultiPoint'), SpatialReference('WGS84')) >>> mpnt.add(wkt1) >>> mpnt.add(wkt1) >>> print(mpnt) MULTIPOINT (-90 30,-90 30) >>> print(mpnt.srs.name) WGS 84 >>> print(mpnt.srs.proj) +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs >>> mpnt.transform(SpatialReference('NAD27')) >>> print(mpnt.proj) +proj=longlat +ellps=clrk66 +datum=NAD27 +no_defs >>> print(mpnt) MULTIPOINT (-89.999930378602485 29.999797886557641,-89.999930378602485 29.999797886557641) The OGRGeomType class is to make it easy to specify an OGR geometry type: >>> from django.contrib.gis.gdal import OGRGeomType >>> gt1 = OGRGeomType(3) # Using an integer for the type >>> gt2 = OGRGeomType('Polygon') # Using a string >>> gt3 = OGRGeomType('POLYGON') # It's case-insensitive >>> print(gt1 == 3, gt1 == 'Polygon') # Equivalence works w/non-OGRGeomType objects True True N)b2a_hex)byrefc_char_pc_doublec_ubytec_void_p string_at)GDALBase)Envelope OGREnvelope) GDALException SRSException) OGRGeomType)geomsrs)CoordTransformSpatialReference) hex_regex json_regex wkt_regex) force_bytesc@seZdZdZejZd|ddZddZddZ e d d Z e d d Z e d dZe ddZe ddZddZddZddZddZddZddZedd Zd!d"Zd#d$ZeeeZed%d&Zed'd(Zed)d*Zed+d,Zed-d.Z ed/d0Z!ed1d2Z"ed3d4Z#ed5d6Z$ed7d8Z%d9d:Z&d;d<Z'ee&e'Z(d=d>Z)d?d@Z*ee)e*Z+dAdBZ,edCdDZ-edEdFZ.edGdHZ/edIdJZ0e0Z1edKdLZ2edMdNZ3edOdPZ4edQdRZ5edSdTZ6dUdVZ7dWdXZ8d}dZd[Z9d\d]Z:d^d_Z;d`daZdfdgZ?dhdiZ@djdkZAdldmZBd|dndoZCedpdqZDedrdsZEdtduZFdvdwZGdxdyZHdzd{ZIdS)~ OGRGeometryzEncapsulate an OGR geometry.NcCsrt|t}|rt|rtt|}d}|rxt|}t|}|ra|dr-t |d}|d dkrNt t |dj}t |tt|dnRt tt|ddtt}n?|rk||}n5t |t t |j}n(t|tr||}nt|t rt |j}nt||jr|}ntdt||std|||_|r||_t|jj|_dS) z=Initialize Geometry on either WKT or an OGR pointer as input.Fsridtype LINEARRINGwktNz4Invalid input type for OGR Geometry construction: %sz)Cannot create OGR Geometry from input: %s) isinstancestrrmatch memoryviewbytesfromhexrrintuppercapi create_geomrnum import_wktrrencodefrom_wktr _from_json _from_wkbptr_typer rptrr GEO_CLASSES geom_type __class__)self geom_inputr str_instancewkt_mjson_mgr7D/usr/lib/python3/dist-packages/django/contrib/gis/gdal/geometries.py__init__?s>    &     zOGRGeometry.__init__cCs$|j}|r |j}nd}t|j|fSN)rrr wkbr1rr7r7r8 __getstate__xs zOGRGeometry.__getstate__cCs>|\}}t|dttt|}|std||_||_dS)Nz.Invalid OGRGeometry loaded from pickled state.)r$from_wkbrrlenr r-r)r1stater;rr-r7r7r8 __setstate__s  zOGRGeometry.__setstate__cCstt|dttt|Sr:)r$r>r rrr?)clsr2r7r7r8r+szOGRGeometry._from_wkbcCs t|Sr:)r$ from_jsonr2r7r7r8r* zOGRGeometry._from_jsonc Cs,|\}}}}td||||||||||f S)z2Construct a Polygon from a bounding box (4-tuple).z,POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s)))r)rBbboxx0y0x1y1r7r7r8 from_bboxs zOGRGeometry.from_bboxcCsttt|Sr:)rr*rrDr7r7r8rCzOGRGeometry.from_jsoncCs|tt|Sr:)r$from_gmlr)rB gml_stringr7r7r8rMrLzOGRGeometry.from_gmlcC ||S)z'Return the union of the two geometries.)unionr1otherr7r7r8__or__rEzOGRGeometry.__or__cCrO)z7Return the intersection of this Geometry and the other.) intersectionrQr7r7r8__and__rEzOGRGeometry.__and__cCrO)z2Return the difference this Geometry and the other.) differencerQr7r7r8__sub__rEzOGRGeometry.__sub__cCrO)z?Return the symmetric difference of this Geometry and the other.)sym_differencerQr7r7r8__xor__rEzOGRGeometry.__xor__cCst|to ||S)z$Is this Geometry equal to the other?)rrequalsrQr7r7r8__eq__zOGRGeometry.__eq__cC|jS)z*WKT is used for the string representation.)rr1r7r7r8__str__zOGRGeometry.__str__cC t|jS)z5Return 0 for points, 1 for lines, and 2 for surfaces.)r$get_dimsr-r^r7r7r8 dimension zOGRGeometry.dimensioncCra)z0Return the coordinate dimension of the Geometry.)r$ get_coord_dimr-r^r7r7r8_get_coord_dim zOGRGeometry._get_coord_dimcCs"|dvrtdt|j|dS)z.Set the coordinate dimension of this Geometry.)z(Geometry dimension must be either 2 or 3N) ValueErrorr$ set_coord_dimr-)r1dimr7r7r8_set_coord_dimszOGRGeometry._set_coord_dimcCra)z/Return the number of elements in this Geometry.)r$get_geom_countr-r^r7r7r8 geom_countrdzOGRGeometry.geom_countcCra)z-Return the number of Points in this Geometry.)r$get_point_countr-r^r7r7r8 point_countrdzOGRGeometry.point_countcCr])z7Alias for `point_count` (same name method in GEOS API.)rqr^r7r7r8 num_pointszOGRGeometry.num_pointscCr])zAlias for `point_count`.rrr^r7r7r8 num_coordsrtzOGRGeometry.num_coordscCstt|jS)z"Return the Type for this Geometry.)rr$ get_geom_typer-r^r7r7r8r/szOGRGeometry.geom_typecCra)z!Return the Name of this Geometry.)r$ get_geom_namer-r^r7r7r8 geom_namerdzOGRGeometry.geom_namecCra)zHReturn the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise.)r$get_arear-r^r7r7r8areardzOGRGeometry.areacCstt|jttS)z&Return the envelope for this Geometry.)r r$ get_enveloper-rr r^r7r7r8envelopeszOGRGeometry.envelopecCrar:)r$is_emptyr-r^r7r7r8emptyrgzOGRGeometry.emptycCs|jjS)zCReturn the envelope as a 4-tuple, instead of as an Envelope object.)r|tupler^r7r7r8extentzOGRGeometry.extentcCs2zt|j}tt|WStyYdSw)z/Return the Spatial Reference for this Geometry.N)r$ get_geom_srsr-rsrs_api clone_srsr )r1srs_ptrr7r7r8_get_srss   zOGRGeometry._get_srscCs`t|tr |j}nt|ttfrt|}|j}n|durd}ntdt|t|j|dS)z+Set the SpatialReference for this geometry.Nz7Cannot assign spatial reference with object of type: %s) rrr-r"r TypeErrorrr$ assign_srs)r1rrsrr7r7r8_set_srs s zOGRGeometry._set_srscCs|j}|r|jSdSr:)rrr<r7r7r8 _get_sridszOGRGeometry._get_sridcCs$t|ts |dur||_dStd)Nz!SRID must be set with an integer.)rr"rr)r1rr7r7r8 _set_srid%s zOGRGeometry._set_sridcCsddlm}||jS)Nr GEOSGeometry)django.contrib.gis.geosrr+r;r1rr7r7r8 _geos_ptr.s  zOGRGeometry._geos_ptrcCsddlm}|||jS)z3Return a GEOSGeometry object from this OGRGeometry.rr)rrrrrr7r7r8geos2s zOGRGeometry.geoscCra)z.Return the GML representation of the Geometry.)r$to_gmlr-r^r7r7r8gml8rdzOGRGeometry.gmlcCst|jS)zttt}}}t|j|t|t|t||j}|dkr3|jfS|dkr=|j|jfS|dkrI|j|j|jfSdStd|)z$Return the Point at the given index.rrrhriz>Index out of range when accessing points of a line string: %s.N) rqrr$ get_pointr-rrvalue IndexError)r1indexrrrrlr7r7r8 __getitem__s   zLineString.__getitem__cCr])z.Return the number of points in the LineString.rrr^r7r7r8__len__+r`zLineString.__len__cstfddttDS)z3Return the tuple representation of this LineString.c3s|]}|VqdSr:r7.0ir^r7r8 2sz#LineString.tuple..)rranger?r^r7r^r8r/szLineString.tuplecsfddttDS)zp Internal routine that returns a sequence (list) corresponding with the given function. csg|]}j|qSr7)r-rrr1r7r8 :sz'LineString._listarr..)rr?)r1rr7rr8_listarr5szLineString._listarrcCr)z#Return the X coordinates in a list.)rr$rr^r7r7r8r<rdz LineString.xcCr)z#Return the Y coordinates in a list.)rr$rr^r7r7r8rArdz LineString.ycCs|jdkr |tjSdS)z#Return the Z coordinates in a list.riN)rrr$rr^r7r7r8rFs  z LineString.zN) rrrrrrrrrrrrr7r7r7r8rs   rc@ eZdZdS) LinearRingNrrrr7r7r7r8rNrc@sTeZdZddZddZeddZeZeddZeZ ed d Z ed d Z d S)PolygoncCr])z4Return the number of interior rings in this Polygon.ror^r7r7r8rTr`zPolygon.__len__cCBd|kr |jkrnnttt|j||jStd|)z$Get the ring at the specified index.rz9Index out of range when accessing rings of a polygon: %s.rorr$r get_geom_refr-rrr1rr7r7r8rX zPolygon.__getitem__cCs|dS)z!Return the shell of this Polygon.rr7r^r7r7r8shell`rz Polygon.shellctfddtjDS)z/Return a tuple of LinearRing coordinate tuples.c3|]}|jVqdSr:rrr^r7r8riz Polygon.tuple..rrror^r7r^r8rfz Polygon.tuplecr)z,Return the number of Points in this Polygon.c3rr:rrrr^r7r8rprz&Polygon.point_count..sumrror^r7r^r8rqlzPolygon.point_countcCs ttd}t|j|j|S)z.Return the centroid (a Point) of this Polygon.r)rrr$ get_centroidr-)r1pr7r7r8centroidrs zPolygon.centroidN) rrrrrrr exterior_ringrrrqrr7r7r7r8rRs   rc@sDeZdZdZddZddZddZedd Zed d Z e Z d S) GeometryCollectionzThe Geometry Collection class.cCr)z(Get the Geometry at the specified index.rz?Index out of range when accessing geometry in a collection: %s.rrr7r7r8rrzGeometryCollection.__getitem__cCr])z.r r^r7r^r8rqr zGeometryCollection.point_countcr)z:Return a tuple representation of this Geometry Collection.c3rr:rrr^r7r8rrz+GeometryCollection.tuple..rr^r7r^r8rrzGeometryCollection.tupleN) rrrrrrrrrqrrr7r7r7r8r|s  rc@r) MultiPointNrr7r7r7r8rrrc@r)MultiLineStringNrr7r7r7r8rrrc@r) MultiPolygonNrr7r7r7r8rrrrrhrie).rrbinasciirctypesrrrrrrdjango.contrib.gis.gdal.baser django.contrib.gis.gdal.enveloper r django.contrib.gis.gdal.errorr r django.contrib.gis.gdal.geomtyper"django.contrib.gis.gdal.prototypesrr$rrdjango.contrib.gis.gdal.srsrrdjango.contrib.gis.geometryrrrdjango.utils.encodingrrrrrrrrrrwkb25bitr.r7r7r7r8sP(     >$4**