Debian Codegraph @

django/contrib/gis/gdal/raster/source.py

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