Skip to content

Commit

Permalink
Active the new relate matrix and add ST_CoveredBy function
Browse files Browse the repository at this point in the history
  • Loading branch information
ebocher committed Sep 3, 2024
1 parent 3226e2d commit 45a2a6e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
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
@@ -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 @@ -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();
}
}

0 comments on commit 45a2a6e

Please sign in to comment.