2

I know how to do this:

nodes = driver.Open(node_nav,0)
node_lyr = nodes.GetLayer()  
node_lyr.SetSpatialFilter(None)

But how do can use the layer methods on a geometry I created from scratch?

multip=ogr.Geometry(ogr.wkbMultiPoint)
for n in nodes:
    p=ogr.Geometry(ogr.wkbPoint)
    x,y = n.node
    x = float(x)/dim
    y = float(y)/dim

    p.AddPoint(x,y)
    multip.AddGeometry(p)

This is the traceback for this attempt:

File "/usr/lib/python2.7/dist-packages/osgeo/ogr.py", line 2927, in <lambda>
 __getattr__ = lambda self, name: _swig_getattr(self, Geometry, name)
File "/usr/lib/python2.7/dist-packages/osgeo/ogr.py", line 55, in _swig_getattr
raise AttributeError(name)
AttributeError: SetSpatialFilter

I don't want to save the geometry in a shape file (like it is described here for example).

LarsVegas
  • 2,536
  • 3
  • 30
  • 47

1 Answers1

6

You could write the geometry to an in-memory dataset using the '/vsimem/path_file.shp' virtual file system.

e.g.

drv = ogr.GetDriverByName( 'ESRI Shapefile' )
ds = drv.CreateDataSource( "/vsimem/blahblah.shp" )
user2856
  • 65,736
  • 6
  • 115
  • 196
  • This looks interesting. Have you any experience using it? Can you provide some example code? – LarsVegas Aug 15 '13 at 05:26
  • @LarsVegas - yes, I use /vsimem regularly with GDAL, seems to work just as well with OGR. There's example code in your link and the OGR API tutorial. The only difference is instead of calling ds = drv.CreateDataSource( r"c:\somedir\blahblah.shp" ) you call ds = drv.CreateDataSource( "/vsimem/blahblah.shp" ) – user2856 Aug 15 '13 at 06:12
  • This works great! Very useful information you provided here. I created a point shape file with ~300.000 geometries and two attribute fields in less than a second. Impressive. – LarsVegas Aug 23 '13 at 11:11