[gdal-dev] get back a layer objet with ogr in python

Francis Markham fmarkham at gmail.com
Thu Aug 5 03:36:55 EDT 2010


The driver and datasource are going out of scope.  The former can be fixed
by using ogr.Open instead of driver.Open The latter can be fixed by
returning it from the function.  A revised version of your code would look
like:

# import modules
from osgeo import ogr

def openShapeLine(shapefile):
   #Ouverture du shapefile avec ogr
   datasource = ogr.Open(shapefile)
   layer = datasource.GetLayer()
   return (datasource, layer)

ds, couche = openShapeLine('/data/axe.shp')

print couche.GetName()


On 5 August 2010 17:23, Ludovic Granjon <ludovic.granjon at u-bourgogne.fr>wrote:

> Hello
>
> I'm trying to develop with ogr in python. I'm on ubuntu 10.4 and I use gdal
> 1.7
> When I try to get back a layer object from a function, I have the following
> message when I do python monscript.py :
> "Erreur de segmentation"
>
> This is my code :
>
> -----------------------
> # import modules
> from osgeo import ogr
>
> def openShapeLine(shapefile):
>    #Ouverture du shapefile avec ogr
>    driver = ogr.GetDriverByName("ESRI Shapefile")
>    datasource = driver.Open(shapefile)
>    layer = datasource.GetLayer()
>    return (layer)
>
> couche = openShapeLine('/data/axe.shp')
>
> print couche.GetName()
> ------------------------
>
> If I do a "print layer.GetName()" inside the openShapeLine function,
> there's no problem ...
>
> Have you any idea ?
>
> Thanks all
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20100805/0e0fc817/attachment-0001.html


More information about the gdal-dev mailing list