Skip to content

Commit

Permalink
Merge pull request #1386 from ebocher/fix_function_srid
Browse files Browse the repository at this point in the history
Fix operations when the input geometries are empty.
  • Loading branch information
ebocher authored Jun 14, 2024
2 parents b218ae9 + 84905fe commit f813bb5
Show file tree
Hide file tree
Showing 39 changed files with 236 additions and 91 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
- Declare the schema for the spatial_ref_sys table
- Add SnapToGrid function
- Write empty geometry with the shapefile driver
- Do not ask for SRID when geometry is empty
- Add the name of the table in the FGB header
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public String write(ProgressVisitor progress, String tableName, File fileName, b
}
rs.beforeFirst();
ProgressVisitor copyProgress = progress.subProcess(recordCount);
doExport(progress, rs, spatialFieldNameAndIndex.first(), "GEOMETRY",srid, recordCount, new FileOutputStream(fileName));
doExport(progress, rs, spatialFieldNameAndIndex.first(), "GEOMETRY",srid, recordCount, new FileOutputStream(fileName), null);
copyProgress.endOfProgress();
return fileName.getAbsolutePath();
} else {
Expand Down Expand Up @@ -183,7 +183,7 @@ private void fgbWrite(ProgressVisitor progress, String tableName, FileOutputStre
String geomCol = geomMetadata.first();
geomMetadata.second();
ResultSet rs = st.executeQuery(String.format("select * from %s", outputTable));
doExport(progress, rs, geomCol, geomMetadata.second().sfs_geometryType, geomMetadata.second().SRID, recordCount, outputStream);
doExport(progress, rs, geomCol, geomMetadata.second().sfs_geometryType, geomMetadata.second().SRID, recordCount, outputStream, tableName);
}
}
} finally {
Expand All @@ -197,12 +197,12 @@ private void fgbWrite(ProgressVisitor progress, String tableName, FileOutputStre
}
}

