JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer
Persistence
Object Identity
Spatial
Introduction

JPOX supports the storage and queries of Spatial types. Spatial types are used to store geographic information like river, montains, cities, and more it can store any kind of spatial information, like circles, lines or polygons.

JPOX allows using Spatial and traditional types simultaneously in persistent objects making JPOX a single interface to manipulate any data of your business.

Spatial Types

Several databases are supported by JPOX and each one has it's own proprietary Spatial type. As like traditional types, Spatial types also have Java Classes that maps to the Spatial types. For example, traditional types like CHAR, VARCHAR have in Java the corresponding java.lang.String class.

The supported types are listed in the Types documentation.

Querying Spatial types

JPOX have functions that can be applied upon spatial types in JDOQL queries. These functions allows comparision between spatial types

Java TypeMethodDescriptionSpecificationJPOX support
Spatialcontains(Geometry,Geometry)Determine if the interior of one geometry intersects the interior of the other geometry. Returns true if the second geometry is completely contained by the first geometry.
Spatialinside(Geometry,Geometry)Determine if the interior of one geometry intersects the interior of the other geometry. Returns true if the first geometry is completely within the second geometry.
Spatialoverlaps(Geometry,Geometry)Compares two geometries of the same dimension. It returns true if their intersection set results in a geometry different from both, but that has the same dimension.
Spatialtouches(Geometry,Geometry)Returns true if all the points common to both geometries can be found only on the boundaries.
Spatialequals(Geometry,Geometry)Returns true if the two geometries are identical.

The following sections provide some examples of what can be done using JDOQL Spatial methods.

Examples
Example 1 - Persistence of Oracle JGeometry

This exemplifies the persistence of a Oracle JGeometry.

public class Geometry
{
    int year;
    oracle.spatial.geometry.JGeometry geometry;
    ...
}
<jdo>
    <package name="org.jpox.samples.jgeometry">
        <class name="Geometry" table="TBLGEOMETRY">
            <field name="year"/>
            <field name="geometry" persistence-modifier="persistent" serialized="false">
            </field>
        <class>
    </package>
</jdo>
tx.begin();
p = new Geometry();
p.setYear(2001);
p.setGeometry(JGeometry.createPoint(new double[]{1,10},1,SRID));
pm.makePersistent(p);
id = JDOHelper.getObjectId(p);
tx.commit();
Example 2 - Queries

In this example, we check if one geometry equals another geometry given as parameter

Query q = pm.newQuery(Geometry.class,"Spatial.equals(geometry,g)");
q.declareParameters("oracle.spatial.geometry.JGeometry g");
Collection c = (Collection)q.execute(JGeometry.createPoint(new double[]{1,10},1,8512));


Example 3 - Queries

In this example, we receive a geometry parameter and verify if it's contained by another geometry.

tx.begin();
Query q = pm.newQuery(Sample.class,"Spatial.contains(shape,g)");
Collection c = (Collection)q.execute(JGeometry.createPoint(new double[]{10,20},1,8512));
tx.commit();

The below example also uses the Spatial.contains method, and illustrates the use of database specific functions. In this case we are using Oracle functions: SDO_GEOMETRY, SDO_ELEM_INFO_ARRAY and SDO_ORDINATE_ARRAY. This functions are performed by the database process and are used to create geometries. Attention to the fact that in another database the names and arguments of these functions may vary. It's also important to note that the function Spatial.contains will always takes two arguments whatever database it is running in the backend.

tx.begin();
Query q = pm.newQuery(Sample.class,
                      "Spatial.contains(shape,Spatial.geometry('SDO_GEOMETRY',2003,null,null,"+
                      "Spatial.geometry('SDO_ELEM_INFO_ARRAY',1,1003,3),Spatial.geometry('SDO_ORDINATE_ARRAY',2,2,4,6)))");
Collection c = (Collection)q.execute();
tx.commit();


Spatial databases
Oracle 10

The usage of spatial types with JPOX and Oracle is not totally transparent. In order of using Spatial types in queries, Oracle requires the creation of spatial indexes for each SD_GEOMETRY column. This will require you to follow these steps:

  • Create the table with the spatial column. You can let JPOX do this for you.
  • Manually, add an entry to the user_sdo_geom_metadata view in Oracle. This entry is the combination of table name, column name and dimension array.
  • Manually, create an spatial index for the SD_GEOMETRY column.

Below is a complete sample script.

CREATE TABLE cola_markets (
mkt_id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape SDO_GEOMETRY);

---------------------------------------------------------------------------
-- Update the USER_SDO_GEOM_METADATA view. This is required
-- before the Spatial index can be created. Do this only once for each
-- layer (that is, table-column combination; here: COLA_MARKETS and SHAPE).
INSERT INTO user_sdo_geom_metadata
(TABLE_NAME,
COLUMN_NAME,
DIMINFO,
SRID)
VALUES (
'cola_markets',
'shape',
SDO_DIM_ARRAY( -- 20X20 grid
SDO_DIM_ELEMENT('X', 0, 20, 0.005),
SDO_DIM_ELEMENT('Y', 0, 20, 0.005)
),
NULL -- SRID
);
-------------------------------------------------------------------
-- CREATE THE SPATIAL INDEX --
-------------------------------------------------------------------
CREATE INDEX cola_spatial_idx
ON cola_markets(shape)
INDEXTYPE IS MDSYS.SPATIAL_INDEX;
-- Preceding statement created an R-tree index.

If you for some reason you want to drop the tables, be sure to delete the entries in user_sdo_geom_metadata before issuing the drop table statement.

delete from USER_SDO_GEOM_METADATA where table_name = ???