HHH-14757 Use geolatte's JTSUtils

This commit is contained in:
Karel Maesen 2021-08-28 14:16:56 +02:00
parent fe1f89d754
commit 3edad14c18
3 changed files with 36 additions and 133 deletions

View File

@ -9,11 +9,11 @@ package org.hibernate.spatial;
import java.util.Locale;
import org.hibernate.spatial.jts.JTSUtils;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.geolatte.geom.jts.JTSUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

View File

@ -1,132 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.jts;
//Note that this Utility class will be available directly from
// geolatte-geom 1.9
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
/**
* Some utility functions for working with JTS geometries
*/
public class JTSUtils {
private JTSUtils() {
}
/**
* Determines equality between geometries taking into
* account all coordinates, and the SRID.
* <p>
* This is used e.g. for Dirty-checking of geometry values
* in Hibernate
*
* @param g1
* @param g2
*
* @return
*/
public static boolean equalsExact3D(Geometry g1, Geometry g2) {
if ( g1 == g2 ) {
return true;
}
if ( g1 == null || g2 == null ) {
return false;
}
if ( !g1.getGeometryType().equals( g2.getGeometryType() ) ) {
return false;
}
if ( g1.getSRID() != g2.getSRID() ) {
return false;
}
//empty geometries of the same type are the same
if ( g1.isEmpty() && g2.isEmpty() ) {
return true;
}
int ng1 = g1.getNumGeometries();
int ng2 = g2.getNumGeometries();
if ( ng1 != ng2 ) {
return false;
}
if ( ng1 == 1 ) {
return equals3DPrimitiveGeometries( g1, g2 );
}
return equalComponentGeometries( g1, g2, ng1 );
}
private static boolean equalComponentGeometries(Geometry g1, Geometry g2, int ng1) {
for ( int gIdx = 0; gIdx < ng1; gIdx++ ) {
if ( !equalsExact3D( g1.getGeometryN( gIdx ), g2.getGeometryN( gIdx ) ) ) {
return false;
}
}
return true;
}
public static boolean equals3D(Coordinate c1, Coordinate c2) {
return c1.x == c2.x && c1.y == c2.y &&
( ( Double.isNaN( c1.z ) && Double.isNaN( c2.z ) ) || c1.z == c2.z ) &&
( ( Double.isNaN( c1.getM() ) && Double.isNaN( c2.getM() ) ) || c1.getM() == c2.getM() );
}
private static boolean equalLineStringCoordinates(LineString g1, LineString g2) {
int np1 = g1.getNumPoints();
int np2 = g2.getNumPoints();
if ( np1 != np2 ) {
return false;
}
for ( int i = 0; i < np1; i++ ) {
if ( !equalsExact3D( g1.getPointN( i ), g2.getPointN( i ) ) ) {
return false;
}
}
return true;
}
private static boolean equalPolygonCoordinates(Polygon g1, Polygon g2) {
int nr1 = g1.getNumInteriorRing();
int nr2 = g2.getNumInteriorRing();
if ( nr1 != nr2 ) {
return false;
}
for ( int i = 0; i < nr1; i++ ) {
if ( !equalLineStringCoordinates( g1.getInteriorRingN( i ), g2.getInteriorRingN( i ) ) ) {
return false;
}
}
return equalLineStringCoordinates( g1.getExteriorRing(), g2.getExteriorRing() );
}
private static boolean equals3DPrimitiveGeometries(Geometry g1, Geometry g2) {
//this method assumes that g1 and g2 are of the same type
assert ( g1.getClass().equals( g2.getClass() ) );
if ( g1 instanceof Point ) {
return equals3D( g1.getCoordinate(), g2.getCoordinate() );
}
if ( g1 instanceof LineString ) {
return equalLineStringCoordinates( (LineString) g1, (LineString) g2 );
}
if ( g1 instanceof Polygon ) {
return equalPolygonCoordinates( (Polygon) g1, (Polygon) g2 );
}
throw new IllegalStateException( "Only simple geometries should be used" );
}
}

View File

@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.geometry;
import org.hibernate.testing.TestForIssue;
import org.junit.Ignore;
import org.junit.Test;
import org.geolatte.geom.codec.Wkt;
import org.geolatte.geom.jts.JTS;
import org.locationtech.jts.geom.Geometry;
import org.geolatte.geom.jts.JTSUtils;
import static org.junit.Assert.assertTrue;
public class JTSUtilsTest {
@Test
@TestForIssue(jiraKey = "HHH-14757")
public void testGeometryCollection() {
Geometry elem2a = JTS.to( Wkt.fromWkt( "GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))" ) );
Geometry elem2b = JTS.to( Wkt.fromWkt( "GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))" ) );
Geometry elem1a = JTS.to( Wkt.fromWkt( "GEOMETRYCOLLECTION(POINT(2 3))" ) );
Geometry elem1b = JTS.to( Wkt.fromWkt( "GEOMETRYCOLLECTION(POINT(2 3))" ) );
assertTrue( JTSUtils.equalsExact3D( elem2a, elem2b ) );
assertTrue( JTSUtils.equalsExact3D( elem1a, elem1b ) );
}
}