Skip to content

Commit

Permalink
HHH-6511 - Fixes Postgis, Mysql, SQL Server and GeoDB dialects for ge…
Browse files Browse the repository at this point in the history
…olatte/jts integration.
  • Loading branch information
maesenka authored and sebersole committed Apr 24, 2015
1 parent 0d5dfa6 commit 290c835
Show file tree
Hide file tree
Showing 72 changed files with 567 additions and 6,598 deletions.
81 changes: 81 additions & 0 deletions hibernate-spatial/scripts/generate-sqlserver-testcases.groovy
@@ -0,0 +1,81 @@
import groovy.sql.Sql
import groovy.xml.MarkupBuilder
import org.geolatte.geom.ByteBuffer

@GrabConfig(systemClassLoader=true)
@Grab(group='com.microsoft', module='sqljdbc', version='2.0')
@Grab(group='org.geolatte', module='geolatte-geom',version='0.12-SNAPSHOT')

//path to the generated TestData XML file
def OUT_FILE = "/tmp/out.xml"

//input test data set
String testdata = new File('test-data-set.xml').text

def records = new XmlSlurper().parseText(testdata)
def wkts = records.Element.wkt

def wkt_srids= wkts.collect{ wkt -> wkt.text() =~ /SRID=(.*);(.*)/ }
.findAll { it.matches() }
.collect {m -> [ m[0][1], m[0][2] ] } //select only matching srid and wkt regions

//add the empty elements

wkt_srids.add( [0, 'POINT EMPTY'])
wkt_srids.add( [0, 'LINESTRING EMPTY'])
wkt_srids.add( [0, 'GEOMETRYCOLLECTION EMPTY'])
wkt_srids.add( [0, 'POLYGON EMPTY'])
wkt_srids.add( [0, 'MULTIPOINT EMPTY'])

//wkt_srids.each{ println( it ) }

sql = Sql.newInstance('jdbc:sqlserver://sqlserver.geovise.com:1433;databaseName=HBS',
'hbs', 'hbs', 'com.microsoft.sqlserver.jdbc.SQLServerDriver')

def writer = new FileWriter( new File(OUT_FILE) )
def xmlOut = new MarkupBuilder(writer)
xmlOut.TestCases() {
def i = 1
wkt_srids.each { el ->
TestCase {
id( i++ )
ewkt( el[1] )
srid( el[0])
db_representation(to_native(el[1], el[0]))
// db_wkt(to_wkt(el[1], el[0]))
// db_wkb(to_wkb(el[1], el[0]))
}
}

}

def to_native(wkt, srid) {
row = sql.firstRow("select Geometry::STGeomFromText('" + wkt + "', ${srid}) ")
buffer = ByteBuffer.from(row[0])
buffer.toString()
}

def to_wkt(wkt, srid) {
row = sql.firstRow("select Geometry::STGeomFromText('" + wkt + "', ${srid}).STAsText() ")
row[0]
}


def to_wkb(wkt, srid) {
row = sql.firstRow("select Geometry::STGeomFromText('" + wkt + "', ${srid}).STAsBinary() ")
buffer = ByteBuffer.from(row[0])
buffer.toString()
}













Expand Up @@ -7,7 +7,7 @@
* @author Karel Maesen, Geovise BVBA * @author Karel Maesen, Geovise BVBA
* creation-date: 10/12/12 * creation-date: 10/12/12
*/ */
public class GeolatteGeometryType extends AbstractSingleColumnStandardBasicType<Geometry> { public class GeolatteGeometryType extends AbstractSingleColumnStandardBasicType<Geometry> implements Spatial {


public static final GeolatteGeometryType INSTANCE = new GeolatteGeometryType(); public static final GeolatteGeometryType INSTANCE = new GeolatteGeometryType();


Expand Down
Expand Up @@ -29,7 +29,7 @@
* *
* @author Karel Maesen * @author Karel Maesen
*/ */
public class JTSGeometryType extends AbstractSingleColumnStandardBasicType<Geometry> { public class JTSGeometryType extends AbstractSingleColumnStandardBasicType<Geometry> implements Spatial {


public static final JTSGeometryType INSTANCE = new JTSGeometryType(); public static final JTSGeometryType INSTANCE = new JTSGeometryType();


Expand Down
11 changes: 11 additions & 0 deletions hibernate-spatial/src/main/java/org/hibernate/spatial/Spatial.java
@@ -0,0 +1,11 @@
package org.hibernate.spatial;

/**
* A marker interface for spatial (geometric/geographic) types.
*
* @author Karel Maesen, Geovise BVBA
* creation-date: 4/18/13
*/
public interface Spatial {

}
Expand Up @@ -24,7 +24,6 @@
import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;


import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery; import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Criterion;
Expand Down Expand Up @@ -57,11 +56,10 @@ public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws


} }



public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] { return new TypedValue[] {
criteriaQuery.getTypedValue( criteria, propertyName, geometry ), criteriaQuery.getTypedValue( criteria, propertyName, geometry ),
new TypedValue( StandardBasicTypes.DOUBLE, Double.valueOf( distance ), EntityMode.POJO ) new TypedValue( StandardBasicTypes.DOUBLE, distance )
}; };
} }
} }
Expand Up @@ -22,7 +22,6 @@
package org.hibernate.spatial.criterion; package org.hibernate.spatial.criterion;


