|
1 # prerequisites imports |
|
2 from ctypes import c_void_p |
|
3 from django.contrib.gis.gdal.error import OGRException |
|
4 from django.contrib.gis.gdal.prototypes.ds import \ |
|
5 get_driver, get_driver_by_name, get_driver_count, get_driver_name, register_all |
|
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(object): |
|
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._ptr = None # Initially NULL |
|
28 self._register() |
|
29 |
|
30 # Checking the alias dictionary (case-insensitive) to see if an alias |
|
31 # exists for the given driver. |
|
32 if dr_input.lower() in self._alias: |
|
33 name = self._alias[dr_input.lower()] |
|
34 else: |
|
35 name = dr_input |
|
36 |
|
37 # Attempting to get the OGR driver by the string name. |
|
38 dr = get_driver_by_name(name) |
|
39 elif isinstance(dr_input, int): |
|
40 self._register() |
|
41 dr = get_driver(dr_input) |
|
42 elif isinstance(dr_input, c_void_p): |
|
43 dr = dr_input |
|
44 else: |
|
45 raise OGRException('Unrecognized input type for OGR Driver: %s' % str(type(dr_input))) |
|
46 |
|
47 # Making sure we get a valid pointer to the OGR Driver |
|
48 if not dr: |
|
49 raise OGRException('Could not initialize OGR Driver on input: %s' % str(dr_input)) |
|
50 self._ptr = dr |
|
51 |
|
52 def __str__(self): |
|
53 "Returns the string name of the OGR Driver." |
|
54 return get_driver_name(self._ptr) |
|
55 |
|
56 def _register(self): |
|
57 "Attempts to register all the data source drivers." |
|
58 # Only register all if the driver count is 0 (or else all drivers |
|
59 # will be registered over and over again) |
|
60 if not self.driver_count: register_all() |
|
61 |
|
62 # Driver properties |
|
63 @property |
|
64 def driver_count(self): |
|
65 "Returns the number of OGR data source drivers registered." |
|
66 return get_driver_count() |