Skip to content

Commit

Permalink
Add ST_SnapToSelf function
Browse files Browse the repository at this point in the history
  • Loading branch information
ebocher committed Jun 18, 2024
1 parent 0f3fb75 commit 6edc50d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
- Do not ask for SRID when geometry is empty
- Add the name of the table in the FGB header
- Fix null way id on OSM file
- Add ST_SnapToSelf function
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.h2gis.functions.spatial.properties.*;
import org.h2gis.functions.spatial.snap.ST_Project;
import org.h2gis.functions.spatial.snap.ST_Snap;
import org.h2gis.functions.spatial.snap.ST_SnapToSelf;
import org.h2gis.functions.spatial.split.ST_LineIntersector;
import org.h2gis.functions.spatial.split.ST_Split;
import org.h2gis.functions.spatial.split.ST_SubDivide;
Expand Down Expand Up @@ -338,7 +339,8 @@ public static Function[] getBuiltInsFunctions() {
new ST_Project(),
new ST_IsProjectedCRS(),
new ST_IsGeographicCRS(),
new ST_SnapToGrid()
new ST_SnapToGrid(),
new ST_SnapToSelf()
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* <a href="https://www.h2database.com">https://www.h2database.com</a>. H2GIS is developed by CNRS
* <a href="https://www.cnrs.fr/">https://www.cnrs.fr/</a>.
* <p>
* 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.
* <p>
* 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 <https://www.gnu.org/licenses/>.
* <p>
* <p>
* For more information, please consult: <a href="https://www.h2gis.org/">https://www.h2gis.org/</a>
* or contact directly: info_at_h2gis.org
*/

package org.h2gis.functions.spatial.snap;

import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.operation.overlay.snap.GeometrySnapper;

import java.sql.SQLException;

/**
* Snaps a geometry to itself with a given tolerance
* Allows optionally cleaning the result to ensure it is topologically valid
*
* @author Erwan Bocher
*/
public class ST_SnapToSelf extends DeterministicScalarFunction {

public ST_SnapToSelf() {
addProperty(PROP_REMARKS, "Snaps a geometry to itself with a given tolerance.\n" +
"Allows optionally cleaning the result to ensure it is topologically valid. true by default");
}

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


/**
* Snaps a geometry to itself with a given tolerance
*
* @param geometryA a geometry to snap
* @param distance the tolerance to use
* @return the snapped geometries
* @throws SQLException
*/
public static Geometry execute(Geometry geometryA, double distance ) throws SQLException {
return execute(geometryA, distance, true);
}
/**
* Snaps a geometry to itself with a given tolerance
*
* @param geometryA a geometry to snap
* @param distance the tolerance to use
* @param clean true to clean the geometry
* @return the snapped geometries
* @throws SQLException
*/
public static Geometry execute(Geometry geometryA, double distance, boolean clean ) throws SQLException {
if (geometryA == null ) {
return null;
}
return GeometrySnapper.snapToSelf(geometryA, distance, clean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ public void test_ST_SnapToGrid2() throws Exception {
rs.close();
}

@Test
public void test_ST_SnapToSelf1() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_SnapToSelf('POLYGON ((0 0, 1 1, 2 1, 3 0.5, 2 -3, 3 0.499, 0 0))'::GEOMETRY, 0.001);");
rs.next();
assertGeometryEquals("POLYGON ((0 0, 1 1, 2 1, 3 0.5, 3 0.499, 0 0))", rs.getBytes(1));
rs.close();
}


@Test
public void test_ST_RingSideBuffer1() throws Exception {
Expand Down

0 comments on commit 6edc50d

Please sign in to comment.