Search in sources :

Example 6 with Polygon

use of org.postgis.Polygon in project tests by datanucleus.

the class PgGeometrySpatialTest method testConvexHull.

@Datastore(POSTGRESQL)
public void testConvexHull() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon convexHull = new Polygon("SRID=4326;POLYGON((110 10,120 25,110 45,100 25,110 10))");
        Query query = pm.newQuery(SampleLineString.class, "geom != null && Spatial.equals(Spatial.convexHull(geom), :convexHull)");
        List list = (List) query.execute(convexHull);
        assertEquals("Wrong number of geometries with a given convex hull returned", 1, list.size());
        assertTrue("LineSting 3 should be in the list of geometries with a given convex hull", list.contains(getSampleLineString(3)));
        query = pm.newQuery(SampleLineString.class, "id == :id");
        query.setResult("Spatial.convexHull(geom)");
        query.setUnique(true);
        Geometry convexHull_read = (Geometry) query.execute(new Long(getSampleLineString(3).getId()));
        assertEquals("Returned convex hull should be a polygon", "POLYGON", convexHull.getTypeString());
        assertEquals("Returned convex hull should have the given srid", 4326, convexHull.getSrid());
        assertTrue("First point of returned convex hull should be equal to given point", convexHull.getFirstPoint().equals(convexHull_read.getFirstPoint()));
    } finally {
        tx.commit();
    }
}
Also used : Geometry(org.postgis.Geometry) SampleLineString(org.datanucleus.samples.pggeometry.SampleLineString) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) Polygon(org.postgis.Polygon) MultiPolygon(org.postgis.MultiPolygon) Datastore(org.datanucleus.tests.annotations.Datastore)

Example 7 with Polygon

use of org.postgis.Polygon in project tests by datanucleus.

the class PgGeometrySpatialTest method testEnvelope.

public void testEnvelope() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon envelope = new Polygon("SRID=4326;POLYGON((100 10,120 10,120 45,100 45,100 10))");
        Query query = pm.newQuery(SampleLineString.class, "geom != null && Spatial.equals(Spatial.envelope(geom), :envelope)");
        List list = (List) query.execute(envelope);
        assertEquals("Wrong number of geometries with a given envelope returned", 1, list.size());
        assertTrue("LineString 3 should be in the list of geometries with a given envelope", list.contains(getSampleLineString(3)));
        query = pm.newQuery(SampleLineString.class, "id == :id");
        query.setResult("Spatial.envelope(geom)");
        query.setUnique(true);
        Geometry envelope_read = (Geometry) query.execute(new Long(getSampleLineString(3).getId()));
        assertEquals("Returned envelope should be equal to a given envelope (geometry type)", envelope.getType(), envelope_read.getType());
        assertEquals("Returned envelope should be equal to a given envelope (srid)", envelope.getSrid(), envelope_read.getSrid());
        assertEquals("Returned envelope should be equal to a given envelope (no. points)", envelope.numPoints(), envelope_read.numPoints());
        assertTrue("Returned envelope should be equal to a given envelope (minx, miny)", envelope.getPoint(0).equals(envelope_read.getPoint(0)));
        assertTrue("Returned envelope should be equal to a given envelope (maxx, maxy)", envelope.getPoint(2).equals(envelope_read.getPoint(2)));
    } finally {
        tx.commit();
    }
}
Also used : Geometry(org.postgis.Geometry) SampleLineString(org.datanucleus.samples.pggeometry.SampleLineString) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) Polygon(org.postgis.Polygon) MultiPolygon(org.postgis.MultiPolygon)

Example 8 with Polygon

use of org.postgis.Polygon in project sqlg by pietermartin.

the class TestGisBulkWithin method testBulkWithinPolygon.

@Test
public void testBulkWithinPolygon() throws SQLException {
    LinearRing linearRing1 = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
    Polygon polygon1 = new Polygon(new LinearRing[] { linearRing1 });
    LinearRing linearRing2 = new LinearRing("1 1, 1 1, 1 2, 1 1, 1 1");
    Polygon polygon2 = new Polygon(new LinearRing[] { linearRing2 });
    LinearRing linearRing3 = new LinearRing("2 2, 1 1, 1 2, 1 1, 2 2");
    Polygon polygon3 = new Polygon(new LinearRing[] { linearRing3 });
    LinearRing linearRing4 = new LinearRing("1 3, 1 2, 2 2, 1 1, 1 3");
    Polygon polygon4 = new Polygon(new LinearRing[] { linearRing4 });
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon1);
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon2);
    Vertex v3 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon3);
    Vertex v4 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon4);
    this.sqlgGraph.tx().commit();
    List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("Gis").has("polygon", P.within(polygon1, polygon3, polygon4)).toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(Arrays.asList(v1, v3, v4).containsAll(vertices));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LinearRing(org.postgis.LinearRing) GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) Polygon(org.postgis.Polygon) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 9 with Polygon

