| 1 |
from ctypes import c_void_p |
| 2 |
|
| 3 |
from django.contrib.gis.gdal.base import GDALBase |
| 4 |
from django.contrib.gis.gdal.error import GDALException |
| 5 |
from django.contrib.gis.gdal.prototypes import ds as capi |
| 6 |
from django.utils.encoding import force_bytes, force_str |
| 7 |
|
| 8 |
|
| 9 |
class Driver(GDALBase): |
| 10 |
""" |
| 11 |
Wrap a GDAL/OGR Data Source Driver. |
| 12 |
For more information, see the C API documentation: |
| 13 |
https://gdal.org/api/vector_c_api.html |
| 14 |
https://gdal.org/api/raster_c_api.html |
| 15 |
""" |
| 16 |
|
| 17 |
# Case-insensitive aliases for some GDAL/OGR Drivers. |
| 18 |
# For a complete list of original driver names see |
| 19 |
# https://gdal.org/drivers/vector/ |
| 20 |
# https://gdal.org/drivers/raster/ |
| 21 |
_alias = { |
| 22 |
# vector |
| 23 |
"esri": "ESRI Shapefile", |
| 24 |
"shp": "ESRI Shapefile", |
| 25 |
"shape": "ESRI Shapefile", |
| 26 |
"tiger": "TIGER", |
| 27 |
"tiger/line": "TIGER", |
| 28 |
# raster |
| 29 |
"tiff": "GTiff", |
| 30 |
"tif": "GTiff", |
| 31 |
"jpeg": "JPEG", |
| 32 |
"jpg": "JPEG", |
| 33 |
} |
| 34 |
|
| 35 |
def __init__(self, dr_input): |
| 36 |
""" |
| 37 |
Initialize an GDAL/OGR driver on either a string or integer input. |
| 38 |
""" |
| 39 |
if isinstance(dr_input, str): |
| 40 |
# If a string name of the driver was passed in |
| 41 |
self.ensure_registered() |
| 42 |
|
| 43 |
# Checking the alias dictionary (case-insensitive) to see if an |
| 44 |
# alias exists for the given driver. |
| 45 |
if dr_input.lower() in self._alias: |
| 46 |
name = self._alias[dr_input.lower()] |
| 47 |
else: |
| 48 |
name = dr_input |
| 49 |
|
| 50 |
# Attempting to get the GDAL/OGR driver by the string name. |
| 51 |
driver = c_void_p(capi.get_driver_by_name(force_bytes(name))) |
| 52 |
elif isinstance(dr_input, int): |
| 53 |
self.ensure_registered() |
| 54 |
driver = capi.get_driver(dr_input) |
| 55 |
elif isinstance(dr_input, c_void_p): |
| 56 |
driver = dr_input |
| 57 |
else: |
| 58 |
raise GDALException( |
| 59 |
"Unrecognized input type for GDAL/OGR Driver: %s" % type(dr_input) |
| 60 |
) |
| 61 |
|
| 62 |
# Making sure we get a valid pointer to the OGR Driver |
| 63 |
if not driver: |
| 64 |
raise GDALException( |
| 65 |
"Could not initialize GDAL/OGR Driver on input: %s" % dr_input |
| 66 |
) |
| 67 |
self.ptr = driver |
| 68 |
|
| 69 |
def __str__(self): |
| 70 |
return self.name |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def ensure_registered(cls): |
| 74 |
""" |
| 75 |
Attempt to register all the data source drivers. |
| 76 |
""" |
| 77 |
# Only register all if the driver count is 0 (or else all drivers will |
| 78 |
# be registered over and over again). |
| 79 |
if not capi.get_driver_count(): |
| 80 |
capi.register_all() |
| 81 |
|
| 82 |
@classmethod |
| 83 |
def driver_count(cls): |
| 84 |
""" |
| 85 |
Return the number of GDAL/OGR data source drivers registered. |
| 86 |
""" |
| 87 |
return capi.get_driver_count() |
| 88 |
|
| 89 |
@property |
| 90 |
def name(self): |
| 91 |
""" |
| 92 |
Return description/name string for this driver. |
| 93 |
""" |
| 94 |
return force_str(capi.get_driver_description(self.ptr)) |