Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 155 → Rev 156

/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/test/TestHQL.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
397,12 → 397,12
System.out.println(i + ": ASSERTION FAILED.");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
 
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
session.close();
}
/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/GeometryCollectionConvertorTest.java
New file
0,0 → 1,56
/*
* $Id:$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 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
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import org.junit.Before;
import org.junit.Test;
 
/**
* Created by IntelliJ IDEA.
* User: maesenka
* Date: Jan 24, 2010
* Time: 5:33:19 PM
* To change this template use File | Settings | File Templates.
*/
public class GeometryCollectionConvertorTest extends AbstractConvertorTest{
 
@Before
public void setUp() {
doDecoding(OpenGisType.GEOMETRYCOLLECTION);
doEncoding();
}
 
@Test
public void test_encoding() {
super.test_encoding();
}
 
@Test
public void test_decoding() {
super.test_decoding();
}
 
}
/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/GeometryTestCases.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
40,6 → 40,7
TEST_WKTS.add(new TestWKT(2, "POINT", "POINT(52.25 2.53)", 4326));
TEST_WKTS.add(new TestWKT(3, "POINT", "POINT(150000 200000)", 31370));
TEST_WKTS.add(new TestWKT(4, "POINT", "POINT(10.0 2.0 1.0 3.0)", 4326));
 
//LINESTRING test cases
TEST_WKTS.add(new TestWKT(5, "LINESTRING", "LINESTRING(10.0 5.0, 20.0 15.0)", 4326));
TEST_WKTS.add(new TestWKT(6, "LINESTRING", "LINESTRING(10.0 5.0, 20.0 15.0, 30.3 22.4, 10 30.0)", 4326));
48,6 → 49,7
TEST_WKTS.add(new TestWKT(9, "LINESTRING", "LINESTRING(10.0 5.0 1, 20.0 15.0 2, 30.3 22.4 5, 10 30.0 2)", 4326));
TEST_WKTS.add(new TestWKT(10, "LINESTRING",
"LINESTRING(10.0 5.0 1 1, 20.0 15.0 2 3, 30.3 22.4 5 10, 10 30.0 2 12)", 4326));
 
//MULTILINESTRING test cases
TEST_WKTS.add(new TestWKT(11, "MULTILINESTRING",
"MULTILINESTRING((10.0 5.0, 20.0 15.0),( 25.0 30.0, 30.0 20.0))", 4326));
60,7 → 62,7
TEST_WKTS.add(new TestWKT(15, "MULTILINESTRING",
"MULTILINESTRING((10.0 5.0 1.0, 20.0 15.0 2.0 0.0, 30.3 22.4 1.0 1.0, 10 30.0 1.0 2.0))", 4326));
 
//Polygon
//Polygon test cases
TEST_WKTS.add(new TestWKT(16, "POLYGON",
"POLYGON( (0 0, 0 10, 10 10, 10 0, 0 0) )", 4326));
TEST_WKTS.add(new TestWKT(17, "POLYGON",
69,7 → 71,7
"POLYGON( (0 0, 0 10, 10 10, 10 0, 0 0), " +
"(2 2, 2 5, 5 5,5 2, 2 2))", 4326));
 
//MULTIPOLYGON
//MULTIPOLYGON test cases
TEST_WKTS.add(new TestWKT(20, "MULTIPOLYGON",
"MULTIPOLYGON( ((10 20, 30 40, 44 50, 10 20)), ((5 0, 20 40, 30 34, 5 0)) )", 4326));
TEST_WKTS.add(new TestWKT(21, "MULTIPOLYGON",
79,12 → 81,16
"( (0 0, 0 50, 50 50, 0 0), (10 10, 10 20, 20 20, 20 10, 10 10) ), " +
"((5 0, 20 40, 30 34, 5 0)) )", 4326));
 
//MultiPoint
//MultiPoint test cases
TEST_WKTS.add(new TestWKT(25, "MULTIPOINT", "MULTIPOINT(21 2, 25 5, 30 3)", 4326));
TEST_WKTS.add(new TestWKT(26, "MULTIPOINT", "MULTIPOINT(21 2)", 4326));
TEST_WKTS.add(new TestWKT(27, "MULTIPOINT", "MULTIPOINT(21 2 1, 25 5 2, 30 3 5)", 4326));
TEST_WKTS.add(new TestWKT(28, "MULTIPOINT", "MULTIPOINT(21 2 1 0, 25 5 2 4, 30 3 5 2)", 4326));
 
