|
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.
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.
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();
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));
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();
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 = ???
|