Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 147 → Rev 148

/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/MultiLineStringConvertorTest.java
New file
0,0 → 1,44
/*
* $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;
 
public class MultiLineStringConvertorTest extends AbstractConvertorTest {
 
@Before
public void setUp() {
doDecoding(OpenGisType.MULTILINESTRING);
doEncoding();
}
 
@Test
public void test_encoding() {
super.test_encoding();
}
 
}
/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.
25,6 → 25,7
 
package org.hibernatespatial.sqlserver;
 
import com.vividsolutions.jts.io.WKTReader;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
33,9 → 34,7
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.*;
 
/**
* <p>Unit test support class.</p>
68,21 → 67,69
private static final DataSource dataSource = createBasicDataSource();
 
private static class GeomTest {
 
final static WKTReader parser = new WKTReader();
 
//TODO -- how to define EMPTY Geomtries in SQL?
final static String[] DATA = new String[]{
"insert into geomtest values (1, 'POINT', Geometry::STGeomFromText('POINT(10 5)', 0))",
"insert into geomtest values (2, 'POINT', Geometry::STGeomFromText('POINT(52.25 2.53)', 4326))",
"insert into geomtest values (3, 'POINT', Geometry::STGeomFromText('POINT(150000 200000)', 31370))",
"insert into geomtest values (4, 'POINT', Geometry::STGeomFromText('POINT(10.0 2.0 1.0 3.0)', 4326))",
"insert into geomtest values (5, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0, 20.0 15.0)', 4326))",
"insert into geomtest values (6, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0, 20.0 15.0, 30.3 22.4, 10 30.0)', 4326))",
"insert into geomtest values (7, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0 0.0, 20.0 15.0 3.0)', 4326))",
"insert into geomtest values (8, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0 0.0 0.0, 20.0 15.0 3.0 1.0)', 4326))",
"insert into geomtest values (9, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0 1, 20.0 15.0 2, 30.3 22.4 5, 10 30.0 2)', 4326))",
"insert into geomtest values (10, 'LINESTRING', Geometry::STGeomFromText('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))",
 
final static List<TestWKT> TEST_WKTS = new ArrayList<TestWKT>();
 
};
// final static String[] DATA = new String[]{
// //POINT
//
// "insert into geomtest values (1, 'POINT', Geometry::STGeomFromText('POINT(10 5)', 0))",
// "insert into geomtest values (2, 'POINT', Geometry::STGeomFromText('POINT(52.25 2.53)', 4326))",
// "insert into geomtest values (3, 'POINT', Geometry::STGeomFromText('POINT(150000 200000)', 31370))",
// "insert into geomtest values (4, 'POINT', Geometry::STGeomFromText('POINT(10.0 2.0 1.0 3.0)', 4326))",
// //LINESTRING
// "insert into geomtest values (5, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0, 20.0 15.0)', 4326))",
// "insert into geomtest values (6, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0, 20.0 15.0, 30.3 22.4, 10 30.0)', 4326))",
// "insert into geomtest values (7, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0 0.0, 20.0 15.0 3.0)', 4326))",
// "insert into geomtest values (8, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0 0.0 0.0, 20.0 15.0 3.0 1.0)', 4326))",
// "insert into geomtest values (9, 'LINESTRING', Geometry::STGeomFromText('LINESTRING(10.0 5.0 1, 20.0 15.0 2, 30.3 22.4 5, 10 30.0 2)', 4326))",
// "insert into geomtest values (10, 'LINESTRING', Geometry::STGeomFromText('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
// "insert into geomtest values (11, 'MULTILINESTRING', Geometry::STGeomFromText('MULTILINESTRING((10.0 5.0, 20.0 15.0),( 25.0 30.0, 30.0 20.0))', 4326))",
// "insert into geomtest values (12, 'MULTILINESTRING', Geometry::STGeomFromText('MULTILINESTRING((10.0 5.0, 20.0 15.0, 30.3 22.4, 10 30.0), " +
// "(40.0 20.0, 42.0 18.0, 43.0 16.0, 40 14.0))', 4326))",
// "insert into geomtest values (13, 'MULTILINESTRING', Geometry::STGeomFromText('MULTILINESTRING((10.0 5.0 1.0, 20.0 15.0 2.0, 30.3 22.4 1.0, 10 30.0 1.0), " +
// "(40.0 20.0 0.0, 42.0 18.0 1.0, 43.0 16.0 2.0, 40 14.0 3.0))', 4326))",
// "insert into geomtest values (14, 'MULTILINESTRING', Geometry::STGeomFromText('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), " +
// "(40.0 20.0 0.0 3.0, 42.0 18.0 1.0 4.0, 43.0 16.0 2.0 5.0, 40 14.0 3.0 6.0))', 4326))",
// "insert into geomtest values (15, 'MULTILINESTRING', Geometry::STGeomFromText('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))"
//
//
// };
 
static {
 
//POINT test cases
TEST_WKTS.add(new TestWKT(1, "POINT", "POINT(10 5)", 0));
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));
TEST_WKTS.add(new TestWKT(7, "LINESTRING", "LINESTRING(10.0 5.0 0.0, 20.0 15.0 3.0)", 4326));
TEST_WKTS.add(new TestWKT(8, "LINESTRING", "LINESTRING(10.0 5.0 0.0 0.0, 20.0 15.0 3.0 1.0)", 4326));
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));
TEST_WKTS.add(new TestWKT(12, "MULTILINESTRING",
"MULTILINESTRING((10.0 5.0, 20.0 15.0, 30.3 22.4, 10 30.0), (40.0 20.0, 42.0 18.0, 43.0 16.0, 40 14.0))", 4326));
TEST_WKTS.add(new TestWKT(13, "MULTILINESTRING",
"MULTILINESTRING((10.0 5.0 1.0, 20.0 15.0 2.0, 30.3 22.4 1.0, 10 30.0 1.0),(40.0 20.0 0.0, 42.0 18.0 1.0, 43.0 16.0 2.0, 40 14.0 3.0))", 4326));
TEST_WKTS.add(new TestWKT(14, "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),(40.0 20.0 0.0 3.0, 42.0 18.0 1.0 4.0, 43.0 16.0 2.0 5.0, 40 14.0 3.0 6.0))", 4326));
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));
 
 
}
}
 
private static DataSource createBasicDataSource() {
129,9 → 176,9
try {
cn = getDataSource().getConnection();
Statement stmt = cn.createStatement();
for (int i = 0; i < GeomTest.DATA.length; i++) {
LOGGER.debug("adding stmt: " + GeomTest.DATA[i]);
stmt.addBatch(GeomTest.DATA[i]);
for (int i = 0; i < GeomTest.TEST_WKTS.size(); i++) {
LOGGER.debug("adding stmt: " + GeomTest.TEST_WKTS.get(i).toSql());
stmt.addBatch(GeomTest.TEST_WKTS.get(i).toSql());
}
int[] insCounts = stmt.executeBatch();
stmt.close();
182,4 → 229,26
return result;
}
 
private static class TestWKT {
 
final String SQL_TEMPLATE = "insert into geomtest values (%d, '%s', Geometry::STGeomFromText('%s', %d))";
final String wkt;
final int id;
final int srid;
final String type;
 
TestWKT(int id, String type, String wkt, int srid) {
this.wkt = wkt;
this.id = id;
this.type = type;
this.srid = srid;
}
 
public String toSql() {
return String.format(SQL_TEMPLATE, id, type, wkt, srid);
}
 
 
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Encoders.java
43,6 → 43,7
//Encoders
ENCODERS.add(new PointEncoder());
ENCODERS.add(new LineStringEncoder());
ENCODERS.add(new MultiLineStringEncoder());
 
}
 
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/AbstractEncoder.java
New file
0,0 → 1,67
/*
* $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.LineString;
import org.hibernatespatial.mgeom.MLineString;
 
public abstract class AbstractEncoder<G extends Geometry> implements Encoder<G> {
 
public SqlGeometryV1 encode(LineString geom) {
SqlGeometryV1 nativeGeom = new SqlGeometryV1();
nativeGeom.setSrid(geom.getSRID());
if (geom.isValid()) nativeGeom.setIsValid();
nativeGeom.setNumberOfPoints(geom.getNumPoints());
if (geom instanceof MLineString)
nativeGeom.setHasMValues();
encodePoints(nativeGeom, geom);
encodeFigures(nativeGeom, geom);
encodeShapes(nativeGeom, geom);
return nativeGeom;
}
 
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 setCoordinate(SqlGeometryV1 nativeGeom, int idx, Coordinate coordinate) {
if (!nativeGeom.hasZValues() && !Double.isNaN(coordinate.z)) {
nativeGeom.setHasZValues();
}
nativeGeom.setCoordinate(idx, coordinate);
}
 
abstract protected void encodeFigures(SqlGeometryV1 nativeGeom, Geometry geom);
 
abstract protected void encodeShapes(SqlGeometryV1 nativeGeom, Geometry geom);
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/LineStringDecoder.java
25,9 → 25,11
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.LineString;
import org.hibernatespatial.mgeom.MCoordinate;
import org.hibernatespatial.mgeom.MLineString;
 
class LineStringDecoder extends AbstractDecoder<LineString> {
 
40,14 → 42,27
}
 
protected LineString createGeometry(SqlGeometryV1 nativeGeom) {
MCoordinate[] coords = new MCoordinate[nativeGeom.getNumPoints()];
for (int idx = 0; idx < nativeGeom.getNumPoints(); idx++) {
coords[idx] = nativeGeom.getCoordinate(idx);
if (nativeGeom.hasMValues()) {
return createMLineString(nativeGeom, 0, nativeGeom.getNumPoints());
}
if (!nativeGeom.hasMValues())
return getGeometryFactory().createLineString(coords);
else
return getGeometryFactory().createMLineString(coords);
return createLineString(nativeGeom, 0, nativeGeom.getNumPoints());
}
 
protected MLineString createMLineString(SqlGeometryV1 nativeGeom, int offset, int nextOffset) {
MCoordinate[] coords = new MCoordinate[nextOffset - offset];
for (int idx = offset, i = 0; idx < nextOffset; idx++, i++) {
coords[i] = (MCoordinate) nativeGeom.getCoordinate(idx);
}
return getGeometryFactory().createMLineString(coords);
}
 
protected LineString createLineString(SqlGeometryV1 nativeGeom, int offset, int nextOffset) {
Coordinate[] coords = new Coordinate[nextOffset - offset];
for (int idx = offset, i = 0; idx < nextOffset; idx++, i++) {
coords[i] = nativeGeom.getCoordinate(idx);
}
return getGeometryFactory().createLineString(coords);
}
 
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/LineStringEncoder.java
28,61 → 28,42
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import org.hibernatespatial.mgeom.MLineString;
 
public class LineStringEncoder implements Encoder<LineString> {
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);
 
 
public SqlGeometryV1 encode(LineString geom) {
SqlGeometryV1 nativeGeom = new SqlGeometryV1();
nativeGeom.setSrid(geom.getSRID());
if (geom.isValid()) nativeGeom.setIsValid();
nativeGeom.setNumberOfPoints(geom.getNumPoints());
if (geom instanceof MLineString)
nativeGeom.setHasMValues();
//TODO -- remove the if/else - this is confusing.
protected void encodePoints(SqlGeometryV1 nativeGeom, Geometry geom) {
if (geom.getNumPoints() == 2) {
encodeSingleLineSegment(nativeGeom, geom);
} else {
encodePoints(nativeGeom, geom);
encodeFigures(nativeGeom);
encodeShapes(nativeGeom);
super.encodePoints(nativeGeom, geom);
}
return nativeGeom;
}
 
private void encodePoints(SqlGeometryV1 nativeGeom, LineString geom) {
 
Coordinate[] coords = geom.getCoordinates();
for (int i = 0; i < coords.length; i++) {
setCoordinate(nativeGeom, i, coords[i]);
}
}
 
private void encodeSingleLineSegment(SqlGeometryV1 nativeGeom, LineString geom) {
private void encodeSingleLineSegment(SqlGeometryV1 nativeGeom, Geometry geom) {
nativeGeom.setIsSingleLineSegment();
Coordinate[] coords = geom.getCoordinates();
setCoordinate(nativeGeom, 0, coords[0]);
setCoordinate(nativeGeom, 1, coords[1]);
}
 
private void setCoordinate(SqlGeometryV1 nativeGeom, int idx, Coordinate coordinate) {
protected void setCoordinate(SqlGeometryV1 nativeGeom, int idx, Coordinate coordinate) {
if (!nativeGeom.hasZValues() && !Double.isNaN(coordinate.z)) {
nativeGeom.setHasZValues();
}
nativeGeom.setCoordinate(idx, coordinate);
}
 
private void encodeFigures(SqlGeometryV1 nativeGeom) {
protected void encodeFigures(SqlGeometryV1 nativeGeom, Geometry geometry) {
nativeGeom.setNumberOfFigures(1);
nativeGeom.setFigure(0, LINESTRING_FIGURE);
}
 
private void encodeShapes(SqlGeometryV1 nativeGeom) {
protected void encodeShapes(SqlGeometryV1 nativeGeom, Geometry geometry) {
nativeGeom.setNumberOfShapes(1);
nativeGeom.setShape(0, LINESTRING_SHAPE);
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiLineStringDecoder.java
New file
0,0 → 1,95
/*
* $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.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import org.hibernatespatial.mgeom.MLineString;
 
public class MultiLineStringDecoder extends AbstractDecoder<MultiLineString> {
 
private LineStringDecoder lineStringDecoder = new LineStringDecoder();
 
public boolean accepts(SqlGeometryV1 nativeGeom) {
return nativeGeom.openGisType() == OpenGisType.MULTILINESTRING;
}
 
protected MultiLineString createNullGeometry() {
return getGeometryFactory().createMultiLineString(new LineString[]{});
}
 
protected MultiLineString createGeometry(SqlGeometryV1 nativeGeom) {
LineString[] lineStrings;
if (nativeGeom.hasMValues()) {
lineStrings = new MLineString[nativeGeom.getNumShapes() - 1];
collectLineStrings(nativeGeom, lineStrings);
return getGeometryFactory().createMultiMLineString((MLineString[]) lineStrings);
} else {
lineStrings = new LineString[nativeGeom.getNumShapes() - 1];
collectLineStrings(nativeGeom, lineStrings);
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];
if (nativeGeom.hasMValues())
lineStrings[i] = lineStringDecoder.createMLineString(nativeGeom, startOffset, nextOffset);
else
lineStrings[i] = lineStringDecoder.createLineString(nativeGeom, startOffset, nextOffset);
}
}
 
 
/**
* 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;
}
pointOffsets[nativeGeom.getNumShapes() - 1] = nativeGeom.getNumPoints();
return pointOffsets;
}
 
}
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/SqlGeometryV1.java
115,12 → 115,26
return result;
}
 
MCoordinate getCoordinate(int index) {
MCoordinate coordinate = new MCoordinate();
// MCoordinate getCoordinate(int index) {
// MCoordinate coordinate = new MCoordinate();
// coordinate.x = points[index].x;
// coordinate.y = points[index].y;
// if (hasZValues()) coordinate.z = zValues[index];
// if (hasMValues()) coordinate.m = mValues[index];
// return coordinate;
// }
 
Coordinate getCoordinate(int index) {
Coordinate coordinate;
if (hasMValues()) {
coordinate = new MCoordinate();
((MCoordinate) coordinate).m = mValues[index];
} else {
coordinate = new Coordinate();
}
coordinate.x = points[index].x;
coordinate.y = points[index].y;
if (hasZValues()) coordinate.z = zValues[index];
if (hasMValues()) coordinate.m = mValues[index];
return coordinate;
}
 
263,7 → 277,7
return size;
}
 
private int getNumShapes() {
int getNumShapes() {
return this.numberOfShapes;
}
 
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Decoders.java
42,6 → 42,8
//Decoders
DECODERS.add(new PointDecoder());
DECODERS.add(new LineStringDecoder());
DECODERS.add(new MultiLineStringDecoder());
 
}
 
 
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/MultiLineStringEncoder.java
New file
0,0 → 1,75
/*
* $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.MultiLineString;
import org.hibernatespatial.mgeom.MultiMLineString;
 
public class MultiLineStringEncoder extends AbstractEncoder<MultiLineString> {
 
public SqlGeometryV1 encode(MultiLineString geom) {
SqlGeometryV1 nativeGeom = new SqlGeometryV1();
nativeGeom.setSrid(geom.getSRID());
if (geom.isValid()) nativeGeom.setIsValid();
nativeGeom.setNumberOfPoints(geom.getNumPoints());
if (geom instanceof MultiMLineString)
nativeGeom.setHasMValues();
encodePoints(nativeGeom, geom);
encodeFigures(nativeGeom, geom);
encodeShapes(nativeGeom, geom);
return nativeGeom;
}
 
protected void encodeFigures(SqlGeometryV1 nativeGeom, Geometry geom) {
nativeGeom.setNumberOfFigures(geom.getNumGeometries());
int offset = 0;
for (int i = 0; i < geom.getNumGeometries(); i++) {
Geometry ls = geom.getGeometryN(i);
Figure figure = new Figure(FigureAttribute.Stroke, offset);
nativeGeom.setFigure(i, figure);
offset += ls.getNumPoints();
}
}
 
protected void encodeShapes(SqlGeometryV1 nativeGeom, Geometry geom) {
MultiLineString mls = (MultiLineString) geom;
//number of shapes is 1 + number of linestrings
nativeGeom.setNumberOfShapes(geom.getNumGeometries() + 1);
//first encode the parent
Shape parent = new Shape(-1, 0, OpenGisType.MULTILINESTRING);
nativeGeom.setShape(0, parent);
for (int i = 0; i < geom.getNumGeometries(); i++) {
Shape shape = new Shape(0, i, OpenGisType.LINESTRING);
nativeGeom.setShape(1 + i, shape);
}
}
 
 
public boolean accepts(Geometry geom) {
return geom instanceof MultiLineString;
}
}