Debian Codegraph @

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

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