You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

source.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import json
  2. import os
  3. import sys
  4. import uuid
  5. from ctypes import (
  6. addressof, byref, c_buffer, c_char_p, c_double, c_int, c_void_p, string_at,
  7. )
  8. from django.contrib.gis.gdal.driver import Driver
  9. from django.contrib.gis.gdal.error import GDALException
  10. from django.contrib.gis.gdal.prototypes import raster as capi
  11. from django.contrib.gis.gdal.raster.band import BandList
  12. from django.contrib.gis.gdal.raster.base import GDALRasterBase
  13. from django.contrib.gis.gdal.raster.const import (
  14. GDAL_RESAMPLE_ALGORITHMS, VSI_DELETE_BUFFER_ON_READ,
  15. VSI_FILESYSTEM_BASE_PATH, VSI_TAKE_BUFFER_OWNERSHIP,
  16. )
  17. from django.contrib.gis.gdal.srs import SpatialReference, SRSException
  18. from django.contrib.gis.geometry import json_regex
  19. from django.utils.encoding import force_bytes, force_text
  20. from django.utils.functional import cached_property
  21. class TransformPoint(list):
  22. indices = {
  23. 'origin': (0, 3),
  24. 'scale': (1, 5),
  25. 'skew': (2, 4),
  26. }
  27. def __init__(self, raster, prop):
  28. x = raster.geotransform[self.indices[prop][0]]
  29. y = raster.geotransform[self.indices[prop][1]]
  30. super().__init__([x, y])
  31. self._raster = raster
  32. self._prop = prop
  33. @property
  34. def x(self):
  35. return self[0]
  36. @x.setter
  37. def x(self, value):
  38. gtf = self._raster.geotransform
  39. gtf[self.indices[self._prop][0]] = value
  40. self._raster.geotransform = gtf
  41. @property
  42. def y(self):
  43. return self[1]
  44. @y.setter
  45. def y(self, value):
  46. gtf = self._raster.geotransform
  47. gtf[self.indices[self._prop][1]] = value
  48. self._raster.geotransform = gtf
  49. class GDALRaster(GDALRasterBase):
  50. """
  51. Wrap a raster GDAL Data Source object.
  52. """
  53. destructor = capi.close_ds
  54. def __init__(self, ds_input, write=False):
  55. self._write = 1 if write else 0
  56. Driver.ensure_registered()
  57. # Preprocess json inputs. This converts json strings to dictionaries,
  58. # which are parsed below the same way as direct dictionary inputs.
  59. if isinstance(ds_input, str) and json_regex.match(ds_input):
  60. ds_input = json.loads(ds_input)
  61. # If input is a valid file path, try setting file as source.
  62. if isinstance(ds_input, str):
  63. try:
  64. # GDALOpen will auto-detect the data source type.
  65. self._ptr = capi.open_ds(force_bytes(ds_input), self._write)
  66. except GDALException as err:
  67. raise GDALException('Could not open the datasource at "{}" ({}).'.format(ds_input, err))
  68. elif isinstance(ds_input, bytes):
  69. # Create a new raster in write mode.
  70. self._write = 1
  71. # Get size of buffer.
  72. size = sys.getsizeof(ds_input)
  73. # Pass data to ctypes, keeping a reference to the ctypes object so
  74. # that the vsimem file remains available until the GDALRaster is
  75. # deleted.
  76. self._ds_input = c_buffer(ds_input)
  77. # Create random name to reference in vsimem filesystem.
  78. vsi_path = os.path.join(VSI_FILESYSTEM_BASE_PATH, str(uuid.uuid4()))
  79. # Create vsimem file from buffer.
  80. capi.create_vsi_file_from_mem_buffer(
  81. force_bytes(vsi_path),
  82. byref(self._ds_input),
  83. size,
  84. VSI_TAKE_BUFFER_OWNERSHIP,
  85. )
  86. # Open the new vsimem file as a GDALRaster.
  87. try:
  88. self._ptr = capi.open_ds(force_bytes(vsi_path), self._write)
  89. except GDALException:
  90. # Remove the broken file from the VSI filesystem.
  91. capi.unlink_vsi_file(force_bytes(vsi_path))
  92. raise GDALException('Failed creating VSI raster from the input buffer.')
  93. elif isinstance(ds_input, dict):
  94. # A new raster needs to be created in write mode
  95. self._write = 1
  96. # Create driver (in memory by default)
  97. driver = Driver(ds_input.get('driver', 'MEM'))
  98. # For out of memory drivers, check filename argument
  99. if driver.name != 'MEM' and 'name' not in ds_input:
  100. raise GDALException('Specify name for creation of raster with driver "{}".'.format(driver.name))
  101. # Check if width and height where specified
  102. if 'width' not in ds_input or 'height' not in ds_input:
  103. raise GDALException('Specify width and height attributes for JSON or dict input.')
  104. # Check if srid was specified
  105. if 'srid' not in ds_input:
  106. raise GDALException('Specify srid for JSON or dict input.')
  107. # Create null terminated gdal options array.
  108. papsz_options = []
  109. for key, val in ds_input.get('papsz_options', {}).items():
  110. option = '{}={}'.format(key, val)
  111. papsz_options.append(option.upper().encode())
  112. papsz_options.append(None)
  113. # Convert papszlist to ctypes array.
  114. papsz_options = (c_char_p * len(papsz_options))(*papsz_options)
  115. # Create GDAL Raster
  116. self._ptr = capi.create_ds(
  117. driver._ptr,
  118. force_bytes(ds_input.get('name', '')),
  119. ds_input['width'],
  120. ds_input['height'],
  121. ds_input.get('nr_of_bands', len(ds_input.get('bands', []))),
  122. ds_input.get('datatype', 6),
  123. byref(papsz_options),
  124. )
  125. # Set band data if provided
  126. for i, band_input in enumerate(ds_input.get('bands', [])):
  127. band = self.bands[i]
  128. if 'nodata_value' in band_input:
  129. band.nodata_value = band_input['nodata_value']
  130. # Instantiate band filled with nodata values if only
  131. # partial input data has been provided.
  132. if band.nodata_value is not None and (
  133. 'data' not in band_input or
  134. 'size' in band_input or
  135. 'shape' in band_input):
  136. band.data(data=(band.nodata_value,), shape=(1, 1))
  137. # Set band data values from input.
  138. band.data(
  139. data=band_input.get('data'),
  140. size=band_input.get('size'),
  141. shape=band_input.get('shape'),
  142. offset=band_input.get('offset'),
  143. )
  144. # Set SRID
  145. self.srs = ds_input.get('srid')
  146. # Set additional properties if provided
  147. if 'origin' in ds_input:
  148. self.origin.x, self.origin.y = ds_input['origin']
  149. if 'scale' in ds_input:
  150. self.scale.x, self.scale.y = ds_input['scale']
  151. if 'skew' in ds_input:
  152. self.skew.x, self.skew.y = ds_input['skew']
  153. elif isinstance(ds_input, c_void_p):
  154. # Instantiate the object using an existing pointer to a gdal raster.
  155. self._ptr = ds_input
  156. else:
  157. raise GDALException('Invalid data source input type: "{}".'.format(type(ds_input)))
  158. def __del__(self):
  159. if self.is_vsi_based:
  160. # Remove the temporary file from the VSI in-memory filesystem.
  161. capi.unlink_vsi_file(force_bytes(self.name))
  162. super().__del__()
  163. def __str__(self):
  164. return self.name
  165. def __repr__(self):
  166. """
  167. Short-hand representation because WKB may be very large.
  168. """
  169. return '<Raster object at %s>' % hex(addressof(self._ptr))
  170. def _flush(self):
  171. """
  172. Flush all data from memory into the source file if it exists.
  173. The data that needs flushing are geotransforms, coordinate systems,
  174. nodata_values and pixel values. This function will be called
  175. automatically wherever it is needed.
  176. """
  177. # Raise an Exception if the value is being changed in read mode.
  178. if not self._write:
  179. raise GDALException('Raster needs to be opened in write mode to change values.')
  180. capi.flush_ds(self._ptr)
  181. @property
  182. def vsi_buffer(self):
  183. if not self.is_vsi_based:
  184. return None
  185. # Prepare an integer that will contain the buffer length.
  186. out_length = c_int()
  187. # Get the data using the vsi file name.
  188. dat = capi.get_mem_buffer_from_vsi_file(
  189. force_bytes(self.name),
  190. byref(out_length),
  191. VSI_DELETE_BUFFER_ON_READ,
  192. )
  193. # Read the full buffer pointer.
  194. return string_at(dat, out_length.value)
  195. @cached_property
  196. def is_vsi_based(self):
  197. return self.name.startswith(VSI_FILESYSTEM_BASE_PATH)
  198. @property
  199. def name(self):
  200. """
  201. Return the name of this raster. Corresponds to filename
  202. for file-based rasters.
  203. """
  204. return force_text(capi.get_ds_description(self._ptr))
  205. @cached_property
  206. def driver(self):
  207. """
  208. Return the GDAL Driver used for this raster.
  209. """
  210. ds_driver = capi.get_ds_driver(self._ptr)
  211. return Driver(ds_driver)
  212. @property
  213. def width(self):
  214. """
  215. Width (X axis) in pixels.
  216. """
  217. return capi.get_ds_xsize(self._ptr)
  218. @property
  219. def height(self):
  220. """
  221. Height (Y axis) in pixels.
  222. """
  223. return capi.get_ds_ysize(self._ptr)
  224. @property
  225. def srs(self):
  226. """
  227. Return the SpatialReference used in this GDALRaster.
  228. """
  229. try:
  230. wkt = capi.get_ds_projection_ref(self._ptr)
  231. if not wkt:
  232. return None
  233. return SpatialReference(wkt, srs_type='wkt')
  234. except SRSException:
  235. return None
  236. @srs.setter
  237. def srs(self, value):
  238. """
  239. Set the spatial reference used in this GDALRaster. The input can be
  240. a SpatialReference or any parameter accepted by the SpatialReference
  241. constructor.
  242. """
  243. if isinstance(value, SpatialReference):
  244. srs = value
  245. elif isinstance(value, (int, str)):
  246. srs = SpatialReference(value)
  247. else:
  248. raise ValueError('Could not create a SpatialReference from input.')
  249. capi.set_ds_projection_ref(self._ptr, srs.wkt.encode())
  250. self._flush()
  251. @property
  252. def srid(self):
  253. """
  254. Shortcut to access the srid of this GDALRaster.
  255. """
  256. return self.srs.srid
  257. @srid.setter
  258. def srid(self, value):
  259. """
  260. Shortcut to set this GDALRaster's srs from an srid.
  261. """
  262. self.srs = value
  263. @property
  264. def geotransform(self):
  265. """
  266. Return the geotransform of the data source.
  267. Return the default geotransform if it does not exist or has not been
  268. set previously. The default is [0.0, 1.0, 0.0, 0.0, 0.0, -1.0].
  269. """
  270. # Create empty ctypes double array for data
  271. gtf = (c_double * 6)()
  272. capi.get_ds_geotransform(self._ptr, byref(gtf))
  273. return list(gtf)
  274. @geotransform.setter
  275. def geotransform(self, values):
  276. "Set the geotransform for the data source."
  277. if len(values) != 6 or not all(isinstance(x, (int, float)) for x in values):
  278. raise ValueError('Geotransform must consist of 6 numeric values.')
  279. # Create ctypes double array with input and write data
  280. values = (c_double * 6)(*values)
  281. capi.set_ds_geotransform(self._ptr, byref(values))
  282. self._flush()
  283. @property
  284. def origin(self):
  285. """
  286. Coordinates of the raster origin.
  287. """
  288. return TransformPoint(self, 'origin')
  289. @property
  290. def scale(self):
  291. """
  292. Pixel scale in units of the raster projection.
  293. """
  294. return TransformPoint(self, 'scale')
  295. @property
  296. def skew(self):
  297. """
  298. Skew of pixels (rotation parameters).
  299. """
  300. return TransformPoint(self, 'skew')
  301. @property
  302. def extent(self):
  303. """
  304. Return the extent as a 4-tuple (xmin, ymin, xmax, ymax).
  305. """
  306. # Calculate boundary values based on scale and size
  307. xval = self.origin.x + self.scale.x * self.width
  308. yval = self.origin.y + self.scale.y * self.height
  309. # Calculate min and max values
  310. xmin = min(xval, self.origin.x)
  311. xmax = max(xval, self.origin.x)
  312. ymin = min(yval, self.origin.y)
  313. ymax = max(yval, self.origin.y)
  314. return xmin, ymin, xmax, ymax
  315. @property
  316. def bands(self):
  317. return BandList(self)
  318. def warp(self, ds_input, resampling='NearestNeighbour', max_error=0.0):
  319. """
  320. Return a warped GDALRaster with the given input characteristics.
  321. The input is expected to be a dictionary containing the parameters
  322. of the target raster. Allowed values are width, height, SRID, origin,
  323. scale, skew, datatype, driver, and name (filename).
  324. By default, the warp functions keeps all parameters equal to the values
  325. of the original source raster. For the name of the target raster, the
  326. name of the source raster will be used and appended with
  327. _copy. + source_driver_name.
  328. In addition, the resampling algorithm can be specified with the "resampling"
  329. input parameter. The default is NearestNeighbor. For a list of all options
  330. consult the GDAL_RESAMPLE_ALGORITHMS constant.
  331. """
  332. # Get the parameters defining the geotransform, srid, and size of the raster
  333. ds_input.setdefault('width', self.width)
  334. ds_input.setdefault('height', self.height)
  335. ds_input.setdefault('srid', self.srs.srid)
  336. ds_input.setdefault('origin', self.origin)
  337. ds_input.setdefault('scale', self.scale)
  338. ds_input.setdefault('skew', self.skew)
  339. # Get the driver, name, and datatype of the target raster
  340. ds_input.setdefault('driver', self.driver.name)
  341. if 'name' not in ds_input:
  342. ds_input['name'] = self.name + '_copy.' + self.driver.name
  343. if 'datatype' not in ds_input:
  344. ds_input['datatype'] = self.bands[0].datatype()
  345. # Instantiate raster bands filled with nodata values.
  346. ds_input['bands'] = [{'nodata_value': bnd.nodata_value} for bnd in self.bands]
  347. # Create target raster
  348. target = GDALRaster(ds_input, write=True)
  349. # Select resampling algorithm
  350. algorithm = GDAL_RESAMPLE_ALGORITHMS[resampling]
  351. # Reproject image
  352. capi.reproject_image(
  353. self._ptr, self.srs.wkt.encode(),
  354. target._ptr, target.srs.wkt.encode(),
  355. algorithm, 0.0, max_error,
  356. c_void_p(), c_void_p(), c_void_p()
  357. )
  358. # Make sure all data is written to file
  359. target._flush()
  360. return target
  361. def transform(self, srid, driver=None, name=None, resampling='NearestNeighbour',
  362. max_error=0.0):
  363. """
  364. Return a copy of this raster reprojected into the given SRID.
  365. """
  366. # Convert the resampling algorithm name into an algorithm id
  367. algorithm = GDAL_RESAMPLE_ALGORITHMS[resampling]
  368. # Instantiate target spatial reference system
  369. target_srs = SpatialReference(srid)
  370. # Create warped virtual dataset in the target reference system
  371. target = capi.auto_create_warped_vrt(
  372. self._ptr, self.srs.wkt.encode(), target_srs.wkt.encode(),
  373. algorithm, max_error, c_void_p()
  374. )
  375. target = GDALRaster(target)
  376. # Construct the target warp dictionary from the virtual raster
  377. data = {
  378. 'srid': srid,
  379. 'width': target.width,
  380. 'height': target.height,
  381. 'origin': [target.origin.x, target.origin.y],
  382. 'scale': [target.scale.x, target.scale.y],
  383. 'skew': [target.skew.x, target.skew.y],
  384. }
  385. # Set the driver and filepath if provided
  386. if driver:
  387. data['driver'] = driver
  388. if name:
  389. data['name'] = name
  390. # Warp the raster into new srid
  391. return self.warp(data, resampling=resampling, max_error=max_error)
  392. @property
  393. def info(self):
  394. """
  395. Return information about this raster in a string format equivalent
  396. to the output of the gdalinfo command line utility.
  397. """
  398. if not capi.get_ds_info:
  399. raise ValueError('GDAL ≥ 2.1 is required for using the info property.')
  400. return capi.get_ds_info(self.ptr, None).decode()