MCPcopy
hub / github.com/django/django / DataSource

Class DataSource

django/contrib/gis/gdal/datasource.py:50–128  ·  view source on GitHub ↗

Wraps an OGR Data Source object.

Source from the content-addressed store, hash-verified

48#
49# The OGR_DS_* routines are relevant here.
50class DataSource(GDALBase):
51 "Wraps an OGR Data Source object."
52
53 destructor = capi.destroy_ds
54
55 def __init__(self, ds_input, ds_driver=False, write=False, encoding="utf-8"):
56 # The write flag.
57 self._write = capi.GDAL_OF_UPDATE if write else capi.GDAL_OF_READONLY
58 # See also https://gdal.org/development/rfc/rfc23_ogr_unicode.html
59 self.encoding = encoding
60
61 Driver.ensure_registered()
62
63 if isinstance(ds_input, (str, Path)):
64 try:
65 # GDALOpenEx will auto-detect the data source type.
66 ds = capi.open_ds(
67 force_bytes(ds_input),
68 self._write | capi.GDAL_OF_VECTOR,
69 None,
70 None,
71 None,
72 )
73 except GDALException:
74 # Making the error message more clear rather than something
75 # like "Invalid pointer returned from OGROpen".
76 raise GDALException('Could not open the datasource at "%s"' % ds_input)
77 elif isinstance(ds_input, self.ptr_type) and isinstance(
78 ds_driver, Driver.ptr_type
79 ):
80 ds = ds_input
81 else:
82 raise GDALException("Invalid data source input type: %s" % type(ds_input))
83
84 if ds:
85 self.ptr = ds
86 driver = capi.get_dataset_driver(ds)
87 self.driver = Driver(driver)
88 else:
89 # Raise an exception if the returned pointer is NULL
90 raise GDALException('Invalid data source file "%s"' % ds_input)
91
92 def __getitem__(self, index):
93 "Allows use of the index [] operator to get a layer at the index."
94 if isinstance(index, str):
95 try:
96 layer = capi.get_layer_by_name(self.ptr, force_bytes(index))
97 except GDALException:
98 raise IndexError("Invalid OGR layer name given: %s." % index)
99 elif isinstance(index, int):
100 if 0 <= index < self.layer_count:
101 layer = capi.get_layer(self._ptr, index)
102 else:
103 raise IndexError(
104 "Index out of range when accessing layers in a datasource: %s."
105 % index
106 )
107 else:

Callers 15

ogrinfoFunction · 0.90
mappingFunction · 0.90
_ogrinspectFunction · 0.90
__init__Method · 0.90
test_simple_layermapMethod · 0.90
test_layermap_strictMethod · 0.90
test01_valid_shpMethod · 0.90
test_ds_input_pathlibMethod · 0.90
test02_invalid_shpMethod · 0.90
test03a_layersMethod · 0.90
test03b_layer_sliceMethod · 0.90
get_layerMethod · 0.90

Calls

no outgoing calls

Tested by 13

test_simple_layermapMethod · 0.72
test_layermap_strictMethod · 0.72
test01_valid_shpMethod · 0.72
test_ds_input_pathlibMethod · 0.72
test02_invalid_shpMethod · 0.72
test03a_layersMethod · 0.72
test03b_layer_sliceMethod · 0.72
get_layerMethod · 0.72
test04_featuresMethod · 0.72
test05_geometriesMethod · 0.72
test06_spatial_filterMethod · 0.72