private String doExport(ProgressVisitor progress, ResultSet rs, String geometryColumn, String geometryType, int srid, int recordCount, FileOutputStream outputStream) throws SQLException, IOException {
private String doExport(ProgressVisitor progress, ResultSet rs, String geometryColumn, String geometryType, int srid, int recordCount, FileOutputStream outputStream, String name) throws SQLException, IOException {

FlatBufferBuilder bufferBuilder = new FlatBufferBuilder();

//Write the header
HeaderMeta header = writeHeader(outputStream, bufferBuilder, recordCount, geometryType, srid, rs.getMetaData());
HeaderMeta header = writeHeader(outputStream, name, bufferBuilder, recordCount, geometryType, srid, rs.getMetaData());

long endHeaderPosition = outputStream.getChannel().position();
//Columns numbers
Expand Down Expand Up @@ -377,7 +377,7 @@ private String fgbWrite(ProgressVisitor progress, ResultSet rs, FileOutputStream
* @throws SQLException
* @throws IOException
*/
private HeaderMeta writeHeader(FileOutputStream outputStream, FlatBufferBuilder bufferBuilder, long rowCount, String geometryType, int srid, ResultSetMetaData metadata) throws SQLException, IOException {
private HeaderMeta writeHeader(FileOutputStream outputStream, String name, FlatBufferBuilder bufferBuilder, long rowCount, String geometryType, int srid, ResultSetMetaData metadata) throws SQLException, IOException {
outputStream.write(Constants.MAGIC_BYTES);
//Get the column information
List<ColumnMeta> columns = new ArrayList<>();
Expand All @@ -396,6 +396,7 @@ private HeaderMeta writeHeader(FileOutputStream outputStream, FlatBufferBuilder
columns.add(column);
}
HeaderMeta headerMeta = new HeaderMeta();
headerMeta.name=name;
headerMeta.featuresCount = rowCount;
headerMeta.geometryType = geometryType(geometryType);
headerMeta.columns = columns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static Geometry setSRID(Geometry geometry, Integer srid) throws IllegalAr
if (geometry == null) {
return null;
}
if(geometry.isEmpty()){
return geometry;
}
if (srid == null) {
throw new IllegalArgumentException("The SRID code cannot be null.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static Geometry getFurthestCoordinate(Point point, Geometry geom) throws
if (point == null || geom == null) {
return null;
}
if(point.isEmpty()||geom.isEmpty()){
return null;
}
if(point.getSRID()!=geom.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static Point closestPoint(Geometry geomA, Geometry geomB) throws SQLExcep
if (geomA == null || geomB == null) {
return null;
}
if(geomA.isEmpty()||geomB.isEmpty()){
return null;
}
if(geomA.getSRID()!=geomB.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static Geometry getFurthestCoordinate(Point point, Geometry geom) throws
if (point == null || geom == null) {
return null;
}
if(point.isEmpty()||geom.isEmpty()){
return null;
}
if(point.getSRID()!=geom.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static Geometry longestLine(Geometry geomA, Geometry geomB) throws SQLExc
if(geomA ==null || geomB==null){
return null;
}
if(geomA.isEmpty()||geomB.isEmpty()){
return null;
}
if(geomA.getSRID()!=geomB.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static Double maxDistance(Geometry geomA, Geometry geomB) throws SQLExcep
if(geomA ==null || geomB==null){
return null;
}
if(geomA.isEmpty()||geomB.isEmpty()){
return null;
}
if(geomA.getSRID()!=geomB.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static Point projectPoint(Geometry point, Geometry geometry) throws SQLEx
if (point == null || geometry==null) {
return null;
}
if(point.isEmpty()||geometry.isEmpty()){
return null;
}
if(point.getSRID()!=geometry.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ public String getJavaStaticMethod() {
* @throws SQLException In case of wrong parameters
*/
public static Geometry isovist(Geometry viewPoint, Geometry lineSegments, double maxDistance) throws SQLException {
if (!(viewPoint instanceof Point) || viewPoint.isEmpty()) {
if (!(viewPoint instanceof Point)) {
throw new SQLException("First parameter of ST_Isovist must be a Point");
}
}
if(viewPoint.isEmpty()||lineSegments.isEmpty()){
return null;
}
if(viewPoint.getSRID()!=lineSegments.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public static Double computeSvf(Point pt, Geometry geoms, double distance, int r
}
if(geoms == null){
return svf;
}
}
if(pt.isEmpty()||geoms.isEmpty()){
return null;
}
if(pt.getSRID()!=geoms.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public static Geometry addPoint(Geometry geometry, Point point) throws SQLExcept
if(geometry == null || point == null){
return null;
}
if(point.isEmpty()){
return geometry;
}
if(geometry.isEmpty()){
return geometry;
}

if(geometry.getSRID()!=point.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down Expand Up @@ -84,6 +91,9 @@ public static Geometry addPoint(Geometry geometry, Point point, int position) th
if(geometry == null || point == null){
return null;
}
if(geometry.isEmpty()||point.isEmpty()){
return geometry;
}
if(geometry.getSRID()!=point.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public static Geometry insertPoint(Geometry geometry, Point point, double tolera
if(geometry == null || point == null){
return null;
}
if(point.isEmpty()||geometry.isEmpty()){
return geometry;
}
if(geometry.getSRID()!=point.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static Geometry removePoint(Geometry geometry, Polygon polygon) throws SQ
if(geometry == null){
return null;
}
if(geometry.isEmpty()||polygon.isEmpty()){
return geometry;
}
if(geometry.getSRID()!=polygon.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
* 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;
* <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 <http://www.gnu.org/licenses/>.
*
*
* <p>
* <p>
* 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.operators;

import java.sql.SQLException;
import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.operation.overlayng.OverlayNG;
import org.locationtech.jts.operation.overlayng.OverlayNGRobust;

import java.sql.SQLException;

/**
* Compute the difference between two Geometries.
*
Expand All @@ -39,7 +40,8 @@ public class ST_Difference extends DeterministicScalarFunction {
*/
public ST_Difference() {
addProperty(PROP_REMARKS, "Compute the difference between two Geometries.\n" +
"If the gridSize argument is provided, the inputs geometries are snapped to a grid of the given size.");;
"If the gridSize argument is provided, the inputs geometries are snapped to a grid of the given size.");
;
}

@Override
Expand All @@ -52,14 +54,15 @@ public String getJavaStaticMethod() {
* @param b Geometry instance
* @return the difference between two geometries
*/
public static Geometry difference(Geometry a,Geometry b) throws SQLException {
if(a==null || b==null) {
public static Geometry difference(Geometry a, Geometry b) throws SQLException {
if (a == null || b == null) {
return null;
}
if(a.getSRID()!=b.getSRID()){
}
if (a.getSRID() == b.getSRID()) {
return OverlayNGRobust.overlay(a, b, OverlayNG.DIFFERENCE);
} else {
throw new SQLException("Operation on mixed SRID geometries not supported");
}
return OverlayNGRobust.overlay(a,b, OverlayNG.DIFFERENCE);
}

/**
Expand All @@ -68,18 +71,19 @@ public static Geometry difference(Geometry a,Geometry b) throws SQLException {
* @param gridSize size of a grid to snap the input geometries
* @return the difference between two geometries
*/
public static Geometry difference(Geometry a,Geometry b, double gridSize) throws SQLException {
if(a==null || b==null) {
public static Geometry difference(Geometry a, Geometry b, double gridSize) throws SQLException {
if (a == null || b == null) {
return null;
}
if(a.getSRID()!=b.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
if (gridSize >= 0) {
PrecisionModel pm = new PrecisionModel(1/gridSize);
return OverlayNG.overlay(a, b, OverlayNG.DIFFERENCE, pm);
if (a.getSRID() == b.getSRID()) {
if (gridSize >= 0) {
PrecisionModel pm = new PrecisionModel(1 / gridSize);
return OverlayNG.overlay(a, b, OverlayNG.DIFFERENCE, pm);
} else {
return OverlayNGRobust.overlay(a, b, OverlayNG.DIFFERENCE);
}
} else {
return OverlayNGRobust.overlay(a, b, OverlayNG.DIFFERENCE);
throw new SQLException("Operation on mixed SRID geometries not supported");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public static Geometry intersection(Geometry a, Geometry b) throws SQLException
if (a == null || b == null) {
return null;
}
if (a.getSRID() != b.getSRID()) {
if (a.getSRID() == b.getSRID()) {
return OverlayNGRobust.overlay(a, b, OverlayNG.INTERSECTION);
} else {
throw new SQLException("Operation on mixed SRID geometries not supported");
}
return OverlayNGRobust.overlay(a, b, OverlayNG.INTERSECTION);
}

/**
Expand All @@ -76,14 +77,15 @@ public static Geometry intersection(Geometry a, Geometry b, double gridSize) thr
if (a == null || b == null) {
return null;
}
if (a.getSRID() != b.getSRID()) {
throw new SQLException("Operation on mixed SRID geometries not supported");
}
if (gridSize >= 0) {
PrecisionModel pm = new PrecisionModel(1/gridSize);
return OverlayNG.overlay(a, b, OverlayNG.INTERSECTION, pm);
if (a.getSRID() == b.getSRID()) {
if (gridSize >= 0) {
PrecisionModel pm = new PrecisionModel(1/gridSize);
return OverlayNG.overlay(a, b, OverlayNG.INTERSECTION, pm);
} else {
return OverlayNGRobust.overlay(a, b, OverlayNG.INTERSECTION);
}
} else {
return OverlayNGRobust.overlay(a, b, OverlayNG.INTERSECTION);
throw new SQLException("Operation on mixed SRID geometries not supported");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ public String getJavaStaticMethod() {
public static Geometry symDifference(Geometry a,Geometry b) throws SQLException {
if(a==null || b==null) {
return null;
}
if(a.getSRID()!=b.getSRID()){
}
if (a.getSRID() == b.getSRID()) {
return OverlayNGRobust.overlay(a,b, OverlayNG.SYMDIFFERENCE);
} else {
throw new SQLException("Operation on mixed SRID geometries not supported");
}
return OverlayNGRobust.overlay(a,b, OverlayNG.SYMDIFFERENCE);
}

/**
Expand All @@ -73,14 +74,16 @@ public static Geometry symDifference(Geometry a,Geometry b, double gridSize) thr
if(a==null || b==null) {
return null;
}
if(a.getSRID()!=b.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
}
if (gridSize >= 0) {
PrecisionModel pm = new PrecisionModel(1/gridSize);
return OverlayNG.overlay(a, b, OverlayNG.SYMDIFFERENCE, pm);
if (a.getSRID() == b.getSRID()) {
if (gridSize >= 0) {
PrecisionModel pm = new PrecisionModel(1/gridSize);
return OverlayNG.overlay(a, b, OverlayNG.SYMDIFFERENCE, pm);
} else {
return OverlayNGRobust.overlay(a, b, OverlayNG.SYMDIFFERENCE);
}
} else {
return OverlayNGRobust.overlay(a, b, OverlayNG.SYMDIFFERENCE);
throw new SQLException("Operation on mixed SRID geometries not supported");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public static Boolean isContains(Geometry surface,Geometry testGeometry) throws
}
if(testGeometry==null) {
return false;
}
}
if(surface.isEmpty() || testGeometry.isEmpty()){
return false;
}

if(surface.getSRID()!=testGeometry.getSRID()){
throw new SQLException("Operation on mixed SRID geometries not supported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static Boolean covers(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");
Expand Down
Loading

0 comments on commit f813bb5

Please sign in to comment.