1 # prerequisites imports
2 from ctypes
import c_void_p
3 from django
.contrib
.gis
.gdal
.base
import GDALBase
4 from django
.contrib
.gis
.gdal
.error
import OGRException
5 from django
.contrib
.gis
.gdal
.prototypes
import ds
as capi
7 # For more information, see the OGR C API source code:
8 # http://www.gdal.org/ogr/ogr__api_8h.html
10 # The OGR_Dr_* routines are relevant here.
11 class Driver(GDALBase
):
12 "Wraps an OGR Data Source Driver."
14 # Case-insensitive aliases for OGR Drivers.
15 _alias
= {'esri' : 'ESRI Shapefile',
16 'shp' : 'ESRI Shapefile',
17 'shape' : 'ESRI Shapefile',
19 'tiger/line' : 'TIGER',
22 def __init__(self
, dr_input
):
23 "Initializes an OGR driver on either a string or integer input."
25 if isinstance(dr_input
, basestring
):
26 # If a string name of the driver was passed in
29 # Checking the alias dictionary (case-insensitive) to see if an alias
30 # exists for the given driver.
31 if dr_input
.lower() in self
._alias
:
32 name
= self
._alias
[dr_input
.lower()]
36 # Attempting to get the OGR driver by the string name.
37 dr
= capi
.get_driver_by_name(name
)
38 elif isinstance(dr_input
, int):
40 dr
= capi
.get_driver(dr_input
)
41 elif isinstance(dr_input
, c_void_p
):
44 raise OGRException('Unrecognized input type for OGR Driver: %s' % str(type(dr_input
)))
46 # Making sure we get a valid pointer to the OGR Driver
48 raise OGRException('Could not initialize OGR Driver on input: %s' % str(dr_input
))
52 "Returns the string name of the OGR Driver."
53 return capi
.get_driver_name(self
.ptr
)
56 "Attempts to register all the data source drivers."
57 # Only register all if the driver count is 0 (or else all drivers
58 # will be registered over and over again)
59 if not self
.driver_count
: capi
.register_all()
63 def driver_count(self
):
64 "Returns the number of OGR data source drivers registered."
65 return capi
.get_driver_count()