jdeolive / geodb

Spatial database bindings for Java.
MIT License
67 stars 25 forks source link

Extent aggregate problem #25

Open GeoUser983 opened 8 years ago

GeoUser983 commented 8 years ago

I'm using the 0.8 version and there is a problem with ST_EXTENT:

@Override protected void add(Geometry geometry) { if (result == null) { result = geometry; } else { if (geometry != null) { result = result.union(geometry.getEnvelope()); } } }

If result is null you set it to geometry. The next geometry gets its envelope added with union. Now, this will work if the geometries are polygon. But when the geometry is for example LineString, the first union will create a geometry collection (linestring from the first iteration and the polygon from GetEnvelope...) which can not be unified (JTS union actually has a check: checkNotGeometryCollection(this);)

The solution is to set the result to geometry.getEnvelope in the first iteration.

jdeolive commented 8 years ago

Thanks for reporting the issue. It makes sense but when I tried to reproduce I could not. Any chance you can provide a sample of geometries that lead to the issue?

GeoUser983 commented 8 years ago

Here is how i can replicate the problem. Firstly i create a database using the geotools (so all the aliases get setup) then i connected the DBeaver to this database and executed the following lines: (also note that the same thing happens on purely geotools created tables)

CREATE TABLE PUBLIC.TEST_EXTENT ( ID INTEGER NOT NULL,
GEOM BLOB );

insert into PUBLIC.TEST_EXTENT(ID, GEOM) VALUES(1, ST_GeomFromText('LINESTRING (401144.32 4886020.44, 401169.16 4886039.33)', 2170)); insert into PUBLIC.TEST_EXTENT(ID, GEOM) VALUES(2, ST_GeomFromText('LINESTRING (401169.16 4886039.33, 401169.48 4886039.56)', 2170)); insert into PUBLIC.TEST_EXTENT(ID, GEOM) VALUES(3, ST_GeomFromText('LINESTRING (401157.54 4886055.71, 401169.48 4886039.56)', 2170));

-- all fine SELECT ST_ENVELOPE(geom) FROM TEST_EXTENT

-- exception: General error: java.lang.IllegalArgumentException: This method does not support GeometryCollection arguments; SQL statement: SELECT ST_Extent(geom) FROM TEST_EXTENT

And with the proposed fix, the last SQL works as intended.