use of org.locationtech.jts.geom.Coordinate in project thingsboard by thingsboard.
the class GeoUtil method contains.
public static synchronized boolean contains(@NonNull String polygonInString, @NonNull Coordinates coordinates) {
if (polygonInString.isEmpty() || polygonInString.isBlank()) {
throw new RuntimeException("Polygon string can't be empty or null!");
}
JsonArray polygonsJson = normalizePolygonsJson(JSON_PARSER.parse(polygonInString).getAsJsonArray());
List<Geometry> polygons = buildPolygonsFromJson(polygonsJson);
Set<Geometry> holes = extractHolesFrom(polygons);
polygons.removeIf(holes::contains);
Geometry globalGeometry = unionToGlobalGeometry(polygons, holes);
var point = jtsCtx.getShapeFactory().getGeometryFactory().createPoint(new Coordinate(coordinates.getLatitude(), coordinates.getLongitude()));
return globalGeometry.contains(point);
}
use of org.locationtech.jts.geom.Coordinate in project thingsboard by thingsboard.
the class GeoUtil method parseCoordinates.
private static List<Coordinate> parseCoordinates(JsonArray coordinatesJson) {
List<Coordinate> result = new LinkedList<>();
for (JsonElement coords : coordinatesJson) {
double x = coords.getAsJsonArray().get(0).getAsDouble();
double y = coords.getAsJsonArray().get(1).getAsDouble();
result.add(new Coordinate(x, y));
}
if (result.size() >= 3) {
result.add(result.get(0));
}
return result;
}
use of org.locationtech.jts.geom.Coordinate in project h2database by h2database.
the class TestSpatial method pointTable.
/**
* This method is called via reflection from the database.
*
* @param x the x position of the point
* @param y the y position of the point
* @return a result set with this point
*/
public static ResultSet pointTable(double x, double y) {
GeometryFactory factory = new GeometryFactory();
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("THE_GEOM", Types.JAVA_OBJECT, "GEOMETRY", 0, 0);
rs.addRow(factory.createPoint(new Coordinate(x, y)));
return rs;
}
use of org.locationtech.jts.geom.Coordinate in project h2database by h2database.
the class TestSpatial method testGeometryDataType.
private void testGeometryDataType() {
GeometryFactory geometryFactory = new GeometryFactory();
Geometry geometry = geometryFactory.createPoint(new Coordinate(0, 0));
assertEquals(Value.GEOMETRY, DataType.getTypeFromClass(geometry.getClass()));
}
use of org.locationtech.jts.geom.Coordinate in project h2database by h2database.
the class TestSpatial method testSpatialValues.
private void testSpatialValues() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("create memory table test" + "(id int primary key, polygon geometry)");
stat.execute("insert into test values(1, " + "'POLYGON ((1 1, 1 2, 2 2, 1 1))')");
ResultSet rs = stat.executeQuery("select * from test");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals("POLYGON ((1 1, 1 2, 2 2, 1 1))", rs.getString(2));
GeometryFactory f = new GeometryFactory();
Polygon polygon = f.createPolygon(new Coordinate[] { new Coordinate(1, 1), new Coordinate(1, 2), new Coordinate(2, 2), new Coordinate(1, 1) });
assertTrue(polygon.equals(rs.getObject(2)));
rs = stat.executeQuery("select * from test where polygon = " + "'POLYGON ((1 1, 1 2, 2 2, 1 1))'");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
stat.executeQuery("select * from test where polygon > " + "'POLYGON ((1 1, 1 2, 2 2, 1 1))'");
stat.executeQuery("select * from test where polygon < " + "'POLYGON ((1 1, 1 2, 2 2, 1 1))'");
stat.execute("drop table test");
conn.close();
deleteDb("spatial");
}
Aggregations