Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
    
AttributeError: SetSpatialFilter This just means the attribute isn't found on the object. –  Nathan W Jul 26 '13 at 12:53

1 Answer 1

up vote 2 down vote accepted

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" )
share|improve this answer
    
This looks interesting. Have you any experience using it? Can you provide some example code? –  LarsVegas Aug 15 '13 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" ) –  Luke Aug 15 '13 at 6:12
    
Ok, thanks for the edit. –  LarsVegas Aug 15 '13 at 7:03
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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