Search in sources :

Example 1 with Polygon

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

the class PgGeometryMappingTest method testPolygonMapping.

public void testPolygonMapping() throws SQLException {
    if (!runTestsForDatastore()) {
        return;
    }
    Polygon polygon = new Polygon("SRID=4326;POLYGON((25 25,75 25,75 75,25 75,25 25),(45 45,55 45,55 55,45 55,45 45))");
    SamplePolygon samplePolygon;
    SamplePolygon samplePolygon_read;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Object id = null;
    try {
        tx.begin();
        samplePolygon = new SamplePolygon(3001, "Polygon 1", polygon);
        pm.makePersistent(samplePolygon);
        id = JDOHelper.getObjectId(samplePolygon);
        samplePolygon = (SamplePolygon) pm.detachCopy(samplePolygon);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        samplePolygon_read = (SamplePolygon) pm.getObjectById(id, true);
        assertEquals(samplePolygon, samplePolygon_read);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) MultiPolygon(org.postgis.MultiPolygon) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) Polygon(org.postgis.Polygon) SampleMultiPolygon(org.datanucleus.samples.pggeometry.SampleMultiPolygon)

Example 2 with Polygon

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

the class PgGeometryMySQLTest method testMbrIntersects.

public void testMbrIntersects() throws SQLException {
    if (!runTestsForDatastore()) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon polygon = new Polygon("SRID=4326;POLYGON((10 10,40 10,40 40,10 40,10 10))");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && MySQL.mbrIntersects(geom, :polygon)");
        List list = (List) query.execute(polygon);
        assertEquals("Wrong number of geometries which intersect with a given polygon returned", 1, list.size());
        assertTrue("Polygon 1 should be in the list of geometries which intersect with a given polygon", list.contains(getSamplePolygon(1)));
    } finally {
        tx.commit();
    }
}
Also used : 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)

Example 3 with Polygon

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

the class PgGeometryMySQLTest method testMbrDisjoint.

public void testMbrDisjoint() throws SQLException {
    if (!runTestsForDatastore()) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon polygon = new Polygon("SRID=4326;POLYGON((10 10,40 10,40 40,10 40,10 10))");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && MySQL.mbrDisjoint(geom, :polygon)");
        List list = (List) query.execute(polygon);
        assertEquals("Wrong number of geometries which are disjoint from a given polygon returned", 1, list.size());
        assertTrue("Polygon 2 should be in the list of geometries which are disjoint from a given polygon", list.contains(getSamplePolygon(2)));
    } finally {
        tx.commit();
    }
}
Also used : 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)

Example 4 with Polygon

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

the class PgGeometrySpatialTest method testDifference.

@Datastore(POSTGRESQL)
public void testDifference() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon polygon = new Polygon("SRID=4326;POLYGON((75 75,100 75,100 80,80 80,75 75))");
        Polygon difference = new Polygon("SRID=4326;POLYGON((80 80,100 100,100 80,80 80))");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && Spatial.equals(Spatial.difference(geom, :polygon), :difference)");
        List list = (List) query.execute(polygon, difference);
        assertEquals("Wrong number of geometries whose difference from a given polygon is equal to a given polygon returned", 1, list.size());
        assertTrue("Polygon 2 should be in the list of geometries whose difference from a given polygon is equal to a given polygon", list.contains(getSamplePolygon(2)));
        query = pm.newQuery(SamplePolygon.class, "id == :id");
        query.setResult("Spatial.difference(geom, Spatial.geomFromText('POLYGON((75 75,100 75,100 80,80 80,75 75))', 4326))");
        query.setUnique(true);
        Geometry difference_read = (Geometry) query.execute(new Long(getSamplePolygon(2).getId()));
        assertEquals("Returned difference should be equal to the given polygon", difference, difference_read);
    } finally {
        tx.commit();
    }
}
Also used : Geometry(org.postgis.Geometry) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) 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 5 with Polygon

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

the class PgGeometrySpatialTest method testDisjoint.

public void testDisjoint() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon polygon = new Polygon("SRID=4326;POLYGON((10 10,40 10,40 40,10 40,10 10))");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && Spatial.disjoint(geom, :polygon)");
        List list = (List) query.execute(polygon);
        assertEquals("Wrong number of geometries which are disjoint from a given polygon returned", 1, list.size());
        assertTrue("Polygon 2 should be in the list of geometries which are disjoint from a given polygon", list.contains(getSamplePolygon(2)));
        query = pm.newQuery(SamplePolygon.class, "id == :id");
        query.setResult("Spatial.disjoint(geom, Spatial.geomFromText('POLYGON((10 10,40 10,40 40,10 40,10 10))', 4326))");
        query.setResultClass(Boolean.class);
        query.setUnique(true);
        Boolean equals = (Boolean) query.execute(new Long(getSamplePolygon(2).getId()));
        assertEquals("Polygon 2 should be disjoint from the given polygon", true, equals.booleanValue());
    } finally {
        tx.commit();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) List(java.util.List) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) Polygon(org.postgis.Polygon) MultiPolygon(org.postgis.MultiPolygon)

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