Wrap a GDAL/OGR Data Source Driver. For more information, see the C API documentation: https://gdal.org/api/vector_c_api.html https://gdal.org/api/raster_c_api.html
| 8 | |
| 9 | |
| 10 | class Driver(GDALBase): |
| 11 | """ |
| 12 | Wrap a GDAL/OGR Data Source Driver. |
| 13 | For more information, see the C API documentation: |
| 14 | https://gdal.org/api/vector_c_api.html |
| 15 | https://gdal.org/api/raster_c_api.html |
| 16 | """ |
| 17 | |
| 18 | # Case-insensitive aliases for some GDAL/OGR Drivers. |
| 19 | # For a complete list of original driver names see |
| 20 | # https://gdal.org/drivers/vector/ |
| 21 | # https://gdal.org/drivers/raster/ |
| 22 | _alias = { |
| 23 | # vector |
| 24 | "esri": "ESRI Shapefile", |
| 25 | "shp": "ESRI Shapefile", |
| 26 | "shape": "ESRI Shapefile", |
| 27 | # raster |
| 28 | "tiff": "GTiff", |
| 29 | "tif": "GTiff", |
| 30 | "jpeg": "JPEG", |
| 31 | "jpg": "JPEG", |
| 32 | } |
| 33 | |
| 34 | if GDAL_VERSION[:2] <= (3, 10): |
| 35 | _alias.update( |
| 36 | { |
| 37 | "tiger": "TIGER", |
| 38 | "tiger/line": "TIGER", |
| 39 | } |
| 40 | ) |
| 41 | |
| 42 | def __init__(self, dr_input): |
| 43 | """ |
| 44 | Initialize an GDAL/OGR driver on either a string or integer input. |
| 45 | """ |
| 46 | if isinstance(dr_input, str): |
| 47 | # If a string name of the driver was passed in |
| 48 | self.ensure_registered() |
| 49 | |
| 50 | # Checking the alias dictionary (case-insensitive) to see if an |
| 51 | # alias exists for the given driver. |
| 52 | if dr_input.lower() in self._alias: |
| 53 | name = self._alias[dr_input.lower()] |
| 54 | else: |
| 55 | name = dr_input |
| 56 | |
| 57 | # Attempting to get the GDAL/OGR driver by the string name. |
| 58 | driver = c_void_p(capi.get_driver_by_name(force_bytes(name))) |
| 59 | elif isinstance(dr_input, int): |
| 60 | self.ensure_registered() |
| 61 | driver = capi.get_driver(dr_input) |
| 62 | elif isinstance(dr_input, c_void_p): |
| 63 | driver = dr_input |
| 64 | else: |
| 65 | raise GDALException( |
| 66 | "Unrecognized input type for GDAL/OGR Driver: %s" % type(dr_input) |
| 67 | ) |