HHH-11198 - Add Geometry cast to Postgis st_extent() function

This commit is contained in:
Karel Maesen 2016-10-29 16:18:13 +02:00
parent 1679e205cc
commit af301bc9ba
5 changed files with 126 additions and 77 deletions

View File

@ -159,9 +159,19 @@ public enum SpatialFunction {
* <p>The semantics are those of the Postgis function ST_Transform(geometry, srid) : geometry. It returns new geometry * <p>The semantics are those of the Postgis function ST_Transform(geometry, srid) : geometry. It returns new geometry
* with its coordinates transformed to the spatial reference system referenced by the srid parameter. * with its coordinates transformed to the spatial reference system referenced by the srid parameter.
*/ */
transform( "common" ); transform( "common" ),
/**
* the extents function
*/
extent( "common" );
private final String description; private final String description;
SpatialFunction(String specification) { SpatialFunction(String specification) {
this.description = specification; this.description = specification;
} }

View File

@ -122,6 +122,7 @@ public class MySQLSpatialDialect extends MySQLDialect implements SpatialDialect
case geomunion: case geomunion:
case dwithin: case dwithin:
case transform: case transform:
case extent:
return false; return false;
default: default:
return true; return true;

View File

@ -7,10 +7,12 @@
package org.hibernate.spatial.dialect.postgis; package org.hibernate.spatial.dialect.postgis;
import java.util.List;
import org.hibernate.boot.model.TypeContributions; import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.PostgreSQL82Dialect; import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.GeolatteGeometryType; import org.hibernate.spatial.GeolatteGeometryType;
import org.hibernate.spatial.JTSGeometryType; import org.hibernate.spatial.JTSGeometryType;
@ -19,6 +21,7 @@ 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;
import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
/** /**
* A Dialect for Postgresql with support for the Postgis spatial types, functions and operators (release 1.3 or higher) * A Dialect for Postgresql with support for the Postgis spatial types, functions and operators (release 1.3 or higher)
@ -208,9 +211,7 @@ public class PostgisDialect extends PostgreSQL82Dialect implements SpatialDialec
//register Spatial Aggregate function //register Spatial Aggregate function
registerFunction( registerFunction(
"extent", new StandardSQLFunction( "extent", new ExtentFunction()
"extent"
)
); );
//other common functions //other common functions
@ -281,7 +282,7 @@ public class PostgisDialect extends PostgreSQL82Dialect implements SpatialDialec
switch ( aggregation ) { switch ( aggregation ) {
case SpatialAggregate.EXTENT: case SpatialAggregate.EXTENT:
final StringBuilder stbuf = new StringBuilder(); final StringBuilder stbuf = new StringBuilder();
stbuf.append( "extent(" ).append( columnName ).append( ")" ); stbuf.append( "st_extent(" ).append( columnName ).append( ")::geometry" );
return stbuf.toString(); return stbuf.toString();
default: default:
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -298,6 +299,22 @@ public class PostgisDialect extends PostgreSQL82Dialect implements SpatialDialec
@Override @Override
public boolean supports(SpatialFunction function) { public boolean supports(SpatialFunction function) {
return ( getFunctions().get( function.toString() ) != null ); return (getFunctions().get( function.toString() ) != null);
}
private static class ExtentFunction extends StandardSQLFunction {
public ExtentFunction() {
super( "st_extent" );
}
@Override
public String render(
Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
String rendered = super.render( firstArgumentType, arguments, sessionFactory );
//add cast
return rendered + "::geometry";
}
} }
} }

View File

@ -60,7 +60,6 @@ public class PostgisUnmarshalTest {
public void testCase(String pgValue, Geometry<?> expected) throws SQLException { public void testCase(String pgValue, Geometry<?> expected) throws SQLException {
PGobject pgo = new PGobject(); PGobject pgo = new PGobject();
System.out.println( "pgValue " + pgValue );
pgo.setValue( pgValue ); pgo.setValue( pgValue );
Geometry<?> received = PGGeometryTypeDescriptor.toGeometry( pgo ); Geometry<?> received = PGGeometryTypeDescriptor.toGeometry( pgo );
assertEquals( String.format( "Failure on %s", pgValue ), expected, received ); assertEquals( String.format( "Failure on %s", pgValue ), expected, received );

View File

@ -676,6 +676,28 @@ public class TestSpatialFunctions extends SpatialFunctionalTestCase {
} }
@Test
public void test_extent_on_jts() throws SQLException {
extent( JTS );
}
@Test
public void test_extent_on_geolatte() throws SQLException {
extent( GEOLATTE );
}
public void extent(String pckg) throws SQLException {
if ( !isSupportedByDialect( SpatialFunction.extent ) ) {
return;
}
// here we just check if we get a result, and can read it
String hql = format(
"SELECT id, extent(geom) from org.hibernate.spatial.integration.%s.GeomEntity group by id", pckg
);
Map<Integer, Object> hsreceived = new HashMap<Integer, Object>();
doInSession( hql, hsreceived, new HashMap<String, Object>() );
}
public <T> void retrieveHQLResultsAndCompare(Map<Integer, T> dbexpected, String hql, String geometryType) { public <T> void retrieveHQLResultsAndCompare(Map<Integer, T> dbexpected, String hql, String geometryType) {
retrieveHQLResultsAndCompare( dbexpected, hql, null, geometryType ); retrieveHQLResultsAndCompare( dbexpected, hql, null, geometryType );
} }