Skip to content

Commit

Permalink
Merge pull request #1376 from ebocher/st_isprojectedCRS
Browse files Browse the repository at this point in the history
ST_IsProjectedCRS and ST_IsGeographicCRS
  • Loading branch information
ebocher authored Nov 6, 2023
2 parents 0fcfdf1 + 42ef677 commit cd23a74
Show file tree
Hide file tree
Showing 5 changed files with 222 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 @@ -32,3 +32,4 @@
+ FlatGeobuf improve data types
+ FlatGeobuf enable select query on writer
+ Fix geojson about issue #1374 geojson without coordinates give h2 problems with coordinate dimension incompatibilities
+ Add ST_IsProjectedCRS and ST_IsGeographicCRS to check if the CRS is projected or geographic
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ public static Function[] getBuiltInsFunctions() {
new ST_MakeArcLine(),
new ST_MakeArcPolygon(),
new ST_MinimumBoundingRadius(),
new ST_Project()
new ST_Project(),
new ST_IsProjectedCRS(),
new ST_IsGeographicCRS()
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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.crs;

import org.cts.CRSFactory;
import org.cts.crs.*;
import org.cts.op.CoordinateOperation;
import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
* Method to check if the CRS of the geometry is geographic
* @author Erwan Bocher, CNRS
*/
public class ST_IsGeographicCRS extends DeterministicScalarFunction {

private static CRSFactory crsf;
private static SpatialRefRegistry srr = new SpatialRefRegistry();
private static Map<EPSGTuple, CoordinateOperation> copPool = new ST_Transform.CopCache(5);

public ST_IsGeographicCRS() {
addProperty(PROP_REMARKS, "ST_IsGeographicCRS takes a geometry and \n"
+ "return true is the coordinate system is geographic, otherwise false. False is the geometry is null \n" +
"and or if its srid is equals to 0.");
}

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

/**
* Return true if the geometry has a geographic CRS
*
* @param geometry
* @return
* @throws SQLException
*/
public static Boolean execute(Connection connection, Geometry geometry) throws SQLException {
if (geometry == null) {
return false;
}
int inputSRID = geometry.getSRID();
if (inputSRID == 0) {
return false;
} else {
if (crsf == null) {
crsf = new CRSFactory();
//Activate the CRSFactory and the internal H2 spatial_ref_sys registry to
// manage Coordinate Reference Systems.
crsf.getRegistryManager().addRegistry(srr);
}
srr.setConnection(connection);
try {
CoordinateReferenceSystem inputCRS = crsf.getCRS(srr.getRegistryName() + ":" + inputSRID);
if(inputCRS instanceof Geographic2DCRS || inputCRS instanceof Geographic3DCRS){
return true;
}

} catch (CRSException ex) {
throw new SQLException("Cannot create the CRS", ex);
} finally {
srr.setConnection(null);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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.crs;

import org.cts.CRSFactory;
import org.cts.crs.CRSException;
import org.cts.crs.CoordinateReferenceSystem;
import org.cts.crs.ProjectedCRS;
import org.cts.op.CoordinateOperation;
import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
* Method to check if the CRS of the geometry is projected
* @author Erwan Bocher, CNRS
*/
public class ST_IsProjectedCRS extends DeterministicScalarFunction {

private static CRSFactory crsf;
private static SpatialRefRegistry srr = new SpatialRefRegistry();
private static Map<EPSGTuple, CoordinateOperation> copPool = new ST_Transform.CopCache(5);

public ST_IsProjectedCRS() {
addProperty(PROP_REMARKS, "ST_IsProjectedCRS takes a geometry and \n"
+ "return true is the coordinate system is projected, otherwise false. False is the geometry is null \n" +
"and or if its srid is equals to 0.");
}

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

/**
* Return true if the CRS is a projected one
*
* @param geometry
* @return
* @throws java.sql.SQLException
*/
public static Boolean execute(Connection connection, Geometry geometry) throws SQLException {
if (geometry == null) {
return false;
}
int inputSRID = geometry.getSRID();
if (inputSRID == 0) {
return false;
} else {
if (crsf == null) {
crsf = new CRSFactory();
//Activate the CRSFactory and the internal H2 spatial_ref_sys registry to
// manage Coordinate Reference Systems.
crsf.getRegistryManager().addRegistry(srr);
}
srr.setConnection(connection);
try {
CoordinateReferenceSystem inputCRS = crsf.getCRS(srr.getRegistryName() + ":" + inputSRID);
if(inputCRS instanceof ProjectedCRS){
return true;
}

} catch (CRSException ex) {
throw new SQLException("Cannot create the CRS", ex);
} finally {
srr.setConnection(null);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,40 @@ public void testST_FindUTMSRID() throws SQLException {
assertTrue(rs.next());
assertEquals(32736, rs.getInt(1));
}

@Test
public void testST_IsProjectedCRS1() throws SQLException {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT ST_IsProjectedCRS('SRID=4326;POINT(3.68 59.04)'::GEOMETRY) as result");
assertTrue(rs.next());
assertFalse( rs.getBoolean(1));

rs = st.executeQuery("SELECT ST_IsProjectedCRS('POINT(3.68 59.04)'::GEOMETRY) as result");
assertTrue(rs.next());
assertFalse( rs.getBoolean(1));
}

@Test
public void testST_IsProjectedCRS2() throws SQLException {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT ST_IsProjectedCRS('SRID=2154;POINT(0 10)'::GEOMETRY) as result");
assertTrue(rs.next());
assertTrue( rs.getBoolean(1));

rs = st.executeQuery("SELECT ST_IsProjectedCRS('SRID=0;POINT(0 10)'::GEOMETRY) as result");
assertTrue(rs.next());
assertFalse( rs.getBoolean(1));
}

@Test
public void testST_IsGeographicCRS() throws SQLException {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT ST_IsGeographicCRS('SRID=4326;POINT(3.68 59.04)'::GEOMETRY) as result");
assertTrue(rs.next());
assertTrue( rs.getBoolean(1));

rs = st.executeQuery("SELECT ST_IsGeographicCRS('POINT(3.68 59.04)'::GEOMETRY) as result");
assertTrue(rs.next());
assertFalse( rs.getBoolean(1));
}
}

0 comments on commit cd23a74

Please sign in to comment.