[Gdal-dev] ogr2ogr creating shapefile

Mateusz Loskot mateusz at loskot.net
Tue Sep 19 14:13:16 EDT 2006


Martin Spott wrote:
> "Frances Collier" wrote:
>> Hello all,
> 
>> I initially posted this on the postgis mailing list but was 
>> informed that it was better suited for this list. So, below is the
>>  problem I've been having. I'll greatly appreciate any help I can 
>> get. Thank you -Frances
> 
>> I have been trying to figure out how to create a shapefile using 
>> ogr2ogr.
> 
> I can confirm getting a segmentation fault unter Linux/ia32 with 
> 'ogr2ogr' from gdal-1.3.2:
> 
> foxtrot: 10:42:53 ~> /opt/GRASS/bin/ogr2ogr -f "ESRI Shapefile" \ 
> ./mangrove.shp PG:'host=localhost user=martin dbname=landcover' \ 
> -sql "SELECT wkb_geometry from mangrove" Segmentation fault
> 
> Running a binary compiled for x86_64 on the same machine reveals a 
> different error message:
> 
> ERROR 2: CPLRealloc(): Out of memory allocating 85052352 bytes. Abort
> 
> 
> 
Frances, Martin,

IMHO, your SQL queries are non-standard queries and geometry returned
from the database is likely in so called "internal format".
So, if use with MySQL, your SQL statement
"SELECT wkb_geometry from mangrove"
will return data in internal format used in MySQL.
If used with PostGIS, it will return data in internal format
used by PostGIS.

There is a bug on the GDAL bugzilla about it.
If you're interested, please see comments posted to the Bug 1230.
Here is my longer explanation:
http://bugzilla.remotesensing.org/show_bug.cgi?id=1230#c17

MySQL documentation suggests something similar I'm trying to tell
in comments:
http://dev.mysql.com/doc/refman/5.1/en/fetching-spatial-data.html

The OGC SFS for SQL specification seems to be not exact in this matter.
It only says:

AsText( ):String —Exports this Geometry to a specific well-known text
representation of Geometry.

AsBinary( ):Binary—Exports this Geometry to a specific well-known binary
representation of Geometry.

So, according to my understanding, in order to get data in OGC-compliant
format, you need to use AsBinary or AsText, otherwise you may get a
"garbage" - internal format.

PostgreSQL/PostGIS uses EWKB/EWKT, extended versions of WKB and WKT,
that's why OGR throws segmentation fault.
If you use CVS version of OGR, no segfaults will be thrown
and you will not get any geometries according to simple but not genuine
rule: garbage in, garbage out.
EWKB, as a non-standard format, is considered as a garbage.

Yes, this is not a very convenient solution, but we don't see any better
at the moment. Unfortunately, it's not very easy to detect if
OGR gets EWKB or WKB, becasause EWKB injects some additional fields
inside the standard WKB stream.


So, please, try to use following SQL statement:
SELECT AsText(wkb_geometry) from mangrove

or if you need WKB binary stream:
SELECT AsBinary(wkb_geometry) from mangrove

and it should work well.


Personally, as I'm a kind of standards purist ;-), I'd use AsText and
AsBinary, otherwise I'd consider the SQL as a not OGC-compliant, so not
portable


Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list