o ac@sdZddlZddlmZmZmZddlmZddlm Z m Z m Z ddl m ZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZmZmZmZm Z ddl!m"Z"ddl#m$Z$m%Z%GdddeZ&GdddZ'e"Gddde&eZ(dS)zh This module contains the 'base' GEOSGeometry object -- all GEOS Geometries inherit from this object. N) addressofbyrefc_double)gdal) hex_regex json_regex wkt_regex) prototypes)GEOSBase) GEOSCoordSeq) GEOSException)GEOM_PTR) ListMixinPreparedGeometry)ewkb_wwkb_rwkb_wwkt_rwkt_w)deconstructible) force_bytes force_strc@sVeZdZdZeZejZdZ ddZ ddZ ddZ d d Z d d Zd dZddZddZddZddZeddZeddZeddZeddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Ze d+d,Z!e d-d.Z"e d/d0Z#e d1d2Z$e d3d4Z%e d5d6Z&e d7d8Z'd9d:Z(e d;d<Z)e d=d>Z*e d?d@Z+e dAdBZ,e dCdDZ-e dEdFZ.dGdHZ/dIdJZ0dKdLZ1dMdNZ2dOdPZ3ddRdSZ4dTdUZ5dVdWZ6dXdYZ7dZd[Z8d\d]Z9e d^d_Z:e:j;d`d_Z:e dadbZe dgdhZ?e didjZ@e@ZAe dkdlZBe dmdnZCe dodpZDe dqdrZEdsdtZFe dudvZGe dwdxZHe dydzZIdd{d|ZJd}d~ZKe ddZLdddZMdddZNe ddZOe ddZPddZQe ddZRddZSe ddZTddZUdddZVddZWe ddZXddZYe ddZZddZ[e ddZ\e ddZ]ddZ^dS)GEOSGeometryBaseNFc Cs||_t|ttfvrI|durFtjdur@ddlm}m}m}m }ddl m }m }ddl m} ddlm} | ||| ||||dt_tj|j}||_|dS)N)GeometryCollectionMultiLineString MultiPoint MultiPolygon) LinearRing LineStringPoint)Polygon)rr)_ptrtyper GEOSGeometry _GEOS_CLASSES collectionsrrrr linestringrr pointr"polygonr# geom_typeid __class__ _post_init) selfptrclsrrrrrr r"r#r8B/usr/lib/python3/dist-packages/django/contrib/gis/geos/geometry.py__init__ s(     zGEOSGeometryBase.__init__cCs*|jrtt|j|j|_dSd|_dS)z"Perform post-initialization setup.N)has_csr capiget_csr6hasz_csr5r8r8r9r4=s*zGEOSGeometryBase._post_initcC|S)z Return a clone because the copy of a GEOSGeometry may contain an invalid pointer location if the original is garbage collected. cloner@r8r8r9__copy__CzGEOSGeometryBase.__copy__cCrA)z The `deepcopy` routine is used by the `Node` class of django.utils.tree; thus, the protocol routine needs to be implemented to return correct copies (clones) of these GEOS objects, which use C pointers. rB)r5memodictr8r8r9 __deepcopy__JzGEOSGeometryBase.__deepcopy__cC|jS)z+EWKT is used for the string representation.)ewktr@r8r8r9__str__RszGEOSGeometryBase.__str__cCsd|jtt|jfS)z8Short-hand representation because WKT may be very large.z<%s object at %s>) geom_typehexrr6r@r8r8r9__repr__VszGEOSGeometryBase.__repr__cCs t|jSN)byteswkbr@r8r8r9_to_pickle_wkb[s zGEOSGeometryBase._to_pickle_wkbcCstt|SrO)rread memoryview)r5rQr8r8r9_from_pickle_wkb^z!GEOSGeometryBase._from_pickle_wkbcCs||jfSrO)rRsridr@r8r8r9 __getstate__aszGEOSGeometryBase.__getstate__cCs6|\}}||}|std||_|||_dS)Nz+Invalid Geometry loaded from pickled state.)rUr r6r4rW)r5staterQrWr6r8r8r9 __setstate__fs  zGEOSGeometryBase.__setstate__cCs t|SrO)rrS)r7rQr8r8r9 _from_wkbp zGEOSGeometryBase._from_wkbcCstt|}d}|dd}t|dkr)|\}}td|}|s"tdt|d}n|}|s1tdtt||dS) N;rr$sSRID=(?P\-?\d+)zEWKT has invalid SRID part.rWz%Expected WKT but got an empty string.rW) rsplitlenrematch ValueErrorintr, _from_wkt)rJrWparts srid_partwktrbr8r8r9 from_ewktts   zGEOSGeometryBase.from_ewktcCs t|SrO)rrS)rhr8r8r9rer\zGEOSGeometryBase._from_wktcCstj|jSrO)r OGRGeometryfrom_gmlgeos)r7 gml_stringr8r8r9rkzGEOSGeometryBase.from_gmlc CsRt|trzt|}Wn ttfyYdSwt|to(|j|jko(||S)zz Equivalence testing, a Geometry may be compared with another Geometry or an EWKT representation. F) isinstancestrr,rircr rW equals_exactr5otherr8r8r9__eq__s  zGEOSGeometryBase.__eq__cCst|j|jfSrO)hashrWrhr@r8r8r9__hash__rVzGEOSGeometryBase.__hash__cC ||S)z0Return the union of this Geometry and the other.)unionrrr8r8r9__or__ zGEOSGeometryBase.__or__cCrw)z7Return the intersection of this Geometry and the other.) intersectionrrr8r8r9__and__rzzGEOSGeometryBase.__and__cCrw)z2Return the difference this Geometry and the other.) differencerrr8r8r9__sub__rzzGEOSGeometryBase.__sub__cCrw)z?Return the symmetric difference of this Geometry and the other.)sym_differencerrr8r8r9__xor__rzzGEOSGeometryBase.__xor__cCs|jr|jSdS)zReturn a string representing the Geometry type, e.g. 'Polygon')r< geos_typer6decoder@r8r8r9rLszGEOSGeometryBase.geom_typecC t|jS)z1Return an integer representing the Geometry type.)r< geos_typeidr6r@r8r8r9r2 zGEOSGeometryBase.geom_typeidcCr)z0Return the number of geometries in the Geometry.)r< get_num_geomsr6r@r8r8r9num_geomrzGEOSGeometryBase.num_geomcCr)z1Return the number of coordinates in the Geometry.)r<get_num_coordsr6r@r8r8r9 num_coordsrzGEOSGeometryBase.num_coordscCrI)z:Return the number points, or coordinates, in the Geometry.)rr@r8r8r9 num_pointszGEOSGeometryBase.num_pointscCr)zCReturn the dimension of this Geometry (0=point, 1=line, 2=surface).)r<get_dimsr6r@r8r8r9dimsrzGEOSGeometryBase.dimscCst|jdS)z9Convert this Geometry to normal form (or canonical form).N)r<geos_normalizer6r@r8r8r9 normalizezGEOSGeometryBase.normalizecCr)zk Return a boolean indicating whether the set of points in this Geometry are empty. )r< geos_isemptyr6r@r8r8r9emptys zGEOSGeometryBase.emptycCr)z/Return whether the geometry has a 3D dimension.)r< geos_haszr6r@r8r8r9r>rzGEOSGeometryBase.haszcCr)z-Return whether or not the geometry is a ring.)r< geos_isringr6r@r8r8r9ringrzGEOSGeometryBase.ringcCr)z*Return false if the Geometry isn't simple.)r< geos_issimpler6r@r8r8r9simplerzGEOSGeometryBase.simplecCr)z#Test the validity of this Geometry.)r< geos_isvalidr6r@r8r8r9validrzGEOSGeometryBase.validcCr)zK Return a string containing the reason for any invalidity. )r<geos_isvalidreasonr6rr@r8r8r9 valid_reasonzGEOSGeometryBase.valid_reasoncCt|j|jS)z/Return true if other.within(this) returns true.)r< geos_containsr6rrr8r8r9containsrzGEOSGeometryBase.containscCr)z Return True if the DE-9IM Intersection Matrix for the two geometries is T*****FF*, *T****FF*, ***T**FF*, or ****T*FF*. If either geometry is empty, return False. )r< geos_coversr6rrr8r8r9coverszGEOSGeometryBase.coverscCr)z Return true if the DE-9IM intersection matrix for the two Geometries is T*T****** (for a point and a curve,a point and an area or a line and an area) 0******** (for two curves). )r< geos_crossesr6rrr8r8r9crossesrzGEOSGeometryBase.crossescCr)zl Return true if the DE-9IM intersection matrix for the two Geometries is FF*FF****. )r< geos_disjointr6rrr8r8r9disjointrzGEOSGeometryBase.disjointcCr)zl Return true if the DE-9IM intersection matrix for the two Geometries is T*F**FFF*. )r< geos_equalsr6rrr8r8r9equalsrzGEOSGeometryBase.equalsrcCst|j|jt|S)zk Return true if the two Geometries are exactly equal, up to a specified tolerance. )r<geos_equalsexactr6float)r5rs tolerancer8r8r9rq%zGEOSGeometryBase.equals_exactcCr)z%Return true if disjoint return false.)r<geos_intersectsr6rrr8r8r9 intersects,rzGEOSGeometryBase.intersectscCr)z Return true if the DE-9IM intersection matrix for the two Geometries is T*T***T** (for two points or two surfaces) 1*T***T** (for two curves). )r< geos_overlapsr6rrr8r8r9overlaps0rzGEOSGeometryBase.overlapscCs4t|tr t|dkrtdt|j|jt|S)z Return true if the elements in the DE-9IM intersection matrix for the two Geometries match the elements in pattern. z#invalid intersection matrix pattern)rorpr`r r<geos_relatepatternr6r)r5rspatternr8r8r9relate_pattern7szGEOSGeometryBase.relate_patterncCr)z Return true if the DE-9IM intersection matrix for the two Geometries is FT*******, F**T***** or F***T****. )r< geos_touchesr6rrr8r8r9touches@rzGEOSGeometryBase.touchescCr)zl Return true if the DE-9IM intersection matrix for the two Geometries is T*F**F***. )r< geos_withinr6rrr8r8r9withinGrzGEOSGeometryBase.withincCst|j}|dkr dS|S)z=Get the SRID for the geometry. Return None if no SRID is set.rN)r< geos_get_sridr6)r5sr8r8r9rWOs zGEOSGeometryBase.sridcCs$t|j|dur ddS|dS)zSet the SRID for the geometry.Nr)r< geos_set_sridr6r5rWr8r8r9rWXs$cCs|j}|r d||jfS|jS)z? Return the EWKT (SRID + WKT) of the Geometry. z SRID=%s;%s)rWrhrr8r8r9rJ^szGEOSGeometryBase.ewktcCs t|jrdnddd|S)zAReturn the WKT (Well-Known Text) representation of this Geometry.r%r$T)dimtrim)rr>writerr@r8r8r9rhfs zGEOSGeometryBase.wktcCt|jrdndd|S)z Return the WKB of this Geometry in hexadecimal form. Please note that the SRID is not included in this representation because it is not a part of the OGC specification (use the `hexewkb` property instead). r%r$r)rr> write_hexr@r8r8r9rMks zGEOSGeometryBase.hexcCr)z Return the EWKB of this Geometry in hexadecimal form. This is an extension of the WKB specification that includes SRID value that are a part of this geometry. r%r$r)rr>rr@r8r8r9hexewkbvszGEOSGeometryBase.hexewkbcCs|jjS)zA Return GeoJSON representation of this Geometry. )ogrjsonr@r8r8r9rrEzGEOSGeometryBase.jsoncCt|jrdnd|S)z Return the WKB (Well-Known Binary) representation of this Geometry as a Python buffer. SRID and Z values are not included, use the `ewkb` property instead. r%r$)rr>rr@r8r8r9rQzGEOSGeometryBase.wkbcCr)z Return the EWKB representation of this Geometry as a Python buffer. This is an extension of the WKB specification that includes any SRID value that are a part of this geometry. r%r$)rr>rr@r8r8r9ewkbrzGEOSGeometryBase.ewkbcCs|j}d||jj|fS)z/Return the KML representation of this Geometry.z <%s>%s)rLrkml)r5gtyper8r8r9rszGEOSGeometryBase.kmlcCst|S)z Return a PreparedGeometry corresponding to this geometry -- it is optimized for the contains, intersects, and covers operations. rr@r8r8r9preparedrHzGEOSGeometryBase.preparedcCstj|jSrO)rrjr[rQr@r8r8r9_ogr_ptrszGEOSGeometryBase._ogr_ptrcCst||jS)z*Return the OGR Geometry for this Geometry.)rrjrsrsr@r8r8r9rzGEOSGeometryBase.ogrc Cs6|jrzt|jWStjtjfyYdSwdS)z:Return the OSR SpatialReference for SRID of this Geometry.N)rWrSpatialReference GDALException SRSExceptionr@r8r8r9rszGEOSGeometryBase.srscCrI)zAlias for `srs` property.)rr@r8r8r9crsrzGEOSGeometryBase.crscCs|j}||kr|r |SdSt|tjrd}n |dus |dkr$tdt||}||| }|r>t ||jdS|rSt |j ||_ ||j|_dStd)ad Requires GDAL. Transform the geometry according to the given transformation object, which may be an integer SRID, and WKT or PROJ string. By default, transform the geometry in-place and return nothing. However if the `clone` keyword is set, don't modify the geometry and return a transformed clone instead. Nrz5Calling transform() with no SRID set is not supportedr^zTransformed WKB was invalid.)rWrCrorCoordTransformr rjr transform _geos_ptrr,r< destroy_geomr6r4)r5ctrCrWgr6r8r8r9rs(    zGEOSGeometryBase.transformcCst||jdS)z'Return Geometry from the given pointer.r^)r,rW)r5gptrr8r8r9 _topologyrnzGEOSGeometryBase._topologycC|t|jS)z9Return the boundary as a newly allocated Geometry object.)rr< geos_boundaryr6r@r8r8r9boundaryrzGEOSGeometryBase.boundarycCs|t|j||S)a Return a geometry that represents all points whose distance from this Geometry is less than or equal to distance. Calculations are in the Spatial Reference System of this Geometry. The optional third parameter sets the number of segment used to approximate a quarter circle (defaults to 8). (Text from PostGIS documentation at ch. 6.1.3) )rr< geos_bufferr6)r5widthquadsegsr8r8r9bufferszGEOSGeometryBase.bufferr@c Cs|t|j|||||S)a  Same as buffer() but allows customizing the style of the buffer. End cap style can be round (1), flat (2), or square (3). Join style can be round (1), mitre (2), or bevel (3). Mitre ratio limit only affects mitered join style. )rr<geos_bufferwithstyler6)r5rr end_cap_style join_style mitre_limitr8r8r9buffer_with_stylesz"GEOSGeometryBase.buffer_with_stylecCr)z The centroid is equal to the centroid of the set of component Geometries of highest dimension (since the lower-dimension geometries contribute zero "weight" to the centroid). )rr< geos_centroidr6r@r8r8r9centroid szGEOSGeometryBase.centroidcCr)zj Return the smallest convex Polygon that contains all the points in the Geometry. )rr<geos_convexhullr6r@r8r8r9 convex_hullszGEOSGeometryBase.convex_hullcC|t|j|jS)zv Return a Geometry representing the points making up this Geometry that do not make up other. )rr<geos_differencer6rrr8r8r9r}rzGEOSGeometryBase.differencecCr)z2Return the envelope for this geometry (a polygon).)rr< geos_enveloper6r@r8r8r9envelope!rzGEOSGeometryBase.envelopecCr)zLReturn a Geometry representing the points shared by this Geometry and other.)rr<geos_intersectionr6rrr8r8r9r{&zGEOSGeometryBase.intersectioncCr)z+Compute an interior point of this Geometry.)rr<geos_pointonsurfacer6r@r8r8r9point_on_surface*rz!GEOSGeometryBase.point_on_surfacecCst|j|jS)zFReturn the DE-9IM intersection matrix for this Geometry and the other.)r< geos_relater6rrrr8r8r9relate/szGEOSGeometryBase.relatecCs,|r |t|j|S|t|j|S)a" Return the Geometry, simplified using the Douglas-Peucker algorithm to the specified tolerance (higher tolerance => less points). If no tolerance provided, defaults to 0. By default, don't preserve topology - e.g. polygons can be split, collapse to lines or disappear holes can be created or disappear, and lines can cross. By specifying preserve_topology=True, the result will have the same dimension and number of components as the input. This is significantly slower. )rr<geos_preservesimplifyr6 geos_simplify)r5rpreserve_topologyr8r8r9simplify3s zGEOSGeometryBase.simplifycCr)z Return a set combining the points in this Geometry not in other, and the points in other not in this Geometry. )rr<geos_symdifferencer6rrr8r8r9rDrzGEOSGeometryBase.sym_differencecCr)z6Return the union of all the elements of this geometry.)rr<geos_unary_unionr6r@r8r8r9 unary_unionKrzGEOSGeometryBase.unary_unioncCr)zIReturn a Geometry representing all the points in this Geometry and other.)rr< geos_unionr6rrr8r8r9rxPrzGEOSGeometryBase.unioncCt|jttS)z Return the area of the Geometry.)r< geos_arear6rrr@r8r8r9areaUszGEOSGeometryBase.areacCs*t|ts tdt|j|jttS)z Return the distance between the closest points on this Geometry and the other. Units will be in those of the coordinate system of the Geometry. z/distance() works only on other GEOS Geometries.)ror, TypeErrorr< geos_distancer6rrrrr8r8r9distanceZs zGEOSGeometryBase.distancecCs^ddlm}|j}t||r|j\}}||}}n|dd\}}|dd\}}||||fS)zr Return the extent of this geometry as a 4-tuple, consisting of (xmin, ymin, xmax, ymax). rr!rr$)r0r"rrotuple)r5r"envxminyminxmaxymaxr8r8r9extentds     zGEOSGeometryBase.extentcCr)zt Return the length of this Geometry (e.g., 0 for point, or the circumference of a Polygon). )r< geos_lengthr6rrr@r8r8r9lengthtszGEOSGeometryBase.lengthcCstt|jS)zClone this Geometry.)r,r< geom_cloner6r@r8r8r9rC|rzGEOSGeometryBase.clone)r)F)r)rrrr)rF)___name__ __module__ __qualname__r-r ptr_typer<r destructorr;r:r4rDrGrKrNrRrUrXrZ classmethodr[ staticmethodrirerkrtrvryr|r~rpropertyrrLr2rrrrrrr>rrrrrrrrrrqrrrrrrWsetterrJrhrMrrgeojsonrQrrrrrrrrrrrrrrr}rr{rrrrrrxrrrrrCr8r8r8r9rs                                   +             rc@sHeZdZdZddZddZddZdd Zed d Z ed d Z dS)LinearGeometryMixinz2 Used for LineString and MultiLineString. cC|t|j|SrO)rr<geos_interpolater6r5rr8r8r9 interpolatezLinearGeometryMixin.interpolatecCrrO)rr<geos_interpolate_normalizedr6rr8r8r9interpolate_normalizedr!z*LinearGeometryMixin.interpolate_normalizedcC.ddlm}t||stdt|j|jSNrr!z%locate_point argument must be a Point)r0r"rorr< geos_projectr6r5r0r"r8r8r9project  zLinearGeometryMixin.projectcCr$r%)r0r"rorr<geos_project_normalizedr6r'r8r8r9project_normalizedr)z&LinearGeometryMixin.project_normalizedcCr)z9 Return the line merge of this Geometry. )rr<geos_linemerger6r@r8r8r9mergedszLinearGeometryMixin.mergedcCr)z@ Return whether or not this Geometry is closed. )r< geos_isclosedr6r@r8r8r9closeds zLinearGeometryMixin.closedN) rrr__doc__r r#r(r+rr-r/r8r8r8r9rs rcs"eZdZdZdfdd ZZS)r,z6A class that, generally, encapsulates a GEOS geometry.Ncs`d}t|tr t|}t|trPt|}|r+|dr!t|d}|t|d}nMt |r9t t|}n?t |rLt j|}|}|j}n,tdt|trX|}n t|trdt |}nt|trpt|j}ntdt||s~td|pt|pd}|r|r||krtd|t|d|p|}|rt|tr||_dSdSdS)a The base constructor for GEOS geometry objects. It may take the following inputs: * strings: - WKT - HEXEWKB (a PostGIS-specific canonical form) - GeoJSON (requires GDAL) * buffer: - WKB The `srid` keyword specifies the Source Reference Identifier (SRID) number for this Geometry. If not provided, it defaults to None. NrWrhz3String input unrecognized as WKT EWKT, and HEXEWKB.z Improper geometry input type: %sz4Could not initialize GEOS Geometry with given input.z$Input geometry already has SRID: %d.)rorPrrprrbrdrerrrrSrrrj from_jsonrrWrcr rTr,r<rr6rr+r rsuperr:)r5 geo_inputrW input_sridwkt_mrrr3r8r9r:sB            zGEOSGeometry.__init__rO)rrrr0r: __classcell__r8r8r6r9r,sr,))r0ractypesrrrdjango.contrib.gisrdjango.contrib.gis.geometryrrrdjango.contrib.gis.geosr r<django.contrib.gis.geos.baser django.contrib.gis.geos.coordseqr django.contrib.gis.geos.errorr django.contrib.gis.geos.libgeosr $django.contrib.gis.geos.mutable_listr django.contrib.gis.geos.preparedr%django.contrib.gis.geos.prototypes.iorrrrrdjango.utils.deconstructrdjango.utils.encodingrrrrr,r8r8r8r9s.         m%