Search in sources :

Example 6 with Geometry

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

the class PgGeometrySpatialTest method testStartPoint.

public void testStartPoint() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Point point = new Point("SRID=4326;POINT(50 0)");
        Query query = pm.newQuery(SampleLineString.class, "geom != null && Spatial.equals(Spatial.startPoint(geom), :point)");
        List list = (List) query.execute(point);
        assertEquals("Wrong number of geometries with a given start point returned", 1, list.size());
        assertTrue("LineString 2 should be in the list of geometries with a given start point", list.contains(getSampleLineString(2)));
        query = pm.newQuery(SampleLineString.class, "id == :id");
        query.setResult("Spatial.startPoint(geom)");
        query.setUnique(true);
        Geometry point_read = (Geometry) query.execute(new Long(getSampleLineString(2).getId()));
        assertEquals("Returned start point should be equal to the given point", point, point_read);
    } 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) MultiPoint(org.postgis.MultiPoint) SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Point(org.postgis.Point)

Example 7 with Geometry

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

the class PgGeometrySpatialTest method testExteriorRingMethod.

public void testExteriorRingMethod() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        LineString exteriorRing = new LineString("SRID=4326;LINESTRING(25 25,75 25,75 75,25 75,25 25)");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && Spatial.asText(Spatial.exteriorRing(geom)) == Spatial.asText(:exteriorRing)");
        List list = (List) query.execute(exteriorRing);
        assertEquals("Wrong number of geometries with a given exterior ring returned", 1, list.size());
        assertTrue("Polygon 1 should be in the list of geometries with a given exterior ring", list.contains(getSamplePolygon(1)));
        query = pm.newQuery(SamplePolygon.class, "id == :id");
        query.setResult("Spatial.exteriorRing(geom)");
        query.setUnique(true);
        Geometry exteriorRing_read = (Geometry) query.execute(new Long(getSamplePolygon(1).getId()));
        assertEquals("Exterior ring of Polygon 1 should be equal to the given linestring", exteriorRing, exteriorRing_read);
    } finally {
        tx.commit();
    }
}
Also used : Geometry(org.postgis.Geometry) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) SampleLineString(org.datanucleus.samples.pggeometry.SampleLineString) MultiLineString(org.postgis.MultiLineString) LineString(org.postgis.LineString) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) List(java.util.List)

Example 8 with Geometry

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

the class PgGeometrySpatialTest method testBuffer.

@Datastore(POSTGRESQL)
public void testBuffer() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Point point = new Point("SRID=4326;POINT(0 0)");
        Query query = pm.newQuery(SamplePoint.class, "geom != null && Spatial.within(geom, Spatial.buffer(:point, 20))");
        List list = (List) query.execute(point);
        assertEquals("Wrong number of geometries which are within the buffer of a given point returned", 1, list.size());
        assertTrue("Point 1 should be in the list of geometries which are within the buffer of a given point", list.contains(getSamplePoint(1)));
        query = pm.newQuery(SamplePoint.class, "id == :id");
        query.setResult("Spatial.buffer(geom, 20)");
        query.setUnique(true);
        Geometry buffer = (Geometry) query.execute(new Long(getSamplePoint(1).getId()));
        assertEquals("Returned buffer should be a polygon", "POLYGON", buffer.getTypeString());
        assertEquals("Returned buffer should have the given srid", 4326, buffer.getSrid());
        assertTrue("First point of returned buffer should have to be in a given distance from Point 1", buffer.getFirstPoint().distance(point) >= 20.0);
    } finally {
        tx.commit();
    }
}
Also used : SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Geometry(org.postgis.Geometry) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) MultiPoint(org.postgis.MultiPoint) SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Point(org.postgis.Point) Datastore(org.datanucleus.tests.annotations.Datastore)

Example 9 with Geometry

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

the class PgGeometrySpatialTest method testCentroid.

@Datastore(POSTGRESQL)
public void testCentroid() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Point centroid = new Point("SRID=4326;POINT(50 50)");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && Spatial.asText(Spatial.centroid(geom)) == Spatial.asText(:centroid)");
        List list = (List) query.execute(centroid);
        assertEquals("Wrong number of geometries with a given centroid returned", 1, list.size());
        assertTrue("Polygon 1 should be in the list of geometries with a given centroid", list.contains(getSamplePolygon(1)));
        query = pm.newQuery(SamplePolygon.class, "id == :id");
        query.setResult("Spatial.centroid(geom)");
        query.setUnique(true);
        Geometry centroid_read = (Geometry) query.execute(new Long(getSamplePolygon(1).getId()));
        assertEquals("Given point shoul be centroid of Polygon 1", centroid, centroid_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) MultiPoint(org.postgis.MultiPoint) SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Point(org.postgis.Point) Datastore(org.datanucleus.tests.annotations.Datastore)

Example 10 with Geometry

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

the class PgGeometrySpatialTest method testInteriorRingNMethod.

public void testInteriorRingNMethod() throws SQLException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Short n = new Short((short) 1);
        LineString interiorRing = new LineString("SRID=4326;LINESTRING(45 45,55 45,55 55,45 55,45 45)");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && Spatial.asText(Spatial.interiorRingN(geom, :n)) == Spatial.asText(:interiorRing)");
        List list = (List) query.execute(n, interiorRing);
        assertEquals("Wrong number of geometries whose " + n + "th interior ring is equal to a given linestring returned", 1, list.size());
        assertTrue("Polygon 1 should be in the list of geometries whose " + n + "th interior ring is equal to a given linestring", list.contains(getSamplePolygon(1)));
        query = pm.newQuery(SamplePolygon.class, "id == :id");
        query.setResult("Spatial.interiorRingN(geom, 1)");
        query.setUnique(true);
        Geometry interiorRing_read = (Geometry) query.execute(new Long(getSamplePolygon(1).getId()));
        assertEquals("First interior ring of Polygon 1 should be equal to the given linestring", interiorRing, interiorRing_read);
    } finally {
        tx.commit();
    }
}
Also used : Geometry(org.postgis.Geometry) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) SampleLineString(org.datanucleus.samples.pggeometry.SampleLineString) MultiLineString(org.postgis.MultiLineString) LineString(org.postgis.LineString) SamplePolygon(org.datanucleus.samples.pggeometry.SamplePolygon) List(java.util.List)

Aggregations

List (java.util.List)16 PersistenceManager (javax.jdo.PersistenceManager)16 Query (javax.jdo.Query)16 Transaction (javax.jdo.Transaction)16 Geometry (org.postgis.Geometry)16 SampleLineString (org.datanucleus.samples.pggeometry.SampleLineString)10 SamplePolygon (org.datanucleus.samples.pggeometry.SamplePolygon)9 Datastore (org.datanucleus.tests.annotations.Datastore)9 SamplePoint (org.datanucleus.samples.pggeometry.SamplePoint)7 MultiPoint (org.postgis.MultiPoint)7 Point (org.postgis.Point)7 LineString (org.postgis.LineString)5 MultiLineString (org.postgis.MultiLineString)5 MultiPolygon (org.postgis.MultiPolygon)4 Polygon (org.postgis.Polygon)4 SampleGeometryCollection (org.datanucleus.samples.pggeometry.SampleGeometryCollection)1