Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 22 → Rev 40

/trunk/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/DialectProvider.java
New file
0,0 → 1,77
/**
* $Id$
*
* This file is part of Spatial Hibernate, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 K.U. Leuven LRD, Spatial Applications Division, Belgium
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.cadrie.com/
*/
package org.hibernatespatial.postgis;
 
import java.util.Map;
 
import org.hibernatespatial.SpatialDialect;
import org.hibernatespatial.spi.SpatialDialectProvider;
 
 
/**
* PostGIS DialectProvider
*
* @author Karel Maesen
*/
public class DialectProvider implements SpatialDialectProvider {
 
/*
* (non-Javadoc)
*
* @see org.hibernatespatial.spi.SpatialDialectProvider#createSpatialDialect(java.lang.String,
* java.util.Map)
*/
public SpatialDialect createSpatialDialect(String dialect, Map map) {
if (dialect.equals(PostgisDialect.class.getCanonicalName())
|| dialect.equals("org.hibernate.dialect.PostgreSQLDialect")
|| dialect.equals("postgis"))
return new PostgisDialect();
else
return null;
}
 
/*
* (non-Javadoc)
*
* @see org.hibernatespatial.spi.SpatialDialectProvider#getDefaultDialect()
*/
public SpatialDialect getDefaultDialect() {
return new PostgisDialect();
}
 
/*
* (non-Javadoc)
*
* @see org.hibernatespatial.spi.SpatialDialectProvider#getSupportedDialects()
*/
public String[] getSupportedDialects() {
return new String[] { PostgisDialect.class.getCanonicalName() };
}
 
}
Property changes:
Added: svn:keywords
+ Id
/trunk/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/PGGeometryUserType.java
New file
0,0 → 1,328
/**
* $Id$
*
* This file is part of Spatial Hibernate, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 K.U. Leuven LRD, Spatial Applications Division, Belgium
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.cadrie.com/
*/
package org.hibernatespatial.postgis;
 
import java.sql.Connection;
import java.sql.Types;
 
import org.hibernatespatial.AbstractDBGeometryType;
import org.postgis.GeometryCollection;
import org.postgis.LineString;
import org.postgis.LinearRing;
import org.postgis.MultiLineString;
import org.postgis.MultiPoint;
import org.postgis.MultiPolygon;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.postgis.Polygon;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
 
