[gdal-dev] API Doubt/ Feature request
Xavier Barnada
xbarnada at gmail.com
Tue Apr 21 10:11:04 PDT 2015
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?
If it's not possible would it be a good improvment?
Best reggards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20150421/38ad70e9/attachment.html>
More information about the gdal-dev
mailing list