PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/gdal/driver.py

https://code.google.com/p/mango-py/
Python | 65 lines | 41 code | 10 blank | 14 comment | 8 complexity | 709b22b07477d227c4eae1776cd04b77 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. # For more information, see the OGR C API source code:
  7. # http://www.gdal.org/ogr/ogr__api_8h.html
  8. #
  9. # The OGR_Dr_* routines are relevant here.
  10. class Driver(GDALBase):
  11. "Wraps an OGR Data Source Driver."
  12. # Case-insensitive aliases for OGR Drivers.
  13. _alias = {'esri' : 'ESRI Shapefile',
  14. 'shp' : 'ESRI Shapefile',
  15. 'shape' : 'ESRI Shapefile',
  16. 'tiger' : 'TIGER',
  17. 'tiger/line' : 'TIGER',
  18. }
  19. def __init__(self, dr_input):
  20. "Initializes an OGR driver on either a string or integer input."
  21. if isinstance(dr_input, basestring):
  22. # If a string name of the driver was passed in
  23. self._register()
  24. # Checking the alias dictionary (case-insensitive) to see if an alias
  25. # exists for the given driver.
  26. if dr_input.lower() in self._alias:
  27. name = self._alias[dr_input.lower()]
  28. else:
  29. name = dr_input
  30. # Attempting to get the OGR driver by the string name.
  31. dr = capi.get_driver_by_name(name)
  32. elif isinstance(dr_input, int):
  33. self._register()
  34. dr = capi.get_driver(dr_input)
  35. elif isinstance(dr_input, c_void_p):
  36. dr = dr_input
  37. else:
  38. raise OGRException('Unrecognized input type for OGR Driver: %s' % str(type(dr_input)))
  39. # Making sure we get a valid pointer to the OGR Driver
  40. if not dr:
  41. raise OGRException('Could not initialize OGR Driver on input: %s' % str(dr_input))
  42. self.ptr = dr
  43. def __str__(self):
  44. "Returns the string name of the OGR Driver."
  45. return capi.get_driver_name(self.ptr)
  46. def _register(self):
  47. "Attempts to register all the data source drivers."
  48. # Only register all if the driver count is 0 (or else all drivers
  49. # will be registered over and over again)
  50. if not self.driver_count: capi.register_all()
  51. # Driver properties
  52. @property
  53. def driver_count(self):
  54. "Returns the number of OGR data source drivers registered."
  55. return capi.get_driver_count()