2008-08-06 02:13:06 +08:00
|
|
|
from ctypes import c_void_p
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
from django.contrib.gis.gdal.base import GDALBase
|
2014-12-18 05:00:05 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2014-12-03 00:53:04 +08:00
|
|
|
from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
|
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
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
class Driver(GDALBase):
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Wrap a GDAL/OGR Data Source Driver.
|
2014-12-03 00:53:04 +08:00
|
|
|
For more information, see the C API source code:
|
2018-09-26 14:48:47 +08:00
|
|
|
https://www.gdal.org/gdal_8h.html - https://www.gdal.org/ogr__api_8h.html
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
# Case-insensitive aliases for some GDAL/OGR Drivers.
|
|
|
|
# For a complete list of original driver names see
|
2018-09-26 14:48:47 +08:00
|
|
|
# https://www.gdal.org/ogr_formats.html (vector)
|
|
|
|
# https://www.gdal.org/formats_list.html (raster)
|
2014-12-03 00:53:04 +08:00
|
|
|
_alias = {
|
|
|
|
# vector
|
|
|
|
'esri': 'ESRI Shapefile',
|
|
|
|
'shp': 'ESRI Shapefile',
|
|
|
|
'shape': 'ESRI Shapefile',
|
|
|
|
'tiger': 'TIGER',
|
|
|
|
'tiger/line': 'TIGER',
|
|
|
|
# raster
|
|
|
|
'tiff': 'GTiff',
|
|
|
|
'tif': 'GTiff',
|
|
|
|
'jpeg': 'JPEG',
|
|
|
|
'jpg': 'JPEG',
|
|
|
|
}
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __init__(self, dr_input):
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Initialize an GDAL/OGR driver on either a string or integer input.
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(dr_input, str):
|
2008-08-06 02:13:06 +08:00
|
|
|
# If a string name of the driver was passed in
|
2014-12-03 00:53:04 +08:00
|
|
|
self.ensure_registered()
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
# Checking the alias dictionary (case-insensitive) to see if an
|
|
|
|
# alias exists for the given driver.
|
2008-08-06 02:13:06 +08:00
|
|
|
if dr_input.lower() in self._alias:
|
|
|
|
name = self._alias[dr_input.lower()]
|
|
|
|
else:
|
|
|
|
name = dr_input
|
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
# Attempting to get the GDAL/OGR driver by the string name.
|
|
|
|
for iface in (vcapi, rcapi):
|
2016-12-16 05:59:08 +08:00
|
|
|
driver = c_void_p(iface.get_driver_by_name(force_bytes(name)))
|
2014-12-03 00:53:04 +08:00
|
|
|
if driver:
|
|
|
|
break
|
2008-08-06 02:13:06 +08:00
|
|
|
elif isinstance(dr_input, int):
|
2014-12-03 00:53:04 +08:00
|
|
|
self.ensure_registered()
|
|
|
|
for iface in (vcapi, rcapi):
|
|
|
|
driver = iface.get_driver(dr_input)
|
|
|
|
if driver:
|
|
|
|
break
|
2008-08-06 02:13:06 +08:00
|
|
|
elif isinstance(dr_input, c_void_p):
|
2014-12-03 00:53:04 +08:00
|
|
|
driver = dr_input
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2017-01-20 17:20:53 +08:00
|
|
|
raise GDALException('Unrecognized input type for GDAL/OGR Driver: %s' % type(dr_input))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Making sure we get a valid pointer to the OGR Driver
|
2014-12-03 00:53:04 +08:00
|
|
|
if not driver:
|
2017-01-20 17:20:53 +08:00
|
|
|
raise GDALException('Could not initialize GDAL/OGR Driver on input: %s' % dr_input)
|
2014-12-03 00:53:04 +08:00
|
|
|
self.ptr = driver
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2014-12-03 00:53:04 +08:00
|
|
|
return self.name
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2014-12-03 00:53:04 +08:00
|
|
|
@classmethod
|
|
|
|
def ensure_registered(cls):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Attempt to register all the data source drivers.
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
2016-08-22 19:59:25 +08:00
|
|
|
# Only register all if the driver counts are 0 (or else all drivers
|
2008-08-06 02:13:06 +08:00
|
|
|
# will be registered over and over again)
|
2016-08-22 19:59:25 +08:00
|
|
|
if not vcapi.get_driver_count():
|
2014-12-03 00:53:04 +08:00
|
|
|
vcapi.register_all()
|
2016-08-22 19:59:25 +08:00
|
|
|
if not rcapi.get_driver_count():
|
2014-12-03 00:53:04 +08:00
|
|
|
rcapi.register_all()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def driver_count(cls):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the number of GDAL/OGR data source drivers registered.
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
|
|
|
return vcapi.get_driver_count() + rcapi.get_driver_count()
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
@property
|
2014-12-03 00:53:04 +08:00
|
|
|
def name(self):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return description/name string for this driver.
|
2014-12-03 00:53:04 +08:00
|
|
|
"""
|
2017-01-26 17:08:08 +08:00
|
|
|
return force_str(rcapi.get_driver_description(self.ptr))
|