Skip to content

Commit

Permalink
Merge pull request #1384 from ebocher/snaptogrid
Browse files Browse the repository at this point in the history
SnapToGrid function
  • Loading branch information
ebocher authored May 21, 2024
2 parents a25c94c + f4f9791 commit b218ae9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Changelog for v2.2.2
- Read CPG file attached to the shapefile-
- Declare the schema for the spatial_ref_sys table
- Add SnapToGrid function
- Write empty geometry with the shapefile driver
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.h2gis.functions.spatial.generalize.ST_PrecisionReducer;
import org.h2gis.functions.spatial.generalize.ST_Simplify;
import org.h2gis.functions.spatial.generalize.ST_SimplifyPreserveTopology;
import org.h2gis.functions.spatial.generalize.ST_SnapToGrid;
import org.h2gis.functions.spatial.linear_referencing.ST_LineInterpolatePoint;
import org.h2gis.functions.spatial.linear_referencing.ST_LineSubstring;
import org.h2gis.functions.spatial.mesh.ST_ConstrainedDelaunay;
Expand Down Expand Up @@ -336,7 +337,8 @@ public static Function[] getBuiltInsFunctions() {
new ST_MinimumBoundingRadius(),
new ST_Project(),
new ST_IsProjectedCRS(),
new ST_IsGeographicCRS()
new ST_IsGeographicCRS(),
new ST_SnapToGrid()
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static Geometry precisionReducer(Geometry geometry, int nbDec) throws SQL
if (geometry == null) {
return null;
}
if(nbDec==0){
return geometry;
}
if (nbDec < 0) {
throw new SQLException("Decimal_places has to be >= 0.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* 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.generalize;

import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.precision.GeometryPrecisionReducer;

import java.sql.SQLException;

/**
* Function to snap a geometry according a grid size
* @author Erwan Bocher, CNRS, 2024
*/
public class ST_SnapToGrid extends DeterministicScalarFunction {

public ST_SnapToGrid() {
addProperty(PROP_REMARKS, "Snap all points of the input geometry to the grid defined by its origin and cell size.");
}

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

/**
* Reduce the geometry precision. cell_size is resolution of grid to snap the points
*
* @param geometry
* @param cell_size
* @return
* @throws SQLException
*/
public static Geometry execute(Geometry geometry, float cell_size) throws SQLException {
if (geometry == null) {
return null;
}
if(cell_size==0){
return geometry;
}
if (cell_size < 0) {
throw new SQLException("cell size has to be >= 0.");
}
PrecisionModel pm = new PrecisionModel(Math.round(1/cell_size));
GeometryPrecisionReducer geometryPrecisionReducer = new GeometryPrecisionReducer(pm);
try {
return geometryPrecisionReducer.reduce(geometry);
} catch (IllegalArgumentException ex) {
return geometry;
}
}

/**
* Computes the scale factor for a given number of decimal places.
*
* @param decimalPlaces
* @return the scale factor
*/
public static double scaleFactorForDecimalPlaces(int decimalPlaces) {
return Math.pow(10.0, decimalPlaces);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ public void test_ST_PrecisionReducer4() throws Exception {
rs.close();
}

@Test
public void test_ST_SnapToGrid1() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_SnapToGrid('LINESTRING(1.1115678 2.123, 4.111111 3.2374897, 4.11112 3.23748667)'::GEOMETRY, 0.001);");
rs.next();
assertGeometryEquals("LINESTRING(1.112 2.123,4.111 3.237)", rs.getBytes(1));
rs.close();
}

@Test
public void test_ST_SnapToGrid2() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_SnapToGrid('LINESTRINGZ(1.1115678 2.123 1, 4.111111 3.2374897 1, 4.11112 3.23748667 1)'::GEOMETRY, 0.001);");
rs.next();
assertGeometryEquals("LINESTRINGZ(1.112 2.123 1,4.111 3.237 1)", rs.getBytes(1));
rs.close();
}


@Test
public void test_ST_RingSideBuffer1() throws Exception {
ResultSet rs = st.executeQuery("SELECT ST_RingSideBuffer('SRID=4326;LINESTRING (-10 10, 10 10)'::GEOMETRY, 10, 3);");
Expand Down

0 comments on commit b218ae9

Please sign in to comment.