org.h2gis.spatialut.GeometryAsserts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-utilities Show documentation
Show all versions of test-utilities Show documentation
Common class for Spatial JUnit Test
The newest version!
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* .
*
* H2GIS is distributed under GPL 3 license. It is produced by CNRS
* .
*
* H2GIS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* H2GIS. If not, see .
*
* For more information, please consult:
* or contact directly: info_at_h2gis.org
*/
package org.h2gis.spatialut;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import org.h2.value.ValueGeometry;
import java.sql.SQLException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Assert with Geometry type
*
* @author Nicolas Fortin
*/
public class GeometryAsserts {
/**
* Default, Epsilon value for metric projections unit test
*/
private static final double EPSILON = .01;
/**
* Check Geometry type,X,Y,Z and SRID
*
* @param expectedWKT Expected value, in WKT
* @param valueWKB Test value, in WKB ex rs.getBytes()
* @throws SQLException If WKT or WKB is not valid
*/
public static void assertGeometryEquals(String expectedWKT, byte[] valueWKB) throws SQLException {
if (expectedWKT == null) {
assertNull(valueWKB);
} else {
assertGeometryEquals(expectedWKT, ValueGeometry.get(valueWKB).getObject());
}
}
/**
* Check Geometry type,X,Y,Z
*
* @param expectedWKT Expected value, in WKT
* @param valueObject Test value geometry ex rs.getObject(i)
* @throws SQLException If WKT or WKB is not valid
*/
public static void assertGeometryEquals(String expectedWKT, Object valueObject) throws SQLException {
assertGeometryEquals(expectedWKT, 0, valueObject);
}
/**
* Check Geometry type,X,Y,Z and SRID
*
* @param expectedWKT Expected value, in WKT
* @param expectedSRID Expected SRID code,
* @param valueObject Test value geometry ex rs.getObject(i)
* @throws SQLException If WKT or WKB is not valid
*/
public static void assertGeometryEquals(String expectedWKT,int expectedSRID, Object valueObject) throws SQLException {
if (expectedWKT == null) {
assertNull(valueObject);
} else {
ValueGeometry expected = ValueGeometry.get(expectedWKT, expectedSRID);
ValueGeometry actual = ValueGeometry.getFromGeometry(((Geometry)valueObject).norm());
expected = ValueGeometry.getFromGeometry(expected.getGeometry().norm());
String moreInfo = "";
if(!actual.equals(expected)) {
if(!GeometryCollection.class.getName().equals(expected.getGeometry().getClass().getName()) &&
!GeometryCollection.class.getName().equals(actual.getGeometry().getClass().getName()) &&
expected.getGeometry().equals(actual.getGeometry())) {
moreInfo = "\n But are topologically equals";
}
}
assertEquals("Expected:\n" + expected.getWKT() + "\nActual:\n" + actual.getWKT()+moreInfo, expected, actual);
}
}
/**
* Check only X,Y and geometry type
*
* @param expectedWKT Expected value, in WKT
* @param valueWKT Test value, in WKT ex rs.getString()
* @throws SQLException
*/
public static void assertGeometryEquals(String expectedWKT, String valueWKT) throws SQLException {
assertGeometryEquals(expectedWKT, ValueGeometry.get(valueWKT).getBytes());
}
/**
* Equals test with epsilon error acceptance.
*
* @param expectedWKT Expected value, in WKT
* @param resultSetObject Geometry, rs.getObject(i)
*/
public static void assertGeometryBarelyEquals(String expectedWKT, Object resultSetObject) {
assertGeometryBarelyEquals(expectedWKT, resultSetObject, EPSILON);
}
/**
* Equals test with epsilon error acceptance.
* @param expectedWKT Expected value, in WKT
* @param resultSetObject Geometry, rs.getObject(i)
* @param epsilon epsilon error acceptance
*/
public static void assertGeometryBarelyEquals(String expectedWKT, Object resultSetObject, double epsilon) {
assertGeometryBarelyEquals(expectedWKT, 0, resultSetObject, epsilon);
}
/**
* Equals test with epsilon error acceptance and SRID.
* @param expectedWKT Expected value, in WKT
* @param expectedSRID Expected SRID Value
* @param resultSetObject
* @param epsilon epsilon error acceptance
*/
public static void assertGeometryBarelyEquals(String expectedWKT,int expectedSRID, Object resultSetObject, double epsilon) {
assertTrue(resultSetObject instanceof Geometry);
Geometry expectedGeometry = ValueGeometry.get(expectedWKT, expectedSRID).getGeometry();
Geometry result = (Geometry) resultSetObject;
assertEquals(expectedGeometry.getGeometryType(), result.getGeometryType());
assertEquals(expectedGeometry.getNumPoints(), result.getNumPoints());
Coordinate[] expectedCoordinates = expectedGeometry.getCoordinates();
Coordinate[] resultCoordinates = result.getCoordinates();
for (int idPoint = 0; idPoint < expectedCoordinates.length; idPoint++) {
assertEquals(expectedCoordinates[idPoint].x, resultCoordinates[idPoint].x, epsilon);
assertEquals(expectedCoordinates[idPoint].y, resultCoordinates[idPoint].y, epsilon);
assertEquals(expectedCoordinates[idPoint].z, resultCoordinates[idPoint].z, epsilon);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy