Development of an internal social media platform with personalised dashboards for students
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.

base.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.contrib.gis.gdal.base import GDALBase
  2. from django.contrib.gis.gdal.prototypes import raster as capi
  3. class GDALRasterBase(GDALBase):
  4. """
  5. Attributes that exist on both GDALRaster and GDALBand.
  6. """
  7. @property
  8. def metadata(self):
  9. """
  10. Return the metadata for this raster or band. The return value is a
  11. nested dictionary, where the first-level key is the metadata domain and
  12. the second-level is the metadata item names and values for that domain.
  13. """
  14. if not capi.get_ds_metadata_domain_list:
  15. raise ValueError('GDAL ≥ 1.11 is required for using the metadata property.')
  16. # The initial metadata domain list contains the default domain.
  17. # The default is returned if domain name is None.
  18. domain_list = ['DEFAULT']
  19. # Get additional metadata domains from the raster.
  20. meta_list = capi.get_ds_metadata_domain_list(self._ptr)
  21. if meta_list:
  22. # The number of domains is unknown, so retrieve data until there
  23. # are no more values in the ctypes array.
  24. counter = 0
  25. domain = meta_list[counter]
  26. while domain:
  27. domain_list.append(domain.decode())
  28. counter += 1
  29. domain = meta_list[counter]
  30. # Free domain list array.
  31. capi.free_dsl(meta_list)
  32. # Retrieve metadata values for each domain.
  33. result = {}
  34. for domain in domain_list:
  35. # Get metadata for this domain.
  36. data = capi.get_ds_metadata(
  37. self._ptr,
  38. (None if domain == 'DEFAULT' else domain.encode()),
  39. )
  40. if not data:
  41. continue
  42. # The number of metadata items is unknown, so retrieve data until
  43. # there are no more values in the ctypes array.
  44. domain_meta = {}
  45. counter = 0
  46. item = data[counter]
  47. while item:
  48. key, val = item.decode().split('=')
  49. domain_meta[key] = val
  50. counter += 1
  51. item = data[counter]
  52. # The default domain values are returned if domain is None.
  53. result[domain or 'DEFAULT'] = domain_meta
  54. return result
  55. @metadata.setter
  56. def metadata(self, value):
  57. """
  58. Set the metadata. Update only the domains that are contained in the
  59. value dictionary.
  60. """
  61. # Loop through domains.
  62. for domain, metadata in value.items():
  63. # Set the domain to None for the default, otherwise encode.
  64. domain = None if domain == 'DEFAULT' else domain.encode()
  65. # Set each metadata entry separately.
  66. for meta_name, meta_value in metadata.items():
  67. capi.set_ds_metadata_item(
  68. self._ptr, meta_name.encode(),
  69. meta_value.encode() if meta_value else None,
  70. domain,
  71. )