Search in sources :

Example 86 with Coordinate

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);
}
Also used : JsonArray(com.google.gson.JsonArray) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate)

Example 87 with Coordinate

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;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) JsonElement(com.google.gson.JsonElement) LinkedList(java.util.LinkedList)

Example 88 with Coordinate

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;
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) SimpleResultSet(org.h2.tools.SimpleResultSet) Coordinate(org.locationtech.jts.geom.Coordinate)

Example 89 with Coordinate

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()));
}
Also used : ValueGeometry(org.h2.value.ValueGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate)

Example 90 with Coordinate

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");
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) Polygon(org.locationtech.jts.geom.Polygon)

Aggregations

Coordinate (org.locationtech.jts.geom.Coordinate)348 Test (org.junit.Test)145 Geometry (org.locationtech.jts.geom.Geometry)69 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)65 LineString (org.locationtech.jts.geom.LineString)57 Point (org.locationtech.jts.geom.Point)57 Polygon (org.locationtech.jts.geom.Polygon)47 LinearRing (org.locationtech.jts.geom.LinearRing)45 AbstractArcTest (eu.esdihumboldt.util.geometry.interpolation.AbstractArcTest)37 ArcByCenterPoint (eu.esdihumboldt.util.geometry.interpolation.model.ArcByCenterPoint)32 ArrayList (java.util.ArrayList)32 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)27 Test (org.junit.jupiter.api.Test)26 Arc (eu.esdihumboldt.util.geometry.interpolation.model.Arc)20 MultiPoint (org.locationtech.jts.geom.MultiPoint)20 ArcByPointsImpl (eu.esdihumboldt.util.geometry.interpolation.model.impl.ArcByPointsImpl)17 ArcByCenterPointImpl (eu.esdihumboldt.util.geometry.interpolation.model.impl.ArcByCenterPointImpl)16 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)15 ArcByPoints (eu.esdihumboldt.util.geometry.interpolation.model.ArcByPoints)14 HashMap (java.util.HashMap)13