Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 56 → Rev 66

/trunk/hibernate-spatial-postgis/src/test/java/org/hibernatespatial/postgis/test/TestPostgisSpatialQueries.java
122,6 → 122,18
public void testHQLSRID() throws Exception {
delegate.testHQLSRID();
}
@Test
public void testExtent() throws Exception {
String sql = "select area(extent(geom)) from $table$";
delegate.testExtent(sql);
}
@Test
public void testHQLExtent() throws Exception {
String sql = "select AREA(extent(geom)) from $table$";
delegate.testHQLExtent(sql);
}
 
@Test
public void testFiltering() throws Exception {
/trunk/hibernate-spatial-postgis/src/test/resources/create-tables.sql
20,7 → 20,13
geom geometry
);
 
CREATE TABLE public.pointtest(
id DECIMAL(10,0),
name VARCHAR(50),
geom geometry
);
 
 
--
-- TODO -- create spatial index
--
--
/trunk/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/PGGeometryUserType.java
38,6 → 38,7
import org.postgis.MultiLineString;
import org.postgis.MultiPoint;
import org.postgis.MultiPolygon;
import org.postgis.PGboxbase;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.postgis.Polygon;
81,41 → 82,62
object = new PGgeometry((org.postgis.Geometry) object);
}
 
if (!(object instanceof PGgeometry)) {
throw new IllegalArgumentException(
"Object to convert is neither PGgeometry, nor org.postgis.Geometry");
if (object instanceof PGgeometry) {
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;
} else if (object instanceof org.postgis.PGboxbase) {
return convertBox((org.postgis.PGboxbase) object);
} else {
throw new IllegalArgumentException("Can't convert object of type "
+ object.getClass().getCanonicalName());
 
}
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 convertBox(PGboxbase box) {
Point ll = box.getLLB();
Point ur = box.getURT();
Coordinate[] ringCoords = new Coordinate[5];
ringCoords[0] = new Coordinate(ll.x, ll.y);
ringCoords[1] = new Coordinate(ur.x, ll.y);
ringCoords[2] = new Coordinate(ur.x, ur.y);
ringCoords[3] = new Coordinate(ll.x, ur.y);
ringCoords[4] = new Coordinate(ll.x, ll.y);
com.vividsolutions.jts.geom.LinearRing shell = geomFactory
.createLinearRing(ringCoords);
return geomFactory.createPolygon(shell, null);
}
 
private Geometry convertGeometryCollection(GeometryCollection collection) {
org.postgis.Geometry[] geometries = collection.getGeometries();
com.vividsolutions.jts.geom.Geometry[] jtsGeometries = new com.vividsolutions.jts.geom.Geometry[geometries.length];
/trunk/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/PostgisDialect.java
33,6 → 33,7
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.CustomType;
import org.hibernate.usertype.UserType;
import org.hibernatespatial.SpatialAggregate;
import org.hibernatespatial.SpatialDialect;
import org.hibernatespatial.SpatialRelation;
 
110,6 → 111,10
PGGeometryUserType.class, null)));
registerFunction("geomunion", new StandardSQLFunction("geomunion",
new CustomType(PGGeometryUserType.class, null)));
//register Spatial Aggregate funciton
registerFunction("extent", new StandardSQLFunction("extent",
new CustomType(PGGeometryUserType.class, null)));
 
}
 
172,11 → 177,22
return new PGGeometryUserType();
}
 
public String getSpatialAggregateSQL(String columnName, int aggregation,
boolean isProjection) {
// todo needs implemented
throw new UnsupportedOperationException(
"This method is not yet supported in PostGis");
/*
* (non-Javadoc)
*
* @see org.hibernatespatial.SpatialDialect#getSpatialAggregateSQL(java.lang.String,
* int)
*/
public String getSpatialAggregateSQL(String columnName, int aggregation) {
switch (aggregation) {
case SpatialAggregate.EXTENT:
StringBuilder stbuf = new StringBuilder();
stbuf.append("extent(").append(columnName).append(")");
return stbuf.toString();
default:
throw new IllegalArgumentException("Aggregation of type "
+ aggregation + " are not supported by this dialect");
}
}
 
}