diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 067ac02f22..e443f52792 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,3 +5,4 @@ - Add ST_OverpassDownloader function - Fix bug when read GeometryCollection with the ST_GeomFromGeoJSON function - Fix github actions +- Fix mixed srid error on empty geometry with ST_Extent #1400 diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/spatial/properties/ST_Extent.java b/h2gis-functions/src/main/java/org/h2gis/functions/spatial/properties/ST_Extent.java index 63c33c55ef..83004ddd5e 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/spatial/properties/ST_Extent.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/spatial/properties/ST_Extent.java @@ -61,15 +61,16 @@ public int getInternalType(int[] inputTypes) throws SQLException { @Override public void add(Object o) throws SQLException { if (o instanceof Geometry) { - Geometry geom = (Geometry) o; - int currentSRID = geom.isEmpty()?0:geom.getSRID(); - if(srid==0){ - srid=currentSRID; + Geometry geom = (Geometry) o; + if(!geom.isEmpty()) { + int currentSRID = geom.getSRID(); + if (srid == 0) { + srid = currentSRID; + } else if (srid != currentSRID) { + throw new SQLException("Operation on mixed SRID geometries not supported"); + } + aggregatedEnvelope.expandToInclude(geom.getEnvelopeInternal()); } - else if(srid!=currentSRID){ - throw new SQLException("Operation on mixed SRID geometries not supported"); - } - aggregatedEnvelope.expandToInclude(geom.getEnvelopeInternal()); } } diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/spatial/SpatialFunctionTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/spatial/SpatialFunctionTest.java index e7a3e4ecf8..679a39661d 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/spatial/SpatialFunctionTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/spatial/SpatialFunctionTest.java @@ -295,6 +295,28 @@ public void test_NULL_ST_Extent() throws Exception { st.execute("drop table ptClouds"); } + @Test + public void test_Empty_ST_Extent() throws Exception { + st.execute("drop table if exists ptClouds"); + st.execute("create table ptClouds(id INTEGER PRIMARY KEY AUTO_INCREMENT, the_geom GEOMETRY(MultiPoint));" + + "insert into ptClouds(the_geom) VALUES (ST_MPointFromText('MULTIPOINT(5 5, 1 2, 3 4, 99 3)',2154))," + + "(ST_MPointFromText('MULTIPOINT(-5 12, 11 22, 34 41, 65 124)',2154))," + + "(ST_MPointFromText('MULTIPOINT EMPTY',2154))," + + "(ST_MPointFromText('MULTIPOINT(1 12, 5 -21, 9 41, 32 124)',2154));"); + ResultSet rs = st.executeQuery("select ST_Extent(the_geom) tableEnv from ptClouds;"); + assertTrue(rs.next()); + Object resultObj = rs.getObject("tableEnv"); + assertTrue(resultObj instanceof Geometry); + Envelope result = ((Geometry) resultObj).getEnvelopeInternal(); + Envelope expected = new Envelope(-5, 99, -21, 124); + assertEquals(expected.getMinX(), result.getMinX(), 1e-12); + assertEquals(expected.getMaxX(), result.getMaxX(), 1e-12); + assertEquals(expected.getMinY(), result.getMinY(), 1e-12); + assertEquals(expected.getMaxY(), result.getMaxY(), 1e-12); + assertFalse(rs.next()); + st.execute("drop table ptClouds"); + } + @Test public void test_TableEnvelope() throws Exception { st.execute("drop table if exists ptClouds");