Skip to content

Commit

Permalink
Merge pull request #1392 from ebocher/h2_jts_update
Browse files Browse the repository at this point in the history
  • Loading branch information
ebocher authored Sep 3, 2024
2 parents fac56e4 + 45a2a6e commit 0701029
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 27 deletions.
3 changes: 2 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
- Fix null way id on OSM file-
- Use new sonatype auth
- Add ST_SnapToSelf function
- Upgrade to H2 2.3.230
- Upgrade to H2 2.3.230
- Upgrade to JTS 1.20
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class Activator implements BundleActivator {

@Override
public void start(BundleContext bc) {
//Set JTS relate to use the new one
System.setProperty("jts.relate", "ng");
for(Function function : H2GISFunctions.getBuiltInsFunctions()) {
bc.registerService(Function.class, function, null);
if(function instanceof DriverFunction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ public static Function[] getBuiltInsFunctions() {
new ST_IsProjectedCRS(),
new ST_IsGeographicCRS(),
new ST_SnapToGrid(),
new ST_SnapToSelf()
new ST_SnapToSelf(),
new ST_CoveredBy()
};
}

Expand Down Expand Up @@ -529,6 +530,8 @@ public static void unRegisterFunction(Statement st, Function function) throws SQ
*/
private static void registerH2GISFunctions(Connection connection, String packagePrepend) throws SQLException {
Statement st = connection.createStatement();
//Set JTS relate to use the new one
System.setProperty("jts.relate", "ng");
for (Function function : getBuiltInsFunctions()) {
try {
registerFunction(st, function, packagePrepend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Geometry execute(Geometry geometry) throws SQLException {
return geom;
}
else{
LargestEmptyCircle lec = new LargestEmptyCircle(geometry, tolerance);
LargestEmptyCircle lec = new LargestEmptyCircle(geometry, null, tolerance);
Geometry geom = lec.getCenter().buffer(lec.getRadiusLine().getLength());
geom.setSRID(geometry.getSRID());
return geom;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* <a href="http://www.h2database.com">http://www.h2database.com</a>. H2GIS is developed by CNRS
* <a href="http://www.cnrs.fr/">http://www.cnrs.fr/</a>.
*
* This code is part of the H2GIS project. H2GIS is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* version 3.0 of the License.
*
* H2GIS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details <http://www.gnu.org/licenses/>.
*
*
* For more information, please consult: <a href="http://www.h2gis.org/">http://www.h2gis.org/</a>
* or contact directly: info_at_h2gis.org
*/

package org.h2gis.functions.spatial.predicates;

import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;

import java.sql.SQLException;

/**
* ST_CoveredBy returns true if no point in geometry B is outside geometry A.
*
* @author Erwan Bocher
*/
public class ST_CoveredBy extends DeterministicScalarFunction {

public ST_CoveredBy() {
addProperty(PROP_REMARKS, "Returns true if this geomA is covered by geomB according the definitions : \n" +
"Every point of this geometry is a point of the other geometry.\n" +
"The DE-9IM Intersection Matrix for the two geometries matches\n" +
" at least one of the following patterns:\n" +
" [T*F**F***]\n" +
" [*TF**F***]\n" +
" [**FT*F***]\n" +
" [**F*TF***]\n");
}

@Override
public String getJavaStaticMethod() {
return "execute";
}

/**
* Returns true if this geomA is covered by geomB
*
* @param geomA Geometry A
* @param geomB Geometry B
* @return if this geomA is covered by geomB
* @throws SQLException
*/
public static Boolean execute(Geometry geomA, Geometry geomB) throws SQLException {
if(geomA == null||geomB == null){
return null;
}
if(geomA.isEmpty() || geomB.isEmpty()){
return false;
}

if(geomA.getSRID()!=geomB.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
return geomA.coveredBy(geomB);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.locationtech.jts.operation.buffer.BufferParameters;
import org.locationtech.jts.operation.relateng.RelateNG;
import org.locationtech.jts.operation.relateng.RelatePredicate;

import java.sql.*;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;


public class RegressionTest {
Expand Down Expand Up @@ -169,4 +172,23 @@ public void testGeometrySRID() throws SQLException {
}
});
}

@Test
public void testRelate() throws SQLException, ParseException {
WKTReader reader = new WKTReader();
Geometry gc0 = reader.read(
"POLYGON ((414188.5999999999 6422867.1, 414193.7 6422866.5, 414205.1 6422859.4, 414223.7 6422846.8, 414229.6 6422843.2, 414235.2 6422835.4, 414224.7 6422837.9, 414219.4 6422842.1, 414210.9 6422849, 414199.2 6422857.6, 414191.1 6422863.4, 414188.5999999999 6422867.1))");
Geometry gc1 = reader.read(
"LINESTRING (414187.2 6422831.6, 414179 6422836.1, 414182.2 6422841.8, 414176.7 6422844, 414184.5 6422859.5, 414188.6 6422867.1)");
assertTrue(RelateNG.relate(gc0, gc1, RelatePredicate.intersects()));
}

@Disabled
@Test
public void testBufferPrecision() throws SQLException, ParseException {
WKTReader wktReader = new WKTReader();
Geometry geom = wktReader.read("POINT(100 90)");
BufferOp bufOp = new BufferOp(geom, new BufferParameters(2));
System.out.println(bufOp.getResultGeometry(50));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ public void test_ST_ISOVIST1() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_Isovist('POINT(0 0)'::GEOMETRY, 'LINESTRING (100 0, 100 100, 0 100)'::GEOMETRY, "
+ "150);");
assertTrue(rs.next());
assertGeometryEquals("POLYGON ((-150 2.8421709430404007E-14, -147.11779206048456 29.26354830241928, -138.581929876693 57.40251485476347, -124.7204418453818 83.33553495294032, -106.06601717798212 106.06601717798213, -83.3355349529403 124.7204418453818, -57.40251485476347 138.58192987669304, -29.263548302419224 147.1177920604846, 0 150, 0 100, 100 100, 100 0, 150 0, 147.1177920604846 -29.26354830241931, 138.58192987669298 -57.40251485476355, 124.7204418453818 -83.33553495294032, 106.06601717798208 -106.06601717798215, 83.3355349529403 -124.72044184538181, 57.402514854763496 -138.58192987669298, 29.263548302419252 -147.11779206048456, -2.8421709430404007E-14 -150, -29.263548302419295 -147.11779206048456, -57.40251485476355 -138.58192987669298, -83.33553495294032 -124.72044184538179, -106.06601717798215 -106.06601717798212, -124.72044184538181 -83.3355349529403, -138.58192987669304 -57.40251485476345, -147.11779206048456 -29.263548302419252, -150 2.8421709430404007E-14)) ", rs.getObject(1));
assertGeometryEquals("POLYGON ((-150 0, -147.11779206048456 29.26354830241928, -138.581929876693 57.40251485476347, -124.7204418453818 83.33553495294032, -106.06601717798212 106.06601717798213, -83.3355349529403 124.7204418453818, -57.40251485476347 138.58192987669304, -29.263548302419224 147.1177920604846, 0 150, 0 100, 100 100, 100 0, 150 0, 147.1177920604846 -29.26354830241931, 138.58192987669298 -57.40251485476355, 124.7204418453818 -83.33553495294032, 106.06601717798208 -106.06601717798215, 83.3355349529403 -124.72044184538181, 57.402514854763496 -138.58192987669298, 29.263548302419252 -147.11779206048456, 0 -150, -29.263548302419295 -147.11779206048456, -57.40251485476355 -138.58192987669298, -83.33553495294032 -124.72044184538179, -106.06601717798215 -106.06601717798212, -124.72044184538181 -83.3355349529403, -138.58192987669304 -57.40251485476345, -147.11779206048456 -29.263548302419252, -150 0))", rs.getObject(1));
rs.close();
}

Expand All @@ -1113,7 +1113,7 @@ public void test_ST_ISOVIST3() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_Isovist('SRID=4326;POINT(0 0)'::GEOMETRY, 'SRID=4326;LINESTRING (100 0, 100 100, 0 100)'::GEOMETRY, "
+ "150);");
assertTrue(rs.next());
assertGeometryEquals("SRID=4326;POLYGON ((-150 2.8421709430404007E-14, -147.11779206048456 29.26354830241928, -138.581929876693 57.40251485476347, -124.7204418453818 83.33553495294032, -106.06601717798212 106.06601717798213, -83.3355349529403 124.7204418453818, -57.40251485476347 138.58192987669304, -29.263548302419224 147.1177920604846, 0 150, 0 100, 100 100, 100 0, 150 0, 147.1177920604846 -29.26354830241931, 138.58192987669298 -57.40251485476355, 124.7204418453818 -83.33553495294032, 106.06601717798208 -106.06601717798215, 83.3355349529403 -124.72044184538181, 57.402514854763496 -138.58192987669298, 29.263548302419252 -147.11779206048456, -2.8421709430404007E-14 -150, -29.263548302419295 -147.11779206048456, -57.40251485476355 -138.58192987669298, -83.33553495294032 -124.72044184538179, -106.06601717798215 -106.06601717798212, -124.72044184538181 -83.3355349529403, -138.58192987669304 -57.40251485476345, -147.11779206048456 -29.263548302419252, -150 2.8421709430404007E-14)) ", rs.getObject(1));
assertGeometryEquals("SRID=4326;POLYGON ((-150 0, -147.11779206048456 29.26354830241928, -138.581929876693 57.40251485476347, -124.7204418453818 83.33553495294032, -106.06601717798212 106.06601717798213, -83.3355349529403 124.7204418453818, -57.40251485476347 138.58192987669304, -29.263548302419224 147.1177920604846, 0 150, 0 100, 100 100, 100 0, 150 0, 147.1177920604846 -29.26354830241931, 138.58192987669298 -57.40251485476355, 124.7204418453818 -83.33553495294032, 106.06601717798208 -106.06601717798215, 83.3355349529403 -124.72044184538181, 57.402514854763496 -138.58192987669298, 29.263548302419252 -147.11779206048456, 0 -150, -29.263548302419295 -147.11779206048456, -57.40251485476355 -138.58192987669298, -83.33553495294032 -124.72044184538179, -106.06601717798215 -106.06601717798212, -124.72044184538181 -83.3355349529403, -138.58192987669304 -57.40251485476345, -147.11779206048456 -29.263548302419252, -150 0))", rs.getObject(1));
rs.close();
}

Expand All @@ -1136,7 +1136,7 @@ public void test_ST_VariableBuffer1() throws Exception {
public void test_ST_VariableBuffer2() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_VariableBuffer('LINESTRING (200 200, 400 200)'::GEOMETRY, 10, 20);");
assertTrue(rs.next());
assertGeometryEquals("POLYGON ((199.5 209.98749217771908, 399 219.97498435543818, 400 220, 403.90180644032256 219.61570560806462, 407.6536686473018 218.47759065022575, 411.11140466039205 216.6293922460509, 414.14213562373095 214.14213562373095, 416.6293922460509 211.11140466039205, 418.47759065022575 207.6536686473018, 419.6157056080646 203.90180644032256, 420 200, 419.6157056080646 196.09819355967744, 418.47759065022575 192.3463313526982, 416.6293922460509 188.88859533960795, 414.14213562373095 185.85786437626905, 411.11140466039205 183.3706077539491, 407.6536686473018 181.52240934977425, 403.90180644032256 180.38429439193538, 399 180.02501564456182, 199.5 190.01250782228092, 198.04909677983872 190.1921471959677, 196.1731656763491 190.76120467488713, 194.44429766980397 191.68530387697456, 192.92893218813452 192.92893218813452, 191.68530387697456 194.44429766980397, 190.76120467488713 196.1731656763491, 190.1921471959677 198.04909677983872, 190 200, 190.1921471959677 201.95090322016128, 190.76120467488713 203.8268343236509, 191.68530387697456 205.55570233019603, 192.92893218813452 207.07106781186548, 194.44429766980397 208.31469612302544, 196.1731656763491 209.23879532511287, 198.04909677983872 209.8078528040323, 199.5 209.98749217771908))", rs.getObject(1));
assertGeometryEquals("POLYGON ((190 200, 190.1921471959677 201.95090322016128, 190.76120467488713 203.8268343236509, 191.68530387697456 205.55570233019603, 192.92893218813452 207.07106781186548, 194.44429766980397 208.31469612302544, 196.1731656763491 209.23879532511287, 198.04909677983872 209.8078528040323, 199.5 209.98749217771908, 399 219.97498435543818, 400 220, 403.90180644032256 219.61570560806462, 407.6536686473018 218.47759065022575, 411.11140466039205 216.6293922460509, 414.14213562373095 214.14213562373095, 416.6293922460509 211.11140466039205, 418.47759065022575 207.6536686473018, 419.6157056080646 203.90180644032256, 420 200, 419.6157056080646 196.09819355967744, 418.47759065022575 192.3463313526982, 416.6293922460509 188.88859533960795, 414.14213562373095 185.85786437626905, 411.11140466039205 183.3706077539491, 407.6536686473018 181.52240934977425, 403.90180644032256 180.38429439193538, 400 180, 399 180.02501564456182, 199.5 190.01250782228092, 198.04909677983872 190.1921471959677, 196.1731656763491 190.76120467488713, 194.44429766980397 191.68530387697456, 192.92893218813452 192.92893218813452, 191.68530387697456 194.44429766980397, 190.76120467488713 196.1731656763491, 190.1921471959677 198.04909677983872, 190 200))", rs.getObject(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2684,11 +2684,37 @@ public void test_ST_IntersectsEmptyGeometry() throws Exception {
assertFalse(rs.getBoolean(1));
rs.close();
}

@Test
public void test_ST_IntersectsPrecision() throws Exception {
ResultSet rs = st.executeQuery("Select st_intersects('POLYGON ((414188.5999999999 6422867.1, 414193.7 6422866.5, 414205.1 6422859.4, 414223.7 6422846.8, 414229.6 6422843.2, 414235.2 6422835.4, 414224.7 6422837.9, 414219.4 6422842.1, 414210.9 6422849, 414199.2 6422857.6, 414191.1 6422863.4, 414188.5999999999 6422867.1))'::GEOMETRY, 'LINESTRING (414187.2 6422831.6, 414179 6422836.1, 414182.2 6422841.8, 414176.7 6422844, 414184.5 6422859.5, 414188.6 6422867.1)'::GEOMETRY) as op ");
rs.next();
assertTrue(rs.getBoolean(1));
rs.close();
}

@Test
public void test_ST_SRIDEmptyGeometry() throws Exception {
ResultSet rs = st.executeQuery("Select st_srid('POINT EMPTY'::GEOMETRY) as the_geom ");
rs.next();
assertEquals(0, rs.getObject(1));
rs.close();
}

@Test
public void test_CoveredBy() throws Exception {
//From PostGIS test
ResultSet rs = st.executeQuery("SELECT ST_CoveredBy(smallc,smallc) As smallinsmall,\n" +
" ST_CoveredBy(smallc, bigc) As smallcoveredbybig,\n" +
" ST_CoveredBy(ST_ExteriorRing(bigc), bigc) As exteriorcoveredbybig,\n" +
" ST_Within(ST_ExteriorRing(bigc),bigc) As exeriorwithinbig\n" +
"FROM (SELECT ST_Buffer(ST_GeomFromText('POINT(1 2)'), 10) As smallc,\n" +
" ST_Buffer(ST_GeomFromText('POINT(1 2)'), 20) As bigc) As foo;");
rs.next();
assertTrue(rs.getBoolean(1));
assertTrue(rs.getBoolean(2));
assertTrue(rs.getBoolean(3));
assertFalse(rs.getBoolean(4));
rs.close();
}
}
Loading

0 comments on commit 0701029

Please sign in to comment.