[gdal-dev] API Doubt/ Feature request

Even Rouault even.rouault at spatialys.com
Tue Apr 21 10:18:59 PDT 2015


Le mardi 21 avril 2015 19:11:04, Xavier Barnada a écrit :
> Hi,
> 
> I'm trying to convert data from one format to another using ogr with python
> (Postgis->SHP) using the following code:
> 
> conn=ogr.Open("PG: host=host dbname=db user=user password=password")
> if conn is None:
>     print 'Could not open a database or GDAL is not correctly installed!'
>     sys.exit(1)
> 
> output = "out.shp"
> 
> # Schema definition of SHP file
> #out_driver = ogr.GetDriverByName('Memory')
> #out_ds = out_driver.CreateDataSource('out')
> out_driver = ogr.GetDriverByName( 'ESRI Shapefile' )
> out_ds = out_driver.CreateDataSource(output)
> out_srs = None
> out_layer = out_ds.CreateLayer("name", out_srs, ogr.wkbMultiLineString)
> fd = ogr.FieldDefn('name',ogr.OFTString)
> out_layer.CreateField(fd)
> 
> layer = conn.ExecuteSQL("select *,'' as TITLE from table")
> 
> feat = layer.GetNextFeature()
> while feat is not None:
>     featDef = ogr.Feature(out_layer.GetLayerDefn())
>     featDef.SetGeometry(feat.GetGeometryRef())
>     featDef.SetField('name',feat.TITLE)
>     out_layer.CreateFeature(featDef)
>     feat.Destroy()
>     feat = layer.GetNextFeature()
> 
> conn.Destroy()
> out_ds.Destroy()
> 
> That code works corrctly and generates the shape file, but this isn't
> adecuate for the program that I'm writing because it's an HTTP service.
> There is any way to get the shapefile data without writing and reading it
> latter(it's inefficient)? I missed to check somthing from the documentation
>  or it's not possible to get the file data as an string?

Xavier,

You could write to a in-memory file with "/vsimem/tmp.shp", and then read 
/vsimem/tmp.shp, .shx and .dbf, to send them to the HTTP output.

http://www.gdal.org/cpl__vsi_8h.html#a66e2e6f093fd42f8a941b962d4c8a19e might 
be worth reading.

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list