//GeometryCollection test cases
TEST_WKTS.add(new TestWKT(30, "GEOMETRYCOLLECTION", "GEOMETRYCOLLECTION(POINT(4 0), LINESTRING(4 2, 5 3))",4326));
TEST_WKTS.add(new TestWKT(31, "GEOMETRYCOLLECTION", "GEOMETRYCOLLECTION(POINT(4 0), LINESTRING(4 2, 5 3), POLYGON((0 0, 3 0, 3 3,0 3, 0 0)))",4326));
 
 
}
}
/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/DataSourceUtils.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
176,7 → 176,6
return result;
}
 
 
private static int sum(int[] insCounts) {
int result = 0;
for (int idx = 0; idx < insCounts.length; idx++) {
185,5 → 184,4
return result;
}
 
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiPolygonEncoder.java
File deleted
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiPointEncoder.java
File deleted
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiLineStringEncoder.java
File deleted
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Encoders.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
25,7 → 25,7
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.*;
 
import java.util.ArrayList;
import java.util.List;
43,14 → 43,15
//Encoders
ENCODERS.add(new PointEncoder());
ENCODERS.add(new LineStringEncoder());
ENCODERS.add(new MultiLineStringEncoder());
ENCODERS.add(new PolygonEncoder());
ENCODERS.add(new MultiPolygonEncoder());
ENCODERS.add(new MultiPointEncoder());
ENCODERS.add(new GeometryCollectionEncoder<MultiPoint>(OpenGisType.MULTIPOINT));
ENCODERS.add(new GeometryCollectionEncoder<MultiLineString>(OpenGisType.MULTILINESTRING));
ENCODERS.add(new GeometryCollectionEncoder<MultiPolygon>(OpenGisType.MULTIPOLYGON));
ENCODERS.add(new GeometryCollectionEncoder<GeometryCollection>(OpenGisType.GEOMETRYCOLLECTION));
 
}
 
