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).

1
  • AttributeError: SetSpatialFilter This just means the attribute isn't found on the object.
    – Nathan W
    Commented Jul 26, 2013 at 12:53

1 Answer 1

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" )
3
  • This looks interesting. Have you any experience using it? Can you provide some example code?
    – LarsVegas
    Commented Aug 15, 2013 at 5: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
    Commented Aug 15, 2013 at 6: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
    Commented Aug 23, 2013 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.