[gdal-dev] Unable to use geom.ExportToWkt() in Python ?
Hey all. I am using the OGR binding for python, to open a SHP and print a geometry's WKT. This works until I try to call geom.ExportToWkt() At that time I simply get "Premature end of script headers" Running it from the command line I get a segmentation fault. The code snippet: driver = ogr.GetDriverByName('ESRI Shapefile') datasource = driver.Open(shapefile,0) layer = datasource.GetLayer() spatialRef = layer.GetSpatialRef() firstgeom = layer.GetFeature(0).GetGeometryRef() wkt= firstgeom.ExportToWkt() Versions: GDAL 1.7.1 Python 2.5.1 The strace output is not particularly helpful to me. Maybe it's helpful to someone else? open("/maps/images.tmp/zipUdUf9L.zip/fedlanl020.dbf", O_RDONLY) = 4 open("/maps/images.tmp/zipUdUf9L.zip/fedlanl020.cpg", O_RDONLY) = -1 ENOENT (No such file or directory) open("/maps/images.tmp/zipUdUf9L.zip/fedlanl020.CPG", O_RDONLY) = -1 ENOENT (No such file or directory) fstat(4, {st_mode=S_IFREG|0644, st_size=55294, ...}) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b8f79cf5000 read(4, "\3i\t\17a\1\0\0\341\0\234\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096) = 4096 open("/maps/images.tmp/zipUdUf9L.zip/fedlanl020.prj", O_RDONLY) = -1 ENOENT (No such file or directory) open("/maps/images.tmp/zipUdUf9L.zip/fedlanl020.PRJ", O_RDONLY) = -1 ENOENT (No such file or directory) stat("/maps/images.tmp/zipUdUf9L.zip/fedlanl020.idm", 0x7fffd87a7c10) = -1 ENOENT (No such file or directory) stat("/usr/lib64/python2.5/site-packages/GDAL-1.7.1-py2.5-linux-x86_64.egg/osgeo/types", 0x7fffd87a5da0) = -1 ENOENT (No such file or directory) open("/usr/lib64/python2.5/site-packages/GDAL-1.7.1-py2.5-linux-x86_64.egg/osgeo/types.so", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib64/python2.5/site-packages/GDAL-1.7.1-py2.5-linux-x86_64.egg/osgeo/typesmodule.so", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib64/python2.5/site-packages/GDAL-1.7.1-py2.5-linux-x86_64.egg/osgeo/types.py", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/lib64/python2.5/site-packages/GDAL-1.7.1-py2.5-linux-x86_64.egg/osgeo/types.pyc", O_RDONLY) = -1 ENOENT (No such file or directory) futex(0x40eceb0, FUTEX_WAKE, 1) = 0 read(3, "5\364c\300\0\0\0\340v\3...@\0\0\0\0x\364c\300\0\0\0`d\321"..., 4096) = 4096 --- SIGSEGV (Segmentation fault) @ 0 (0) --- I do wonder about those "no such file or directory" at the end, where it's trying to open something called "types" and trying everything it can. True, there are no files named "types" Is that relevant? -- HostGIS, Open Source solutions for the global GIS community Greg Allensworth - SysAdmin, Programmer, GIS Person, Security Network+ Server+ A+ Security+ Linux+ PHP PostgreSQL MySQL DHTML/JavaScript/AJAX "No one cares if you can back up — only if you can recover." ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev
Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ?
Gregor at HostGIS wrote: Hey all. I am using the OGR binding for python, to open a SHP and print a geometry's WKT. This works until I try to call geom.ExportToWkt() At that time I simply get "Premature end of script headers" Running it from the command line I get a segmentation fault. The code snippet: driver = ogr.GetDriverByName('ESRI Shapefile') datasource = driver.Open(shapefile,0) layer = datasource.GetLayer() spatialRef = layer.GetSpatialRef() firstgeom = layer.GetFeature(0).GetGeometryRef() wkt= firstgeom.ExportToWkt() Gregor, Geometries do not have the ExportToWkt() method. This is a method on the ogr.SpatialReference class. Perhaps try: wkt = spatialRef.ExportToWkt() Best regards, -- ---+-- I set the clouds in motion - turn up | Frank Warmerdam, warmer...@pobox.com light and sound - activate the windows | http://pobox.com/~warmerdam and watch the world go round - Rush| Geospatial Programmer for Rent ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev
Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ?
That's strange, the docs show ExportAsWkt as being a method of a Geometry, as does the source code for the Python wrapper. Well, you would know best... If I do spatialref.ExportAsWkt() it gives the WKT of the SRS, right? What if I want a feature's geometry? Would I need to generate it myself from the geometry type and iterating over the vertices? Sent from my Verizon Wireless Phone - Reply message - From: "Frank Warmerdam" Date: Tue, Nov 23, 2010 6:31 pm Subject: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? To: "Gregor at HostGIS" Cc: Gregor at HostGIS wrote: > Hey all. I am using the OGR binding for python, to open a SHP and print > a geometry's WKT. This works until I try to call geom.ExportToWkt() At > that time I simply get "Premature end of script headers" Running it from > the command line I get a segmentation fault. > > The code snippet: > > driver = ogr.GetDriverByName('ESRI Shapefile') > datasource = driver.Open(shapefile,0) > layer = datasource.GetLayer() > spatialRef = layer.GetSpatialRef() > firstgeom = layer.GetFeature(0).GetGeometryRef() > wkt= firstgeom.ExportToWkt() Gregor, Geometries do not have the ExportToWkt() method. This is a method on the ogr.SpatialReference class. Perhaps try: wkt = spatialRef.ExportToWkt() Best regards, -- ---+-- I set the clouds in motion - turn up | Frank Warmerdam, warmer...@pobox.com light and sound - activate the windows | http://pobox.com/~warmerdam and watch the world go round - Rush| Geospatial Programmer for Rent ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev
Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ?
Gregor at HostGIS wrote: That's strange, the docs show ExportAsWkt as being a method of a Geometry, as does the source code for the Python wrapper. Well, you would know best... If I do spatialref.ExportAsWkt() it gives the WKT of the SRS, right? What if I want a feature's geometry? Would I need to generate it myself from the geometry type and iterating over the vertices? Gregor, Garr, disregard me. I had SRS WKT on the brain and forgot all about geometry WKT. > driver = ogr.GetDriverByName('ESRI Shapefile') > datasource = driver.Open(shapefile,0) > layer = datasource.GetLayer() > spatialRef = layer.GetSpatialRef() > firstgeom = layer.GetFeature(0).GetGeometryRef() > wkt= firstgeom.ExportToWkt() GetFeature() makes a copy of the feature, and right after the GetGeometryRef() call the feature is destroyed because there is no obvious remaining reference to it. This means the geometry pointer (a reference to the internal geometry of the feature) is now pointing to a deleted object and the ExportToWkt() crashes. The trick is to keep around the feature till you are done with the geometry. feat = layer.GetFeature(0) firstgeom = feat.GetGeometryRef() wkt = firstgeom.ExportToWkt() There are efforts in recent versions of the bindings to avoid this sort of problem with layers and datasources but even in trunk I suspect grabbing a geometry handle from within a feature will not keep the feature alive. Best regards, -- ---+-- I set the clouds in motion - turn up | Frank Warmerdam, warmer...@pobox.com light and sound - activate the windows | http://pobox.com/~warmerdam and watch the world go round - Rush| Geospatial Programmer for Rent ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev
RE: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? [SEC=UNCLASSIFIED]
You are running into a python "gotcha" - http://trac.osgeo.org/gdal/wiki/PythonGotchas Your reference to the feature is going out of scope when you use the "firstgeom = layer.GetFeature(0).GetGeometryRef()" syntax. The following works syntax fine: feat = layer.GetFeature(0) firstgeom = feat.GetGeometryRef() print firstgeom.ExportToWkt() Luke From: gdal-dev-boun...@lists.osgeo.org [mailto:gdal-dev-boun...@lists.osgeo.org] On Behalf Of Gregor at HostGIS Sent: Wednesday, 24 November 2010 1:50 PM To: Frank Warmerdam Cc: gdal-dev@lists.osgeo.org Subject: Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? That's strange, the docs show ExportAsWkt as being a method of a Geometry, as does the source code for the Python wrapper. Well, you would know best... If I do spatialref.ExportAsWkt() it gives the WKT of the SRS, right? What if I want a feature's geometry? Would I need to generate it myself from the geometry type and iterating over the vertices? Sent from my Verizon Wireless Phone - Reply message - From: "Frank Warmerdam" Date: Tue, Nov 23, 2010 6:31 pm Subject: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? To: "Gregor at HostGIS" Cc: Gregor at HostGIS wrote: > Hey all. I am using the OGR binding for python, to open a SHP and print > a geometry's WKT. This works until I try to call geom.ExportToWkt() At > that time I simply get "Premature end of script headers" Running it from > the command line I get a segmentation fault. > > The code snippet: > > driver = ogr.GetDriverByName('ESRI Shapefile') > datasource = driver.Open(shapefile,0) > layer = datasource.GetLayer() > spatialRef = layer.GetSpatialRef() > firstgeom = layer.GetFeature(0).GetGeometryRef() > wkt= firstgeom.ExportToWkt() Gregor, Geometries do not have the ExportToWkt() method. This is a method on the ogr.SpatialReference class. Perhaps try: wkt = spatialRef.ExportToWkt() Best regards, -- ---+ -- I set the clouds in motion - turn up | Frank Warmerdam, warmer...@pobox.com light and sound - activate the windows | http://pobox.com/~warmerdam and watch the world go round - Rush| Geospatial Programmer for Rent If you have received this transmission in error please notify us immediately by return e-mail and delete all copies. If this e-mail or any attachments have been sent to you in error, that error does not constitute waiver of any confidentiality, privilege or copyright in respect of information in the e-mail or attachments. Please consider the environment before printing this email. ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev
Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? [SEC=UNCLASSIFIED]
Luke, you are so full of awesome. That page answers my question and has other good tips. Thank you so much. Sent from my Verizon Wireless Phone - Reply message - From: "Pinner, Luke" Date: Tue, Nov 23, 2010 7:04 pm Subject: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? [SEC=UNCLASSIFIED] To: "Gregor at HostGIS" Cc: You are running into a python "gotcha" - http://trac.osgeo.org/gdal/wiki/PythonGotchas Your reference to the feature is going out of scope when you use the "firstgeom = layer.GetFeature(0).GetGeometryRef()" syntax. The following works syntax fine: feat = layer.GetFeature(0) firstgeom = feat.GetGeometryRef() print firstgeom.ExportToWkt() Luke From: gdal-dev-boun...@lists.osgeo.org [mailto:gdal-dev-boun...@lists.osgeo.org] On Behalf Of Gregor at HostGIS Sent: Wednesday, 24 November 2010 1:50 PM To: Frank Warmerdam Cc: gdal-dev@lists.osgeo.org Subject: Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? That's strange, the docs show ExportAsWkt as being a method of a Geometry, as does the source code for the Python wrapper. Well, you would know best... If I do spatialref.ExportAsWkt() it gives the WKT of the SRS, right? What if I want a feature's geometry? Would I need to generate it myself from the geometry type and iterating over the vertices? Sent from my Verizon Wireless Phone - Reply message - From: "Frank Warmerdam" Date: Tue, Nov 23, 2010 6:31 pm Subject: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? To: "Gregor at HostGIS" Cc: Gregor at HostGIS wrote: > Hey all. I am using the OGR binding for python, to open a SHP and print > a geometry's WKT. This works until I try to call geom.ExportToWkt() At > that time I simply get "Premature end of script headers" Running it from > the command line I get a segmentation fault. > > The code snippet: > > driver = ogr.GetDriverByName('ESRI Shapefile') > datasource = driver.Open(shapefile,0) > layer = datasource.GetLayer() > spatialRef = layer.GetSpatialRef() > firstgeom = layer.GetFeature(0).GetGeometryRef() > wkt= firstgeom.ExportToWkt() Gregor, Geometries do not have the ExportToWkt() method. This is a method on the ogr.SpatialReference class. Perhaps try: wkt = spatialRef.ExportToWkt() Best regards, -- ---+ -- I set the clouds in motion - turn up | Frank Warmerdam, warmer...@pobox.com light and sound - activate the windows | http://pobox.com/~warmerdam and watch the world go round - Rush| Geospatial Programmer for Rent If you have received this transmission in error please notify us immediately by return e-mail and delete all copies. If this e-mail or any attachments have been sent to you in error, that error does not constitute waiver of any confidentiality, privilege or copyright in respect of information in the e-mail or attachments. Please consider the environment before printing this email. ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev
Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? [SEC=UNCLASSIFIED]
Luke, you are so full of awesome. That page answers my question and has other good tips. Thank you so much. Sent from my Verizon Wireless Phone - Reply message - From: "Pinner, Luke" Date: Tue, Nov 23, 2010 7:04 pm Subject: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? [SEC=UNCLASSIFIED] To: "Gregor at HostGIS" Cc: You are running into a python "gotcha" - http://trac.osgeo.org/gdal/wiki/PythonGotchas Your reference to the feature is going out of scope when you use the "firstgeom = layer.GetFeature(0).GetGeometryRef()" syntax. The following works syntax fine: feat = layer.GetFeature(0) firstgeom = feat.GetGeometryRef() print firstgeom.ExportToWkt() Luke From: gdal-dev-boun...@lists.osgeo.org [mailto:gdal-dev-boun...@lists.osgeo.org] On Behalf Of Gregor at HostGIS Sent: Wednesday, 24 November 2010 1:50 PM To: Frank Warmerdam Cc: gdal-dev@lists.osgeo.org Subject: Re: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? That's strange, the docs show ExportAsWkt as being a method of a Geometry, as does the source code for the Python wrapper. Well, you would know best... If I do spatialref.ExportAsWkt() it gives the WKT of the SRS, right? What if I want a feature's geometry? Would I need to generate it myself from the geometry type and iterating over the vertices? Sent from my Verizon Wireless Phone - Reply message - From: "Frank Warmerdam" Date: Tue, Nov 23, 2010 6:31 pm Subject: [gdal-dev] Unable to use geom.ExportToWkt() in Python ? To: "Gregor at HostGIS" Cc: Gregor at HostGIS wrote: > Hey all. I am using the OGR binding for python, to open a SHP and print > a geometry's WKT. This works until I try to call geom.ExportToWkt() At > that time I simply get "Premature end of script headers" Running it from > the command line I get a segmentation fault. > > The code snippet: > > driver = ogr.GetDriverByName('ESRI Shapefile') > datasource = driver.Open(shapefile,0) > layer = datasource.GetLayer() > spatialRef = layer.GetSpatialRef() > firstgeom = layer.GetFeature(0).GetGeometryRef() > wkt= firstgeom.ExportToWkt() Gregor, Geometries do not have the ExportToWkt() method. This is a method on the ogr.SpatialReference class. Perhaps try: wkt = spatialRef.ExportToWkt() Best regards, -- ---+ -- I set the clouds in motion - turn up | Frank Warmerdam, warmer...@pobox.com light and sound - activate the windows | http://pobox.com/~warmerdam and watch the world go round - Rush| Geospatial Programmer for Rent If you have received this transmission in error please notify us immediately by return e-mail and delete all copies. If this e-mail or any attachments have been sent to you in error, that error does not constitute waiver of any confidentiality, privilege or copyright in respect of information in the e-mail or attachments. Please consider the environment before printing this email. ___ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev