Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 81 → Rev 82

/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/org/hibernatespatial/postgis/test/cfg/HSConfigurationTest.java
New file
0,0 → 1,65
package org.hibernatespatial.postgis.test.cfg;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
 
import java.io.File;
 
import org.hibernate.cfg.Configuration;
import org.hibernatespatial.HBSpatialExtension;
import org.hibernatespatial.cfg.HSConfiguration;
import org.hibernatespatial.cfg.HSProperty;
import org.junit.Test;
 
import com.vividsolutions.jts.geom.PrecisionModel;
 
public class HSConfigurationTest {
 
private static final String hibernate_config_location = "/Users/maesenka/workspaces/hibernate-spatial/hibernate-spatial-mysql/src/test/java/hibernate.cfg.xml";
private static final String hs_config_location = "/Users/maesenka/workspaces/hibernate-spatial/hibernate-spatial/src/test/java/hibernate-spatial.cfg.xml";
@Test
public void testConfigure(){
HSConfiguration config = new HSConfiguration();
Configuration hibConfig = new Configuration();
hibConfig.configure(new File(hibernate_config_location));
config.configure(hibConfig);
assertEquals("org.hibernatespatial.mysql.MySQLSpatialDialect", config.getProperty(HSProperty.DEFAULT_DIALECT));
config.configure();
testResults(config);
}
@Test
public void testConfigureFile(){
HSConfiguration config = new HSConfiguration();
config.configure(new File(hs_config_location));
testResults(config);
}
@Test
public void testConfigureFailure(){
HSConfiguration config = new HSConfiguration();
config.configure("non-existing-file");
}
@Test
public void testHBSpatExtConfigure(){
HSConfiguration config = new HSConfiguration();
config.configure();
HBSpatialExtension.setConfiguration(config);
PrecisionModel pm = HBSpatialExtension.getDefaultGeomFactory().getPrecisionModel();
double scale = pm.getScale();
assertEquals(5.0, scale);
assertFalse(pm.isFloating());
}
private void testResults(HSConfiguration config){
assertEquals("org.hibernatespatial.postgis.PostgisDialect", config.getProperty(HSProperty.DEFAULT_DIALECT));
assertEquals("FIXED", config.getProperty(HSProperty.PRECISION_MODEL));
assertEquals("5", config.getProperty(HSProperty.PRECISION_MODEL_SCALE));
}
}
/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/org/hibernatespatial/postgis/test/GenerateData.java
New file
0,0 → 1,40
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 Geovise BVBA
* 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.hibernatespatial.org/
*/
package org.hibernatespatial.postgis.test;
 
import org.hibernatespatial.test.model.DataGenerator;
 
public class GenerateData {
 
public static void main(String[] args) {
DataGenerator generator = new DataGenerator();
generator.generate();
}
 
}
Property changes:
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/org/hibernatespatial/postgis/test/TestPostgisSpatialQueries.java
New file
0,0 → 1,254
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 Geovise BVBA
* 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.hibernatespatial.org/
*/
package org.hibernatespatial.postgis.test;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
import junit.framework.JUnit4TestAdapter;
 
import org.hibernatespatial.HBSpatialExtension;
import org.hibernatespatial.cfg.HSConfiguration;
import org.hibernatespatial.test.TestSpatialQueries;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
 
public class TestPostgisSpatialQueries {
 
private final static String DBNAME = "test";
 
private static Connection conn;
 
private static TestSpatialQueries delegate;
 
static {
HSConfiguration config = new HSConfiguration();
config.configure();
HBSpatialExtension.setConfiguration(config);
String url = "jdbc:postgresql://localhost:5432/" + DBNAME;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, "postgres", "");
delegate = new TestSpatialQueries();
} catch (Exception e) {
e.printStackTrace();
}
// create the delegate
 
}
 
@BeforeClass
public static void setUpBeforeClass() throws Exception {
delegate.setUpBeforeClass(conn);
}
 
@AfterClass
public static void tearDownAfterClass() throws Exception {
delegate.tearDownAfterClass();
}
 
@Test
public void testHQLGeometryType() throws Exception {
delegate.testHQLGeometryType();
}
 
@Test
public void testHQLAsBinary() throws Exception {
delegate.testHQLAsBinary();
}
 
@Test
public void testHQLAsText() throws Exception {
delegate.testHQLAsText();
}
 
@Test
public void testHQLDimension() throws Exception {
delegate.testHQLDimension();
}
 
@Test
public void testHQLEnvelope() throws Exception {
delegate.testHQLEnvelope();
}
 
@Test
public void testHQLIntersectsLineString() throws Exception {
String sqlString = "select count(*) from $table$ where intersects(geomfromtext(?, 31370), geom)";
delegate.testHQLIntersectsLineString(sqlString);
}
 
@Test
public void testHQLIsEmpty() throws Exception {
String sql = "select count(*) from $table$ where geom is not null and isempty(geom)";
delegate.testHQLIsEmpty(sql);
}
 
@Test
public void testHQLIsSimple() throws Exception {
String sql = "select count(*) from $table$ where geom is not null and issimple(geom)";
delegate.testHQLIsSimple(sql);
}
 
@Test
public void testHQLOverlaps() throws Exception {
String sql = "select count(*) from $table$ where overlaps(geomfromtext(?, 31370), geom) and geom is not null";
delegate.testHQLOverlaps(sql);
}
 
@Test
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 {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370)";
delegate.testFiltering(sql);
}
 
@Test
public void testContains() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and contains(geom, geomFromText(?, 31370))";
delegate.testContains(sql);
}
 
@Test
public void testCrosses() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and crosses(geom, geomFromText(?, 31370))";
delegate.testCrosses(sql);
}
 
@Test
public void testDisjoint() throws Exception {
String sql = "select count(*) from $table$ where disjoint(geom, geomFromText(?, 31370))";
delegate.testDisjoint(sql);
}
 
@Test
public void testHQLDisjoint() throws Exception {
String sql = "select count(*) from $table$ where disjoint(geomfromtext(?, 31370), geom) and geom is not null";
delegate.testHQLDisjoint(sql);
}
 
@Test
public void testEquals() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and equals(geom, geomFromText(?, 31370))";
delegate.testEquals(sql);
}
 
@Test
public void testIntersects() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and intersects(geom, geomFromText(?, 31370))";
delegate.testIntersects(sql);
}
 
@Test
public void testOverlaps() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and overlaps(geom, geomFromText(?, 31370))";
delegate.testOverlaps(sql);
}
 
@Test
public void testTouches() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and touches(geom, geomFromText(?, 31370))";
delegate.testTouches(sql);
}
 
@Test
public void testWithin() throws Exception {
String sql = "select count(*) from $table$ where geom && geomFromText(?,31370) and within(geom, geomFromText(?, 31370))";
delegate.testWithin(sql);
}
 
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(TestPostgisSpatialQueries.class);
}
 
@Test
public void testHQLBoundary() throws Exception {
delegate.testHQLBoundary();
}
 
@Test
public void testHQLRelateLineString() throws Exception {
delegate.testHQLRelateLineString();
}
 
@Test
public void testHQLDistance() throws Exception {
delegate.testHQLDistance();
}
 
@Test
public void testHQLBuffer() throws Exception {
delegate.testHQLBuffer();
}
 
@Test
public void testHQLConvexHull() throws Exception {
delegate.testHQLConvexHull();
}
 
@Test
public void testHQLIntersection() throws Exception {
delegate.testHQLIntersection();
}
 
@Test
public void testHQLDifference() throws Exception {
delegate.testHQLDifference();
}
 
@Test
public void testHQLSymDifference() throws Exception {
delegate.testHQLSymDifference();
}
 
@Test
public void testHQLUnion() throws Exception {
delegate.testHQLUnion();
}
 
}
Property changes:
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/org/hibernatespatial/postgis/test/TestPostgisCRUD.java
New file
0,0 → 1,70
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 Geovise BVBA
* 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.hibernatespatial.org/
*/
package org.hibernatespatial.postgis.test;
 
import junit.framework.JUnit4TestAdapter;
 
import org.hibernatespatial.test.TestCRUD;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
 
public class TestPostgisCRUD {
 
private final static TestCRUD delegate;
 
static {
delegate = new TestCRUD();
}
 
@BeforeClass
public static void setUpBeforeClass() throws Exception {
delegate.setUpBeforeClass();
}
 
@AfterClass
public static void tearDownAfterClass() throws Exception {
delegate.tearDownAfterClass();
}
 
@Test
public void testSaveLineStringEntity() throws Exception {
delegate.testSaveLineStringEntity();
}
 
@Test
public void testSaveNullLineStringEntity() throws Exception {
delegate.testSaveNullLineStringEntity();
}
 
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(TestPostgisCRUD.class);
}
 
}
Property changes:
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/hibernate-spatial.cfg.xml
New file
0,0 → 1,6
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-spatial>
<default_dialect>org.hibernatespatial.postgis.PostgisDialect</default_dialect>
<precision_model>FIXED</precision_model>
<precision_model_scale>5</precision_model_scale>
</hibernate-spatial>
/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/log4j.properties
New file
0,0 → 1,49
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
### set log levels - for more verbose logging change 'info' to 'debug' ###
 
log4j.rootLogger=debug, stdout
 
log4j.logger.org.hibernatespatial.test=debug
 
log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
 
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
 
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
 
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
 
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
 
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
 
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
 
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
 
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
 
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
Property changes:
Added: svn:executable
+
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/test/java/hibernate.cfg.xml
New file
0,0 → 1,28
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernatespatial.postgis.PostgisDialect</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
 
 
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
 
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Property changes:
Added: svn:executable
+
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/test/resources/create-db.sh
New file
0,0 → 1,18
#! /bin/sh
export DBASE=test
export DBUSER=postgres
export POSTGRES_SHARE=/usr/local/pgsql/share
 
dropdb -U $DBUSER $DBASE
 
createdb -U $DBUSER $DBASE
 
echo "Creating language"
createlang -U $DBUSER plpgsql $DBASE
 
echo "Loading postgis extension"
psql -U $DBUSER -d $DBASE -f $POSTGRES_SHARE/lwpostgis.sql
psql -U $DBUSER -d $DBASE -f $POSTGRES_SHARE/lwpostgis_upgrade.sql
 
echo "Loading test schema"
psql -U $DBUSER -d $DBASE -f ./create-tables.sql
Property changes:
Added: svn:executable
+
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/test/resources/create-tables.sql
New file
0,0 → 1,32
--
-- Create test tables
--
 
CREATE TABLE public.linestringtest (
id DECIMAL(10,0),
name VARCHAR(50),
geom geometry
);
 
CREATE TABLE public.multilinestringtest(
id DECIMAL(10,0),
name VARCHAR(50),
geom geometry
);
 
CREATE TABLE public.polygontest(
id DECIMAL(10,0),
name VARCHAR(50),
geom geometry
);
 
CREATE TABLE public.pointtest(
id DECIMAL(10,0),
name VARCHAR(50),
geom geometry
);
 
 
--
-- TODO -- create spatial index
--
Property changes:
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/main/java/META-INF/MANIFEST.MF
New file
0,0 → 1,3
Manifest-Version: 1.0
Class-Path:
 
Property changes:
Added: svn:keywords
+ Id
/tags/1.0-M1/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/DialectProvider.java
New file
0,0 → 1,76
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 Geovise BVBA
* 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.hibernatespatial.org/
*/
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) {
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
/tags/1.0-M1/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/PGGeometryUserType.java
New file
0,0 → 1,361
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 Geovise BVBA
* 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.hibernatespatial.org/
*/
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.PGboxbase;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.postgis.Polygon;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
 
/**
* 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 };
 
public int[] sqlTypes() {
return geometryTypes;
}
 
/**
* Converts the native geometry object to a JTS <code>Geometry</code>.
*
* @param object
* 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;
 
// in some cases, Postgis returns not PGgeometry objects
// but org.postgis.Geometry instances.
// This has been observed when retrieving GeometryCollections
// as the result of an SQL-operation such as Union.
if (object instanceof org.postgis.Geometry) {
object = new PGgeometry((org.postgis.Geometry) object);
}
 
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());
break;
default:
throw new RuntimeException("Unknown type of PGgeometry");
}
 
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());
 
}
 
}
 
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 = getGeometryFactory()
.createLinearRing(ringCoords);
return getGeometryFactory().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];
for (int i = 0; i < geometries.length; i++) {
jtsGeometries[i] = convert2JTS(geometries[i]);
}
com.vividsolutions.jts.geom.GeometryCollection jtsGCollection = getGeometryFactory()
.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 = getGeometryFactory()
.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 = getGeometryFactory()
.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] = getGeometryFactory().createLineString(toJTSCoordinates(mlstr
.getLine(i).getPoints()));
}
com.vividsolutions.jts.geom.MultiLineString out = getGeometryFactory()
.createMultiLineString(lstrs);
out.setSRID(mlstr.srid);
return out;
}
 
protected com.vividsolutions.jts.geom.Geometry convertPolygon(
Polygon polygon) {
com.vividsolutions.jts.geom.LinearRing shell = getGeometryFactory()
.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] = getGeometryFactory()
.createLinearRing(toJTSCoordinates(polygon.getRing(r)
.getPoints()));
}
out = getGeometryFactory().createPolygon(shell, rings);
} else {
out = getGeometryFactory().createPolygon(shell, null);
}
out.setSRID(polygon.srid);
return out;
}
 
protected com.vividsolutions.jts.geom.Point convertPoint(Point pnt) {
com.vividsolutions.jts.geom.Point g = getGeometryFactory()
.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 = getGeometryFactory()
.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
/tags/1.0-M1/hibernate-spatial-postgis/src/main/java/org/hibernatespatial/postgis/PostgisDialect.java
New file
0,0 → 1,198
/**
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2007 Geovise BVBA
* 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.hibernatespatial.org/
*/
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.SpatialAggregate;
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)));
//register Spatial Aggregate funciton
registerFunction("extent", new StandardSQLFunction("extent",
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();
}
 
/*
* (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");
}
}
 
}
Property changes:
Added: svn:keywords
+ Id
/tags/1.0-M1/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
/tags/1.0-M1/hibernate-spatial-postgis/src/site/site.xml
New file
0,0 → 1,26
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Hibernate Spatial Postgis provider">
<bannerLeft>
<name>Hibernate Spatial Postgis Provider</name>
<href>http://www.hibernatespatial.org/hibernate-spatial-postgis/index.html</href>
</bannerLeft>
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-default-skin</artifactId>
</skin>
 
<publishDate format="yyyy MMM dd" />
<body>
<menu name="Overview" inherit="top">
<item name="Introduction" href="index.html"/>
<item name="Usage" href="usage.html"/>
</menu>
 
</body>
</project>
/tags/1.0-M1/hibernate-spatial-postgis/src/site/xdoc/usage.xml
New file
0,0 → 1,48
<?xml version="1.0" encoding="UTF-8"?>
 
<document>
<header />
 
 
<body>
<section name="Hibernate Spatial Postgis Provider Usage">
<p>
To have Hibernate Spatial use this provider, simply drop
the jar on the class-path alongside the
hibernate-spatial.jar. When the Hibernate Spatial
library is loaded it will find the provider and use it
to retrieve the Hibernate dialect for Postgresql with
the Postgis extension. This dialect is a subclass of
<code>org.hibernate.dialect.PostgreSQLDialect</code>
</p>
<p>
With the provider installed, you can use it by setting
the dialect in the Hibernate configuration file as in
the following snippet.
</p>
<source>
...
&lt;property name="hibernate.dialect"&gt;
org.hibernatespatial.postgis.PostgisDialect
&lt;/property&gt;
...
</source>
 
<p>
When there is more than one Hibernate Spatial provider
on the Class-Path, you should explicitly label the
dialect for the Geometry valued properties.
</p>
<source>
...
&lt;property name="geometry"
type="org.hibernatespatial.GeometryUserType"&gt;
&lt;column name="geom" /&gt;
&lt;param name="dialect"&gt;postgis&lt;/param&gt;
&lt;/property&gt;
...
</source>
</section>
 
</body>
</document>
/tags/1.0-M1/hibernate-spatial-postgis/src/site/xdoc/index.xml
New file
0,0 → 1,15
<?xml version="1.0" encoding="UTF-8"?>
 
<document>
<header />
 
<body>
<section name="The Hibernate Spatial Postgis Provider">
<p>
The Postgis Provider adds support for Postgis to
Hibernate Spatial. It has been tested with Postgis 1.1.6
and should work with any version later than that.
</p>
</section>
</body>
</document>
/tags/1.0-M1/hibernate-spatial-postgis/pom.xml
New file
0,0 → 1,46
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
<!-- $Id$ -->
 
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.hibernatespatial</groupId>
<artifactId>hibernate-spatial-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../hibernate-spatial-maven</relativePath>
</parent>
<artifactId>hibernate-spatial-postgis</artifactId>
<packaging>jar</packaging>
<name>Postgis DialectProvider</name>
<url>http://www.hibernatespatial.org/hibernate-spatial-postgis</url>
<description>
Postgis (Postgresql) dialect provider for Hibernate Spatial
</description>
 
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.1-407.jdbc3</version>
</dependency>
</dependencies>
</project>
Property changes:
Added: svn:keywords
+ Id