2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
DataSource is a wrapper for the OGR Data Source object, which provides
|
|
|
|
an interface for reading vector geometry data from many different file
|
|
|
|
formats (including ESRI shapefiles).
|
|
|
|
|
|
|
|
When instantiating a DataSource object, use the filename of a
|
|
|
|
GDAL-supported data source. For example, a SHP file or a
|
|
|
|
TIGER/Line file from the government.
|
|
|
|
|
|
|
|
The ds_driver keyword is used internally when a ctypes pointer
|
|
|
|
is passed in directly.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
ds = DataSource('/home/foo/bar.shp')
|
|
|
|
for layer in ds:
|
|
|
|
for feature in layer:
|
|
|
|
# Getting the geometry for the feature.
|
|
|
|
g = feature.geom
|
|
|
|
|
|
|
|
# Getting the 'description' field for the feature.
|
|
|
|
desc = feature['description']
|
|
|
|
|
|
|
|
# We can also increment through all of the fields
|
|
|
|
# attached to this feature.
|
|
|
|
for field in feature:
|
|
|
|
# Get the name of the field (e.g. 'description')
|
|
|
|
nm = field.name
|
|
|
|
|
|
|
|
# Get the type (integer) of the field, e.g. 0 => OFTInteger
|
|
|
|
t = field.type
|
|
|
|
|
|
|
|
# Returns the value the field; OFTIntegers return ints,
|
|
|
|
# OFTReal returns floats, all else returns string.
|
|
|
|
val = field.value
|
|
|
|
"""
|
2011-07-13 17:35:51 +08:00
|
|
|
from ctypes import byref
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.base import GDALBase
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.gdal.driver import Driver
|
2017-09-04 09:19:37 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.gdal.layer import Layer
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.prototypes import ds as capi
|
2017-01-26 17:08:08 +08:00
|
|
|
from django.utils.encoding import force_bytes, force_str
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# For more information, see the OGR C API source code:
|
2018-09-26 14:48:47 +08:00
|
|
|
# https://www.gdal.org/ogr__api_8h.html
|
2008-08-06 02:13:06 +08:00
|
|
|
#
|
|
|
|
# The OGR_DS_* routines are relevant here.
|
2009-03-08 07:02:48 +08:00
|
|
|
class DataSource(GDALBase):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Wraps an OGR Data Source object."
|
2016-12-16 05:59:08 +08:00
|
|
|
destructor = capi.destroy_ds
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-10-07 04:56:47 +08:00
|
|
|
def __init__(self, ds_input, ds_driver=False, write=False, encoding='utf-8'):
|
2008-08-06 02:13:06 +08:00
|
|
|
# The write flag.
|
|
|
|
if write:
|
|
|
|
self._write = 1
|
|
|
|
else:
|
|
|
|
self._write = 0
|
2018-09-26 14:48:47 +08:00
|
|
|
# See also https://trac.osgeo.org/gdal/wiki/rfc23_ogr_unicode
|
2012-10-07 04:56:47 +08:00
|
|
|
self.encoding = encoding
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
Driver.ensure_registered()
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(ds_input, str):
|
2008-08-06 02:13:06 +08:00
|
|
|
# The data source driver is a void pointer.
|
2009-03-08 07:02:48 +08:00
|
|
|
ds_driver = Driver.ptr_type()
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
|
|
|
# OGROpen will auto-detect the data source type.
|
2012-09-24 01:59:27 +08:00
|
|
|
ds = capi.open_ds(force_bytes(ds_input), self._write, byref(ds_driver))
|
2014-12-18 05:00:05 +08:00
|
|
|
except GDALException:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Making the error message more clear rather than something
|
|
|
|
# like "Invalid pointer returned from OGROpen".
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Could not open the datasource at "%s"' % ds_input)
|
2009-03-08 07:02:48 +08:00
|
|
|
elif isinstance(ds_input, self.ptr_type) and isinstance(ds_driver, Driver.ptr_type):
|
2008-08-06 02:13:06 +08:00
|
|
|
ds = ds_input
|
|
|
|
else:
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Invalid data source input type: %s' % type(ds_input))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-10-17 20:27:34 +08:00
|
|
|
if ds:
|
2009-03-08 07:02:48 +08:00
|
|
|
self.ptr = ds
|
|
|
|
self.driver = Driver(ds_driver)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2012-07-20 20:22:00 +08:00
|
|
|
# Raise an exception if the returned pointer is NULL
|
2014-12-18 05:00:05 +08:00
|
|
|
raise GDALException('Invalid data source file "%s"' % ds_input)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"Allows use of the index [] operator to get a layer at the index."
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(index, str):
|
2017-09-13 01:19:44 +08:00
|
|
|
try:
|
|
|
|
layer = capi.get_layer_by_name(self.ptr, force_bytes(index))
|
|
|
|
except GDALException:
|
2017-09-02 15:38:17 +08:00
|
|
|
raise IndexError('Invalid OGR layer name given: %s.' % index)
|
2008-08-06 02:13:06 +08:00
|
|
|
elif isinstance(index, int):
|
2017-09-01 22:48:43 +08:00
|
|
|
if 0 <= index < self.layer_count:
|
|
|
|
layer = capi.get_layer(self._ptr, index)
|
|
|
|
else:
|
2017-09-02 15:38:17 +08:00
|
|
|
raise IndexError('Index out of range when accessing layers in a datasource: %s.' % index)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
raise TypeError('Invalid index type: %s' % type(index))
|
2016-11-15 06:40:28 +08:00
|
|
|
return Layer(layer, self)
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __len__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the number of layers within the data source."
|
2008-08-06 02:13:06 +08:00
|
|
|
return self.layer_count
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return OGR GetName and Driver for the Data Source."
|
2017-01-20 17:20:53 +08:00
|
|
|
return '%s (%s)' % (self.name, self.driver)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def layer_count(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the number of layers in the data source."
|
2009-03-08 07:02:48 +08:00
|
|
|
return capi.get_layer_count(self._ptr)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-01-25 04:31:57 +08:00
|
|
|
"Return the name of the data source."
|
2012-10-07 04:56:47 +08:00
|
|
|
name = capi.get_ds_name(self._ptr)
|
2017-01-26 17:08:08 +08:00
|
|
|
return force_str(name, self.encoding, strings_only=True)
|