wrap a gdalogr data source driver for more information see the c api documentation https gdal orgapivectorcapi html https gdal orgapirastercapi html caseinsensitive aliases for some gdalogr drivers for a complete list of original driver names see https gdal orgdriversvector https gdal orgdriversraster vector raster initialize an gdalogr driver on either a string or integer input if a string name of the driver was passed in checking the alias dictionary caseinsensitive to see if an alias exists for the given driver attempting to get the gdalogr driver by the string name making sure we get a valid pointer to the ogr driver attempt to register all the data source drivers only register all if the driver counts are 0 or else all drivers will be registered over and over again return the number of gdalogr data source drivers registered return descriptionname string for this driver 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 case insensitive aliases for some gdal ogr drivers for a complete list of original driver names see https gdal org drivers vector https gdal org drivers raster vector raster initialize an gdal ogr driver on either a string or integer input if a string name of the driver was passed in checking the alias dictionary case insensitive to see if an alias exists for the given driver attempting to get the gdal ogr driver by the string name making sure we get a valid pointer to the ogr driver attempt to register all the data source drivers only register all if the driver counts are 0 or else all drivers will be registered over and over again return the number of gdal ogr data source drivers registered return description name string for this driver
from ctypes import c_void_p

from django.contrib.gis.gdal.base import GDALBase
from django.contrib.gis.gdal.error import GDALException
from django.contrib.gis.gdal.prototypes import ds as vcapi
from django.contrib.gis.gdal.prototypes import raster as rcapi
from django.utils.encoding import force_bytes, force_str


class Driver(GDALBase):
    

    
    
    
    
    _alias = {
        
        "esri": "ESRI Shapefile",
        "shp": "ESRI Shapefile",
        "shape": "ESRI Shapefile",
        "tiger": "TIGER",
        "tiger/line": "TIGER",
        
        "tiff": "GTiff",
        "tif": "GTiff",
        "jpeg": "JPEG",
        "jpg": "JPEG",
    }

    def __init__(self, dr_input):
        
        if isinstance(dr_input, str):
            
            self.ensure_registered()

            
            
            if dr_input.lower() in self._alias:
                name = self._alias[dr_input.lower()]
            else:
                name = dr_input

            
            for iface in (vcapi, rcapi):
                driver = c_void_p(iface.get_driver_by_name(force_bytes(name)))
                if driver:
                    break
        elif isinstance(dr_input, int):
            self.ensure_registered()
            for iface in (vcapi, rcapi):
                driver = iface.get_driver(dr_input)
                if driver:
                    break
        elif isinstance(dr_input, c_void_p):
            driver = dr_input
        else:
            raise GDALException(
                "Unrecognized input type for GDAL/OGR Driver: %s" % type(dr_input)
            )

        
        if not driver:
            raise GDALException(
                "Could not initialize GDAL/OGR Driver on input: %s" % dr_input
            )
        self.ptr = driver

    def __str__(self):
        return self.name

    @classmethod
    def ensure_registered(cls):
        
        
        
        if not vcapi.get_driver_count():
            vcapi.register_all()
        if not rcapi.get_driver_count():
            rcapi.register_all()

    @classmethod
    def driver_count(cls):
        
        return vcapi.get_driver_count() + rcapi.get_driver_count()

    @property
    def name(self):
        
        return force_str(rcapi.get_driver_description(self.ptr))