private static Encoder<? extends Geometry> encoderFor(Geometry geom) {
public static Encoder<? extends Geometry> encoderFor(Geometry geom) {
for (Encoder<? extends Geometry> encoder : ENCODERS) {
if (encoder.accepts(geom))
return encoder;
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/AbstractEncoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
29,42 → 29,80
import com.vividsolutions.jts.geom.Geometry;
import org.hibernatespatial.mgeom.MGeometry;
 
import java.util.ArrayList;
import java.util.List;
 
 
//TODO -- determining whether geometry has Z- or M-Values should be more straightforward.
 
abstract class AbstractEncoder<G extends Geometry> implements Encoder<G> {
 
public SqlGeometryV1 encode(G geom) {
SqlGeometryV1 nativeGeom = new SqlGeometryV1();
nativeGeom.setSrid(geom.getSRID());
if (geom.isValid()) nativeGeom.setIsValid();
nativeGeom.setNumberOfPoints(geom.getNumPoints());
if (hasMValues(geom))
 
if (hasMValues(geom)) {
nativeGeom.setHasMValues();
encodePoints(nativeGeom, geom);
encodeFigures(nativeGeom, geom);
encodeShapes(nativeGeom, geom);
}
 
List<Coordinate> coordinates = new ArrayList<Coordinate>();
List<Figure> figures = new ArrayList<Figure>();
List<Shape> shapes = new ArrayList<Shape>();
 
encode(geom, -1, coordinates, figures, shapes);
encodePoints(nativeGeom,coordinates);
encodeFigures(nativeGeom,figures);
encodeShapes(nativeGeom, shapes);
 
return nativeGeom;
}
 
/**
* Appends the points, figures, shapes to the resp. lists
*
* @param geom geometry to serialization
* @param parentShapeIndex index of the parent Shape for the geometry
* @param coordinates coordinate list to append to
* @param figures figure list to append to
* @param shapes shape list to append to
*/
protected abstract void encode(Geometry geom, int parentShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes);
 
protected void encodeShapes(SqlGeometryV1 nativeGeom, List<Shape> shapes) {
nativeGeom.setNumberOfShapes(shapes.size());
for (int i = 0; i < shapes.size();i++){
nativeGeom.setShape(i,shapes.get(i));
}
}
 
protected void encodeFigures(SqlGeometryV1 nativeGeom, List<Figure> figures) {
nativeGeom.setNumberOfFigures(figures.size());
for (int i = 0; i < figures.size();i++){
nativeGeom.setFigure(i,figures.get(i));
}
}
 
protected boolean hasMValues(G geom) {
return geom instanceof MGeometry;
}
 
protected void encodePoints(SqlGeometryV1 nativeGeom, Geometry geom) {
 
Coordinate[] coords = geom.getCoordinates();
for (int i = 0; i < coords.length; i++) {
setCoordinate(nativeGeom, i, coords[i]);
protected void encodePoints(SqlGeometryV1 nativeGeom, List<Coordinate> coordinates) {
nativeGeom.setNumberOfPoints(coordinates.size());
nativeGeom.allocateMValueArray();
for (int i = 0; i < coordinates.size(); i++) {
setCoordinate(nativeGeom, i, coordinates.get(i));
}
}
 
protected void setCoordinate(SqlGeometryV1 nativeGeom, int idx, Coordinate coordinate) {
if (!nativeGeom.hasZValues() && !Double.isNaN(coordinate.z)) {
nativeGeom.setHasZValues();
nativeGeom.allocateZValueArray();
}
 
nativeGeom.setCoordinate(idx, coordinate);
}
 
abstract protected void encodeFigures(SqlGeometryV1 nativeGeom, G geom);
 
abstract protected void encodeShapes(SqlGeometryV1 nativeGeom, G geom);
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/LineStringDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
32,8 → 32,9
 
class LineStringDecoder extends AbstractDecoder<LineString> {
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return nativeGeom.openGisType() == OpenGisType.LINESTRING;
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.LINESTRING;
}
 
protected LineString createNullGeometry() {
44,12 → 45,18
return createLineString(nativeGeom, 0, nativeGeom.getNumPoints());
}
 
@Override
protected LineString createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
int figureOffset = nativeGeom.getStartFigureForShape(shapeIndex);
//linestring shapes have exactly one figure
int startOffset = nativeGeom.getStartPointForFigure(figureOffset);
int endOffset = nativeGeom.getEndPointForFigure(figureOffset);
return createLineString(nativeGeom, startOffset, endOffset);
}
 
protected LineString createLineString(SqlGeometryV1 nativeGeom, int offset, int nextOffset) {
Coordinate[] coords = createCoordinateArray(nextOffset - offset, nativeGeom);
for (int idx = offset, i = 0; idx < nextOffset; idx++, i++) {
coords[i] = nativeGeom.getCoordinate(idx);
}
return createLineString(coords, nativeGeom);
Coordinate[] coordinates = nativeGeom.coordinateRange(offset, nextOffset);
return createLineString(coordinates, nativeGeom);
}
 
private LineString createLineString(Coordinate[] coords, SqlGeometryV1 nativeGeom) {
61,12 → 68,5
 
}
 
private Coordinate[] createCoordinateArray(int size, SqlGeometryV1 nativeGeom) {
if (nativeGeom.hasMValues()) {
return new MCoordinate[size];
} else {
return new Coordinate[size];
}
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Decoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
37,5 → 37,7
 
boolean accepts(SqlGeometryV1 nativeGeom);
 
boolean accepts(OpenGisType type);
 
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/OpenGisType.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
25,26 → 25,34
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.*;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
enum OpenGisType {
POINT((byte) 1),
LINESTRING((byte) 2),
POLYGON((byte) 3),
MULTIPOINT((byte) 4),
MULTILINESTRING((byte) 5),
MULTIPOLYGON((byte) 6),
GEOMETRYCOLLECTION((byte) 7),
INVALID_TYPE((byte) 0);
POINT((byte) 1, Point.class),
LINESTRING((byte) 2, LineString.class),
POLYGON((byte) 3, Polygon.class),
MULTIPOINT((byte) 4, MultiPoint.class),
MULTILINESTRING((byte) 5, MultiLineString.class),
MULTIPOLYGON((byte) 6, MultiPolygon.class),
GEOMETRYCOLLECTION((byte) 7, GeometryCollection.class),
INVALID_TYPE((byte) 0, null);
 
final byte byteValue;
final Class geomClass;
 
OpenGisType(byte v) {
byteValue = v;
OpenGisType(byte v, Class geomClass) {
this.byteValue = v;
this.geomClass = geomClass;
}
 
boolean typeOf(Object o){
return geomClass.isAssignableFrom(o.getClass());
}
 
static OpenGisType valueOf(byte b) {
for (OpenGisType t : values()) {
if (t.byteValue == b) return t;
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/GeometryCollectionEncoder.java
New file
0,0 → 1,69
/*
* $Id:$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 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
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
 
import java.util.List;
 
/**
* @Author Karel Maesen
*/
public class GeometryCollectionEncoder<T extends GeometryCollection> extends AbstractEncoder<T> {
 
private final OpenGisType openGisType;
 
GeometryCollectionEncoder(OpenGisType openGisType){
this.openGisType = openGisType;
}
 
public boolean accepts(Geometry geom) {
return this.openGisType.typeOf(geom);
}
 
@Override
protected void encode(Geometry geom, int parentShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
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);
encodeComponent(component, thisShapeIndex, coordinates, figures, shapes);
}
}
 
protected Shape createShape(int parentShapeIndex, List<Figure> figures) {
Shape thisShape = new Shape(parentShapeIndex, figures.size(), this.openGisType);
return thisShape;
}
 
protected void encodeComponent(Geometry geom, int thisShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
AbstractEncoder<? extends Geometry> encoder = (AbstractEncoder<? extends Geometry>) Encoders.encoderFor(geom);
encoder.encode(geom, thisShapeIndex, coordinates, figures, shapes);
}
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PolygonDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
31,8 → 31,9
 
public class PolygonDecoder extends AbstractDecoder<Polygon> {
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return nativeGeom.openGisType().equals(OpenGisType.POLYGON);
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.POLYGON;
}
 
protected Polygon createNullGeometry() {
40,41 → 41,30
}
 
protected Polygon createGeometry(SqlGeometryV1 nativeGeom) {
return createGeometry(nativeGeom, 0, nativeGeom.getNumFigures());
return createGeometry(nativeGeom, 0);
}
 
protected Polygon createGeometry(SqlGeometryV1 nativeGeom, int startFigureOffset, int nextShapeFigureOffset) {
protected Polygon createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
//polygons consist of one exterior ring figure, and several interior ones.
LinearRing[] holes = new LinearRing[nextShapeFigureOffset - startFigureOffset - 1];
int startFigure = nativeGeom.getStartFigureForShape(shapeIndex);
int endFigure = nativeGeom.getEndFigureForShape(shapeIndex);
LinearRing[] holes = new LinearRing[endFigure - startFigure - 1];
LinearRing shell = null;
for (int figureIdx = startFigureOffset, i = 0; figureIdx < nextShapeFigureOffset; figureIdx++) {
Figure figure = nativeGeom.getFigure(figureIdx);
int nextPntOffset = getNextPointOffset(nativeGeom, figureIdx);
if (figure.isInteriorRing()) {
holes[i++] = toLinearRing(nativeGeom, figure.pointOffset, nextPntOffset);
for (int figureIdx = startFigure, i = 0; figureIdx < endFigure; figureIdx++) {
int startPnt = nativeGeom.getStartPointForFigure(figureIdx);
int endPnt = nativeGeom.getEndPointForFigure(figureIdx);
if (nativeGeom.isFigureInteriorRing(figureIdx)) {
holes[i++] = toLinearRing(nativeGeom, startPnt, endPnt);
} else {
shell = toLinearRing(nativeGeom, figure.pointOffset, nextPntOffset);
shell = toLinearRing(nativeGeom, startPnt, endPnt);
}
}
return getGeometryFactory().createPolygon(shell, holes);
 
}
 
private LinearRing toLinearRing(SqlGeometryV1 nativeGeom, int pointOffset, int nextPntOffset) {
Coordinate[] coordinates = new Coordinate[nextPntOffset - pointOffset];
for (int pIdx = pointOffset, cIdx = 0; pIdx < nextPntOffset; pIdx++, cIdx++) {
coordinates[cIdx] = nativeGeom.getCoordinate(pIdx);
}
Coordinate[] coordinates = nativeGeom.coordinateRange(pointOffset, nextPntOffset);
return getGeometryFactory().createLinearRing(coordinates);
}
 
 
private int getNextPointOffset(SqlGeometryV1 nativeGeom, int figureIndex) {
int next = figureIndex + 1;
if (next == nativeGeom.getNumFigures()) {
return nativeGeom.getNumPoints();
}
return nativeGeom.getFigure(next).pointOffset;
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/LineStringEncoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
29,47 → 29,31
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
 
import java.util.List;
 
class LineStringEncoder extends AbstractEncoder<LineString> {
 
private final static Figure LINESTRING_FIGURE = new Figure(FigureAttribute.Stroke, 0);
 
private final static Shape LINESTRING_SHAPE = new Shape(-1, 0, OpenGisType.LINESTRING);
 
 
protected void encodePoints(SqlGeometryV1 nativeGeom, Geometry geom) {
if (geom.getNumPoints() == 2) {
encodeSingleLineSegment(nativeGeom, geom);
} else {
super.encodePoints(nativeGeom, geom);
@Override
protected void encode(Geometry geom, int parentShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
if (! (geom instanceof LineString)) throw new IllegalArgumentException("Require LineString geometry");
int figureOffset = figures.size();
int pointOffset = coordinates.size();
for (Coordinate coordinate : geom.getCoordinates()) {
coordinates.add(coordinate);
}
figures.add(new Figure(FigureAttribute.Stroke, pointOffset));
shapes.add(new Shape(parentShapeIndex, figureOffset, OpenGisType.LINESTRING));
}
 
private void encodeSingleLineSegment(SqlGeometryV1 nativeGeom, Geometry geom) {
nativeGeom.setIsSingleLineSegment();
Coordinate[] coords = geom.getCoordinates();
setCoordinate(nativeGeom, 0, coords[0]);
setCoordinate(nativeGeom, 1, coords[1]);
}
 
protected void setCoordinate(SqlGeometryV1 nativeGeom, int idx, Coordinate coordinate) {
if (!nativeGeom.hasZValues() && !Double.isNaN(coordinate.z)) {
nativeGeom.setHasZValues();
@Override
protected void encodePoints(SqlGeometryV1 nativeGeom, List<Coordinate> coordinates){
super.encodePoints(nativeGeom, coordinates);
if (coordinates.size() == 2){
nativeGeom.setIsSingleLineSegment();
}
nativeGeom.setCoordinate(idx, coordinate);
}
 
protected void encodeFigures(SqlGeometryV1 nativeGeom, LineString geometry) {
nativeGeom.setNumberOfFigures(1);
nativeGeom.setFigure(0, LINESTRING_FIGURE);
}
 
protected void encodeShapes(SqlGeometryV1 nativeGeom, LineString geometry) {
nativeGeom.setNumberOfShapes(1);
nativeGeom.setShape(0, LINESTRING_SHAPE);
}
 
 
public boolean accepts(Geometry geom) {
return geom instanceof LineString;
return geom instanceof LineString;
}
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PointDecoder.java
34,8 → 34,9
*/
class PointDecoder extends AbstractDecoder<Point> {
 
public boolean accepts(SqlGeometryV1 sqlNative) {
return (sqlNative.openGisType() == OpenGisType.POINT);
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.POINT;
}
 
protected Point createNullGeometry() {
43,8 → 44,19
}
 
protected Point createGeometry(SqlGeometryV1 nativeGeom) {
return getGeometryFactory().createPoint(nativeGeom.getCoordinate(0));
return createPoint(nativeGeom, 0);
}
 
@Override
protected Point createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
int figureOffset = nativeGeom.getStartFigureForShape(shapeIndex);
int pntOffset = nativeGeom.getStartPointForFigure(figureOffset);
return createPoint(nativeGeom, pntOffset);
}
 
private Point createPoint(SqlGeometryV1 nativeGeom, int pntOffset){
return getGeometryFactory().createPoint(nativeGeom.getCoordinate(pntOffset));
}
 
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Encoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
36,5 → 36,5
public SqlGeometryV1 encode(T geom);
 
public boolean accepts(Geometry geom);
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiLineStringDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
33,8 → 33,10
 
private final LineStringDecoder lineStringDecoder = new LineStringDecoder();
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return nativeGeom.openGisType() == OpenGisType.MULTILINESTRING;
 
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.MULTILINESTRING;
}
 
protected MultiLineString createNullGeometry() {
42,51 → 44,30
}
 
protected MultiLineString createGeometry(SqlGeometryV1 nativeGeom) {
return createGeometry(nativeGeom, 0);
}
 
@Override
protected MultiLineString createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
LineString[] lineStrings;
int startChildIndex = shapeIndex + 1;
int endChildShapeIndex = nativeGeom.getEndChildShape(shapeIndex);
if (nativeGeom.hasMValues()) {
lineStrings = new MLineString[nativeGeom.getNumShapes() - 1];
collectLineStrings(nativeGeom, lineStrings);
lineStrings = new MLineString[endChildShapeIndex - startChildIndex];
collectLineStrings(nativeGeom, lineStrings, startChildIndex, endChildShapeIndex);
return getGeometryFactory().createMultiMLineString((MLineString[]) lineStrings);
} else {
lineStrings = new LineString[nativeGeom.getNumShapes() - 1];
collectLineStrings(nativeGeom, lineStrings);
lineStrings = new LineString[endChildShapeIndex - startChildIndex];
collectLineStrings(nativeGeom, lineStrings, startChildIndex, endChildShapeIndex);
return getGeometryFactory().createMultiLineString(lineStrings);
}
 
}
 
private void collectLineStrings(SqlGeometryV1 nativeGeom, LineString[] lineStrings) {
int[] pointOffsets = collectPointOffsets(nativeGeom);
for (int i = 0; i < pointOffsets.length - 1; i++) {
int startOffset = pointOffsets[i];
int nextOffset = pointOffsets[i + 1];
lineStrings[i] = lineStringDecoder.createLineString(nativeGeom, startOffset, nextOffset);
}
}
private void collectLineStrings(SqlGeometryV1 nativeGeom, LineString[] lineStrings, int startChild, int endChild) {
 
 
/**
* Returns an array of point offsets for quickly returning the parts
* in the point array the correspond to the linestrings.
* <p/>
* The last offset points beyond the end of the point array.
*
* @param nativeGeom
* @return
*/
private int[] collectPointOffsets(SqlGeometryV1 nativeGeom) {
int[] pointOffsets = new int[nativeGeom.getNumShapes()];
 
// first shape is the parent shape; second and following are linestrings.
for (int shpIdx = 1; shpIdx < nativeGeom.getNumShapes(); shpIdx++) {
Shape shape = nativeGeom.getShape(shpIdx);
assert (shape.openGisType == OpenGisType.LINESTRING);
int figureOffset = shape.figureOffset;
Figure figure = nativeGeom.getFigure(figureOffset);
pointOffsets[shpIdx - 1] = figure.pointOffset;
for (int idx = startChild, i = 0; idx < endChild; idx++, i++){
lineStrings[i] = lineStringDecoder.createGeometry(nativeGeom, idx);
}
pointOffsets[nativeGeom.getNumShapes() - 1] = nativeGeom.getNumPoints();
return pointOffsets;
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Figure.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
47,15 → 47,15
buffer.putInt(pointOffset);
}
 
public boolean isInteriorRing() {
boolean isInteriorRing() {
return this.figureAttribute.equals(FigureAttribute.InteriorRing);
}
 
public boolean isExteriorRing() {
boolean isExteriorRing() {
return this.figureAttribute.equals(FigureAttribute.ExteriorRing);
}
 
public boolean isStroke() {
boolean isStroke() {
return this.figureAttribute.equals(FigureAttribute.Stroke);
}
 
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/SqlGeometryV1.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
68,7 → 68,7
}
 
//TODO -- refactor: all iterations in separate methods.
static byte[] store(SqlGeometryV1 sqlNative) {
public static byte[] store(SqlGeometryV1 sqlNative) {
int capacity = sqlNative.calculateCapacity();
ByteBuffer buffer = ByteBuffer.allocate(capacity);
buffer.order(ByteOrder.LITTLE_ENDIAN);
109,7 → 109,7
return buffer.array();
}
 
static SqlGeometryV1 load(byte[] bytes) {
public static SqlGeometryV1 load(byte[] bytes) {
SqlGeometryV1 result = new SqlGeometryV1(bytes);
result.parse();
return result;
124,7 → 124,7
// return coordinate;
// }
 
Coordinate getCoordinate(int index) {
public Coordinate getCoordinate(int index) {
Coordinate coordinate;
if (hasMValues()) {
coordinate = new MCoordinate();
138,11 → 138,69
return coordinate;
}
 
Figure getFigure(int index) {
public int getStartFigureForShape(int shapeIndex){
return getShape(shapeIndex).figureOffset;
}
 
/**
* Returns the index in the figure array after the last figure of the specified shape.
* @param shapeIndex index to shape in shape array
* @return index after last shape figure
*/
public int getEndFigureForShape(int shapeIndex) {
int nextIdx = shapeIndex + 1;
if (nextIdx == getNumShapes())
return getNumFigures();
return getShape(nextIdx).figureOffset;
}
 
public int getStartPointForFigure(int figureIndex){
return getFigure(figureIndex).pointOffset;
}
 
/**
* Returns the index in the point array after the last point of the specified figure.
* @param figureIndex index to shape in shape array
* @return index after last shape figure
*/
public int getEndPointForFigure(int figureIndex) {
int next = figureIndex + 1;
if (next == getNumFigures()) {
return getNumPoints();
}
return getFigure(next).pointOffset;
}
 
public boolean isFigureInteriorRing(int figureIdx) {
return getFigure(figureIdx).isInteriorRing();
}
 
public OpenGisType getOpenGisTypeOfShape(int shpIdx) {
return getShape(shpIdx).openGisType;
}
 
public Coordinate[] coordinateRange(int start, int end){
Coordinate[] coordinates = createCoordinateArray(end - start);
for (int idx = start, i = 0; idx < end; idx++, i++) {
coordinates[i] = getCoordinate(idx);
}
return coordinates;
}
 
private Coordinate[] createCoordinateArray(int size) {
if (hasMValues()) {
return new MCoordinate[size];
} else {
return new Coordinate[size];
}
}
 
 
private Figure getFigure(int index) {
return figures[index];
}
 
Shape getShape(int index) {
private Shape getShape(int index) {
return shapes[index];
}
 
170,14 → 228,23
}
 
void setHasZValues() {
this.zValues = new double[this.numberOfPoints];
 
serializationPropertiesByte |= hasZValuesMask;
 
}
 
void allocateZValueArray(){
if (this.hasZValues())
this.zValues = new double[this.numberOfPoints];
}
 
void allocateMValueArray(){
if (this.hasMValues())
this.mValues = new double[this.numberOfPoints];
}
 
void setHasMValues() {
this.mValues = new double[this.numberOfPoints];
 
serializationPropertiesByte |= hasMValuesMask;
}
 
191,7 → 258,7
}
 
void setIsSingleLineSegment() {
setNumberOfPoints(2);
// setNumberOfPoints(2);
serializationPropertiesByte |= isSingleLineSegment;
}
 
220,8 → 287,18
 
if (isSingleLineSegment() ||
isSinglePoint()) {
return; //parsing can stop here
//generate figure and shape.
// These are assumed, not explicitly encoded in the
// serialized data. See specs.
setNumberOfFigures(1);
setFigure(0, new Figure(FigureAttribute.Stroke, 0));
setNumberOfShapes(1);
OpenGisType gisType = isSinglePoint() ? OpenGisType.POINT : OpenGisType.LINESTRING;
setShape(0, new Shape(-1,0,gisType));
return;
}
//in all other cases, figures and shapes are
//explicitly encoded.
readFigures();
readShapes();
}
379,6 → 456,24
return this.numberOfFigures;
}
 
 
/**
* Returns the index after the last child shape of the specified shape.
* @param shapeIndex index of parent shape
* @return index one past the last child shape
*/
public int getEndChildShape(int shapeIndex) {
int childIndex = shapeIndex + 1;
while (childIndex < shapes.length){
if (shapes[childIndex].parentOffset == shapeIndex){
childIndex++;
} else {
break;
}
}
return childIndex;
}
 
private static class Point {
final double x;
final double y;
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PolygonEncoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
25,10 → 25,13
 
package org.hibernatespatial.sqlserver.convertors;
 
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;
 
public class PolygonEncoder extends AbstractEncoder<Polygon> {
 
private static Shape SHAPE = new Shape(-1, 0, OpenGisType.POLYGON);
37,38 → 40,47
return geom instanceof Polygon;
}
 
protected void encodeFigures(SqlGeometryV1 nativeGeom, Polygon geom) {
nativeGeom.setNumberOfFigures(geom.getNumInteriorRing() + 1);
int pointOffset = 0;
int figure = 0;
addFiguresForPolygon(nativeGeom, geom, pointOffset, figure);
@Override
protected void encode(Geometry geom, int parentShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
if (! (geom instanceof Polygon)) throw new IllegalArgumentException("Polygon geometry expected.");
Polygon polygon = (Polygon)geom;
int figureOffset = figures.size();
shapes.add( new Shape(parentShapeIndex, figureOffset, OpenGisType.POLYGON));
 
int pointOffset = coordinates.size();
addExteriorRing(polygon, coordinates, figures);
addInteriorRings(polygon, coordinates, figures);
 
}
 
protected void addFiguresForPolygon(SqlGeometryV1 nativeGeom, Polygon geom, int pointOffset, int figure) {
pointOffset = addExteriorRing(nativeGeom, geom, pointOffset, figure);
for (int ring = 0; ring < geom.getNumInteriorRing(); ring++) {
pointOffset = addInteriorRing(nativeGeom, geom, ring, pointOffset, ++figure);
 
private void addInteriorRings(Polygon geom, List<Coordinate> coordinates, List<Figure> figures){
for (int idx = 0; idx < geom.getNumInteriorRing(); idx++){
addInteriorRing(geom.getInteriorRingN(idx), coordinates, figures);
}
}
 
private int addInteriorRing(SqlGeometryV1 nativeGeom, Polygon geom, int ring, int pointOffset, int numFigure) {
LineString ls = geom.getInteriorRingN(ring);
private void addInteriorRing(LineString ring, List<Coordinate> coordinates, List<Figure> figures) {
int pointOffset = coordinates.size();
addPoints(ring, coordinates);
Figure figure = new Figure(FigureAttribute.InteriorRing, pointOffset);
nativeGeom.setFigure(numFigure, figure);
pointOffset += ls.getNumPoints();
return pointOffset;
figures.add(figure);
 
}
 
private int addExteriorRing(SqlGeometryV1 nativeGeom, Polygon geom, int offset, int numFigure) {
private void addPoints(LineString ring, List<Coordinate> coordinates) {
for (Coordinate c : ring.getCoordinates()){
coordinates.add(c);
}
}
 
private void addExteriorRing(Polygon geom, List<Coordinate> coordinates, List<Figure> figures) {
LineString shell = geom.getExteriorRing();
int offset = coordinates.size();
addPoints(shell, coordinates);
Figure exterior = new Figure(FigureAttribute.ExteriorRing, offset);
nativeGeom.setFigure(numFigure, exterior);
offset += shell.getNumPoints();
return offset;
figures.add(exterior);
}
 
protected void encodeShapes(SqlGeometryV1 nativeGeom, Polygon geom) {
nativeGeom.setNumberOfShapes(1);
nativeGeom.setShape(0, SHAPE);
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiPolygonDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
32,8 → 32,9
 
private final PolygonDecoder polygonDecoder = new PolygonDecoder();
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return nativeGeom.openGisType().equals(OpenGisType.MULTIPOLYGON);
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.MULTIPOLYGON;
}
 
protected MultiPolygon createNullGeometry() {
41,20 → 42,19
}
 
protected MultiPolygon createGeometry(SqlGeometryV1 nativeGeom) {
Polygon[] polygons = new Polygon[nativeGeom.getNumShapes() - 1];
for (int shpIdx = 1; shpIdx < nativeGeom.getNumShapes(); shpIdx++) {
Shape shape = nativeGeom.getShape(shpIdx);
int figureOffset = shape.figureOffset;
int nextFigureOffset = getNextShapeFigureOffset(nativeGeom, shpIdx);
polygons[shpIdx - 1] = polygonDecoder.createGeometry(nativeGeom, figureOffset, nextFigureOffset);
return createGeometry(nativeGeom, 0);
}
 
@Override
protected MultiPolygon createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
int startChildShape = shapeIndex + 1;
int endChildShape = nativeGeom.getEndChildShape(shapeIndex);
Polygon[] polygons = new Polygon[endChildShape - startChildShape];
for (int i = 0; i < polygons.length; i++){
polygons[i] = polygonDecoder.createGeometry(nativeGeom,startChildShape++);
}
return getGeometryFactory().createMultiPolygon(polygons);
}
 
private int getNextShapeFigureOffset(SqlGeometryV1 nativeGeom, int shpIdx) {
int nextIdx = shpIdx + 1;
if (nextIdx == nativeGeom.getNumShapes())
return nativeGeom.getNumFigures();
return nativeGeom.getShape(nextIdx).figureOffset;
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Decoders.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
42,10 → 42,11
//Decoders
DECODERS.add(new PointDecoder());
DECODERS.add(new LineStringDecoder());
DECODERS.add(new PolygonDecoder());
DECODERS.add(new MultiLineStringDecoder());
DECODERS.add(new PolygonDecoder());
DECODERS.add(new MultiPolygonDecoder());
DECODERS.add(new MultiPointDecoder());
DECODERS.add(new GeometryCollectionDecoder());
}
 
 
63,4 → 64,12
return decoder.decode(sqlGeom);
}
 
public static Decoder<? extends Geometry> decoderFor(OpenGisType type){
for (Decoder<? extends Geometry> decoder : DECODERS) {
if (decoder.accepts(type))
return decoder;
}
throw new IllegalArgumentException("No decoder for type " + type);
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/AbstractDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
43,12 → 43,22
return result;
}
 
public abstract boolean accepts(SqlGeometryV1 nativeGeom);
public boolean accepts(OpenGisType type){
return type == getOpenGisType();
}
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return accepts(nativeGeom.openGisType());
}
 
protected abstract OpenGisType getOpenGisType();
 
protected abstract G createNullGeometry();
 
protected abstract G createGeometry(SqlGeometryV1 nativeGeom);
 
protected abstract G createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex);
 
protected MGeometryFactory getGeometryFactory() {
return this.geometryFactory;
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PointEncoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
30,13 → 30,25
import com.vividsolutions.jts.geom.Point;
import org.hibernatespatial.mgeom.MCoordinate;
 
import java.util.List;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
class PointEncoder implements Encoder<Point> {
class PointEncoder extends AbstractEncoder<Point> {
 
/**
* Encodes a point as an <code>SQLGeometryV1</code> object.
*
* This is a specific implementation because points don't explicitly serialize figure and shape components.
*
* @param geom Geometry to serialize
* @return
*/
@Override
public SqlGeometryV1 encode(Point geom) {
 
SqlGeometryV1 sqlGeom = new SqlGeometryV1();
sqlGeom.setSrid(geom.getSRID());
sqlGeom.setIsSinglePoint();
45,14 → 57,28
Coordinate coordinate = geom.getCoordinate();
if (is3DPoint(coordinate)) {
sqlGeom.setHasZValues();
sqlGeom.allocateZValueArray();
}
if (isMPoint(coordinate)) {
sqlGeom.setHasMValues();
sqlGeom.allocateMValueArray();
}
sqlGeom.setCoordinate(0, coordinate);
return sqlGeom;
}
 
@Override
protected void encode(Geometry geom, int parentIdx, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
if (!(geom instanceof Point)) throw new IllegalArgumentException("Require Point geometry");
int pntOffset = coordinates.size();
int figureOffset = figures.size();
coordinates.add(geom.getCoordinate());
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);
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiPointDecoder.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
31,8 → 31,10
 
public class MultiPointDecoder extends AbstractDecoder<MultiPoint> {
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return nativeGeom.openGisType() == OpenGisType.MULTIPOINT;
 
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.MULTIPOINT;
}
 
protected MultiPoint createNullGeometry() {
40,6 → 42,7
}
 
protected MultiPoint createGeometry(SqlGeometryV1 nativeGeom) {
//optimized multipoint decoding
Coordinate[] coords = new Coordinate[nativeGeom.getNumPoints()];
for (int cIdx = 0; cIdx < nativeGeom.getNumPoints(); cIdx++) {
coords[cIdx] = nativeGeom.getCoordinate(cIdx);
47,5 → 50,18
return getGeometryFactory().createMultiPoint(coords);
}
 
@Override
protected MultiPoint createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
int startChildIdx = shapeIndex + 1;
int endchildIdx = nativeGeom.getEndChildShape(shapeIndex);
Coordinate[] coordinates = new Coordinate[endchildIdx - startChildIdx];
for (int i = 0 ; i < coordinates.length; i++){
int figureOffset = nativeGeom.getStartFigureForShape(startChildIdx++);
int pntIdx = nativeGeom.getStartPointForFigure(figureOffset);
coordinates[i] = nativeGeom.getCoordinate(pntIdx);
}
return getGeometryFactory().createMultiPoint(coordinates);
}
 
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/GeometryCollectionDecoder.java
New file
0,0 → 1,68
/*
* $Id:$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 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
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
 
/**
* Created by IntelliJ IDEA.
* User: maesenka
* Date: Jan 24, 2010
* Time: 5:34:40 PM
* To change this template use File | Settings | File Templates.
*/
public class GeometryCollectionDecoder extends AbstractDecoder<GeometryCollection> {
 
 
@Override
protected OpenGisType getOpenGisType() {
return OpenGisType.GEOMETRYCOLLECTION;
}
 
@Override
protected GeometryCollection createNullGeometry() {
return getGeometryFactory().createGeometryCollection(null);
}
 
@Override
protected GeometryCollection createGeometry(SqlGeometryV1 nativeGeom) {
return createGeometry(nativeGeom, 0);
}
 
@Override
protected GeometryCollection createGeometry(SqlGeometryV1 nativeGeom, int shapeIndex) {
int startChildIdx = shapeIndex + 1;
int endChildIdx = nativeGeom.getEndChildShape(shapeIndex);
Geometry[] geometries = new Geometry[endChildIdx - startChildIdx];
for (int childIdx = startChildIdx, i = 0; childIdx < endChildIdx; childIdx++, i++){
AbstractDecoder<?> decoder = (AbstractDecoder<?>)Decoders.decoderFor(nativeGeom.getOpenGisTypeOfShape(childIdx));
geometries[i] = decoder.createGeometry(nativeGeom,childIdx);
}
return getGeometryFactory().createGeometryCollection(geometries);
}
 
}