|
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 |
|
6 |
|
7 # For more information, see the OGR C API source code: |
|
8 # http://www.gdal.org/ogr/ogr__api_8h.html |
|
9 # |
|
10 # The OGR_Dr_* routines are relevant here. |
|
11 class Driver(GDALBase): |
|
12 "Wraps an OGR Data Source Driver." |
|
13 |
|
14 # Case-insensitive aliases for OGR Drivers. |
|
15 _alias = {'esri' : 'ESRI Shapefile', |
|
16 'shp' : 'ESRI Shapefile', |
|
17 'shape' : 'ESRI Shapefile', |
|
18 'tiger' : 'TIGER', |
|
19 'tiger/line' : 'TIGER', |
|
20 } |
|
21 |
|
22 def __init__(self, dr_input): |
|
23 "Initializes an OGR driver on either a string or integer input." |
|
24 |
|
25 if isinstance(dr_input, basestring): |
|
26 # If a string name of the driver was passed in |
|
27 self._register() |
|
28 |
|
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()] |
|
33 else: |
|
34 name = dr_input |
|
35 |
|
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): |
|
39 self._register() |
|
40 dr = capi.get_driver(dr_input) |
|
41 elif isinstance(dr_input, c_void_p): |
|
42 dr = dr_input |
|
43 else: |
|
44 raise OGRException('Unrecognized input type for OGR Driver: %s' % str(type(dr_input))) |
|
45 |
|
46 # Making sure we get a valid pointer to the OGR Driver |
|
47 if not dr: |
|
48 raise OGRException('Could not initialize OGR Driver on input: %s' % str(dr_input)) |
|
49 self.ptr = dr |
|
50 |
|
51 def __str__(self): |
|
52 "Returns the string name of the OGR Driver." |
|
53 return capi.get_driver_name(self.ptr) |
|
54 |
|
55 def _register(self): |
|
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() |
|
60 |
|
61 # Driver properties |
|
62 @property |
|
63 def driver_count(self): |
|
64 "Returns the number of OGR data source drivers registered." |
|
65 return capi.get_driver_count() |