o aUD@s2ddlmZddlmZmZddlmZmZddlm Z ddl m Z ddl m Z ddlmZmZmZmZmZmZmZmZddlmZdd lmZeeefZGd d d ZGd d d eeZGdddeZ GdddZ!GdddZ"Gddde"eZ#GdddeZ$GdddeZ%GdddeZ&GdddeZ'GdddeZ(Gd d!d!eZ)Gd"d#d#eZ*Gd$d%d%e"e Z+Gd&d'd'e"e Z,Gd(d)d)e"e Z-Gd*d+d+Z.Gd,d-d-e.e"eZ/Gd.d/d/e Z0Gd0d1d1e Z1Gd2d3d3eZ2Gd4d5d5eZ3Gd6d7d7e"e Z4ej5Gd8d9d9e"eeZ6Gd:d;d;e.e"eZ7Gdd?d?e Z9Gd@dAdAeZ:GdBdCdCeZ;GdDdEdEeZGdJdKdKeZ?GdLdMdMe!e Z@GdNdOdOe!e ZAGdPdQdQe"e ZBGdRdSdSe ZGdTdUdUe@ZCGdVdWdWe"e ZDdXS)Y)Decimal)BaseSpatialField GeometryField) AreaField DistanceField) GEOSGeometry) FieldError)NotSupportedError) BinaryField BooleanField FloatFieldFunc IntegerField TextField TransformValue)Cast)cached_propertycs`eZdZdZdZfddZeddZeddZ dfd d Z fd d Z dddZ Z S) GeoFuncMixinN)rc stj|i||jD]P}|j|}t|tsq z|j}Wn ty)d}Ynw|j}t|t r9|rDt|t sDt d|j |df|j sM|sMtd|s\t|t |j dd|j|<q dS)Nz9%s function requires a geometric argument in position %d.z$SRID is required for all geometries.srid) output_field)super__init__geom_param_possource_expressions isinstancerrrvaluerr TypeErrornamer ValueError)self expressionsextraposexprrgeom __class__H/usr/lib/python3/dist-packages/django/contrib/gis/db/models/functions.pyrs&      zGeoFuncMixin.__init__cCs|jjSN)r)__name__r"r*r*r+r *szGeoFuncMixin.namecCs|j|jdjSNr)rrfieldr.r*r*r+ geo_field.szGeoFuncMixin.geo_fieldc s:|jdur|dur|j|j}tj||fd|i|S)Nfunction)r2opsspatial_function_namer ras_sql)r"compiler connectionr2 extra_contextr(r*r+r52szGeoFuncMixin.as_sqlc stj|i|}|}|jD]}||}t|ts*td|j|dt|j fq|j j }|jddD]}|j |}|j j } | |krSt||j|i||j |<q6|S)Nz<%s function requires a GeometryField in position %s, got %s.r)rresolve_expressionget_source_fieldsrrrrr typer-r1rrrr) r"argskwargsres source_fieldsr%r0 base_sridr& expr_sridr(r*r+r97s&   zGeoFuncMixin.resolve_expressioncCs,t|ds|rt||std||f|S)Nr9z2The %s parameter has the wrong type: should be %s.)hasattrrr)r"r param_name check_typesr*r*r+ _handle_paramNs zGeoFuncMixin._handle_paramr,rBN)r- __module__ __qualname__r2rrpropertyr rr1r5r9rF __classcell__r*r*r(r+rs    rc@ eZdZdS)GeoFuncNr-rHrIr*r*r*r+rMXrMc@seZdZeddZdS)GeomOutputGeoFunccCst|jjdS)Nr)rr1rr.r*r*r+r]szGeomOutputGeoFunc.output_fieldN)r-rHrIrrr*r*r*r+rP\srPc@eZdZdZddZdS)SQLiteDecimalToFloatMixinz By default, Decimal values are converted to str by the SQLite backend, which is not acceptable by the GIS functions expecting numeric values. cKs4|}|dd|D|j||fi|S)NcSs4g|]}t|drt|jtrtt|jn|qS)r)rCrrrrfloat).0r&r*r*r+ is $z7SQLiteDecimalToFloatMixin.as_sqlite..)copyset_source_expressionsget_source_expressionsr5)r"r6r7r8rVr*r*r+ as_sqlitegs  z#SQLiteDecimalToFloatMixin.as_sqliteN)r-rHrI__doc__rYr*r*r*r+rRbs rRc@rQ)OracleToleranceMixing?cKsPt||jd|jdt}|}|g|||j ||fi|S)N tolerance) rrFr$getr\ NUMERIC_TYPESrVrWrXr5)r"r6r7r8r\cloner*r*r+ as_oracletszOracleToleranceMixin.as_oracleN)r-rHrIr\r`r*r*r*r+r[qs r[cs4eZdZdZeddZfddZddZZS)ArearcC t|jSr,)rr1r.r*r*r+r zArea.output_fieldc s2|jjs|j|rtdtj||fi|S)Nz2Area on geodetic coordinate systems not supported.)featuressupports_area_geodeticr1geodeticr rr5r"r6r7r8r(r*r+r5z Area.as_sqlcKs0|j|rd|d<d|d<|j||fi|S)Nz+%(function)s(%(expressions)s, %(spheroid)d)templateTspheroid)r1rfr5rgr*r*r+rYs zArea.as_sqlite) r-rHrIarityrrr5rYrKr*r*r(r+ras   rac@eZdZeZdZdZdS)AzimuthrrNr-rHrIr rrkrr*r*r*r+rmrmcs0eZdZeZdfdd ZfddZZS) AsGeoJSONFc sj|g}|dur|||dtd}|r|rd}n |rd}n|r#d}|r*||tj|i|dS)N precisionrrrnappendrFintrr)r" expressionbboxcrsrtr$r#optionsr(r*r+rs zAsGeoJSON.__init__c s<|}|}||ddtt|j||fi|S)Nr)rXrVrWrrrr5)r"r6r7r8rr_r(r*r+r`szAsGeoJSON.as_oracle)FFrs)r-rHrIrrrr`rKr*r*r(r+rrsrrcs4eZdZdZeZdfdd ZfddZZS) AsGML)rrnrsc s:||g}|dur|||dttj|i|dSNrtrv)r"ryversionrtr$r#r(r*r+rszAsGML.__init__c sX|}|d}|}||dg|jdkrdnd|d<tt|j||fi|S)NrrruzSDO_UTIL.TO_GML311GEOMETRYzSDO_UTIL.TO_GMLGEOMETRYr2)rXrVrWrrr}r5)r"r6r7r8rrr_r(r*r+r`s zAsGML.as_oracle)rnrs) r-rHrIrrrrr`rKr*r*r(r+r}s r}cs$eZdZeZdfdd ZZS)AsKMLrsc 8|g}|dur|||dttj|i|dSr~rvr"ryrtr$r#r(r*r+rzAsKML.__init__)rsr-rHrIrrrrKr*r*r(r+rrcs$eZdZeZdfdd ZZS)AsSVGFrsc s@t|dr|nt|}||||dtg}tj|i|dS)Nr9rt)rCrxrFrr)r"ryrelativertr$r#r(r*r+rs  zAsSVG.__init__)Frsrr*r*r(r+rrrc@eZdZeZdZdS)AsWKBrN)r-rHrIr rrkr*r*r*r+rrc@r)AsWKTrN)r-rHrIrrrkr*r*r*r+rrrcs*eZdZdfdd ZfddZZS)BoundingCircle0c stj||fi|dSr,)rr)r"rynum_segr$r(r*r+rszBoundingCircle.__init__c s6|}||dgtt|j||fi|Sr/)rVrWrXrrr`r"r6r7r8r_r(r*r+r`szBoundingCircle.as_oracle)r)r-rHrIrr`rKr*r*r(r+rsrc@eZdZdZdS)CentroidrNr-rHrIrkr*r*r*r+rrc@eZdZdZdZdS) DifferencernroNr-rHrIrkrr*r*r*r+rrc@s eZdZeddZddZdS)DistanceResultMixincCrbr,)rr1r.r*r*r+rrcz DistanceResultMixin.output_fieldcCs|jjo |jjdkS)Ni)r1 geographyrr.r*r*r+source_is_geographysz'DistanceResultMixin.source_is_geographyN)r-rHrIrrrr*r*r*r+rs  rcs>eZdZdZdZd fdd ZfddZfddZZS) DistanceroNc s6||g}|dur||dt|_tj|i|dS)Nrj)rFboolrjrr)r"expr1expr2rjr$r#r(r*r+rszDistance.__init__c s|}d}|jd}|}|jj|kr-t|tr||j_nt|t|jj |d|jd<|sQ|j |rQ|j rK|j d}|jt|j |n|j d}tt|j||fd|i|S)Nr)rrDistanceSpheroidDistanceSpherer2)rVrrrrrrrrrr1rfrjr3r4rwrrr5)r"r6r7r8r_r2rrr(r*r+ as_postgresql s"       zDistance.as_postgresqlc s<|j|rd|d<tt|j|d<tj||fi|S)Nz8COALESCE(%(function)s(%(expressions)s, %(spheroid)s), 0)rirj)r1rfrxrrjrr5rgr(r*r+rY$s zDistance.as_sqliter,) r-rHrIrrjrrrYrKr*r*r(r+rs  rc@r)EnveloperNrr*r*r*r+r,rrc@r)ForcePolygonCWrNrr*r*r*r+r0rrcs,eZdZeZdfdd ZddZZS)GeoHashNc rr~rvrr(r*r+r7rzGeoHash.__init__cKs:|}t|jdkr|jtd|j||fi|S)Nrnd)rVlenrrwrr5rr*r*r+as_mysql=szGeoHash.as_mysqlr,)r-rHrIrrrrrKr*r*r(r+r4src@s"eZdZeZdZdZdZdZdS)GeometryDistancernrBz <-> roN) r-rHrIr rrkr2 arg_joinerrr*r*r*r+rEs rc@r) IntersectionrnroNrr*r*r*r+rMrrcs&eZdZdZeZfddZZS)IsValidisvalidc s&tj||fi|\}}d||fS)Nz%CASE %s WHEN 'TRUE' THEN 1 ELSE 0 END)rr`)r"r6r7r8sqlparamsr(r*r+r`Ws zIsValid.as_oracle)r-rHrI lookup_namer rr`rKr*r*r(r+rRsrcsBeZdZd fdd ZfddZfddZfdd ZZS) LengthTc s||_tj|fi|dSr,)rjrr)r"rrjr$r(r*r+r]szLength.__init__c s2|j|r|jjstdtj||fi|S)Nz6This backend doesn't support Length on geodetic fields)r1rfrdsupports_length_geodeticr rr5rgr(r*r+r5arhz Length.as_sqlc s|}d}|r|jt|jn,|j|r-|j d}|jt|j|nt dd| D}|dkr@|jj }t t|j||fd|i|S)NLengthSpheroidcss|]}|r|jVqdSr,dimrTfr*r*r+ psz'Length.as_postgresql..rnr2)rVrrrwrrjr1rfr3r4minr:length3drrr5)r"r6r7r8r_r2rr(r*r+rfs  zLength.as_postgresqlc s8d}|j|r|jr dnd}tj||fd|i|S)NGeodesicLengthGreatCircleLengthr2)r1rfrjrr5)r"r6r7r8r2r(r*r+rYus zLength.as_sqlite)T)r-rHrIrr5rrYrKr*r*r(r+r\s   rc@rl)LineLocatePointrnroNrpr*r*r*r+r|rqrc@rL) MakeValidNrNr*r*r*r+rrOrc@r)MemSizerNr-rHrIrrrkr*r*r*r+rrrc@r) NumGeometriesrNrr*r*r*r+rrrc@r) NumPointsrNrr*r*r*r+rrrcs,eZdZdZfddZfddZZS) Perimeterrc s`d}|j|r|stdtdd|D}|dkr#|jj}tj ||fd|i|S)Nz.rnr2) r1rfrr rr:r3 perimeter3drr5)r"r6r7r8r2rr(r*r+rszPerimeter.as_postgresqlc s*|j|r tdtj||fi|S)Nz+Perimeter cannot use a non-projected field.)r1rfr rr5rgr(r*r+rYs zPerimeter.as_sqlite)r-rHrIrkrrYrKr*r*r(r+rs  rc@r)PointOnSurfacerNrr*r*r*r+rrrc@r)ReverserNrr*r*r*r+rrrcseZdZdfdd ZZS)Scalec sP|||dt||dtg}|dkr|||dttj|i|dS)Nxyrz)rFr^rwrr)r"ryrrrr$r#r(r*r+rs  zScale.__init__)rr-rHrIrrKr*r*r(r+rsrceZdZfddZZS) SnapToGridcst|}|g}|dvr|fdd|Dn'|dkr;|gfdd|ddDfdd|d dD7}ntd tj|i|dS) N)rrncsg|] }|dtqS)rBrFr^rTargr.r*r+rUsz'SnapToGrid.__init__..c3|] }|dtVqdSrGrrr.r*r+rz&SnapToGrid.__init__..rnc3rrGrrr.r*r+rrrz2Must provide 1, 2, or 4 arguments to `SnapToGrid`.)rextendr!rr)r"ryr<r$nargsr#r(r.r+rszSnapToGrid.__init__rr*r*r(r+rrc@r) SymDifferencernroNrr*r*r*r+rrrcr)rc s>|||dtg}d|vrt|d|d<tj|i|dS)Nrrr)rFrxrrr)r"ryrr$r#r(r*r+rs  zTransform.__init__rr*r*r(r+rrrcr) Translatec s@|}t|jdkr|jtdtt|j||fi|S)Nrr)rVrrrwrrrrYrr(r*r+rYszTranslate.as_sqlite)r-rHrIrYrKr*r*r(r+rrrc@r)UnionrnroNrr*r*r*r+rrrN)Edecimalr#django.contrib.gis.db.models.fieldsrr django.contrib.gis.db.models.sqlrrdjango.contrib.gis.geosrdjango.core.exceptionsr django.dbr django.db.modelsr r r r rrrrdjango.db.models.functionsrdjango.utils.functionalrrxrSr^rrMrPrRr[rarmrrr}rrrrrrrrrrrrrrregister_lookuprrrrrrrrrrrrrrrr*r*r*r+sd    (   F    +