diff --git a/hibernate-spatial/databases/sqlserver2008_spatial/resources/hibernate.properties b/hibernate-spatial/databases/sqlserver2008_spatial/resources/hibernate.properties index ff699c16ae..3e283f3c4e 100644 --- a/hibernate-spatial/databases/sqlserver2008_spatial/resources/hibernate.properties +++ b/hibernate-spatial/databases/sqlserver2008_spatial/resources/hibernate.properties @@ -24,7 +24,7 @@ hibernate.dialect org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver -hibernate.connection.url jdbc:sqlserver://192.168.0.100:1433;databaseName=HBS +hibernate.connection.url jdbc:sqlserver://sqlserver.geovise.com:1433;databaseName=HBS hibernate.connection.username hbs hibernate.connection.password hbs diff --git a/hibernate-spatial/hibernate-spatial.gradle b/hibernate-spatial/hibernate-spatial.gradle index 6b07e53192..7b2bfe8cfc 100644 --- a/hibernate-spatial/hibernate-spatial.gradle +++ b/hibernate-spatial/hibernate-spatial.gradle @@ -27,7 +27,12 @@ dependencies { compile([group: 'com.vividsolutions', name: 'jts', version: '1.12']) { transitive = false } - compile([group: 'org.geolatte', name: 'geolatte-geom', version: '0.11-SNAPSHOT']) + compile([group: 'org.postgis', name: 'postgis-jdbc', version: '1.5.3']) + compile([group: 'postgresql', name: 'postgresql', version: '8.4-701.jdbc4']) + + + compile([group: 'org.geolatte', name: 'geolatte-geom', version: '0.12-SNAPSHOT']) + compile(libraries.dom4j) { transitive = false @@ -38,7 +43,7 @@ dependencies { testCompile(project(':hibernate-testing')) testRuntime([group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4']) // testRuntime([group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.2.0.3']) -// testRuntime([group: 'com.microsoft', name: 'sqljdbc', version: '2.0']) + testRuntime([group: 'com.microsoft', name: 'sqljdbc', version: '2.0']) // testRuntime("org.opengeo:geodb:0.7") // testRuntime("mysql:mysql-connector-java:5.1.15") testRuntime("postgresql:postgresql:8.4-701.jdbc4") diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/SqlServer2008GeometryTypeDescriptor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/SqlServer2008GeometryTypeDescriptor.java index f7766ce9f5..c671ae3b50 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/SqlServer2008GeometryTypeDescriptor.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/SqlServer2008GeometryTypeDescriptor.java @@ -21,7 +21,15 @@ package org.hibernate.spatial.dialect.sqlserver; -import com.vividsolutions.jts.geom.Geometry; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +import org.geolatte.geom.Geometry; + import org.hibernate.spatial.GeometrySqlTypeDescriptor; import org.hibernate.spatial.dialect.sqlserver.convertors.Decoders; import org.hibernate.spatial.dialect.sqlserver.convertors.Encoders; @@ -32,8 +40,6 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.sql.BasicBinder; import org.hibernate.type.descriptor.sql.BasicExtractor; -import java.sql.*; - /** * @author Karel Maesen, Geovise BVBA * creation-date: 8/23/11 diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractDecoder.java index 31f5b96ef8..c2f5563f45 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractDecoder.java @@ -21,30 +21,20 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Geometry; -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.Geometry; abstract class AbstractDecoder implements Decoder { - private final MGeometryFactory geometryFactory; - - public AbstractDecoder(MGeometryFactory factory) { - this.geometryFactory = factory; - } - public G decode(SqlServerGeometry nativeGeom) { if ( !accepts( nativeGeom ) ) { throw new IllegalArgumentException( getClass().getSimpleName() + " received object of type " + nativeGeom.openGisType() ); } if ( nativeGeom.isEmpty() ) { G nullGeom = createNullGeometry(); - setSrid( nativeGeom, nullGeom ); return nullGeom; } - G result = createGeometry( nativeGeom ); - setSrid( nativeGeom, result ); - return result; + return createGeometry( nativeGeom ); } public boolean accepts(OpenGisType type) { @@ -63,15 +53,4 @@ abstract class AbstractDecoder implements Decoder { protected abstract G createGeometry(SqlServerGeometry nativeGeom, int shapeIndex); - protected MGeometryFactory getGeometryFactory() { - return this.geometryFactory; - } - - protected void setSrid(SqlServerGeometry sqlServerGeom, G result) { - if ( sqlServerGeom.getSrid() != null ) { - result.setSRID( sqlServerGeom.getSrid() ); - } - } - - } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractEncoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractEncoder.java index fd9d00569e..72ebaab5bc 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractEncoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractEncoder.java @@ -24,31 +24,28 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.ArrayList; import java.util.List; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; - -import org.hibernate.spatial.jts.mgeom.MGeometry; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.PointCollection; abstract class AbstractEncoder implements Encoder { public SqlServerGeometry encode(G geom) { SqlServerGeometry nativeGeom = new SqlServerGeometry(); - nativeGeom.setSrid( geom.getSRID() ); - if ( geom.isValid() ) { - nativeGeom.setIsValid(); - } + int srid = geom.getSRID(); + nativeGeom.setSrid( srid < 0 ? 0 : srid ); + nativeGeom.setIsValid(); - if ( hasMValues( geom ) ) { + if ( geom.isMeasured() ) { nativeGeom.setHasMValues(); } - List coordinates = new ArrayList(); + CountingPointSequenceBuilder coordinates = new CountingPointSequenceBuilder(geom.getDimensionalFlag()); List
figures = new ArrayList
(); List shapes = new ArrayList(); encode( geom, -1, coordinates, figures, shapes ); - encodePoints( nativeGeom, coordinates ); + encodePoints( nativeGeom, coordinates.toPointSequence() ); encodeFigures( nativeGeom, figures ); encodeShapes( nativeGeom, shapes ); return nativeGeom; @@ -63,7 +60,7 @@ abstract class AbstractEncoder implements Encoder { * @param figures figure list to append to * @param shapes shape list to append to */ - protected abstract void encode(Geometry geom, int parentShapeIndex, List coordinates, List
figures, List shapes); + protected abstract void encode(Geometry geom, int parentShapeIndex, CountingPointSequenceBuilder coordinates, List
figures, List shapes); protected void encodeShapes(SqlServerGeometry nativeGeom, List shapes) { nativeGeom.setNumberOfShapes( shapes.size() ); @@ -79,21 +76,17 @@ abstract class AbstractEncoder implements Encoder { } } - protected boolean hasMValues(G geom) { - return geom instanceof MGeometry; - } - - protected void encodePoints(SqlServerGeometry nativeGeom, List coordinates) { + protected void encodePoints(SqlServerGeometry nativeGeom, PointCollection coordinates) { nativeGeom.setNumberOfPoints( coordinates.size() ); nativeGeom.allocateMValueArray(); for ( int i = 0; i < coordinates.size(); i++ ) { - setCoordinate( nativeGeom, i, coordinates.get( i ) ); + setCoordinate( nativeGeom, i, coordinates); } } - protected void setCoordinate(SqlServerGeometry nativeGeom, int idx, Coordinate coordinate) { - if ( !nativeGeom.hasZValues() && !Double.isNaN( coordinate.z ) ) { + protected void setCoordinate(SqlServerGeometry nativeGeom, int idx, PointCollection coordinate) { + if ( !nativeGeom.hasZValues() && !Double.isNaN( coordinate.getZ(idx) ) ) { nativeGeom.setHasZValues(); nativeGeom.allocateZValueArray(); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractGeometryCollectionDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractGeometryCollectionDecoder.java index f49963d12c..018cedf03c 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractGeometryCollectionDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractGeometryCollectionDecoder.java @@ -24,17 +24,11 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.ArrayList; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryCollection; - -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.GeometryCollection; abstract class AbstractGeometryCollectionDecoder extends AbstractDecoder { - public AbstractGeometryCollectionDecoder(MGeometryFactory factory) { - super( factory ); - } - @Override protected OpenGisType getOpenGisType() { return OpenGisType.GEOMETRYCOLLECTION; diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/CountingPointSequenceBuilder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/CountingPointSequenceBuilder.java new file mode 100644 index 0000000000..76d5a4b9a2 --- /dev/null +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/CountingPointSequenceBuilder.java @@ -0,0 +1,61 @@ +package org.hibernate.spatial.dialect.sqlserver.convertors; + +import org.geolatte.geom.*; + +/** + * @author Karel Maesen, Geovise BVBA + * creation-date: 10/19/12 + */ +public class CountingPointSequenceBuilder implements PointSequenceBuilder { + + final private PointSequenceBuilder delegate; + private int num = 0; + + public CountingPointSequenceBuilder(DimensionalFlag df) { + delegate = PointSequenceBuilders.variableSized(df); + } + + @Override + public PointSequenceBuilder add(double[] coordinates) { + num++; + return delegate.add(coordinates); + } + + @Override + public PointSequenceBuilder add(double x, double y) { + num++; + return delegate.add(x, y); + } + + @Override + public PointSequenceBuilder add(double x, double y, double zOrm) { + num++; + return delegate.add(x, y, zOrm); + } + + @Override + public PointSequenceBuilder add(double x, double y, double z, double m) { + num++; + return delegate.add(x, y, z, m); + } + + @Override + public PointSequenceBuilder add(Point pnt) { + num++; + return delegate.add(pnt); + } + + @Override + public DimensionalFlag getDimensionalFlag() { + return delegate.getDimensionalFlag(); + } + + @Override + public PointSequence toPointSequence() { + return delegate.toPointSequence(); + } + + public int getNumAdded(){ + return num; + } +} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoder.java index 2aee18b9fb..cd9c9c8572 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoder.java @@ -21,7 +21,8 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Geometry; + +import org.geolatte.geom.Geometry; /** * Decodes native database objects to geometries of type T. diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoders.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoders.java index 4cb89abe86..d5594a4185 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoders.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Decoders.java @@ -21,14 +21,11 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; +import org.geolatte.geom.Geometry; + import java.util.ArrayList; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; - -import org.hibernate.spatial.jts.JTS; -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; - /** * Decodes SQL Server Geometry objects to JTS Geometrys. * @@ -39,16 +36,14 @@ public class Decoders { final private static List> DECODERS = new ArrayList>(); static { - MGeometryFactory factory = JTS.getDefaultGeometryFactory(); - //Decoders - DECODERS.add( new PointDecoder( factory ) ); - DECODERS.add( new LineStringDecoder( factory ) ); - DECODERS.add( new PolygonDecoder( factory ) ); - DECODERS.add( new MultiLineStringDecoder( factory ) ); - DECODERS.add( new MultiPolygonDecoder( factory ) ); - DECODERS.add( new MultiPointDecoder( factory ) ); - DECODERS.add( new GeometryCollectionDecoder( factory ) ); + DECODERS.add( new PointDecoder() ); + DECODERS.add( new LineStringDecoder() ); + DECODERS.add( new PolygonDecoder() ); + DECODERS.add( new MultiLineStringDecoder( ) ); + DECODERS.add( new MultiPolygonDecoder( ) ); + DECODERS.add( new MultiPointDecoder( ) ); + DECODERS.add( new GeometryCollectionDecoder( ) ); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoder.java index c3186025b9..831333d0c1 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoder.java @@ -21,7 +21,7 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Geometry; +import org.geolatte.geom.Geometry; /** * An Encoder can encode diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoders.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoders.java index 11fdc4d470..8a15b6c2b8 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoders.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/Encoders.java @@ -24,10 +24,11 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.ArrayList; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryCollection; -import com.vividsolutions.jts.geom.MultiLineString; -import com.vividsolutions.jts.geom.MultiPolygon; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.GeometryCollection; +import org.geolatte.geom.MultiLineString; +import org.geolatte.geom.MultiPoint; +import org.geolatte.geom.MultiPolygon; /** * Serializes a JTS Geometry to a byte-array. @@ -44,7 +45,7 @@ public class Encoders { ENCODERS.add( new PointEncoder() ); ENCODERS.add( new LineStringEncoder() ); ENCODERS.add( new PolygonEncoder() ); - ENCODERS.add( new MultiPointEncoder() ); + ENCODERS.add( new GeometryCollectionEncoder(OpenGisType.MULTIPOINT) ); ENCODERS.add( new GeometryCollectionEncoder( OpenGisType.MULTILINESTRING ) ); ENCODERS.add( new GeometryCollectionEncoder( OpenGisType.MULTIPOLYGON ) ); ENCODERS.add( new GeometryCollectionEncoder( OpenGisType.GEOMETRYCOLLECTION ) ); diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionDecoder.java index 402e214a67..c4bb2f9cc5 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionDecoder.java @@ -23,10 +23,8 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryCollection; - -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.GeometryCollection; /** * Decoder for GeometryCollections. @@ -35,9 +33,6 @@ import org.hibernate.spatial.jts.mgeom.MGeometryFactory; */ class GeometryCollectionDecoder extends AbstractGeometryCollectionDecoder { - public GeometryCollectionDecoder(MGeometryFactory factory) { - super( factory ); - } @Override protected OpenGisType getOpenGisType() { @@ -46,7 +41,7 @@ class GeometryCollectionDecoder extends AbstractGeometryCollectionDecoder geometries, boolean hasM) { Geometry[] geomArray = geometries != null ? geometries.toArray( new Geometry[geometries.size()] ) : null; - return getGeometryFactory().createGeometryCollection( geomArray ); + return new GeometryCollection( geomArray ); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionEncoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionEncoder.java index c0bc7a6ea3..575ecc4808 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionEncoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/GeometryCollectionEncoder.java @@ -23,9 +23,8 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryCollection; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.GeometryCollection; /** * Encoder for GeometryCollections. @@ -45,7 +44,7 @@ class GeometryCollectionEncoder extends AbstractEn } @Override - protected void encode(Geometry geom, int parentShapeIndex, List coordinates, List
figures, List shapes) { + protected void encode(Geometry geom, int parentShapeIndex, CountingPointSequenceBuilder coordinates, List
figures, List shapes) { if ( geom.isEmpty() ) { shapes.add( new Shape( parentShapeIndex, -1, this.openGisType ) ); return; @@ -53,8 +52,12 @@ class GeometryCollectionEncoder extends AbstractEn int thisShapeIndex = shapes.size(); Shape thisShape = createShape( parentShapeIndex, figures ); shapes.add( thisShape ); - for ( int i = 0; i < geom.getNumGeometries(); i++ ) { - Geometry component = geom.getGeometryN( i ); + if (! (geom instanceof GeometryCollection)) { + throw new IllegalArgumentException( "Expect GeometryCollection argument." ); + } + GeometryCollection gc = (GeometryCollection) geom; + for ( int i = 0; i < gc.getNumGeometries(); i++ ) { + Geometry component = gc.getGeometryN( i ); encodeComponent( component, thisShapeIndex, coordinates, figures, shapes ); } } @@ -64,7 +67,7 @@ class GeometryCollectionEncoder extends AbstractEn return thisShape; } - protected void encodeComponent(Geometry geom, int thisShapeIndex, List coordinates, List
figures, List shapes) { + protected void encodeComponent(Geometry geom, int thisShapeIndex, CountingPointSequenceBuilder coordinates, List
figures, List shapes) { AbstractEncoder encoder = (AbstractEncoder) Encoders.encoderFor( geom ); encoder.encode( geom, thisShapeIndex, coordinates, figures, shapes ); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringDecoder.java index 633f9f4e11..2aa7d9a6e1 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringDecoder.java @@ -21,18 +21,12 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.CoordinateSequence; -import com.vividsolutions.jts.geom.LineString; - -import org.hibernate.spatial.jts.mgeom.MCoordinate; -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.LineString; +import org.geolatte.geom.PointSequence; +import org.geolatte.geom.crs.CrsId; class LineStringDecoder extends AbstractDecoder { - public LineStringDecoder(MGeometryFactory factory) { - super( factory ); - } @Override protected OpenGisType getOpenGisType() { @@ -40,7 +34,7 @@ class LineStringDecoder extends AbstractDecoder { } protected LineString createNullGeometry() { - return getGeometryFactory().createLineString( (CoordinateSequence) null ); + return LineString.createEmpty(); } protected LineString createGeometry(SqlServerGeometry nativeGeom) { @@ -58,19 +52,7 @@ class LineStringDecoder extends AbstractDecoder { } protected LineString createLineString(SqlServerGeometry nativeGeom, IndexRange pntIndexRange) { - Coordinate[] coordinates = nativeGeom.coordinateRange( pntIndexRange ); - return createLineString( coordinates, nativeGeom.hasMValues() ); + PointSequence coordinates = nativeGeom.coordinateRange( pntIndexRange ); + return new LineString(coordinates, CrsId.valueOf(nativeGeom.getSrid())); } - - private LineString createLineString(Coordinate[] coords, boolean hasM) { - if ( hasM ) { - return getGeometryFactory().createMLineString( (MCoordinate[]) coords ); - } - else { - return getGeometryFactory().createLineString( coords ); - } - - } - - } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringEncoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringEncoder.java index 8ff88b87f6..7c224e57c2 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringEncoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringEncoder.java @@ -23,14 +23,17 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.LineString; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.LineString; +import org.geolatte.geom.Point; +import org.geolatte.geom.PointCollection; +import org.geolatte.geom.PointSequence; + class LineStringEncoder extends AbstractEncoder { @Override - protected void encode(Geometry geom, int parentShapeIndex, List coordinates, List
figures, List shapes) { + protected void encode(Geometry geom, int parentShapeIndex, CountingPointSequenceBuilder coordinates, List
figures, List shapes) { if ( !( geom instanceof LineString ) ) { throw new IllegalArgumentException( "Require LineString geometry" ); } @@ -39,16 +42,16 @@ class LineStringEncoder extends AbstractEncoder { return; } int figureOffset = figures.size(); - int pointOffset = coordinates.size(); - for ( Coordinate coordinate : geom.getCoordinates() ) { - coordinates.add( coordinate ); + int pointOffset = coordinates.getNumAdded(); + for ( Point point : (PointSequence)geom.getPoints() ) { + coordinates.add( point ); } figures.add( new Figure( FigureAttribute.Stroke, pointOffset ) ); shapes.add( new Shape( parentShapeIndex, figureOffset, OpenGisType.LINESTRING ) ); } @Override - protected void encodePoints(SqlServerGeometry nativeGeom, List coordinates) { + protected void encodePoints(SqlServerGeometry nativeGeom, PointCollection coordinates) { super.encodePoints( nativeGeom, coordinates ); if ( coordinates.size() == 2 ) { nativeGeom.setIsSingleLineSegment(); diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiLineStringDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiLineStringDecoder.java index 66cf1a6c9b..e348f33018 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiLineStringDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiLineStringDecoder.java @@ -23,19 +23,13 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.MultiLineString; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.LineString; +import org.geolatte.geom.MultiLineString; -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; -import org.hibernate.spatial.jts.mgeom.MLineString; class MultiLineStringDecoder extends AbstractGeometryCollectionDecoder { - public MultiLineStringDecoder(MGeometryFactory factory) { - super( factory ); - } - @Override protected OpenGisType getOpenGisType() { return OpenGisType.MULTILINESTRING; @@ -44,12 +38,8 @@ class MultiLineStringDecoder extends AbstractGeometryCollectionDecoder geometries, boolean hasM) { - if ( hasM ) { - MLineString[] mlAr = geometries != null ? geometries.toArray( new MLineString[geometries.size()] ) : null; - return getGeometryFactory().createMultiMLineString( mlAr ); - } LineString[] lAr = geometries != null ? geometries.toArray( new LineString[geometries.size()] ) : null; - return getGeometryFactory().createMultiLineString( lAr ); + return new MultiLineString( lAr ); } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointDecoder.java index 14183afcf4..7625917236 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointDecoder.java @@ -23,11 +23,9 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.MultiPoint; -import com.vividsolutions.jts.geom.Point; - -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.MultiPoint; +import org.geolatte.geom.Point; /** * Decoder for GeometryCollections. @@ -37,11 +35,6 @@ import org.hibernate.spatial.jts.mgeom.MGeometryFactory; class MultiPointDecoder extends AbstractGeometryCollectionDecoder { - public MultiPointDecoder(MGeometryFactory factory) { - super( factory ); - } - - @Override protected OpenGisType getOpenGisType() { return OpenGisType.MULTIPOINT; @@ -50,7 +43,7 @@ class MultiPointDecoder extends AbstractGeometryCollectionDecoder { @Override protected MultiPoint createGeometry(List geometries, boolean hasM) { Point[] points = geometries != null ? geometries.toArray( new Point[geometries.size()] ) : null; - return getGeometryFactory().createMultiPoint( points ); + return new MultiPoint( points ); } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointEncoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointEncoder.java deleted file mode 100644 index 8dccbe2b8b..0000000000 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPointEncoder.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * This file is part of Hibernate Spatial, an extension to the - * hibernate ORM solution for spatial (geographic) data. - * - * Copyright © 2007-2012 Geovise BVBA - * - * 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 - */ - -package org.hibernate.spatial.dialect.sqlserver.convertors; - -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.MultiPoint; - -import org.hibernate.spatial.jts.mgeom.MCoordinate; - - -class MultiPointEncoder extends GeometryCollectionEncoder { - - public MultiPointEncoder() { - super( OpenGisType.MULTIPOINT ); - } - - @Override - protected boolean hasMValues(MultiPoint geom) { - for ( Coordinate c : geom.getCoordinates() ) { - if ( !( c instanceof MCoordinate ) ) { - return false; - } - if ( !Double.isNaN( ( (MCoordinate) c ).m ) ) { - return true; - } - } - return false; - } -} diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPolygonDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPolygonDecoder.java index ead521fa63..c3d2cd549f 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPolygonDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/MultiPolygonDecoder.java @@ -23,19 +23,12 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.MultiPolygon; -import com.vividsolutions.jts.geom.Polygon; - -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.MultiPolygon; +import org.geolatte.geom.Polygon; class MultiPolygonDecoder extends AbstractGeometryCollectionDecoder { - public MultiPolygonDecoder(MGeometryFactory factory) { - super( factory ); - } - - @Override protected OpenGisType getOpenGisType() { return OpenGisType.MULTIPOLYGON; @@ -44,7 +37,7 @@ class MultiPolygonDecoder extends AbstractGeometryCollectionDecoder geometries, boolean hasM) { Polygon[] polygons = geometries != null ? geometries.toArray( new Polygon[geometries.size()] ) : null; - return getGeometryFactory().createMultiPolygon( polygons ); + return new MultiPolygon( polygons ); } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/OpenGisType.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/OpenGisType.java index d6a2536382..c4ff58fa8b 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/OpenGisType.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/OpenGisType.java @@ -21,13 +21,14 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.GeometryCollection; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.MultiLineString; -import com.vividsolutions.jts.geom.MultiPoint; -import com.vividsolutions.jts.geom.MultiPolygon; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; + +import org.geolatte.geom.GeometryCollection; +import org.geolatte.geom.LineString; +import org.geolatte.geom.MultiLineString; +import org.geolatte.geom.MultiPoint; +import org.geolatte.geom.MultiPolygon; +import org.geolatte.geom.Point; +import org.geolatte.geom.Polygon; /** * The type of geometry. diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointDecoder.java index 2a328ac09c..87459ecf39 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointDecoder.java @@ -21,10 +21,11 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Point; -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.DimensionalFlag; +import org.geolatte.geom.Point; +import org.geolatte.geom.PointSequence; +import org.geolatte.geom.crs.CrsId; /** * @author Karel Maesen, Geovise BVBA. @@ -32,9 +33,6 @@ import org.hibernate.spatial.jts.mgeom.MGeometryFactory; */ class PointDecoder extends AbstractDecoder { - public PointDecoder(MGeometryFactory factory) { - super( factory ); - } @Override protected OpenGisType getOpenGisType() { @@ -42,7 +40,7 @@ class PointDecoder extends AbstractDecoder { } protected Point createNullGeometry() { - return getGeometryFactory().createPoint( (Coordinate) null ); + return Point.createEmpty(); } protected Point createGeometry(SqlServerGeometry nativeGeom) { @@ -60,8 +58,10 @@ class PointDecoder extends AbstractDecoder { } private Point createPoint(SqlServerGeometry nativeGeom, int pntOffset) { - return getGeometryFactory().createPoint( nativeGeom.getCoordinate( pntOffset ) ); - } + DimensionalFlag df = DimensionalFlag.valueOf(nativeGeom.hasZValues(), nativeGeom.hasMValues()); + PointSequence pointSequence = nativeGeom.coordinateRange(new IndexRange(pntOffset, pntOffset + 1)); + return new Point(pointSequence, CrsId.valueOf(nativeGeom.getSrid())); + } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointEncoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointEncoder.java index e2447fa094..d8333038b7 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointEncoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointEncoder.java @@ -23,11 +23,9 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; import java.util.List; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.Point; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.Point; -import org.hibernate.spatial.jts.mgeom.MCoordinate; /** * @author Karel Maesen, Geovise BVBA. @@ -48,7 +46,8 @@ class PointEncoder extends AbstractEncoder { public SqlServerGeometry encode(Point geom) { SqlServerGeometry sqlServerGeom = new SqlServerGeometry(); - sqlServerGeom.setSrid( geom.getSRID() ); + int srid = geom.getSRID(); + sqlServerGeom.setSrid( srid < 0 ? 0 : srid ); sqlServerGeom.setIsValid(); if ( geom.isEmpty() ) { @@ -61,21 +60,20 @@ class PointEncoder extends AbstractEncoder { sqlServerGeom.setIsSinglePoint(); sqlServerGeom.setNumberOfPoints( 1 ); - Coordinate coordinate = geom.getCoordinate(); - if ( is3DPoint( coordinate ) ) { + if ( geom.is3D() ) { sqlServerGeom.setHasZValues(); sqlServerGeom.allocateZValueArray(); } - if ( isMPoint( coordinate ) ) { + if ( geom.isMeasured() ) { sqlServerGeom.setHasMValues(); sqlServerGeom.allocateMValueArray(); } - sqlServerGeom.setCoordinate( 0, coordinate ); + sqlServerGeom.setCoordinate( 0, geom.getPoints() ); return sqlServerGeom; } - @Override - protected void encode(Geometry geom, int parentIdx, List coordinates, List
figures, List shapes) { + @Override + protected void encode(Geometry geom, int parentIdx, CountingPointSequenceBuilder coordinates, List
figures, List shapes) { if ( !( geom instanceof Point ) ) { throw new IllegalArgumentException( "Require Point geometry" ); } @@ -83,24 +81,15 @@ class PointEncoder extends AbstractEncoder { shapes.add( new Shape( parentIdx, -1, OpenGisType.POINT ) ); return; } - int pntOffset = coordinates.size(); + int pntOffset = coordinates.getNumAdded(); int figureOffset = figures.size(); - coordinates.add( geom.getCoordinate() ); + coordinates.add( geom.getPointN(0) ); Figure figure = new Figure( FigureAttribute.Stroke, pntOffset ); figures.add( figure ); Shape shape = new Shape( parentIdx, figureOffset, OpenGisType.POINT ); shapes.add( shape ); } - private boolean isMPoint(Coordinate coordinate) { - return ( coordinate instanceof MCoordinate ) && - !Double.isNaN( ( (MCoordinate) coordinate ).m ); - } - - private boolean is3DPoint(Coordinate coordinate) { - return !Double.isNaN( coordinate.z ); - } - public boolean accepts(Geometry geom) { return geom instanceof Point; } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonDecoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonDecoder.java index a84d3e728b..94ce79f1ca 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonDecoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonDecoder.java @@ -21,29 +21,24 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.LinearRing; -import com.vividsolutions.jts.geom.Polygon; - -import org.hibernate.spatial.jts.mgeom.MGeometryFactory; +import org.geolatte.geom.LinearRing; +import org.geolatte.geom.PointSequence; +import org.geolatte.geom.Polygon; +import org.geolatte.geom.crs.CrsId; /** * @author Karel Maesen, Geovise BVBA */ class PolygonDecoder extends AbstractDecoder { - public PolygonDecoder(MGeometryFactory factory) { - super( factory ); - } - @Override protected OpenGisType getOpenGisType() { return OpenGisType.POLYGON; } protected Polygon createNullGeometry() { - return getGeometryFactory().createPolygon( null, null ); - } + return Polygon.createEmpty(); + } protected Polygon createGeometry(SqlServerGeometry nativeGeom) { return createGeometry( nativeGeom, 0 ); @@ -55,23 +50,24 @@ class PolygonDecoder extends AbstractDecoder { } //polygons consist of one exterior ring figure, and several interior ones. IndexRange figureRange = nativeGeom.getFiguresForShape( shapeIndex ); - LinearRing[] holes = new LinearRing[figureRange.length() - 1]; - LinearRing shell = null; - for ( int figureIdx = figureRange.start, i = 0; figureIdx < figureRange.end; figureIdx++ ) { + LinearRing[] rings = new LinearRing[figureRange.length()]; + //the rings should contain all inner rings from index 1 to index length - 1 + // index = 0 should be reserved for the shell. + for ( int figureIdx = figureRange.start, i = 1; figureIdx < figureRange.end; figureIdx++ ) { IndexRange pntIndexRange = nativeGeom.getPointsForFigure( figureIdx ); if ( nativeGeom.isFigureInteriorRing( figureIdx ) ) { - holes[i++] = toLinearRing( nativeGeom, pntIndexRange ); + rings[i++] = toLinearRing( nativeGeom, pntIndexRange ); } else { - shell = toLinearRing( nativeGeom, pntIndexRange ); + rings[0] = toLinearRing( nativeGeom, pntIndexRange ); } } - return getGeometryFactory().createPolygon( shell, holes ); + return new Polygon(rings); } private LinearRing toLinearRing(SqlServerGeometry nativeGeom, IndexRange range) { - Coordinate[] coordinates = nativeGeom.coordinateRange( range ); - return getGeometryFactory().createLinearRing( coordinates ); + PointSequence pointSequence = nativeGeom.coordinateRange(range); + return new LinearRing(pointSequence, CrsId.valueOf(nativeGeom.getSrid())); } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonEncoder.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonEncoder.java index 2b15039dab..18905b7d90 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonEncoder.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/PolygonEncoder.java @@ -21,12 +21,9 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import java.util.List; +import org.geolatte.geom.*; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.Polygon; +import java.util.List; /** * Encoder for Polygons. @@ -40,7 +37,7 @@ class PolygonEncoder extends AbstractEncoder { } @Override - protected void encode(Geometry geom, int parentShapeIndex, List coordinates, List
figures, List shapes) { + protected void encode(Geometry geom, int parentShapeIndex, CountingPointSequenceBuilder coordinates, List
figures, List shapes) { if ( !( geom instanceof Polygon ) ) { throw new IllegalArgumentException( "Polygon geometry expected." ); } @@ -52,36 +49,36 @@ class PolygonEncoder extends AbstractEncoder { int figureOffset = figures.size(); shapes.add( new Shape( parentShapeIndex, figureOffset, OpenGisType.POLYGON ) ); - int pointOffset = coordinates.size(); + int pointOffset = coordinates.getNumAdded(); addExteriorRing( polygon, coordinates, figures ); addInteriorRings( polygon, coordinates, figures ); } - private void addInteriorRings(Polygon geom, List coordinates, List
figures) { + private void addInteriorRings(Polygon geom, CountingPointSequenceBuilder coordinates, List
figures) { for ( int idx = 0; idx < geom.getNumInteriorRing(); idx++ ) { addInteriorRing( geom.getInteriorRingN( idx ), coordinates, figures ); } } - private void addInteriorRing(LineString ring, List coordinates, List
figures) { - int pointOffset = coordinates.size(); + private void addInteriorRing(LineString ring,CountingPointSequenceBuilder coordinates, List
figures) { + int pointOffset = coordinates.getNumAdded(); addPoints( ring, coordinates ); Figure figure = new Figure( FigureAttribute.InteriorRing, pointOffset ); figures.add( figure ); } - private void addPoints(LineString ring, List coordinates) { - for ( Coordinate c : ring.getCoordinates() ) { + private void addPoints(LineString ring, CountingPointSequenceBuilder coordinates) { + for ( Point c : ring.getPoints() ) { coordinates.add( c ); } } - private void addExteriorRing(Polygon geom, List coordinates, List
figures) { + private void addExteriorRing(Polygon geom, CountingPointSequenceBuilder coordinates, List
figures) { LineString shell = geom.getExteriorRing(); - int offset = coordinates.size(); + int offset = coordinates.getNumAdded(); addPoints( shell, coordinates ); Figure exterior = new Figure( FigureAttribute.ExteriorRing, offset ); figures.add( exterior ); diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/SqlServerGeometry.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/SqlServerGeometry.java index e745071eb1..7197887c63 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/SqlServerGeometry.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/sqlserver/convertors/SqlServerGeometry.java @@ -25,6 +25,11 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import com.vividsolutions.jts.geom.Coordinate; +import org.geolatte.geom.DimensionalFlag; +import org.geolatte.geom.PointCollection; +import org.geolatte.geom.PointSequence; +import org.geolatte.geom.PointSequenceBuilder; +import org.geolatte.geom.PointSequenceBuilders; import org.hibernate.spatial.jts.mgeom.MCoordinate; @@ -117,22 +122,18 @@ public class SqlServerGeometry { return result; } - Coordinate getCoordinate(int index) { - Coordinate coordinate; - if ( hasMValues() ) { - coordinate = new MCoordinate(); - ( (MCoordinate) coordinate ).m = mValues[index]; - } - else { - coordinate = new Coordinate(); - } - coordinate.x = points[2 * index]; - coordinate.y = points[2 * index + 1]; - if ( hasZValues() ) { - coordinate.z = zValues[index]; - } - return coordinate; - } + void copyCoordinate(int index, double[] coords, DimensionalFlag df) { + coords[0] = points[2 * index]; + coords[1] = points[2 * index + 1]; + if (hasZValues()) { + assert(df.is3D()); + coords[df.Z] = zValues[index]; + } + if (hasMValues()) { + assert (df.isMeasured()); + coords[df.M] = mValues[index]; + } + } boolean isParentShapeOf(int parent, int child) { return getShape( child ).parentOffset == parent; @@ -186,13 +187,16 @@ public class SqlServerGeometry { return getShape( shpIdx ).openGisType; } - Coordinate[] coordinateRange(IndexRange range) { - Coordinate[] coordinates = createCoordinateArray( range.end - range.start ); - for ( int idx = range.start, i = 0; idx < range.end; idx++, i++ ) { - coordinates[i] = getCoordinate( idx ); - } - return coordinates; - } + PointSequence coordinateRange(IndexRange range) { + DimensionalFlag df = DimensionalFlag.valueOf(hasZValues(), hasMValues()); + PointSequenceBuilder psBuilder = PointSequenceBuilders.fixedSized(range.end - range.start, df); + double[] coordinates = new double[df.getCoordinateDimension()]; + for (int idx = range.start, i = 0; idx < range.end; idx++, i++) { + copyCoordinate(idx, coordinates, df); + psBuilder.add(coordinates); + } + return psBuilder.toPointSequence(); + } private Coordinate[] createCoordinateArray(int size) { if ( hasMValues() ) { @@ -212,14 +216,14 @@ public class SqlServerGeometry { return shapes[index]; } - void setCoordinate(int index, Coordinate coordinate) { - points[2 * index] = coordinate.x; - points[2 * index + 1] = coordinate.y; + void setCoordinate(int index, PointCollection coordinate) { + points[2 * index] = coordinate.getX(index); + points[2 * index + 1] = coordinate.getY(index); if ( hasZValues() ) { - zValues[index] = coordinate.z; + zValues[index] = coordinate.getZ(index); } if ( hasMValues() ) { - mValues[index] = ( (MCoordinate) coordinate ).m; + mValues[index] = coordinate.getM(index); } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractConvertorTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractConvertorTest.java index 7c05aa8777..9d3c12d2bb 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractConvertorTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/AbstractConvertorTest.java @@ -31,7 +31,10 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import com.vividsolutions.jts.geom.Geometry; +import org.geolatte.geom.Geometry; +import org.geolatte.geom.PointCollection; +import org.geolatte.geom.PointCollectionEquality; +import org.geolatte.geom.PointCollectionPointEquality; import org.hibernate.spatial.Log; import org.hibernate.spatial.LogFactory; @@ -41,6 +44,7 @@ import org.hibernate.spatial.testing.TestData; import org.hibernate.spatial.testing.TestSupport; import org.hibernate.spatial.testing.dialects.sqlserver.SQLServerExpressionTemplate; import org.hibernate.spatial.testing.dialects.sqlserver.SQLServerTestSupport; +import org.hibernate.testing.AfterClassOnce; import static org.junit.Assert.assertTrue; @@ -54,7 +58,8 @@ public abstract class AbstractConvertorTest extends SpatialFunctionalTestCase { private final static TestSupport support = new SQLServerTestSupport(); - private DataSourceUtils dataSourceUtils; + protected PointCollectionEquality pointCollectionEquality = new PointCollectionPointEquality(); + Map decodedGeoms; Map rawResults; @@ -79,17 +84,6 @@ public abstract class AbstractConvertorTest extends SpatialFunctionalTestCase { throw new RuntimeException( e ); } } -// -// public void afterClass() { -// try { -// String sql = dataSourceUtils.parseSqlIn("sqlserver/drop-sqlserver-test-schema.sql"); -// dataSourceUtils.executeStatement(sql); -// } catch (SQLException e) { -// throw new RuntimeException(e); -// } catch (IOException e) { -// throw new RuntimeException(e); -// } -// } public void doDecoding(OpenGisType type) { rawResults = dataSourceUtils.rawDbObjects( type.toString() ); @@ -107,7 +101,7 @@ public abstract class AbstractConvertorTest extends SpatialFunctionalTestCase { encodedGeoms = new HashMap(); for ( Integer id : decodedGeoms.keySet() ) { Geometry geom = decodedGeoms.get( id ); - byte[] bytes = Encoders.encode( geom ); + byte[] bytes = Encoders.encode(geom); encodedGeoms.put( id, bytes ); } } @@ -125,10 +119,31 @@ public abstract class AbstractConvertorTest extends SpatialFunctionalTestCase { for ( Integer id : decodedGeoms.keySet() ) { Geometry expected = expectedGeoms.get( id ); Geometry received = decodedGeoms.get( id ); - assertTrue( "Wrong decoding for case " + id, expected.equalsExact( received ) ); + assertTrue( "Wrong decoding for case " + id, expected.equals( received ) ); } } + @AfterClassOnce + public void afterClassOnce(){ + try { + String sql = dataSourceUtils.parseSqlIn( "sqlserver/drop-sqlserver-test-schema.sql" ); + dataSourceUtils.executeStatement( sql ); + } + catch ( SQLException e ) { + throw new RuntimeException( e ); + } + catch ( IOException e ) { + throw new RuntimeException( e ); + } + } + + public void prepareTest() { + } + + public void assertPointCollectionEquality(PointCollection received, PointCollection expected) { + assertTrue( pointCollectionEquality.equals( received, expected )); + } + @Override protected Log getLogger() { return LOG; diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringConvertorTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringConvertorTest.java index 0feb9d596e..bc84e88d97 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringConvertorTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/LineStringConvertorTest.java @@ -21,17 +21,18 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.LineString; + +import org.geolatte.geom.DimensionalFlag; +import org.geolatte.geom.LineString; +import org.geolatte.geom.PointCollection; +import org.geolatte.geom.PointSequenceBuilders; import org.junit.Test; import org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect; -import org.hibernate.spatial.jts.mgeom.MCoordinate; import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.RequiresDialect; import static junit.framework.Assert.assertEquals; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; @RequiresDialect(SqlServer2008SpatialDialect.class) @@ -65,53 +66,28 @@ public class LineStringConvertorTest extends AbstractConvertorTest { @Test public void test_coordinates() { - Coordinate[] received = decodedGeoms.get( 5 ).getCoordinates(); - MCoordinate[] expected = new MCoordinate[] { - new MCoordinate( 10.0, 5.0 ), - new MCoordinate( 20.0, 15.0 ) - }; - assertArrayEquals( received, expected ); + PointCollection received = decodedGeoms.get( 5 ).getPoints(); + PointCollection expected = PointSequenceBuilders.fixedSized( 2, DimensionalFlag.XY ).add(10, 5).add(20,15).toPointSequence(); + assertPointCollectionEquality( received, expected ); - received = decodedGeoms.get( 6 ).getCoordinates(); - expected = new MCoordinate[] { - new MCoordinate( 10.0, 5.0 ), - new MCoordinate( 20.0, 15.0 ), - new MCoordinate( 30.3, 22.4 ), - new MCoordinate( 10.0, 30.0 ) - }; - assertArrayEquals( expected, received ); + received = decodedGeoms.get( 6 ).getPoints(); + expected = PointSequenceBuilders.fixedSized( 4, DimensionalFlag.XY).add(10,5).add(20,15).add(30.3, 22.4).add(10,30).toPointSequence(); + assertPointCollectionEquality( received, expected ); - received = decodedGeoms.get( 7 ).getCoordinates(); - expected = new MCoordinate[] { - new MCoordinate( 10.0, 5.0 ), - new MCoordinate( 20.0, 15.0 ) - }; - expected[0].z = 0; - expected[1].z = 3; - assertArrayEquals( expected, received ); + + received = decodedGeoms.get( 7 ).getPoints(); + expected = PointSequenceBuilders.fixedSized( 2, DimensionalFlag.XYZ).add(10,5,0).add(20,15,3).toPointSequence(); + assertPointCollectionEquality( received, expected ); //case 9 - received = decodedGeoms.get( 9 ).getCoordinates(); - expected = new MCoordinate[] { - new MCoordinate( 10, 5 ), - new MCoordinate( 20, 15 ), - new MCoordinate( 30.3, 22.4 ), - new MCoordinate( 10, 30 ) - }; - expected[0].z = 1; - expected[1].z = 2; - expected[2].z = 5; - expected[3].z = 2; - assertArrayEquals( expected, received ); + received = decodedGeoms.get( 9 ).getPoints(); + expected = PointSequenceBuilders.fixedSized( 4, DimensionalFlag.XYZ).add(10,5,1).add(20,15,2).add(30.3, 22.4,5).add(10,30,2).toPointSequence(); + assertPointCollectionEquality( received, expected ); //case 10 - received = decodedGeoms.get( 10 ).getCoordinates(); - expected[0].m = 1; - expected[1].m = 3; - expected[2].m = 10; - expected[3].m = 12; - assertArrayEquals( expected, received ); - + received = decodedGeoms.get( 10 ).getPoints(); + expected = PointSequenceBuilders.fixedSized( 4, DimensionalFlag.XYZM).add(10,5,1,1).add(20,15,2,3).add(30.3, 22.4,5,10).add(10,30,2,12).toPointSequence(); + assertPointCollectionEquality( received, expected ); } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointConvertorTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointConvertorTest.java index 5f463b6214..9b22498a69 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointConvertorTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/sqlserver/convertors/PointConvertorTest.java @@ -21,12 +21,12 @@ package org.hibernate.spatial.dialect.sqlserver.convertors; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Point; +import org.geolatte.geom.Point; +import org.geolatte.geom.Points; +import org.geolatte.geom.crs.CrsId; import org.junit.Test; import org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect; -import org.hibernate.spatial.jts.mgeom.MCoordinate; import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.RequiresDialect; @@ -48,7 +48,7 @@ public class PointConvertorTest extends AbstractConvertorTest { @Test public void test_verify_srid() { - assertEquals( 0, decodedGeoms.get( 1 ).getSRID() ); + assertEquals( -1, decodedGeoms.get( 1 ).getSRID() ); assertEquals( 4326, decodedGeoms.get( 2 ).getSRID() ); assertEquals( 31370, decodedGeoms.get( 3 ).getSRID() ); } @@ -62,16 +62,15 @@ public class PointConvertorTest extends AbstractConvertorTest { @Test public void test_coordinates() { - Coordinate expected; - Coordinate received; - expected = new Coordinate( 10.0, 5.0 ); - assertEquals( expected, decodedGeoms.get( 1 ).getCoordinate() ); - expected = new Coordinate( 52.25, 2.53 ); - assertEquals( expected, decodedGeoms.get( 2 ).getCoordinate() ); - expected = new Coordinate( 150000.0, 200000.0 ); - assertEquals( expected, decodedGeoms.get( 3 ).getCoordinate() ); - expected = new MCoordinate( 10.0, 2.0, 1.0, 3.0 ); - assertEquals( expected, decodedGeoms.get( 4 ).getCoordinate() ); + Point expected; + expected = Points.create( 10.0, 5.0); + assertEquals( expected, decodedGeoms.get( 1 ).getPointN(0) ); + expected = Points.create(52.25, 2.53, CrsId.valueOf(4326)); + assertEquals( expected, decodedGeoms.get( 2 ).getPointN( 0 ) ); + expected = Points.create(150000.0, 200000.0, CrsId.valueOf(31370)); + assertEquals( expected, decodedGeoms.get( 3 ).getPointN( 0 ) ); + expected = Points.create(10.0, 2.0, 1.0, 3.0, CrsId.valueOf(4326)); + assertEquals( expected, decodedGeoms.get( 4 ).getPointN( 0 ) ); } @Test diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/DataSourceUtils.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/DataSourceUtils.java index 3fd90c4ae8..1dba9a40a4 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/DataSourceUtils.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/DataSourceUtils.java @@ -21,32 +21,21 @@ package org.hibernate.spatial.testing; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.StringWriter; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import javax.sql.DataSource; - -import com.vividsolutions.jts.geom.Geometry; import org.apache.commons.dbcp.BasicDataSource; +import org.geolatte.geom.Geometry; import org.geolatte.geom.codec.Wkt; import org.geolatte.geom.codec.WktDecodeException; import org.geolatte.geom.codec.WktDecoder; -import org.geolatte.geom.jts.JTS; - import org.hibernate.spatial.Log; import org.hibernate.spatial.LogFactory; +import javax.sql.DataSource; +import java.io.*; +import java.sql.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + /** *

Unit testsuite-suite support class.

* @@ -221,8 +210,7 @@ public class DataSourceUtils { cn.commit(); stmt.close(); LOG.info( "Loaded " + sum( insCounts ) + " rows." ); - } - finally { + }finally { try { if ( cn != null ) { cn.close(); @@ -363,7 +351,9 @@ public class DataSourceUtils { for ( TestDataElement testDataElement : testData ) { if ( testDataElement.type.equalsIgnoreCase( type ) ) { try { - result.put( testDataElement.id, JTS.to( decoder.decode( testDataElement.wkt ) ) ); + //to ensure expected geometries have the correct SRID, we prepend to the WKT string + String wkt = "SRID=" + testDataElement.srid + ";"+testDataElement.wkt; + result.put( testDataElement.id, decoder.decode( wkt ) ); } catch ( WktDecodeException e ) { System.out diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/GeometryEquality.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/GeometryEquality.java index b74cb9b0df..b7fe6108c4 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/GeometryEquality.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/GeometryEquality.java @@ -36,12 +36,18 @@ import org.hibernate.spatial.jts.mgeom.MCoordinate; public class GeometryEquality { public boolean test(Geometry geom1, Geometry geom2) { + return test( geom1, geom2, false ); + } + + private boolean test(Geometry geom1, Geometry geom2, boolean ignoreSRID) { if ( geom1 == null ) { return geom2 == null; } if ( geom1.isEmpty() ) { - return geom2.isEmpty() && equalSRID( geom1, geom2 ); + return geom2.isEmpty(); } + if (!ignoreSRID && !equalSRID(geom1, geom2)) return false; + if ( geom1 instanceof GeometryCollection ) { if ( !( geom2 instanceof GeometryCollection ) ) { return false; @@ -51,7 +57,7 @@ public class GeometryEquality { for ( int partIndex = 0; partIndex < expectedCollection.getNumGeometries(); partIndex++ ) { Geometry partExpected = expectedCollection.getGeometryN( partIndex ); Geometry partReceived = receivedCollection.getGeometryN( partIndex ); - if ( !test( partExpected, partReceived ) ) { + if ( !test( partExpected, partReceived, true ) ) { return false; } } @@ -71,7 +77,7 @@ public class GeometryEquality { */ private boolean equalSRID(Geometry geom1, Geometry geom2) { return geom1.getSRID() == geom2.getSRID() || - ( geom1.getSRID() <1 && geom2.getSRID() < 1); + ( geom1.getSRID() < 1 && geom2.getSRID() < 1); } /** @@ -84,7 +90,7 @@ public class GeometryEquality { */ protected boolean testSimpleGeometryEquality(Geometry geom1, Geometry geom2) { //return geom1.equals(geom2); - return testTypeAndVertexEquality( geom1, geom2 ) && equalSRID( geom1, geom2 ); + return testTypeAndVertexEquality( geom1, geom2); } protected boolean testTypeAndVertexEquality(Geometry geom1, Geometry geom2) { diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/sqlserver/SqlServerExpectationsFactory.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/sqlserver/SqlServerExpectationsFactory.java index 2bbe030d91..692be6624a 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/sqlserver/SqlServerExpectationsFactory.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/sqlserver/SqlServerExpectationsFactory.java @@ -23,6 +23,7 @@ package org.hibernate.spatial.testing.dialects.sqlserver; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Point; +import org.geolatte.geom.jts.JTS; import org.hibernate.spatial.dialect.sqlserver.convertors.Decoders; import org.hibernate.spatial.testing.AbstractExpectationsFactory; @@ -141,7 +142,7 @@ public class SqlServerExpectationsFactory extends AbstractExpectationsFactory { @Override protected Geometry decode(Object o) { - return Decoders.decode( (byte[]) o ); + return JTS.to( Decoders.decode( (byte[]) o ) ); } @Override diff --git a/hibernate-spatial/src/test/resources/hibernate.properties b/hibernate-spatial/src/test/resources/hibernate.properties index 02987d5f93..857fcdbe35 100644 --- a/hibernate-spatial/src/test/resources/hibernate.properties +++ b/hibernate-spatial/src/test/resources/hibernate.properties @@ -30,10 +30,10 @@ hibernate.show_sql false hibernate.max_fetch_depth 5 -hibernate.dialect org.hibernate.dialect.H2Dialect -hibernate.connection.driver_class org.h2.Driver -hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE -hibernate.connection.username sa +#hibernate.dialect org.hibernate.dialect.H2Dialect +#hibernate.connection.driver_class org.h2.Driver +#hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE +#hibernate.connection.username sa #hibernate.cache.region_prefix hibernate.test #hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory @@ -58,12 +58,12 @@ hibernate.connection.username sa #hibernate.connection.password hbs -#hibernate.dialect org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect -#hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver -#hibernate.connection.url jdbc:sqlserver://192.168.0.100:1433;databaseName=HBS -#hibernate.connection.username hbs -#hibernate.connection.password hbs -#hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory +hibernate.dialect org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect +hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver +hibernate.connection.url jdbc:sqlserver://sqlserver.geovise.com:1433;databaseName=HBS +hibernate.connection.username hbs +hibernate.connection.password hbs + ## ## MySQL 5 dialects diff --git a/hibernate-spatial/src/test/resources/sqlserver/hibernate-spatial-sqlserver-test.properties b/hibernate-spatial/src/test/resources/sqlserver/hibernate-spatial-sqlserver-test.properties index 47d33b3dc2..01a8a471eb 100644 --- a/hibernate-spatial/src/test/resources/sqlserver/hibernate-spatial-sqlserver-test.properties +++ b/hibernate-spatial/src/test/resources/sqlserver/hibernate-spatial-sqlserver-test.properties @@ -2,7 +2,7 @@ # $Id: hibernate-spatial-sqlserver-test.properties 182 2010-03-20 17:30:12Z maesenka $ # -jdbcUrl = jdbc:sqlserver://192.168.0.100:1433;databaseName=HBS +jdbcUrl = jdbc:sqlserver://sqlserver.geovise.com:1433;databaseName=HBS jdbcDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver jdbcUser = hbs jdbcPass = hbs diff --git a/hibernate-spatial/src/test/resources/test-data-set.xml b/hibernate-spatial/src/test/resources/test-data-set.xml index 95cba85ade..93464f5e20 100644 --- a/hibernate-spatial/src/test/resources/test-data-set.xml +++ b/hibernate-spatial/src/test/resources/test-data-set.xml @@ -255,43 +255,43 @@ 50 POINT POINT EMPTY - 4326 + 0 51 LINESTRING LINESTRING EMPTY - 4326 + 0 52 POLYGON POLYGON EMPTY - 4326 + 0 53 MULTIPOINT MULTIPOINT EMPTY - 4326 + 0 54 MULTILINESTRING MULTILINESTRING EMPTY - 4326 + 0 55 MULTIPOLYGON MULTIPOLYGON EMPTY - 4326 + 0 56 GEOMETRYCOLLECTION GEOMETRYCOLLECTION EMPTY - 4326 + 0