Skip to content
This repository
Browse code

support for osgeo.ogr for reading OGR geometries (besides direct use …

…of libgdal)
  • Loading branch information...
commit d12194c3fa0d9fb437d98cc7bcf9f95280eca0e4 1 parent e40e0a8
Oliver Tonnhofer authored April 09, 2013

Showing 1 changed file with 88 additions and 3 deletions. Show diff stats Hide diff stats

  1. 91  mapproxy/util/ogr.py
91  mapproxy/util/ogr.py
@@ -13,6 +13,8 @@
13 13
 # See the License for the specific language governing permissions and
14 14
 # limitations under the License.
15 15
 
  16
+import os
  17
+import sys
16 18
 from mapproxy.util.lib import load_library
17 19
 import ctypes
18 20
 from ctypes import c_void_p, c_char_p, c_int
@@ -70,12 +72,10 @@ def init_libgdal():
70 72
 
71 73
     return libgdal
72 74
 
73  
-libgdal = init_libgdal()
74  
-
75 75
 class OGRShapeReaderError(Exception):
76 76
     pass
77 77
 
78  
-class OGRShapeReader(object):
  78
+class CtypesOGRShapeReader(object):
79 79
     def __init__(self, datasource):
80 80
         self.datasource = datasource
81 81
         self.opened = False
@@ -133,6 +133,91 @@ def close(self):
133 133
     def __del__(self):
134 134
         self.close()
135 135
 
  136
+
  137
+class OSGeoOGRShapeReader(object):
  138
+    def __init__(self, datasource):
  139
+        self.datasource = datasource
  140
+        self.opened = False
  141
+        self._ds = None
  142
+
  143
+    def open(self):
  144
+        if self.opened: return
  145
+        self._ds = ogr.Open(self.datasource, False)
  146
+        if self._ds is None:
  147
+            msg = gdal.GetLastErrorMsg()
  148
+            if not msg:
  149
+                msg = 'failed to open %s' % self.datasource
  150
+            raise OGRShapeReaderError(msg)
  151
+
  152
+    def wkts(self, where=None):
  153
+        if not self.opened: self.open()
  154
+
  155
+        if where:
  156
+            if not where.lower().startswith('select'):
  157
+                layer = self._ds.GetLayerByIndex(0)
  158
+                name = layer.GetName()
  159
+                where = 'select * from %s where %s' % (name, where)
  160
+            layer = self._ds.ExecuteSQL(where)
  161
+        else:
  162
+            layer = self._ds.GetLayerByIndex(0)
  163
+        if layer is None:
  164
+            msg = gdal.GetLastErrorMsg()
  165
+            raise OGRShapeReaderError(msg)
  166
+
  167
+        layer.ResetReading()
  168
+        while True:
  169
+            feature = layer.GetNextFeature()
  170
+            if feature is None:
  171
+                break
  172
+            geom = feature.geometry()
  173
+            yield geom.ExportToWkt()
  174
+
  175
+    def close(self):
  176
+        if self.opened:
  177
+            self._ds = None
  178
+            self.opened = False
  179
+
  180
+
  181
+ogr = gdal = None
  182
+def try_osgeoogr_import():
  183
+    global ogr, gdal
  184
+    try:
  185
+        from osgeo import ogr; ogr
  186
+        from osgeo import gdal; gdal
  187
+    except ImportError:
  188
+        return
  189
+    return OSGeoOGRShapeReader
  190
+
  191
+libgdal = None
  192
+def try_libogr_import():
  193
+    global libgdal
  194
+    libgdal = init_libgdal()
  195
+    if libgdal is not None:
  196
+        return CtypesOGRShapeReader
  197
+
  198
+ogr_imports = []
  199
+if 'MAPPROXY_USE_OSGEOOGR' in os.environ:
  200
+    ogr_imports = [try_osgeoogr_import]
  201
+
  202
+if 'MAPPROXY_USE_LIBOGR' in os.environ:
  203
+    ogr_imports = [try_libogr_import]
  204
+
  205
+if not ogr_imports:
  206
+    if sys.platform == 'win32':
  207
+        # prefer osgeo.ogr on windows
  208
+        ogr_imports = [try_osgeoogr_import, try_libogr_import]
  209
+    else:
  210
+        ogr_imports = [try_libogr_import, try_osgeoogr_import]
  211
+
  212
+for try_import in ogr_imports:
  213
+    res = try_import()
  214
+    if res:
  215
+        OGRShapeReader = res
  216
+        break
  217
+else:
  218
+    raise ImportError('could not find osgeo.ogr package or libgdal')
  219
+
  220
+
136 221
 if __name__ == '__main__':
137 222
     import sys
138 223
     reader = OGRShapeReader(sys.argv[1])

0 notes on commit d12194c

Please sign in to comment.
Something went wrong with that request. Please try again.