gaiaToWkb - "geom-type or SRID not allowed"

427 views
Skip to first unread message

Sacha

unread,
Dec 5, 2011, 10:42:40 AM12/5/11
to SpatiaLite Users
I am trying to use the gaiaToWkb (or gaiaToEWKB) to write my
geometries to my database, but when I execute my statement to insert a
blob created using gaiaToWkb I get the error:

"objectstore.Geometry violates Geometry constraint [geom-type or SRID
not allowed]"

objectstore is my table and Geometry is my geometry column created as
follows:

"SELECT AddGeometryColumn('objectstore', 'Geometry', 4326, 'POLYGON',
'XY')"

I was previously using gaiaToSpatiaLiteBlobWkb to create my blob. This
was working fine when writing and reading from the database in C++,
but I now also want to query geometries from my Java application. In
the Java I am using JTS to store geometries and therefore need to use
standard WKB so that I can use their WKBReader to read geometries (I
had no success reading spatialite blobs as WKB by using the AsBinary()
function).

Any help would be greatly appreciated.

a.fu...@lqt.it

unread,
Dec 5, 2011, 11:37:57 AM12/5/11
to spatiali...@googlegroups.com
Hi Sascha,

it's not at all difficult; you simply have to carefully consider
this few basic elements:

a) SpatiaLite stores any Geometry object using it's own "internal
BLOB-Geometry format" (please note well: closely related to WKB,
but not exactly WKB ...)

b) WKB comes in two distinct flavors: "pure" WKB (2D), and the GEOS/PostGIS
own "extended" WKB (EWKB) supporting 3D as well; I'm not really sure
about this, but I imagine that JTS supports EWKB

c) SpatiaLite supports any possible method allowing to convert any format
into any other one: both at C-API level and as SQL functions.

---------------------------


> I was previously using gaiaToSpatiaLiteBlobWkb to create my blob. This
> was working fine when writing and reading from the database in C++

> but I now also want to query geometries from my Java application.
>

So, you cannot still continue using the powerful C-API,
due to obvious language limitations.
Forget using gaiaToSpatiaLiteBlobWkb().
But you can still use the other language-independent method,
i.e. using SQL functions.

obviously: "internal BLOB-Geometry" isn't WKB. you have to convert
your raw BLOBs into standard WKB before.

conclusion:
===========
INSERT INTO objectstore (id, geom)
VALUES(1, ST_GeomFromWKB(?, 4326));

this SQL statement will convert your WKB geometry
into the corresponding internal-BLOB representation;
please note, WKB is a binary notation, so you are
expected to bind your WKB as a BLOB object.

> the Java I am using JTS to store geometries and therefore need to use
> standard WKB so that I can use their WKBReader to read geometries
>

SELECT id, ST_AsBinary(geom) FROM objectstore;

and this SQL query will directly return any Geometry
in the WKB notation, as expected by JTS.

this will surely work for 2D: for 3D I presume using instead
GeomFromEWKB() and AsEWKB() could be safest: you can easily
perform some test by yourself.

> I had no success reading spatialite blobs as WKB by using the
> AsBinary() function).
>

are you really sure about this ?
a) check 2D/3D and consider using EWKB
b) WKB is a binary notation: and accordingly to this
you have to fetch a BLOB object from the result-set.

bye Sandro

Sacha

unread,
Dec 6, 2011, 4:37:26 AM12/6/11
to SpatiaLite Users
Thanks Sandro!

I made the changes you suggested with ST_GeomFromWKB() and AsBinary()
and it works now! The problem I was having with JTS in the java was my
incorrect retrieval of the column bytes. I was getting the raw bytes
for my geometry column rather than the AsBinary() bytes. The JTS
WKBReader now reads the bytes succesfully back into the expected
geometry.

All I need to do now is solve the issue "no such function:
RTreeIntersects" as mentioned in my other thread about the win32
binaries.

Thanks again.

Reply all
Reply to author
Forward
0 new messages