import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery; import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Criterion;
Expand Down Expand Up @@ -53,7 +52,7 @@ public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws


public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[] { return new TypedValue[] {
new TypedValue( StandardBasicTypes.INTEGER, Integer.valueOf( srid ), EntityMode.POJO ) new TypedValue( StandardBasicTypes.INTEGER, srid )
}; };
} }


Expand Down
Expand Up @@ -26,9 +26,8 @@
import java.sql.SQLException; import java.sql.SQLException;


import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;


import org.hibernate.spatial.jts.JTS;
import org.hibernate.spatial.jts.mgeom.MGeometryFactory;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicBinder; import org.hibernate.type.descriptor.sql.BasicBinder;
Expand All @@ -40,6 +39,8 @@
*/ */
public abstract class AbstractGeometryValueBinder<X> extends BasicBinder<X> { public abstract class AbstractGeometryValueBinder<X> extends BasicBinder<X> {


private static GeometryFactory geometryFactory = new GeometryFactory();

public AbstractGeometryValueBinder(JavaTypeDescriptor<X> javaDescriptor, SqlTypeDescriptor sqlDescriptor) { public AbstractGeometryValueBinder(JavaTypeDescriptor<X> javaDescriptor, SqlTypeDescriptor sqlDescriptor) {
super( javaDescriptor, sqlDescriptor ); super( javaDescriptor, sqlDescriptor );
} }
Expand All @@ -50,8 +51,8 @@ protected void doBind(PreparedStatement st, X value, int index, WrapperOptions o
st.setObject( index, dbGeom ); st.setObject( index, dbGeom );
} }


protected MGeometryFactory getGeometryFactory() { protected GeometryFactory getGeometryFactory() {
return JTS.getDefaultGeometryFactory(); return geometryFactory;
} }


protected abstract Object toNative(Geometry jtsGeom, Connection connection); protected abstract Object toNative(Geometry jtsGeom, Connection connection);
Expand Down
Expand Up @@ -21,30 +21,34 @@


package org.hibernate.spatial.dialect; package org.hibernate.spatial.dialect;


import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;

import org.hibernate.spatial.GeometrySqlTypeDescriptor; import org.hibernate.spatial.GeometrySqlTypeDescriptor;
import org.hibernate.spatial.jts.JTS;
import org.hibernate.spatial.jts.mgeom.MGeometryFactory;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicExtractor; import org.hibernate.type.descriptor.sql.BasicExtractor;


import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/** /**
* @author Karel Maesen, Geovise BVBA * @author Karel Maesen, Geovise BVBA
* creation-date: 1/19/12 * creation-date: 1/19/12
*/ */
public abstract class AbstractGeometryValueExtractor<X> extends BasicExtractor<X> { public abstract class AbstractGeometryValueExtractor<X> extends BasicExtractor<X> {


// later this will need to be configurable. So define this only once for both
// extractor and binder.
private static GeometryFactory geometryFactory = new GeometryFactory();

public AbstractGeometryValueExtractor(JavaTypeDescriptor<X> javaDescriptor, GeometrySqlTypeDescriptor typeDescriptor) { public AbstractGeometryValueExtractor(JavaTypeDescriptor<X> javaDescriptor, GeometrySqlTypeDescriptor typeDescriptor) {
super( javaDescriptor, typeDescriptor ); super( javaDescriptor, typeDescriptor );
} }


protected MGeometryFactory getGeometryFactory() { protected GeometryFactory getGeometryFactory() {
return JTS.getDefaultGeometryFactory(); return geometryFactory;
} }


@Override @Override
Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.spatial.GeometrySqlTypeDescriptor; import org.hibernate.spatial.GeometrySqlTypeDescriptor;
import org.hibernate.spatial.JTSGeometryType;
import org.hibernate.spatial.SpatialDialect; import org.hibernate.spatial.SpatialDialect;
import org.hibernate.spatial.SpatialFunction; import org.hibernate.spatial.SpatialFunction;
import org.hibernate.spatial.SpatialRelation; import org.hibernate.spatial.SpatialRelation;
Expand Down Expand Up @@ -118,8 +117,7 @@ public GeoDBDialect() {
); );
registerFunction( registerFunction(
"envelope", new StandardSQLFunction( "envelope", new StandardSQLFunction(
"ST_Envelope", "ST_Envelope"
JTSGeometryType.INSTANCE
) )
); );
registerFunction( registerFunction(
Expand Down Expand Up @@ -212,8 +210,7 @@ public GeoDBDialect() {
); );
registerFunction( registerFunction(
"buffer", new StandardSQLFunction( "buffer", new StandardSQLFunction(
"ST_Buffer", "ST_Buffer"
JTSGeometryType.INSTANCE
) )
); );
// NOT YET AVAILABLE IN GEODB // NOT YET AVAILABLE IN GEODB
Expand Down
Expand Up @@ -5,7 +5,6 @@
import java.util.Map; import java.util.Map;


import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.spatial.JTSGeometryType;
import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.StandardBasicTypes;


/** /**
Expand Down Expand Up @@ -36,8 +35,7 @@ class MySQLSpatialFunctions implements Iterable<Map.Entry<String, StandardSQLFun
); );
functionsToRegister.put( functionsToRegister.put(
"envelope", new StandardSQLFunction( "envelope", new StandardSQLFunction(
"envelope", "envelope"
JTSGeometryType.INSTANCE
) )
); );
functionsToRegister.put( functionsToRegister.put(
Expand All @@ -64,12 +62,11 @@ class MySQLSpatialFunctions implements Iterable<Map.Entry<String, StandardSQLFun
StandardBasicTypes.BOOLEAN StandardBasicTypes.BOOLEAN
) )
); );
functionsToRegister.put( // functionsToRegister.put(
"boundary", new StandardSQLFunction( // "boundary", new StandardSQLFunction(
"boundary", // "boundary"
JTSGeometryType.INSTANCE // )
) // );
);


// Register functions for spatial relation constructs // Register functions for spatial relation constructs
functionsToRegister.put( functionsToRegister.put(
Expand Down Expand Up @@ -120,54 +117,50 @@ class MySQLSpatialFunctions implements Iterable<Map.Entry<String, StandardSQLFun
StandardBasicTypes.BOOLEAN StandardBasicTypes.BOOLEAN
) )
); );
functionsToRegister.put( // functionsToRegister.put(
"relate", new StandardSQLFunction( // "relate", new StandardSQLFunction(
"relate", // "relate",
StandardBasicTypes.BOOLEAN // StandardBasicTypes.BOOLEAN
) // )
); // );

//
// register the spatial analysis functions // // register the spatial analysis functions
functionsToRegister.put( // functionsToRegister.put(
"distance", new StandardSQLFunction( // "distance", new StandardSQLFunction(
"distance", // "distance",
StandardBasicTypes.DOUBLE // StandardBasicTypes.DOUBLE
) // )
); // );
functionsToRegister.put( // functionsToRegister.put(
"buffer", new StandardSQLFunction( // "buffer", new StandardSQLFunction(
"buffer", // "buffer"
JTSGeometryType.INSTANCE // )
) // );
); // functionsToRegister.put(
functionsToRegister.put( // "convexhull", new StandardSQLFunction(
"convexhull", new StandardSQLFunction( // "convexhull"
"convexhull", // )
JTSGeometryType.INSTANCE // );
) // functionsToRegister.put(
); // "difference", new StandardSQLFunction(
functionsToRegister.put( // "difference"
"difference", new StandardSQLFunction( // )
"difference", // );
JTSGeometryType.INSTANCE // functionsToRegister.put(
) // "intersection", new StandardSQLFunction(
); // "intersection"
functionsToRegister.put( // )
"intersection", new StandardSQLFunction( // );
"intersection", JTSGeometryType.INSTANCE // functionsToRegister.put(
) // "symdifference", new StandardSQLFunction(
); // "symdifference"
functionsToRegister.put( // )
"symdifference", new StandardSQLFunction( // );
"symdifference", JTSGeometryType.INSTANCE // functionsToRegister.put(
) // "geomunion", new StandardSQLFunction(
); // "union"
functionsToRegister.put( // )
"geomunion", new StandardSQLFunction( // );
"union",
JTSGeometryType.INSTANCE
)
);
} }


@Override @Override
Expand Down

0 comments on commit 290c835

Please sign in to comment.