o 3a-F@sddlZddlZddlZddlZddlmZmZmZmZm Z m Z m Z m Z ddl mZddlmZddlmZddlmZddlmZddlmZmZmZmZdd lmZmZdd l m!Z!dd l"m#Z#m$Z$dd l%m&Z&Gd dde'Z(GdddeZ)dS)N) addressofbyrefc_bufferc_char_pc_doublec_intc_void_p string_at)Driver) GDALException)rasterBandList)GDALRasterBase)GDAL_RESAMPLE_ALGORITHMSVSI_DELETE_BUFFER_ON_READVSI_FILESYSTEM_BASE_PATHVSI_TAKE_BUFFER_OWNERSHIP)SpatialReference SRSException) json_regex) force_bytes force_str)cached_propertycs\eZdZddddZfddZeddZejd dZed d Zejd d ZZ S) TransformPoint)r)))originscaleskewcsH|j|j|d}|j|j|d}t||g||_||_dS)Nrr) geotransformindicessuper__init___raster_prop)selfr propxy __class__G/usr/lib/python3/dist-packages/django/contrib/gis/gdal/raster/source.pyr&s  zTransformPoint.__init__cC|dSNrr/r)r/r/r0r+&zTransformPoint.xcC(|jj}|||j|jd<||j_dSr2r'r#r$r(r)valuegtfr/r/r0r+* cCr1Nrr/r3r/r/r0r,0r4zTransformPoint.ycCr5r;r6r7r/r/r0r,4r:) __name__ __module__ __qualname__r$r&propertyr+setterr, __classcell__r/r/r-r0rs    rcsHeZdZdZejZd7ddZfddZddZ d d Z d d Z e d dZ eddZe ddZeddZe ddZe ddZe ddZejddZe ddZejddZe dd Zejd!d Ze d"d#Ze d$d%Ze d&d'Ze d(d)Ze d*d+Zd8d.d/Zd9d1d2Z , -d:d3d4Ze d5d6Z Z!S); GDALRasterz0 Wrap a raster GDAL Data Source object. FcCs~|rdnd|_tt|trt|rt|}t|trR| t s0t j |s0td|z tt||j|_WdStyQ}ztd||d}~wwt|trd|_t|}t||_t j t tt}tt|t|j|tz tt||j|_WdStytt|tdwt|t rd|_t|!dd}|j"dkrd|vrtd |j"d |vsd |vrtd d |vrtdg}|!di#D]\}} d|| } |$| %&q|$dt't(||}t)|jt|!dd|d |d |!dt(|!dg|!ddt||_t*|!dgD]G\} } |j+| } d| vr]| d| _,| j,dur]d| vsTd| vsTd| vr]| j-| j,fdd| j-| !d| !d| !d| !ddq,|!d |_.d|vr|d\|j/_0|j/_1d|vr|d\|j2_0|j2_1d |vr|d \|j3_0|j3_1dSdSt|t4r||_dStd!t5|)"Nrrz(Unable to read raster source input "%s".z+Could not open the datasource at "{}" ({}).z1Failed creating VSI raster from the input buffer.driverMEMnamez5Specify name for creation of raster with driver "{}".widthheightz;Specify width and height attributes for JSON or dict input.sridz$Specify srid for JSON or dict input. papsz_optionsz{}={} nr_of_bandsbandsdatatype nodata_valuedatasizeshape)rr)rPrRoffset)rPrQrRrSr r!r"z%Invalid data source input type: "{}".)6_writer ensure_registered isinstancestrrmatchjsonloads startswithrospathexistsr capiopen_dsr_ptrformatbytessys getsizeofr _ds_inputjoinuuiduuid4create_vsi_file_from_mem_bufferrrunlink_vsi_filedictgetrEitemsappendupperencoderlen create_ds enumeraterLrOrPsrsr r+r,r!r"rtype)r)ds_inputwriteerrrQvsi_pathrCrIkeyvaloptioni band_inputbandr/r/r0r&As                        zGDALRaster.__init__cs$|jr tt|jtdSN) is_vsi_basedr_rkrrEr%__del__r3r-r/r0rszGDALRaster.__del__cCs|jSr)rEr3r/r/r0__str__szGDALRaster.__str__cCsdtt|jS)zJ Short-hand representation because WKB may be very large. z)hexrrar3r/r/r0__repr__szGDALRaster.__repr__cCs|jstdt|jdS)a  Flush all data from memory into the source file if it exists. The data that needs flushing are geotransforms, coordinate systems, nodata_values and pixel values. This function will be called automatically wherever it is needed. z9Raster needs to be opened in write mode to change values.N)rTr r_flush_dsrar3r/r/r0_flushszGDALRaster._flushcCs4|jsdSt}tt|jt|t}t||j Sr) rrr_get_mem_buffer_from_vsi_filerrErrr r8)r) out_lengthdatr/r/r0 vsi_buffers zGDALRaster.vsi_buffercCs|jo|jtSr)rarEr[rr3r/r/r0rszGDALRaster.is_vsi_basedcCstt|jS)zi Return the name of this raster. Corresponds to filename for file-based rasters. )rr_get_ds_descriptionrar3r/r/r0rEszGDALRaster.namecCst|j}t|S)z> Return the GDAL Driver used for this raster. )r_ get_ds_driverrar )r) ds_driverr/r/r0rCs zGDALRaster.drivercC t|jS)z+ Width (X axis) in pixels. )r_ get_ds_xsizerar3r/r/r0rF zGDALRaster.widthcCr)z, Height (Y axis) in pixels. )r_ get_ds_ysizerar3r/r/r0rGrzGDALRaster.heightcCs:zt|j}|s WdSt|ddWStyYdSw)zF Return the SpatialReference used in this GDALRaster. Nwkt)srs_type)r_get_ds_projection_refrarr)r)rr/r/r0ru s  zGDALRaster.srscCsPt|tr|}nt|ttfrt|}ntdt|j|j | dS)z Set the spatial reference used in this GDALRaster. The input can be a SpatialReference or any parameter accepted by the SpatialReference constructor. z/Could not create a SpatialReference from input.N) rVrintrW ValueErrorr_set_ds_projection_refrarrqr)r)r8rur/r/r0rus   cCs|jjS)zA Shortcut to access the srid of this GDALRaster. )rurHr3r/r/r0rH(szGDALRaster.sridcCs ||_dS)zE Shortcut to set this GDALRaster's srs from an srid. N)ru)r)r8r/r/r0rH/ cCs$td}t|jt|t|S)z Return the geotransform of the data source. Return the default geotransform if it does not exist or has not been set previously. The default is [0.0, 1.0, 0.0, 0.0, 0.0, -1.0]. rN)rr_get_ds_geotransformrarlist)r)r9r/r/r0r#6s zGDALRaster.geotransformcCsPt|dkstdd|Dstdtd|}t|jt||dS)z)Set the geotransform for the data source.rNcss|] }t|ttfVqdSr)rVrfloat).0r+r/r/r0 Esz*GDALRaster.geotransform..z.Geotransform must consist of 6 numeric values.N) rrallrrr_set_ds_geotransformrarr)r)valuesr/r/r0r#Bs   cC t|dS)z3 Coordinates of the raster origin. r rr3r/r/r0r LrzGDALRaster.origincCr)z@ Pixel scale in units of the raster projection. r!rr3r/r/r0r!SrzGDALRaster.scalecCr)z7 Skew of pixels (rotation parameters). r"rr3r/r/r0r"ZrzGDALRaster.skewcCsp|jj|jj|j}|jj|jj|j}t||jj}t||jj}t||jj}t||jj}||||fS)zJ Return the extent as a 4-tuple (xmin, ymin, xmax, ymax). )r r+r!rFr,rGminmax)r)xvalyvalxminxmaxyminymaxr/r/r0extentas zGDALRaster.extentcCst|Srr r3r/r/r0rLqr4zGDALRaster.bandsNearestNeighbourc Cs|d|j|d|j|d|jj|d|j|d|j|d|j|d|jj d|vrB|j d |jj |d<d |vrO|j d  |d <d d |j D|d<t |dd}t |}t|j|jj|j|jj|d|ttt ||S)a  Return a warped GDALRaster with the given input characteristics. The input is expected to be a dictionary containing the parameters of the target raster. Allowed values are width, height, SRID, origin, scale, skew, datatype, driver, and name (filename). By default, the warp functions keeps all parameters equal to the values of the original source raster. For the name of the target raster, the name of the source raster will be used and appended with _copy. + source_driver_name. In addition, the resampling algorithm can be specified with the "resampling" input parameter. The default is NearestNeighbor. For a list of all options consult the GDAL_RESAMPLE_ALGORITHMS constant. rFrGrHr r!r"rCrE_copy.rMrcSsg|]}d|jiqSrOr)rbndr/r/r0 sz#GDALRaster.warp..rLTrxr) setdefaultrFrGrurHr r!r"rCrErLrMrBrr_reproject_imagerarrqrr)r)rw resampling max_errortarget algorithmr/r/r0warpus,  zGDALRaster.warpNc Csr|r|}n|jjdkr|jd|jj}n tjttt}t t |jj t ||j tttt|jdS)z"Return a clone of this GDALRaster.rDrr)rCrEr\r]rgrrWrhrirBr_copy_dsrarrrrrT)r)rE clone_namer/r/r0clones"  zGDALRaster.clonec Cst|}t|tr |}nt|ttfrt|}ntd|j|jkr/|r*||jjkr/| |St |j |j j|j||t}t|}|j|j|j|jj|jjg|jj|jjg|jj|jjgd} |rj|| d<|rp|| d<|j| ||dS)zk Return a copy of this raster reprojected into the given spatial reference system. zETransform only accepts SpatialReference, string, and integer objects.)rHrFrGr r!r"rCrE)rr)rrVrrrW TypeErrorrHrCrErr_auto_create_warped_vrtrarurrqrrBrFrGr r+r,r!r"r) r)rurCrErrr target_srsrrPr/r/r0 transforms6    zGDALRaster.transformcCs tjstdt|jdS)z Return information about this raster in a string format equivalent to the output of the gdalinfo command line utility. u5GDAL ≥ 2.1 is required for using the info property.N)r_ get_ds_inforptrdecoder3r/r/r0infoszGDALRaster.info)F)rrr)NNrr)"r<r=r>__doc__r_close_ds destructorr&rrrrr?rrrrErCrFrGrur@rHr#r r!r"rrLrrrrrAr/r/r-r0rB;sb  ~                   7 0rB)*rYr\rdrhctypesrrrrrrrr django.contrib.gis.gdal.driverr django.contrib.gis.gdal.errorr "django.contrib.gis.gdal.prototypesr r_#django.contrib.gis.gdal.raster.bandr#django.contrib.gis.gdal.raster.baser$django.contrib.gis.gdal.raster.constrrrrdjango.contrib.gis.gdal.srsrrdjango.contrib.gis.geometryrdjango.utils.encodingrrdjango.utils.functionalrrrrBr/r/r/r0s"(       #