/**
* Specific <code>GeometryType</code> for Postgis geometry type
*
* @author Karel Maesen
*/
public class PGGeometryUserType extends AbstractDBGeometryType {
 
private static final int[] geometryTypes = new int[] { Types.STRUCT };
 
private static final GeometryFactory geomFactory = new GeometryFactory();
 
public int[] sqlTypes() {
return geometryTypes;// PostgisDialect.getGeometrySQLType()
}
 
/**
* Converts the native geometry object to a JTS <code>Geometry</code>.
*
* @param geomObj
* native database geometry object (depends on the JDBC spatial
* extension of the database)
* @return JTS geometry corresponding to geomObj.
*/
public Geometry convert2JTS(Object object) {
if (object == null)
return null;
PGgeometry geom = (PGgeometry) object;
com.vividsolutions.jts.geom.Geometry out = null;
switch (geom.getGeoType()) {
case org.postgis.Geometry.POINT:
out = convertPoint((org.postgis.Point) geom.getGeometry());
break;
case org.postgis.Geometry.LINESTRING:
out = convertLineString((org.postgis.LineString) geom
.getGeometry());
break;
case org.postgis.Geometry.POLYGON:
out = convertPolygon((org.postgis.Polygon) geom.getGeometry());
break;
case org.postgis.Geometry.MULTILINESTRING:
out = convertMultiLineString((org.postgis.MultiLineString) geom
.getGeometry());
break;
case org.postgis.Geometry.MULTIPOINT:
out = convertMultiPoint((org.postgis.MultiPoint) geom
.getGeometry());
break;
case org.postgis.Geometry.MULTIPOLYGON:
out = convertMultiPolygon((org.postgis.MultiPolygon) geom
.getGeometry());
break;
case org.postgis.Geometry.GEOMETRYCOLLECTION:
out = convertGeometryCollection((org.postgis.GeometryCollection) geom
.getGeometry());
}
 
return out;
}
 
private Geometry convertGeometryCollection(GeometryCollection collection) {
org.postgis.Geometry[] geometries = collection.getGeometries();
com.vividsolutions.jts.geom.Geometry[] jtsGeometries = new com.vividsolutions.jts.geom.GeometryCollection[geometries.length];
for (int i = 0; i < geometries.length; i++) {
jtsGeometries[i] = convert2JTS(geometries[i]);
}
com.vividsolutions.jts.geom.GeometryCollection jtsGCollection = geomFactory
.createGeometryCollection(jtsGeometries);
return jtsGCollection;
}
 
private Geometry convertMultiPolygon(MultiPolygon pgMultiPolygon) {
com.vividsolutions.jts.geom.Polygon[] polygons = new com.vividsolutions.jts.geom.Polygon[pgMultiPolygon
.numPolygons()];
 
for (int i = 0; i < polygons.length; i++) {
Polygon pgPolygon = pgMultiPolygon.getPolygon(i);
polygons[i] = (com.vividsolutions.jts.geom.Polygon) convertPolygon(pgPolygon);
}
 
com.vividsolutions.jts.geom.MultiPolygon out = geomFactory
.createMultiPolygon(polygons);
out.setSRID(pgMultiPolygon.srid);
return out;
}
 
private Geometry convertMultiPoint(MultiPoint pgMultiPoint) {
com.vividsolutions.jts.geom.Point[] points = new com.vividsolutions.jts.geom.Point[pgMultiPoint
.numPoints()];
 
for (int i = 0; i < points.length; i++) {
points[i] = convertPoint(pgMultiPoint.getPoint(i));
}
com.vividsolutions.jts.geom.MultiPoint out = geomFactory
.createMultiPoint(points);
out.setSRID(pgMultiPoint.srid);
return out;
}
 
private com.vividsolutions.jts.geom.Geometry convertMultiLineString(
MultiLineString mlstr) {
 
com.vividsolutions.jts.geom.LineString[] lstrs = new com.vividsolutions.jts.geom.LineString[mlstr
.numLines()];
 
for (int i = 0; i < mlstr.numLines(); i++) {
lstrs[i] = geomFactory.createLineString(toJTSCoordinates(mlstr
.getLine(i).getPoints()));
}
com.vividsolutions.jts.geom.MultiLineString out = geomFactory
.createMultiLineString(lstrs);
out.setSRID(mlstr.srid);
return out;
}
 
protected com.vividsolutions.jts.geom.Geometry convertPolygon(
Polygon polygon) {
com.vividsolutions.jts.geom.LinearRing shell = geomFactory
.createLinearRing(toJTSCoordinates(polygon.getRing(0)
.getPoints()));
com.vividsolutions.jts.geom.Polygon out = null;
if (polygon.numRings() > 1) {
com.vividsolutions.jts.geom.LinearRing[] rings = new com.vividsolutions.jts.geom.LinearRing[polygon
.numRings() - 1];
for (int r = 1; r < polygon.numRings(); r++) {
rings[r - 1] = geomFactory
.createLinearRing(toJTSCoordinates(polygon.getRing(r)
.getPoints()));
}
out = geomFactory.createPolygon(shell, rings);
} else {
out = geomFactory.createPolygon(shell, null);
}
out.setSRID(polygon.srid);
return out;
}
 
protected com.vividsolutions.jts.geom.Point convertPoint(Point pnt) {
com.vividsolutions.jts.geom.Point g = geomFactory
.createPoint(new Coordinate(pnt.x, pnt.y));
g.setSRID(pnt.getSrid());
return g;
}
 
protected com.vividsolutions.jts.geom.LineString convertLineString(
org.postgis.LineString lstr) {
com.vividsolutions.jts.geom.LineString out = geomFactory
.createLineString(toJTSCoordinates(lstr.getPoints()));
out.setSRID(lstr.getSrid());
return out;
}
 
private com.vividsolutions.jts.geom.Coordinate[] toJTSCoordinates(
Point[] points) {
Coordinate[] coordinates = new Coordinate[points.length];
for (int i = 0; i < points.length; i++) {
coordinates[i] = new Coordinate(points[i].x, points[i].y);
}
return coordinates;
}
 
private Point[] toPoints(Coordinate[] coordinates) {
Point[] points = new Point[coordinates.length];
for (int i = 0; i < coordinates.length; i++) {
points[i] = new Point(coordinates[i].x, coordinates[i].y);
}
return points;
}
 
/**
* Converts a JTS <code>Geometry</code> to a native geometry object.
*
* @param jtsGeom
* JTS Geometry to convert
* @param connection
* the current database connection
* @return native database geometry object corresponding to jtsGeom.
*/
public Object conv2DBGeometry(Geometry jtsGeom, Connection connection) {
org.postgis.Geometry geom = null;
if (jtsGeom.getClass() == com.vividsolutions.jts.geom.Point.class) {
geom = convertJTSPoint((com.vividsolutions.jts.geom.Point) jtsGeom);
} else if (jtsGeom.getClass() == com.vividsolutions.jts.geom.LineString.class) {
geom = convertJTSLineString((com.vividsolutions.jts.geom.LineString) jtsGeom);
} else if (jtsGeom.getClass() == com.vividsolutions.jts.geom.MultiLineString.class) {
geom = convertJTSMultiLineSTring((com.vividsolutions.jts.geom.MultiLineString) jtsGeom);
} else if (jtsGeom.getClass() == com.vividsolutions.jts.geom.Polygon.class) {
geom = convertJTSPolygon((com.vividsolutions.jts.geom.Polygon) jtsGeom);
} else if (jtsGeom.getClass() == com.vividsolutions.jts.geom.MultiPoint.class) {
geom = convertJTSMultiPoint((com.vividsolutions.jts.geom.MultiPoint) jtsGeom);
} else if (jtsGeom.getClass() == com.vividsolutions.jts.geom.MultiPolygon.class) {
geom = convertJTSMultiPolygon((com.vividsolutions.jts.geom.MultiPolygon) jtsGeom);
}
 
if (geom != null)
return new PGgeometry(geom);
else
throw new UnsupportedOperationException("Conversion of "
+ jtsGeom.getClass().getSimpleName()
+ " to PGgeometry not supported");
}
 
private MultiPolygon convertJTSMultiPolygon(
com.vividsolutions.jts.geom.MultiPolygon multiPolygon) {
Polygon[] pgPolygons = new Polygon[multiPolygon.getNumGeometries()];
for (int i = 0; i < pgPolygons.length; i++) {
pgPolygons[i] = convertJTSPolygon((com.vividsolutions.jts.geom.Polygon) multiPolygon
.getGeometryN(i));
}
MultiPolygon mpg = new MultiPolygon(pgPolygons);
mpg.setSrid(multiPolygon.getSRID());
return mpg;
}
 
private MultiPoint convertJTSMultiPoint(
com.vividsolutions.jts.geom.MultiPoint multiPoint) {
Point[] pgPoints = new Point[multiPoint.getNumGeometries()];
for (int i = 0; i < pgPoints.length; i++) {
pgPoints[i] = convertJTSPoint((com.vividsolutions.jts.geom.Point) multiPoint
.getGeometryN(i));
}
MultiPoint mp = new MultiPoint(pgPoints);
mp.setSrid(multiPoint.getSRID());
return mp;
}
 
private Polygon convertJTSPolygon(
com.vividsolutions.jts.geom.Polygon jtsPolygon) {
int numRings = jtsPolygon.getNumInteriorRing();
org.postgis.LinearRing[] rings = new org.postgis.LinearRing[numRings + 1];
rings[0] = convertJTSLineStringToLinearRing(jtsPolygon
.getExteriorRing());
for (int i = 0; i < numRings; i++) {
rings[i + 1] = convertJTSLineStringToLinearRing(jtsPolygon
.getInteriorRingN(i));
}
Polygon polygon = new org.postgis.Polygon(rings);
polygon.setSrid(jtsPolygon.getSRID());
return polygon;
}
 
private LinearRing convertJTSLineStringToLinearRing(
com.vividsolutions.jts.geom.LineString lineString) {
LinearRing lr = new org.postgis.LinearRing(toPoints(lineString
.getCoordinates()));
lr.setSrid(lineString.getSRID());
return lr;
}
 
private LineString convertJTSLineString(
com.vividsolutions.jts.geom.LineString string) {
LineString ls = new org.postgis.LineString(toPoints(string
.getCoordinates()));
ls.setSrid(string.getSRID());
return ls;
}
 
private MultiLineString convertJTSMultiLineSTring(
com.vividsolutions.jts.geom.MultiLineString string) {
org.postgis.LineString[] lines = new org.postgis.LineString[string
.getNumGeometries()];
for (int i = 0; i < string.getNumGeometries(); i++) {
lines[i] = new org.postgis.LineString(toPoints(string
.getGeometryN(i).getCoordinates()));
}
MultiLineString mls = new MultiLineString(lines);
mls.setSrid(string.getSRID());
return mls;
}
 
private Point convertJTSPoint(com.vividsolutions.jts.geom.Point point) {
org.postgis.Point pgPoint = new org.postgis.Point();
pgPoint.srid = point.getSRID();
pgPoint.x = point.getX();
pgPoint.y = point.getY();
pgPoint.haveMeasure = false;
pgPoint.dimension = 2;
return pgPoint;
}
 
}
Property changes:
Added: svn:keywords
+ Id
/trunk/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/PostgisDialect.java
New file
0,0 → 1,181
/**
* $Id$
*
* This file is part of Spatial Hibernate, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 K.U. Leuven LRD, Spatial Applications Division, Belgium
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.cadrie.com/
*/
package org.hibernatespatial.postgis;
 
