Search in sources :

Example 1 with SamplePoint

use of org.datanucleus.samples.pggeometry.SamplePoint in project tests by datanucleus.

the class PgGeometryMappingTest method testPointMapping.

public void testPointMapping() throws SQLException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = new Point("SRID=4326;POINT(10 10)");
    SamplePoint samplePoint;
    SamplePoint samplePoint_read;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Object id = null;
    try {
        tx.begin();
        samplePoint = new SamplePoint(1001, "Point 1", point);
        pm.makePersistent(samplePoint);
        id = JDOHelper.getObjectId(samplePoint);
        samplePoint = (SamplePoint) pm.detachCopy(samplePoint);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        samplePoint_read = (SamplePoint) pm.getObjectById(id, true);
        assertEquals(samplePoint, samplePoint_read);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) MultiPoint(org.postgis.MultiPoint) SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Point(org.postgis.Point) SampleMultiPoint(org.datanucleus.samples.pggeometry.SampleMultiPoint)

Example 2 with SamplePoint

use of org.datanucleus.samples.pggeometry.SamplePoint in project tests by datanucleus.

the class PgGeometrySpatialTest method testAsText.

public void testAsText() throws SQLException {
    SamplePoint point1 = getSamplePoint(1);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        String wkt = "POINT(10 10)";
        Query query = pm.newQuery(SamplePoint.class, "geom != null && Spatial.asText(geom) == :wkt");
        List list = (List) query.execute(wkt);
        assertEquals("Wrong number of geometries with a given wkt returned", 1, list.size());
        assertTrue("Point 1 should be in the list of geometries with a given wkt", list.contains(point1));
        query = pm.newQuery(SamplePoint.class, "id == :id");
        query.setResultClass(String.class);
        query.setResult("Spatial.asText(geom)");
        list = (List) query.execute(new Long(point1.getId()));
        assertEquals("Wrong number of geometries with a given id returned", 1, list.size());
        String wkt_read = (String) list.get(0);
        assertEquals("WKT of Point 1 should be equal to a given point's wkt", wkt, wkt_read);
    } finally {
        tx.commit();
    }
}
Also used : SamplePoint(org.datanucleus.samples.pggeometry.SamplePoint) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) SampleLineString(org.datanucleus.samples.pggeometry.SampleLineString) MultiLineString(org.postgis.MultiLineString) LineString(org.postgis.LineString)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 SamplePoint (org.datanucleus.samples.pggeometry.SamplePoint)2 List (java.util.List)1 Query (javax.jdo.Query)1 SampleLineString (org.datanucleus.samples.pggeometry.SampleLineString)1 SampleMultiPoint (org.datanucleus.samples.pggeometry.SampleMultiPoint)1 LineString (org.postgis.LineString)1 MultiLineString (org.postgis.MultiLineString)1 MultiPoint (org.postgis.MultiPoint)1 Point (org.postgis.Point)1