use of org.postgis.Polygon in project voltdb by VoltDB.

the class VoltDBOsmSink method process.

public void process(WayContainer wayContainer) {
    Way way;
    List<Long> nodeIds;
    way = wayContainer.getEntity();
    nodeIds = new ArrayList<Long>(way.getWayNodes().size());
    for (WayNode wayNode : way.getWayNodes()) {
        nodeIds.add(wayNode.getNodeId());
    }
    // Keep invalid ways out of the database if desired by the user
    if (way.getWayNodes().size() > 1 || keepInvalidWays) {
        for (Tag tag : way.getTags()) {
            try {
                client.callProcedure(new InsertCallback(), INS_WAY_TAGS_PROC, way.getId(), tag.getKey(), tag.getValue());
            } catch (NoConnectionsException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Add these to the ways_nodes_table;
        int sequence = 0;
        for (Long nodeId : nodeIds) {
            try {
                client.callProcedure(new InsertCallback(), INS_WAYS_NODES_PROC, way.getId(), nodeId, sequence);
            } catch (NoConnectionsException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sequence++;
        }
        StringBuffer sb = new StringBuffer();
        // if the first node id == the last nodeId, we know that this is a
        // closed loop.
        long n0 = nodeIds.get(0);
        long nn = nodeIds.get(nodeIds.size() - 1);
        if (n0 == nn) {
            if (enableBboxBuilder) {
                Polygon pg = wayGeometryBuilder.createPolygon(way);
                pg.outerWKT(sb);
            }
        } else {
            // it's a lineString, but we don't support it yet.
            if (enableLinestringBuilder) {
                LineString lineString = wayGeometryBuilder.createWayLinestring(way);
                lineString.outerWKT(sb);
            } else {
                return;
            }
        }
        String bbox = sb.toString();
        try {
            client.callProcedure(new InsertCallback(), INS_WAYS_PROC, way.getId(), way.getVersion(), way.getUser().getId(), way.getTimestamp(), way.getChangesetId(), bbox);
        } catch (NoConnectionsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) IOException(java.io.IOException) LineString(org.postgis.LineString) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) NoConnectionsException(org.voltdb.client.NoConnectionsException) LineString(org.postgis.LineString) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag) Polygon(org.postgis.Polygon)

Example 10 with Polygon

use of org.postgis.Polygon in project voltdb by VoltDB.

the class WayPolygonGeometryBuilder method createPolygon.

/**
     * OSM stores each ring of a polygon independently, the rings of a polygon
     * need to be combined through relationships. This method only returns a
     * single ring of a Polygon,
     *
     * @param way
     * @return
     */
public Polygon createPolygon(Way way) {
    LinearRing[] rings = new LinearRing[1];
    rings[0] = createRing(way);
    Polygon pg = new Polygon(rings);
    return pg;
}
Also used : LinearRing(org.postgis.LinearRing) Polygon(org.postgis.Polygon)

Aggregations

Polygon (org.postgis.Polygon)19 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 SamplePolygon (org.datanucleus.samples.pggeometry.SamplePolygon)14 List (java.util.List)13 Query (javax.jdo.Query)13 MultiPolygon (org.postgis.MultiPolygon)10 Geometry (org.postgis.Geometry)4 LinearRing (org.postgis.LinearRing)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 Datastore (org.datanucleus.tests.annotations.Datastore)3 Test (org.junit.Test)3 GeographyPolygon (org.umlg.sqlg.gis.GeographyPolygon)3 BaseTest (org.umlg.sqlg.test.BaseTest)3 SampleLineString (org.datanucleus.samples.pggeometry.SampleLineString)2 LineString (org.postgis.LineString)2 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 SampleMultiPolygon (org.datanucleus.samples.pggeometry.SampleMultiPolygon)1