import org.hibernate.Hibernate;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.CustomType;
import org.hibernate.usertype.UserType;
import org.hibernatespatial.SpatialDialect;
import org.hibernatespatial.SpatialRelation;
 
 
/**
* Extends the PostgreSQLDialect by also including information on spatial
* operators, constructors and processing functions.
*
* @author Karel Maesen
*/
public class PostgisDialect extends PostgreSQLDialect implements
SpatialDialect {
 
public PostgisDialect() {
super();
registerColumnType(java.sql.Types.STRUCT, "geometry");
 
// registering OGC functions
// (spec_simplefeatures_sql_99-04.pdf)
 
// section 2.1.1.1
// Registerfunction calls for registering geometry functions:
// first argument is the OGC standard functionname, second the name as
// it occurs in the spatial dialect
registerFunction("dimension", new StandardSQLFunction("dimension",
Hibernate.INTEGER));
registerFunction("geometrytype", new StandardSQLFunction(
"geometrytype", Hibernate.STRING));
registerFunction("srid", new StandardSQLFunction("srid",
Hibernate.INTEGER));
registerFunction("envelope", new StandardSQLFunction("envelope",
new CustomType(PGGeometryUserType.class, null)));
registerFunction("astext", new StandardSQLFunction("astext",
Hibernate.STRING));
registerFunction("asbinary", new StandardSQLFunction("asbinary",
Hibernate.BINARY));
registerFunction("isempty", new StandardSQLFunction("isempty",
Hibernate.BOOLEAN));
registerFunction("issimple", new StandardSQLFunction("issimple",
Hibernate.BOOLEAN));
registerFunction("boundary", new StandardSQLFunction("boundary",
new CustomType(PGGeometryUserType.class, null)));
 
// Register functions for spatial relation constructs
registerFunction("overlaps", new StandardSQLFunction("overlaps",
Hibernate.BOOLEAN));
registerFunction("intersects", new StandardSQLFunction("intersects",
Hibernate.BOOLEAN));
registerFunction("equals", new StandardSQLFunction("equals",
Hibernate.BOOLEAN));
registerFunction("contains", new StandardSQLFunction("contains",
Hibernate.BOOLEAN));
registerFunction("crosses", new StandardSQLFunction("crosses",
Hibernate.BOOLEAN));
registerFunction("disjoint", new StandardSQLFunction("disjoint",
Hibernate.BOOLEAN));
registerFunction("touches", new StandardSQLFunction("touches",
Hibernate.BOOLEAN));
registerFunction("within", new StandardSQLFunction("within",
Hibernate.BOOLEAN));
registerFunction("relate", new StandardSQLFunction("relate",
Hibernate.BOOLEAN));
 
// register the spatial analysis functions
registerFunction("distance", new StandardSQLFunction("distance",
Hibernate.DOUBLE));
registerFunction("buffer", new StandardSQLFunction("buffer",
new CustomType(PGGeometryUserType.class, null)));
registerFunction("convexhull", new StandardSQLFunction("convexhull",
new CustomType(PGGeometryUserType.class, null)));
registerFunction("difference", new StandardSQLFunction("difference",
new CustomType(PGGeometryUserType.class, null)));
registerFunction("intersection",
new StandardSQLFunction("intersection", new CustomType(
PGGeometryUserType.class, null)));
registerFunction("symdifference", new StandardSQLFunction(
"symdifference",
new CustomType(PGGeometryUserType.class, null)));
registerFunction("geomunion", new StandardSQLFunction("geomunion",
new CustomType(PGGeometryUserType.class, null)));
 
}
 
/*
* (non-Javadoc)
*
* @see org.walkonweb.spatial.dialect.SpatialEnabledDialect#getSpatialRelateExpression(java.lang.String,
* int, boolean)
*/
public String getSpatialRelateSQL(String columnName, int spatialRelation,
boolean hasFilter) {
switch (spatialRelation) {
case SpatialRelation.WITHIN:
return hasFilter ? "(" + columnName + " && ? AND within("
+ columnName + ", ?))" : " within(" + columnName + ",?)";
case SpatialRelation.CONTAINS:
return hasFilter ? "(" + columnName + " && ? AND contains("
+ columnName + ", ?))" : " contains(" + columnName
+ ", ?)";
case SpatialRelation.CROSSES:
return hasFilter ? "(" + columnName + " && ? AND crosses("
+ columnName + ", ?))" : " crosses(" + columnName + ", ?)";
case SpatialRelation.OVERLAPS:
return hasFilter ? "(" + columnName + " && ? AND overlaps("
+ columnName + ", ?))" : " overlaps(" + columnName
+ ", ?)";
case SpatialRelation.DISJOINT:
return hasFilter ? "(" + columnName + " && ? AND disjoint("
+ columnName + ", ?))" : " disjoint(" + columnName
+ ", ?)";
case SpatialRelation.INTERSECTS:
return hasFilter ? "(" + columnName + " && ? AND intersects("
+ columnName + ", ?))" : " intersects(" + columnName
+ ", ?)";
case SpatialRelation.TOUCHES:
return hasFilter ? "(" + columnName + " && ? AND touches("
+ columnName + ", ?))" : " touches(" + columnName + ", ?)";
case SpatialRelation.EQUALS:
return hasFilter ? "(" + columnName + " && ? AND equals("
+ columnName + ", ?))" : " equals(" + columnName + ", ?)";
default:
throw new IllegalArgumentException(
"Spatial relation is not known by this dialect");
}
 
}
 
/*
* (non-Javadoc)
*
* @see org.walkonweb.spatial.dialect.SpatialEnabledDialect#getSpatialFilterExpression(java.lang.String)
*/
public String getSpatialFilterExpression(String columnName) {
return "(" + columnName + " && ? ) ";
}
 
/*
* (non-Javadoc)
*
* @see org.hibernatespatial.SpatialDialect#getGeometryUserType()
*/
public UserType getGeometryUserType() {
return new PGGeometryUserType();
}
 
}
Property changes:
Added: svn:keywords
+ Id
/trunk/hibernate-spatial-postgis/src/main/resources/META-INF/services/com.cadrie.hibernate.spatial.spi.SpatialDialectProvider
File deleted
\ No newline at end of file
/trunk/hibernate-spatial-postgis/src/main/resources/META-INF/services/org.hibernatespatial.spi.SpatialDialectProvider
New file
0,0 → 1,0
org.hibernatespatial.postgis.DialectProvider
Property changes:
Added: svn:keywords
+ Id