Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 269 → Rev 272

/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/test/GeometryEquality.java
84,7 → 84,7
 
private boolean testCoordinateEquality(Coordinate c1, Coordinate c2) {
if (c1 instanceof MCoordinate) {
if (!(c1 instanceof MCoordinate)) return false;
if (!(c2 instanceof MCoordinate)) return false;
MCoordinate mc1 = (MCoordinate) c1;
MCoordinate mc2 = (MCoordinate) c2;
if (!Double.isNaN(mc1.m) && mc1.m != mc2.m) return false;
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/test/mgeom/TestMLineStringGetCoordinatesBetween.java
1,5 → 1,5
/*
* $Id:$
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
28,6 → 28,7
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.PrecisionModel;
import org.hibernatespatial.mgeom.MCoordinate;
import org.hibernatespatial.mgeom.*;
import org.junit.Before;
import org.junit.Test;
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/test/mgeom/TestEventLocator.java
27,6 → 27,7
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.PrecisionModel;
import org.hibernatespatial.mgeom.MCoordinate;
import org.hibernatespatial.mgeom.*;
import org.junit.Before;
import org.junit.Test;
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/test/mgeom/MLineStringTest.java
33,6 → 33,7
import com.vividsolutions.jts.geom.CoordinateSequenceComparator;
import com.vividsolutions.jts.geom.PrecisionModel;
import junit.framework.TestCase;
import org.hibernatespatial.mgeom.MCoordinate;
import org.hibernatespatial.mgeom.*;
 
/**
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/jts/test/TestMeasuredGeometryConstruction.java
New file
0,0 → 1,77
package org.hibernatespatial.jts.test;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequenceFactory;
 
import org.junit.Test;
 
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
/**
* Tests the construction of <code>MeasuredGeometry</code>s.
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 29, 2010
*/
public class TestMeasuredGeometryConstruction {
 
MeasuredCoordinateSequenceFactory coordinateFactory = new MeasuredCoordinateSequenceFactory();
GeometryFactory geometryFactory = new GeometryFactory(coordinateFactory);
 
@Test
public void test_linestring_created_with_measuredcoordinatesequence() {
 
MeasuredCoordinateSequence coordinates = coordinateFactory.create(3, 2);
coordinates.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(1,2,3));
coordinates.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(3,4,5));
coordinates.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(6,7,8));
LineString lineString = geometryFactory.createLineString(coordinates);
 
assertTrue(lineString.getCoordinateN(0).equals2D(new Coordinate(1,2)));
assertTrue(lineString.getCoordinateN(1).equals2D(new Coordinate(3,4)));
assertTrue(lineString.getCoordinateN(2).equals2D(new Coordinate(6,7)));
 
 
assertEquals(3, lineString.getCoordinateSequence().getOrdinate(0, CoordinateSequence.M), Math.ulp(10d));
assertEquals(5, lineString.getCoordinateSequence().getOrdinate(1, CoordinateSequence.M), Math.ulp(10d));
assertEquals(8, lineString.getCoordinateSequence().getOrdinate(2, CoordinateSequence.M), Math.ulp(10d));
 
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(0, CoordinateSequence.Z)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(1, CoordinateSequence.Z)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(2, CoordinateSequence.Z)));
}
 
@Test
public void test_linestring_created_with_coordinatesequence() {
 
Coordinate[] coordinates = new Coordinate[]{
new Coordinate(1,2),
new Coordinate(3,4),
new Coordinate(6,7)};
 
LineString lineString = geometryFactory.createLineString(coordinates);
 
assertTrue(lineString.getCoordinateN(0).equals2D(new Coordinate(1,2)));
assertTrue(lineString.getCoordinateN(1).equals2D(new Coordinate(3,4)));
assertTrue(lineString.getCoordinateN(2).equals2D(new Coordinate(6,7)));
 
 
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(0, CoordinateSequence.M)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(1, CoordinateSequence.M)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(2, CoordinateSequence.M)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(0, CoordinateSequence.Z)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(1, CoordinateSequence.Z)));
assertTrue(Double.isNaN(lineString.getCoordinateSequence().getOrdinate(2, CoordinateSequence.Z)));
 
 
}
 
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/jts/geom/TestMeasuredCoordinateSequence.java
New file
0,0 → 1,307
package org.hibernatespatial.jts.geom;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
import org.hibernatespatial.jts.geom.MeasuredCoordinateArraySequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequenceFactory;
import org.junit.Test;
 
import static org.junit.Assert.*;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 22, 2010
*/
public class TestMeasuredCoordinateSequence {
 
 
private MeasuredCoordinateSequence seq;
 
@Test
public void test_constructor_size() {
 
MeasuredCoordinateSequenceFactory mcsf = new MeasuredCoordinateSequenceFactory();
seq = mcsf.create(2, 4);
Coordinate c0 = new Coordinate(1, 2, 3);
seq.setMeasuredCoordinate(0, new MeasuredCoordinate(c0, 4));
Coordinate c1 = new Coordinate(5, 6, 7);
seq.setMeasuredCoordinate(1, new MeasuredCoordinate(c1, 8));
assertEquals(c0, seq.getCoordinate(0));
assertEquals(4d, seq.getM(0), Math.ulp(4d));
assertEquals(c1, seq.getCoordinate(1));
assertEquals(8d, seq.getM(1), Math.ulp(8d));
 
try {
seq.setMeasuredCoordinate(2, new MeasuredCoordinate(c1, 8));
fail();
} catch (ArrayIndexOutOfBoundsException e) {
}
 
 
}
 
@Test
public void test_constructor_coordinate_array() {
Coordinate[] coordinates = new Coordinate[]{
new Coordinate(1, 1),
new Coordinate(2, 3),
new Coordinate(4, 5)
};
 
seq = new MeasuredCoordinateArraySequence(coordinates);
assertEquals(coordinates.length, seq.size());
assertEquals(4, seq.getDimension());
assertEquals(4, seq.getOrdinate(2, CoordinateSequence.X), Math.ulp(4f));
assertEquals(5, seq.getOrdinate(2, CoordinateSequence.Y), Math.ulp(5f));
assertEquals(Double.NaN, seq.getOrdinate(2, CoordinateSequence.M), 0.1f);
//test null parameter
seq = new MeasuredCoordinateArraySequence((Coordinate[])null);
assertEquals(0, seq.size());
 
}
 
@Test
public void test_constructor_coordinateseq() {
CoordinateSequence s = new CoordinateArraySequence(10);
seq = new MeasuredCoordinateArraySequence(s);
assertEquals(10, seq.size());
 
MeasuredCoordinateSequence ms = new MeasuredCoordinateArraySequence(3);
ms.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(1, 2, 1));
ms.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(3, 4, 2));
ms.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(5, 6, 3));
seq = new MeasuredCoordinateArraySequence((CoordinateSequence) ms);
 
assertEquals(1, seq.getOrdinate(0, CoordinateSequence.X), Math.ulp(10));
assertEquals(2, seq.getOrdinate(0, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(0, CoordinateSequence.Z)));
assertEquals(1, seq.getOrdinate(0, CoordinateSequence.M), Math.ulp(10));
 
assertEquals(3, seq.getOrdinate(1, CoordinateSequence.X), Math.ulp(10));
assertEquals(4, seq.getOrdinate(1, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(1, CoordinateSequence.Z)));
assertEquals(2, seq.getOrdinate(1, CoordinateSequence.M), Math.ulp(10));
 
assertEquals(5, seq.getOrdinate(2, CoordinateSequence.X), Math.ulp(10));
assertEquals(6, seq.getOrdinate(2, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(2, CoordinateSequence.Z)));
assertEquals(3, seq.getOrdinate(2, CoordinateSequence.M), Math.ulp(10));
 
//test null paramater
seq = new MeasuredCoordinateArraySequence((CoordinateSequence)null);
assertEquals(0, seq.size());
 
}
 
@Test
public void test_constructor_coordinateseq_doubles() {
CoordinateSequence s = new CoordinateArraySequence(10);
double[] measures = new double[10];
seq = new MeasuredCoordinateArraySequence(s, measures);
assertEquals(10, seq.size());
 
try {
double[] m2 = new double[8];
seq = new MeasuredCoordinateArraySequence(s, m2);
fail();
} catch (IllegalArgumentException e) {
}
 
seq = new MeasuredCoordinateArraySequence((CoordinateSequence) null, null);
assertEquals(0, seq.size());
 
//test null paramater
seq = new MeasuredCoordinateArraySequence((CoordinateSequence)null, null);
assertEquals(0, seq.size());
 
}
 
@Test
public void test_constructor_coordinates_doubles() {
Coordinate[] s = new Coordinate[10];
double[] measures = new double[10];
seq = new MeasuredCoordinateArraySequence(s, measures);
assertEquals(10, seq.size());
 
try {
double[] m2 = new double[8];
seq = new MeasuredCoordinateArraySequence(s, m2);
fail();
} catch (IllegalArgumentException e) {
}
 
seq = new MeasuredCoordinateArraySequence((Coordinate[]) null, null);
assertEquals(0, seq.size());
 
}
 
@Test
public void test_constructor_measurecoordinateseq() {
MeasuredCoordinateSequence ms = new MeasuredCoordinateArraySequence(3);
ms.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(1, 2, 1));
ms.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(3, 4, 2));
ms.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(5, 6, 3));
 
seq = new MeasuredCoordinateArraySequence(ms);
assertEquals(1, seq.getOrdinate(0, CoordinateSequence.X), Math.ulp(10));
assertEquals(2, seq.getOrdinate(0, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(0, CoordinateSequence.Z)));
assertEquals(1, seq.getOrdinate(0, CoordinateSequence.M), Math.ulp(10));
 
assertEquals(3, seq.getOrdinate(1, CoordinateSequence.X), Math.ulp(10));
assertEquals(4, seq.getOrdinate(1, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(1, CoordinateSequence.Z)));
assertEquals(2, seq.getOrdinate(1, CoordinateSequence.M), Math.ulp(10));
 
assertEquals(5, seq.getOrdinate(2, CoordinateSequence.X), Math.ulp(10));
assertEquals(6, seq.getOrdinate(2, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(2, CoordinateSequence.Z)));
assertEquals(3, seq.getOrdinate(2, CoordinateSequence.M), Math.ulp(10));
 
seq = new MeasuredCoordinateArraySequence((MeasuredCoordinateSequence) null);
assertEquals(0, seq.size());
}
 
@Test
public void test_constructor_measurecoordinates() {
MeasuredCoordinate[] ms = new MeasuredCoordinate[3];
ms[0] = MeasuredCoordinate.to2DM(1, 2, 1);
ms[1] = MeasuredCoordinate.to2DM(3, 4, 2);
ms[2] = MeasuredCoordinate.to2DM(5, 6, 3);
 
seq = new MeasuredCoordinateArraySequence(ms);
assertEquals(1, seq.getOrdinate(0, CoordinateSequence.X), Math.ulp(10));
assertEquals(2, seq.getOrdinate(0, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(0, CoordinateSequence.Z)));
assertEquals(1, seq.getOrdinate(0, CoordinateSequence.M), Math.ulp(10));
 
assertEquals(3, seq.getOrdinate(1, CoordinateSequence.X), Math.ulp(10));
assertEquals(4, seq.getOrdinate(1, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(1, CoordinateSequence.Z)));
assertEquals(2, seq.getOrdinate(1, CoordinateSequence.M), Math.ulp(10));
 
assertEquals(5, seq.getOrdinate(2, CoordinateSequence.X), Math.ulp(10));
assertEquals(6, seq.getOrdinate(2, CoordinateSequence.Y), Math.ulp(10));
assertTrue(Double.isNaN(seq.getOrdinate(2, CoordinateSequence.Z)));
assertEquals(3, seq.getOrdinate(2, CoordinateSequence.M), Math.ulp(10));
 
seq = new MeasuredCoordinateArraySequence((MeasuredCoordinate[]) null);
assertEquals(0, seq.size());
 
}
 
@Test
public void test_can_set_m_ordinates() {
seq = new MeasuredCoordinateArraySequence(2);
seq.setOrdinate(0, CoordinateSequence.X, 0);
seq.setOrdinate(0, CoordinateSequence.Y, 1);
seq.setOrdinate(0, CoordinateSequence.Z, 2);
seq.setOrdinate(0, CoordinateSequence.M, 3);
for (int i = 0; i < seq.getDimension(); i++) {
assertEquals(i, seq.getOrdinate(0, i), Math.ulp(10));
}
}
 
@Test
public void test_if_on_create_m_ordinates_are_NaN() {
seq = new MeasuredCoordinateArraySequence(2);
seq.setOrdinate(0, CoordinateSequence.X, 0);
seq.setOrdinate(0, CoordinateSequence.Y, 1);
seq.setOrdinate(0, CoordinateSequence.Z, 2);
assertTrue(Double.isNaN(seq.getOrdinate(0, CoordinateSequence.M)));
assertTrue(Double.isNaN(seq.getM(0)));
}
 
 
@Test
public void test_get_m() {
seq = new MeasuredCoordinateArraySequence(2);
seq.setOrdinate(0, CoordinateSequence.X, 0);
seq.setOrdinate(0, CoordinateSequence.Y, 1);
seq.setOrdinate(0, CoordinateSequence.Z, 2);
seq.setOrdinate(0, CoordinateSequence.M, 3);
assertEquals(0, seq.getX(0), Math.ulp(10d));
assertEquals(1, seq.getY(0), Math.ulp(10d));
assertEquals(2, seq.getZ(0), Math.ulp(10d));
assertEquals(3, seq.getM(0), Math.ulp(10d));
 
}
 
@Test
public void test_setCoordinateAndM() {
seq = new MeasuredCoordinateArraySequence(2);
seq.setMeasuredCoordinate(0, MeasuredCoordinate.to3DM(0, 1, 2, 3));
for (int i = 0; i < seq.getDimension(); i++) {
assertEquals(i, seq.getOrdinate(0, i), Math.ulp(10d));
}
 
}
 
@Test
public void test_test_copy_measured_coordinate_index_coordinate(){
 
seq = new MeasuredCoordinateArraySequence(4);
seq.setMeasuredCoordinate(2, MeasuredCoordinate.to3DM(0, 1, 2, 3));
 
MeasuredCoordinate mCoordinate = new MeasuredCoordinate();
 
seq.getMeasuredCoordinate(0, mCoordinate);
assertEquals(0, mCoordinate.getX(), Math.ulp(10d));
assertEquals(0, mCoordinate.getY(), Math.ulp(10d));
assertEquals(Double.NaN, mCoordinate.getZ(), Math.ulp(10d));
assertEquals(Double.NaN, mCoordinate.getM(), Math.ulp(10d));
 
seq.getMeasuredCoordinate(2, mCoordinate);
assertEquals(0, mCoordinate.getX(), Math.ulp(10d));
assertEquals(1, mCoordinate.getY(), Math.ulp(10d));
assertEquals(2, mCoordinate.getZ(), Math.ulp(10d));
assertEquals(3, mCoordinate.getM(), Math.ulp(10d));
 
 
seq.setMeasuredCoordinate(1, MeasuredCoordinate.to2D(0, 1));
seq.getMeasuredCoordinate(1, mCoordinate);
assertEquals(0, mCoordinate.getX(), Math.ulp(10d));
assertEquals(1, mCoordinate.getY(), Math.ulp(10d));
assertEquals(Double.NaN, mCoordinate.getZ(), Math.ulp(10d));
assertEquals(Double.NaN, mCoordinate.getM(), Math.ulp(10d));
 
seq.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(0, 1, 2));
seq.getMeasuredCoordinate(3, mCoordinate);
assertEquals(0, mCoordinate.getX(), Math.ulp(10d));
assertEquals(1, mCoordinate.getY(), Math.ulp(10d));
assertEquals(Double.NaN, mCoordinate.getZ(), Math.ulp(10d));
assertEquals(2, mCoordinate.getM(), Math.ulp(10d));
 
 
}
 
@Test
public void test_clone() {
seq = new MeasuredCoordinateArraySequence(2);
MeasuredCoordinate mc0 = MeasuredCoordinate.to3DM(0, 1, 2, 3);
MeasuredCoordinate mc1 = MeasuredCoordinate.to3DM(4, 5, 6, 7);
seq.setMeasuredCoordinate(0, mc0);
seq.setMeasuredCoordinate(1, mc1);
 
MeasuredCoordinateArraySequence cloned = (MeasuredCoordinateArraySequence) seq.clone();
//check that clone has same values as original
 
assertTrue(mc0.equals3DWithMeasure(cloned.getMeasuredCoordinate(0)));
assertTrue(mc1.equals3DWithMeasure(cloned.getMeasuredCoordinate(1)));
 
//check independence
cloned.setOrdinate(0, CoordinateSequence.M, 10);
cloned.setOrdinate(1, CoordinateSequence.M, 11);
//check that clone has same values as original
assertEquals(3, seq.getOrdinate(0, CoordinateSequence.M), Math.ulp(10d));
assertEquals(7, seq.getOrdinate(1, CoordinateSequence.M), Math.ulp(10d));
 
assertFalse(mc0.equals3DWithMeasure(cloned.getMeasuredCoordinate(0)));
assertFalse(mc1.equals3DWithMeasure(cloned.getMeasuredCoordinate(1)));
}
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/jts/linearref/TestMeasuredLineString.java
New file
0,0 → 1,428
package org.hibernatespatial.jts.linearref;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequenceFactory;
import org.junit.Before;
import org.junit.Test;
 
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 29, 2010
*/
public class TestMeasuredLineString {
 
MeasuredCoordinateSequenceFactory coordinateFactory = new MeasuredCoordinateSequenceFactory();
GeometryFactory geometryFactory = new GeometryFactory(coordinateFactory);
MeasuredLineString mli;
LineString[] testLineStrings;
 
@Before
public void setUp() {
 
MeasuredCoordinateSequence seq0 = coordinateFactory.create(4, 4);
seq0.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(1, 2, 0));
seq0.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(2, 3, 1));
seq0.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(3, 4, 2));
seq0.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(4, 5, 3));
 
//monotonic increasing
MeasuredCoordinateSequence seq1 = coordinateFactory.create(4, 4);
seq1.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(0, 0, 0));
seq1.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(1, 0, 1));
seq1.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(2, 0, 2));
seq1.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(3, 0, 3));
 
//monotonic decreasing
MeasuredCoordinateSequence seq2 = coordinateFactory.create(4, 4);
seq2.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(3, 0, 3));
seq2.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(2, 0, 2));
seq2.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(1, 0, 1));
seq2.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(0, 0, 0));
 
//non-monotonic
MeasuredCoordinateSequence seq3 = coordinateFactory.create(4, 4);
seq3.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(1, 0, 0));
seq3.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(2, 0, 5));
seq3.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(3, 0, 2));
seq3.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(8, 0, 7));
 
//non-measured
MeasuredCoordinateSequence seq4 = coordinateFactory.create(4, 4);
seq4.setMeasuredCoordinate(0, MeasuredCoordinate.to2D(1, 0));
seq4.setMeasuredCoordinate(1, MeasuredCoordinate.to2D(2, 0));
seq4.setMeasuredCoordinate(2, MeasuredCoordinate.to2D(3, 0));
seq4.setMeasuredCoordinate(3, MeasuredCoordinate.to2D(8, 0));
 
//non-strict monotonic increasing
MeasuredCoordinateSequence seq5 = coordinateFactory.create(4, 4);
seq5.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(0, 0, 0));
seq5.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(1, 0, 1));
seq5.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(2, 0, 1));
seq5.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(3, 0, 2));
 
//monotonic decreasing
MeasuredCoordinateSequence seq6 = coordinateFactory.create(4, 4);
seq6.setMeasuredCoordinate(0, MeasuredCoordinate.to2DM(3, 0, 2));
seq6.setMeasuredCoordinate(1, MeasuredCoordinate.to2DM(2, 0, 1));
seq6.setMeasuredCoordinate(2, MeasuredCoordinate.to2DM(1, 0, 1));
seq6.setMeasuredCoordinate(3, MeasuredCoordinate.to2DM(0, 0, 0));
 
 
testLineStrings = new LineString[]{
geometryFactory.createLineString(seq0),
geometryFactory.createLineString(seq1),
geometryFactory.createLineString(seq2),
geometryFactory.createLineString(seq3),
geometryFactory.createLineString(seq4),
geometryFactory.createLineString(seq5),
geometryFactory.createLineString(seq6)
};
 
}
 
@Test
public void test_constructor_on_null() {
try {
mli = new MeasuredLineString(null);
fail();
} catch (IllegalArgumentException e) {
//OK
}
}
 
@Test
public void test_get_measure_at_coordinate() {
 
mli = new MeasuredLineString(testLineStrings[1]);
 
double received = mli.getMeasureAtCoordinate(new Coordinate(1.5d, 0d), 0.01d);
assertEquals(1.5d, received, Math.ulp(5d));
 
received = mli.getMeasureAtCoordinate(new Coordinate(0.0d, 0d), 0.01d);
assertEquals(0d, received, Math.ulp(5d));
 
received = mli.getMeasureAtCoordinate(new Coordinate(2.0d, 0d), 0.01d);
assertEquals(2d, received, Math.ulp(5d));
 
 
received = mli.getMeasureAtCoordinate(new Coordinate(3.009d, 0d), 0.01d);
assertEquals(3d, received, Math.ulp(5d));
 
received = mli.getMeasureAtCoordinate(new Coordinate(5.0d, 0d), 0.01d);
assertTrue(Double.isNaN(received));
 
}
 
@Test
public void test_get_coordinate_at_measure_monotone_increasing() {
 
mli = new MeasuredLineString(testLineStrings[1]);
 
assertNull(mli.getCoordinateAtMeasure(-1d));
assertNull(mli.getCoordinateAtMeasure(3.2d));
 
assertEquals(MeasuredCoordinate.to2DM(0, 0, 0), mli.getCoordinateAtMeasure(0));
 
assertEquals(MeasuredCoordinate.to2DM(0.8, 0, 0.8), mli.getCoordinateAtMeasure(0.8));
assertEquals(MeasuredCoordinate.to2DM(2.1, 0.0, 2.1), mli.getCoordinateAtMeasure(2.1));
assertEquals(MeasuredCoordinate.to2DM(3.0, 0, 3.0), mli.getCoordinateAtMeasure(3));
 
}
 
@Test
public void test_get_coordinate_at_measure_non_strict_monotone_increasing() {
 
mli = new MeasuredLineString(testLineStrings[5]);
assertNull(mli.getCoordinateAtMeasure(-1d));
assertNull(mli.getCoordinateAtMeasure(3.2d));
assertEquals(MeasuredCoordinate.to2DM(0, 0, 0), mli.getCoordinateAtMeasure(0));
assertEquals(MeasuredCoordinate.to2DM(1.0, 0, 1.0), mli.getCoordinateAtMeasure(1.0));
 
}
 
@Test
public void test_get_coordinate_at_measure_monotone_decreasing() {
 
mli = new MeasuredLineString(testLineStrings[2]);
 
assertNull(mli.getCoordinateAtMeasure(-1d));
assertNull(mli.getCoordinateAtMeasure(3.2d));
 
assertEquals(MeasuredCoordinate.to2DM(0, 0, 0), mli.getCoordinateAtMeasure(0));
 
assertEquals(MeasuredCoordinate.to2DM(0.8, 0, 0.8), mli.getCoordinateAtMeasure(0.8));
assertEquals(MeasuredCoordinate.to2DM(2.1, 0.0, 2.1), mli.getCoordinateAtMeasure(2.1));
assertEquals(MeasuredCoordinate.to2DM(3.0, 0, 3.0), mli.getCoordinateAtMeasure(3));
 
}
 
@Test
public void test_get_coordinate_at_measure_non_monotone() {
 
mli = new MeasuredLineString(testLineStrings[3]);
 
assertNull(mli.getCoordinateAtMeasure(-1d));
assertNull(mli.getCoordinateAtMeasure(9d));
 
assertEquals(MeasuredCoordinate.to2DM(1, 0, 0), mli.getCoordinateAtMeasure(0));
 
assertEquals(MeasuredCoordinate.to2DM(1.5, 0, 2.5), mli.getCoordinateAtMeasure(2.5));
assertEquals(MeasuredCoordinate.to2DM(2.0, 0.0, 5.0d), mli.getCoordinateAtMeasure(5.0d));
assertEquals(MeasuredCoordinate.to2DM(7.0, 0, 6.0), mli.getCoordinateAtMeasure(6));
 
}
 
@Test
public void test_min_measure() {
for (int i = 0; i < 4; i++) {
mli = new MeasuredLineString(testLineStrings[i]);
assertEquals(0.0d, mli.getMinMeasure(), Math.ulp(1.0d));
}
mli = new MeasuredLineString(testLineStrings[4]);
assertTrue(Double.isNaN(mli.getMinMeasure()));
 
}
 
@Test
public void test_max_measure() {
for (int i = 0; i < 3; i++) {
mli = new MeasuredLineString(testLineStrings[i]);
assertEquals(3.0d, mli.getMaxMeasure(), Math.ulp(1.0d));
}
 
mli = new MeasuredLineString(testLineStrings[3]);
assertEquals(7.0d, mli.getMaxMeasure(), Math.ulp(1.0d));
 
mli = new MeasuredLineString(testLineStrings[4]);
assertTrue(Double.isNaN(mli.getMaxMeasure()));
 
}
 
@Test
public void test_get_coordinates_between_monotone_increasing() {
 
mli = new MeasuredLineString(testLineStrings[1]);
MeasuredCoordinateSequence[] sequences = mli.getCoordinatesBetween(1.5d, 2.5d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 2.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 2.5d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(0.25d, 0.75d);
assertEquals(1, sequences.length);
assertEquals(2, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.25d, 0.0d, 0.25d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(0.75d, 0.0d, 0.75d), sequences[0].getMeasuredCoordinate(1));
 
sequences = mli.getCoordinatesBetween(0d, 1.5d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(1.5d, 0d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(-1d, 1.5d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(-1d, -0.5d);
assertEquals(1, sequences.length);
assertEquals(0, sequences[0].size());
 
sequences = mli.getCoordinatesBetween(0.5d, 5d);
assertEquals(1, sequences.length);
assertEquals(4, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 2.0d), sequences[0].getMeasuredCoordinate(2));
assertEquals(MeasuredCoordinate.to2DM(3.0d, 0.0d, 3.0d), sequences[0].getMeasuredCoordinate(3));
 
sequences = mli.getCoordinatesBetween(2.5d, 2.75d);
assertEquals(1, sequences.length);
assertEquals(2, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 2.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(2.75d, 0.0d, 2.75d), sequences[0].getMeasuredCoordinate(1));
 
sequences = mli.getCoordinatesBetween(0.0d, 0.0d);
assertEquals(1, sequences.length);
assertEquals(0, sequences[0].size());
 
 
sequences = mli.getCoordinatesBetween(0.0d, 1.0d);
assertEquals(1, sequences.length);
assertEquals(2, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
 
 
}
 
@Test
public void test_get_coordinates_between_monotone_decreasing() {
mli = new MeasuredLineString(testLineStrings[2]);
MeasuredCoordinateSequence[] sequences = mli.getCoordinatesBetween(1.5d, 2.5d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 2.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 2.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(0d, 1.5d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(1.5d, 0d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(-1d, 1.5d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(1.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(2));
 
sequences = mli.getCoordinatesBetween(-1d, -0.5d);
assertEquals(1, sequences.length);
assertEquals(0, sequences[0].size());
 
sequences = mli.getCoordinatesBetween(0.5d, 5d);
assertEquals(1, sequences.length);
assertEquals(4, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(3.0d, 0.0d, 3.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 2.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(2));
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(3));
 
sequences = mli.getCoordinatesBetween(0.0d, 0.0d);
assertEquals(1, sequences.length);
assertEquals(0, sequences[0].size());
 
 
sequences = mli.getCoordinatesBetween(0.0d, 1.0d);
assertEquals(1, sequences.length);
assertEquals(2, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(0.0d, 0.0d, 0.0d), sequences[0].getMeasuredCoordinate(1));
 
 
}
 
@Test
public void test_get_coordinates_between_non_strict_monotone_increasing() {
mli = new MeasuredLineString(testLineStrings[5]);
 
MeasuredCoordinateSequence[] sequences = mli.getCoordinatesBetween(0.5d, 1.5d);
assertEquals(1, sequences.length);
assertEquals(4, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(2));
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(3));
 
sequences = mli.getCoordinatesBetween(0.5d, 1.0d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(2));
 
 
sequences = mli.getCoordinatesBetween(1.5d, 0.5d);
assertEquals(1, sequences.length);
assertEquals(4, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(2));
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(3));
}
 
@Test
public void test_get_coordinates_between_non_strict_monotone_decreasing() {
mli = new MeasuredLineString(testLineStrings[6]);
 
MeasuredCoordinateSequence[] sequences = mli.getCoordinatesBetween(0.5d, 1.5d);
assertEquals(1, sequences.length);
assertEquals(4, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(2));
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(3));
 
sequences = mli.getCoordinatesBetween(0.5d, 1.0d);
assertEquals(1, sequences.length);
assertEquals(3, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(2));
 
 
sequences = mli.getCoordinatesBetween(1.5d, 0.5d);
assertEquals(1, sequences.length);
assertEquals(4, sequences[0].size());
assertEquals(MeasuredCoordinate.to2DM(2.5d, 0.0d, 1.5d), sequences[0].getMeasuredCoordinate(0));
assertEquals(MeasuredCoordinate.to2DM(2.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(1));
assertEquals(MeasuredCoordinate.to2DM(1.0d, 0.0d, 1.0d), sequences[0].getMeasuredCoordinate(2));
assertEquals(MeasuredCoordinate.to2DM(0.5d, 0.0d, 0.5d), sequences[0].getMeasuredCoordinate(3));
}
 
@Test
public void test_get_coordinates_between_non_monotone() {
mli = new MeasuredLineString(testLineStrings[3]);
try {
MeasuredCoordinateSequence[] sequences = mli.getCoordinatesBetween(1.5d, 2.5d);
fail();
} catch(IllegalStateException e){
//OK
}
}
 
@Test
public void test_locate_along(){
mli = new MeasuredLineString(testLineStrings[1]);
Point received = (Point)mli.locateAlong(0.0d);
MeasuredCoordinateSequence mseq = (MeasuredCoordinateSequence)received.getCoordinateSequence();
assertEquals(MeasuredCoordinate.to2DM(0,0,0), mseq.getMeasuredCoordinate(0));
 
received = (Point)mli.locateAlong(0.5d);
mseq = (MeasuredCoordinateSequence)received.getCoordinateSequence();
assertEquals(MeasuredCoordinate.to2DM(0.5,0,0.5), mseq.getMeasuredCoordinate(0));
 
received = (Point)mli.locateAlong(2.5d);
mseq = (MeasuredCoordinateSequence)received.getCoordinateSequence();
assertEquals(MeasuredCoordinate.to2DM(2.5,0,2.5), mseq.getMeasuredCoordinate(0));
 
received = (Point)mli.locateAlong(3.5d);
assertTrue(received.isEmpty());
 
}
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/wkb/TestWKBGeometryType.java
New file
0,0 → 1,25
package org.hibernatespatial.wkb;
 
import org.junit.Test;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public class TestWKBGeometryType {
 
@Test
public void test_code_point(){
WKBGeometryType wgt = WKBGeometryType.valueOf(1);
assertEquals(WKBGeometryType.POINT, wgt);
 
}
 
 
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/wkb/TestWKB.java
New file
0,0 → 1,81
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import org.hibernatespatial.test.EWKTReader;
import org.hibernatespatial.test.GeometryEquality;
import org.junit.Test;
 
import java.util.*;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public class TestWKB {
 
public static final Map<String, String> testcases = new HashMap<String, String>();
public static final EWKTReader wktReader = new EWKTReader();
public static final GeometryEquality equalityTest = new GeometryEquality();
 
static {
testcases.put("POINT(10 3)", "010100000000000000000024400000000000000840");
testcases.put("POINT(1 2 3)", "0101000080000000000000F03F00000000000000400000000000000840");
testcases.put("POINT(1 2 3 4)", "01010000C0000000000000F03F000000000000004000000000000008400000000000001040");
testcases.put("POINTM(1 2 4)", "0101000040000000000000F03F00000000000000400000000000001040");
testcases.put("SRID=4326;POINT(1 2 3 4)", "01010000E0E6100000000000000000F03F000000000000004000000000000008400000000000001040");
testcases.put(" LINESTRING(1 2,3 4,5 6)", "010200000003000000000000000000F03F00000000000000400000000000000840000000000000104000000000000014400000000000001840");
 
}
 
 
@Test
public void test_decode() throws ParseException, UnsupportedConversionException {
for (String wkt : testcases.keySet()) {
Geometry geom;
if (wkt.startsWith("SRID=")) {
String[] tokens=wkt.split(";");
int srid = getSrid(tokens[0]);
geom = wktReader.read(tokens[1]);
geom.setSRID(srid);
}else{
geom = wktReader.read(wkt);
}
Bytes bytes = Bytes.from(testcases.get(wkt));
Geometry received = WKB.fromWKB(bytes);
assertTrue("Failure decoding: " + wkt, equalityTest.test(geom, received));
}
}
 
private int getSrid(String sridDeclaration) {
String[] tokens = sridDeclaration.split("=");
return Integer.parseInt(tokens[1]);
}
 
@Test
public void test_encode() throws ParseException, UnsupportedConversionException {
for (String wkt : testcases.keySet()) {
Geometry geom = wktReader.read(wkt);
Bytes bytes = WKB.toWKB(geom, WKBByteOrder.NDR);
assertEquals("Failure encoding: " + wkt, testcases.get(wkt), bytes.toString());
}
}
 
 
//TODO -- use all test cases -- find a cleaner way to test for ndr/xdr encoding/decoding
@Test
public void test_point_with_ndr_byte_order() throws ParseException, UnsupportedConversionException {
String wkt = "POINT(10 3)";
Geometry geom = wktReader.read(wkt);
Bytes bytes = WKB.toWKB(geom, WKBByteOrder.NDR);
assertNotNull(bytes);
System.out.println("bytes = " + bytes.toString());
assertTrue(equalityTest.test(geom, WKB.fromWKB(bytes)));
 
 
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/test/java/org/hibernatespatial/wkb/TestBytes.java
New file
0,0 → 1,242
package org.hibernatespatial.wkb;
 
import org.junit.Test;
 
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Oct 29, 2010
*/
public class TestBytes {
 
@Test
public void testBytesFromString() {
 
String byteTxt = "0120E6FF";
Bytes bStream = Bytes.from(byteTxt);
 
assertEquals(4, bStream.limit());
 
assertEquals(1, bStream.get());
assertEquals(32, bStream.get());
assertEquals((byte) 230, bStream.get());
assertEquals((byte) 255, bStream.get());
 
}
 
@Test
public void test_bytes_from_odd_length_string() {
String byteTxt = "0120E6FFE";
Bytes bStream = Bytes.from(byteTxt);
assertEquals(4, bStream.limit());
 
}
 
@Test
public void test_throws_NumberFormatException_if_string_not_hexadecimal() {
try {
Bytes.from("02FFKK");
fail();
} catch (NumberFormatException e) {
//OK
}
}
 
@Test
public void test_return_empty_Bytes_on_empty_string() {
Bytes bytes = Bytes.from("");
assertTrue(bytes.isEmpty());
assertEquals(0, bytes.limit());
}
 
@Test
public void test_throw_IllegalArgumentException_on_null_string() {
try {
Bytes bytes = Bytes.from((String) null);
fail();
} catch (IllegalArgumentException e) {
//OK
}
}
 
 
@Test
public void test_toString(){
String hxStr = "037FB4C7";
Bytes bytes = Bytes.from(hxStr);
assertEquals(hxStr, bytes.toString());
 
}
 
@Test
public void test_big_endian_int() {
String hxStr = "037FB4C7";
int expectedValue = 58700999;
 
Bytes bytes = Bytes.from(hxStr);
bytes.setWKBByteOrder(WKBByteOrder.XDR);
assertEquals(expectedValue, bytes.getInt());
}
 
@Test
public void test_string_or_bytes_gives_same_result() {
String hxStr = "037FB4C7";
byte[] byteArray = new byte[]{0x03, 0x7F, (byte) 0xB4, (byte) 0xC7};
int expectedValue = 58700999;
 
Bytes bytes = Bytes.from(hxStr);
bytes.setWKBByteOrder(WKBByteOrder.XDR);
assertEquals(expectedValue, bytes.getInt());
 
Bytes bytesFromArray = Bytes.from(byteArray);
bytesFromArray.setWKBByteOrder(WKBByteOrder.XDR);
assertEquals(expectedValue, bytesFromArray.getInt());
}
 
@Test
public void test_little_endian_int() {
String hxStr = "C7B47F03";
int expectedValue = 58700999;
 
Bytes bytes = Bytes.from(hxStr);
bytes.setWKBByteOrder(WKBByteOrder.NDR);
assertEquals(expectedValue, bytes.getInt());
}
 
@Test
public void test_insufficient_bytes_for_retrieving_long_throws_exception() {
String hxStr = "C7B47F03";
Bytes bytes = Bytes.from(hxStr);
try {
Long lng = bytes.getLong();
} catch (RuntimeException e) {
//OK
}
 
}
 
@Test
public void test_get_unsigned_as_long() {
String hxStr = "FFFFFFFF";
Bytes bytes = Bytes.from(hxStr);
long expected = 4294967295L;
assertEquals(expected, bytes.getUInt());
 
bytes = Bytes.from("80000000");
assertEquals(2147483648L, bytes.getUInt());
 
bytes = Bytes.from("00000001");
assertEquals(1L, bytes.getUInt());
 
bytes = Bytes.from("7FFFFFFF");
assertEquals(2147483647L, bytes.getUInt());
 
}
 
@Test
public void test_retrieve_byteorder() {
String hxStr = "FFFFFFFF";
Bytes bytes = Bytes.from(hxStr);
bytes.setWKBByteOrder(WKBByteOrder.XDR);
assertEquals(WKBByteOrder.XDR, bytes.getWKBByteOrder());
bytes.setWKBByteOrder(WKBByteOrder.NDR);
assertEquals(WKBByteOrder.NDR, bytes.getWKBByteOrder());
}
 
@Test
public void test_create_bytes_having_specified_size() {
Bytes bytes = Bytes.allocate(100);
assertEquals(100, bytes.capacity());
}
 
@Test
public void test_put_xdr_double() {
double expected = 1234.56789;
Bytes bytes = Bytes.allocate(8);
bytes.setWKBByteOrder(WKBByteOrder.XDR);
bytes.putDouble(expected);
bytes.rewind();
double d = bytes.getDouble();
assertEquals(expected, d);
}
 
@Test
public void test_put_ndr_double() {
double expected = 1234.56789;
Bytes bytes = Bytes.allocate(8);
bytes.setWKBByteOrder(WKBByteOrder.NDR);
bytes.putDouble(expected);
bytes.rewind();
double d = bytes.getDouble();
assertEquals(expected, d);
}
 
@Test
public void test_put_byte() {
byte expected = (byte) 0xFF;
Bytes bytes = Bytes.allocate(1);
bytes.put(expected);
bytes.rewind();
assertEquals(expected, bytes.get());
}
 
@Test
public void test_put_int() {
int expected = 0xFF;
Bytes bytes = Bytes.allocate(4);
bytes.putInt(expected);
bytes.rewind();
assertEquals(expected, bytes.getInt());
}
 
@Test
public void test_put_long() {
long expected = Long.MAX_VALUE;
Bytes bytes = Bytes.allocate(8);
bytes.putLong(expected);
bytes.rewind();
assertEquals(expected, bytes.getLong());
}
 
 
@Test
public void test_put_float() {
float expected = 1234.567f;
Bytes bytes = Bytes.allocate(4);
bytes.putFloat(expected);
bytes.rewind();
assertEquals(expected, bytes.getFloat());
}
 
@Test
public void test_put_uint() {
long expected = 4294967295L;
Bytes bytes = Bytes.allocate(4);
bytes.putUInt(expected);
bytes.rewind();
assertEquals(expected, bytes.getUInt());
}
 
@Test
public void test_put_too_large_value_in_uint_throws_exception() {
long value = Bytes.UINT_MAX_VALUE;
Bytes bytes = Bytes.allocate(4);
bytes.putUInt(value);
bytes.rewind();
assertEquals(value, bytes.getUInt());
value++;
Bytes bytes2 = Bytes.allocate(4);
try {
bytes2.putUInt(value);
fail();
} catch (RuntimeException e) {
//OK
}
}
 
 
}
 
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBGeometryType.java
New file
0,0 → 1,32
package org.hibernatespatial.wkb;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public enum WKBGeometryType {
 
POINT((byte)1),
LINESTRING((byte)2);
 
byte code;
 
private WKBGeometryType(byte code){
this.code = code;
}
 
public static WKBGeometryType valueOf(int code){
//drop the higher-order bits
byte codeByte = (byte)code;
for (WKBGeometryType type : values()){
if (type.code == codeByte) return type;
}
throw new IllegalArgumentException("Numeric code " + code + " not known");
 
}
 
 
public long getTypeCode() {
return this.code;
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBLineStringEncoder.java
New file
0,0 → 1,31
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.LineString;
import org.hibernatespatial.jts.geom.CoordinateDimension;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 12, 2010
*/
public class WKBLineStringEncoder extends WKBEncoder<LineString> {
@Override
protected long getGeometryType() {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}
 
@Override
protected void writeNumComponents(LineString geometry, Bytes output) {
//To change body of implemented methods use File | Settings | File Templates.
}
 
@Override
protected void writeComponents(LineString geometry, CoordinateDimension dimension, Bytes output) {
//To change body of implemented methods use File | Settings | File Templates.
}
 
@Override
protected int calculateSize(LineString geom, CoordinateDimension dimension) {
return 0; //To change body of implemented methods use File | Settings | File Templates.
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKB.java
New file
0,0 → 1,80
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import org.hibernatespatial.mgeom.MGeometryFactory;
 
import java.util.HashMap;
import java.util.Map;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Oct 29, 2010
*/
public class WKB {
 
public static final int Z_FLAG = 0x80000000;
public static final int M_FLAG = 0x40000000;
public static final int SRID_FLAG = 0x20000000;
 
 
public static class Encoders {
 
private static final Map ENCODERS = new HashMap();
 
static {
register(Point.class, new WKBPointEncoder());
register(LineString.class, new WKBLineStringEncoder());
 
}
public static <G extends Geometry> void register(Class<G> geometryClass, WKBEncoder<G> encoderClass){
ENCODERS.put(geometryClass, encoderClass);
}
 
public static <G extends Geometry> WKBEncoder<G> getFor(G geometry){
return (WKBEncoder<G>)ENCODERS.get(geometry.getClass());
}
 
}
 
public static class Decoders {
 
private static final Map<WKBGeometryType, WKBElementDecoder> DECODERS = new HashMap<WKBGeometryType, WKBElementDecoder>();
private static final WKBDecoder DECODER;
 
static {
GeometryFactory factory = new MGeometryFactory();
DECODER = new WKBDecoder();
DECODERS.put(WKBGeometryType.POINT, new WKBPointDecoder(factory));
DECODERS.put(WKBGeometryType.LINESTRING, new WKBLineStringDecoder(factory));
}
 
public static WKBElementDecoder getFor(WKBGeometryType type){
return DECODERS.get(type);
}
 
public static WKBDecoder getWKBDecoder(){
return DECODER;
}
 
}
 
 
 
 
public static <G extends Geometry> Bytes toWKB(G geometry) throws UnsupportedConversionException {
return toWKB(geometry, null);
}
 
public static <G extends Geometry> Bytes toWKB(G geometry, WKBByteOrder byteOrder) throws UnsupportedConversionException {
WKBEncoder<G> encoder = Encoders.getFor(geometry);
return encoder.encode(geometry, byteOrder);
}
 
public static Geometry fromWKB(Bytes bytes) {
return Decoders.getWKBDecoder().decode(bytes);
}
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBByteOrder.java
New file
0,0 → 1,44
package org.hibernatespatial.wkb;
 
import java.nio.ByteOrder;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Oct 29, 2010
*/
public enum WKBByteOrder {
 
XDR(ByteOrder.BIG_ENDIAN, (byte)0),
NDR(ByteOrder.LITTLE_ENDIAN, (byte)1);
 
ByteOrder order;
byte orderByte;
private WKBByteOrder(ByteOrder order, byte orderByte){
this.order = order;
this.orderByte = orderByte;
}
 
 
public static WKBByteOrder valueOf(byte orderByte){
for(WKBByteOrder wbo : values()){
if(orderByte == wbo.byteValue()) return wbo;
}
throw new IllegalArgumentException("Order byte must be 0 or 1");
}
 
ByteOrder getByteOrder(){
return this.order;
}
 
byte byteValue() {
return orderByte;
}
 
static WKBByteOrder valueOf(ByteOrder byteOrder){
for (WKBByteOrder wkbOrder : values()){
if (wkbOrder.getByteOrder() == byteOrder) return wkbOrder;
}
throw new IllegalArgumentException();
}
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBElementDecoder.java
New file
0,0 → 1,37
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.hibernatespatial.jts.geom.CoordinateDimension;
import org.hibernatespatial.mgeom.MCoordinate;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 12, 2010
*/
public abstract class WKBElementDecoder {
 
private final GeometryFactory factory;
 
public WKBElementDecoder(GeometryFactory factory){
this.factory = factory;
}
 
protected GeometryFactory factory(){
return this.factory;
}
 
public abstract Geometry decode(Bytes nativeGeom, CoordinateDimension dimension);
 
protected Coordinate readCoordinate(Bytes bytes, CoordinateDimension dimension) {
MCoordinate coordinate = new MCoordinate(bytes.getDouble(), bytes.getDouble());
if (dimension.hasZ()) {
coordinate.z = bytes.getDouble();
}
if (dimension.hasM()){
coordinate.m = bytes.getDouble();
}
return coordinate;
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBEncoder.java
New file
0,0 → 1,90
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import org.hibernatespatial.jts.geom.CoordinateDimension;
import org.hibernatespatial.mgeom.MCoordinate;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public abstract class WKBEncoder<G extends Geometry> {
 
 
public Bytes encode(G geom) {
return encode(geom, null);
}
 
public Bytes encode(G geom, WKBByteOrder wbo) {
CoordinateDimension dimension = CoordinateDimension.valueFor(hasZ(geom), hasM(geom));
Bytes output = Bytes.allocate(calculateSize(geom, dimension));
if (wbo != null) {
output.setWKBByteOrder(wbo);
}
writeByteOrder(output);
writeTypeCodeAndSRID(geom, dimension, output);
writeNumComponents(geom, output);
writeComponents(geom, dimension, output);
output.rewind();
return output;
}
 
protected void writeByteOrder(Bytes output){
output.put(output.getWKBByteOrder().byteValue());
}
 
 
protected void writeCoordinate(Coordinate coordinate, CoordinateDimension dimension, Bytes output) {
output.putDouble(coordinate.x);
output.putDouble(coordinate.y);
if(dimension.hasZ()){
output.putDouble(coordinate.z);
}
if (dimension.hasM()){
double m = 0.0;
if (coordinate instanceof MCoordinate){
m = ((MCoordinate)coordinate).m;
}
output.putDouble(m);
}
}
 
protected boolean hasM(Geometry geom) {
Coordinate co = geom.getCoordinate();
if (co instanceof MCoordinate) {
return ! Double.isNaN(((MCoordinate)co).m);
}
return false;
}
 
protected boolean hasZ(Geometry geom) {
return !Double.isNaN(geom.getCoordinate().z);
}
 
protected void writeTypeCodeAndSRID(G geometry, CoordinateDimension dimension, Bytes output){
long typeCode = getGeometryType();
boolean hasSRID = (geometry.getSRID() > 0);
if (hasSRID)
typeCode |= WKB.SRID_FLAG;
if (dimension.hasM())
typeCode |= WKB.M_FLAG;
if (dimension.hasZ())
typeCode |= WKB.Z_FLAG;
output.putUInt(typeCode);
if(hasSRID){
output.putInt(geometry.getSRID());
}
 
}
 
protected abstract long getGeometryType();
 
protected abstract void writeNumComponents(G geometry, Bytes output);
 
protected abstract void writeComponents(G geometry, CoordinateDimension dimension, Bytes output);
 
protected abstract int calculateSize(G geom, CoordinateDimension dimension);
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBPointDecoder.java
New file
0,0 → 1,26
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.hibernatespatial.jts.geom.CoordinateDimension;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public class WKBPointDecoder extends WKBElementDecoder {
 
public WKBPointDecoder(GeometryFactory factory) {
super(factory);
}
 
public Geometry decode(Bytes bytes, CoordinateDimension dimension) {
Coordinate coordinate = readCoordinate(bytes, dimension);
return factory().createPoint(coordinate);
}
 
 
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/Bytes.java
New file
0,0 → 1,198
package org.hibernatespatial.wkb;
 
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Oct 29, 2010
*/
public class Bytes {
 
static final long UINT_MAX_VALUE = 4294967295L;
final private ByteBuffer buffer;
 
private Bytes(ByteBuffer buffer) {
this.buffer = buffer;
}
 
public static Bytes from(String text) {
if (text == null) throw new IllegalArgumentException("Null not allowed.");
int size = text.length() / 2; // this will drop the last char, if text is not even.
byte[] byteArray = new byte[size];
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
for (int i = 0; i < text.length() - 1; i += 2) {
byte b = (byte) Integer.parseInt(text.substring(i, i + 2), 16);
buffer.put(b);
}
buffer.rewind();
return new Bytes(buffer);
}
 
public String toString(){
StringBuilder builder = new StringBuilder();
rewind();
for (int i = 0; i < limit(); i++){
int hexValue = readByte();
appendByte(builder, hexValue);
}
return builder.toString();
}
 
private void appendByte(StringBuilder builder, int hexValue) {
String byteStr = Integer.toHexString(hexValue).toUpperCase();
if (byteStr.length() == 1 ) {
builder.append("0").append(byteStr);
} else {
builder.append(byteStr);
}
}
 
private int readByte() {
int hexValue = get();
hexValue = (hexValue << 24) >>> 24;
return hexValue;
}
 
public static Bytes from(byte[] bytes){
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return new Bytes(buffer);
}
 
public static Bytes allocate(int capacity) {
return new Bytes(ByteBuffer.allocate(capacity));
}
 
public byte get() {
try{
return this.buffer.get();
} catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
public void put(byte value){
try{
this.buffer.put(value);
}catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
public int capacity(){
return this.buffer.capacity();
}
 
public int limit() {
return this.buffer.limit();
}
 
public Bytes limit(int newLimit) {
buffer.limit(newLimit);
return this;
}
 
public void rewind(){
this.buffer.rewind();
}
 
public boolean isEmpty() {
return buffer.limit() == 0;
}
 
public void setWKBByteOrder(WKBByteOrder wbo){
this.buffer.order(wbo.getByteOrder());
}
 
public int getInt() {
try {
return this.buffer.getInt();
}catch (BufferUnderflowException e){
throw new RuntimeException(e);
}
}
 
public void putInt(int value){
try{
this.buffer.putInt(value);
}catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
 
public long getLong(){
try {
return this.buffer.getLong();
}catch (BufferUnderflowException e){
throw new RuntimeException(e);
}
}
 
public void putLong(long value){
try{
this.buffer.putLong(value);
}catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
public float getFloat(){
try {
return this.buffer.getFloat();
}catch (BufferUnderflowException e){
throw new RuntimeException(e);
}
}
 
public void putFloat(float value){
try{
this.buffer.putFloat(value);
}catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
public double getDouble(){
try {
return this.buffer.getDouble();
}catch (BufferUnderflowException e){
throw new RuntimeException(e);
}
}
 
public void putDouble(Double value){
try{
this.buffer.putDouble(value);
}catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
public long getUInt() {
try {
long value = this.buffer.getInt(); // read integer in a long
return (value << 32) >>> 32; //shift 32-bytes left (dropping all
} catch(BufferUnderflowException e){
throw new RuntimeException(e);
}
}
 
public void putUInt(long value){
if (value > UINT_MAX_VALUE ) throw new RuntimeException("Value received doesn't fit in unsigned integer");
try{
this.buffer.putInt((int)value);
}catch(BufferOverflowException e){
throw new RuntimeException(e);
}
}
 
public WKBByteOrder getWKBByteOrder() {
ByteOrder order = this.buffer.order();
return WKBByteOrder.valueOf(order);
}
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBLineStringDecoder.java
New file
0,0 → 1,29
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.hibernatespatial.jts.geom.CoordinateDimension;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 12, 2010
*/
public class WKBLineStringDecoder extends WKBElementDecoder {
 
public WKBLineStringDecoder(GeometryFactory factory) {
super(factory);
}
 
@Override
public Geometry decode(Bytes bytes, CoordinateDimension dimension) {
int numPoints = bytes.getInt();
CoordinateSequence coordinates = factory().getCoordinateSequenceFactory().create(numPoints, dimension.getNumDimensions());
for (long i = 0; i < numPoints; numPoints++){
Coordinate co = readCoordinate(bytes,dimension);
// TO BE COMPLETED......
}
return factory().createLinearRing(coordinates);
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBDecoder.java
New file
0,0 → 1,57
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Geometry;
import org.hibernatespatial.jts.geom.CoordinateDimension;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public class WKBDecoder {
 
 
public Geometry decode(Bytes bytes){
bytes.rewind();
alignByteOrder(bytes);
int typeCode = readTypeCode(bytes);
WKBGeometryType type = WKBGeometryType.valueOf(typeCode);
int srid = readSRID(bytes, typeCode);
WKBElementDecoder decoder = WKB.Decoders.getFor(type);
Geometry geom = decoder.decode(bytes, getCoordinateDimension(typeCode));
setSRID(geom, srid, typeCode);
return geom;
}
 
private CoordinateDimension getCoordinateDimension(int typeCode){
boolean hasM = (typeCode & WKB.M_FLAG) == WKB.M_FLAG;
boolean hasZ = (typeCode & WKB.Z_FLAG) == WKB.Z_FLAG;
return CoordinateDimension.valueFor(hasZ, hasM);
}
 
private void setSRID(Geometry geom, int srid, int typeCode) {
if (hasSrid(typeCode)) geom.setSRID(srid);
}
 
private int readSRID(Bytes bytes, int typeCode) {
if (hasSrid(typeCode)){
return bytes.getInt();
}
return -1;
}
 
private boolean hasSrid(int typeCode) {
return (typeCode & WKB.SRID_FLAG) == WKB.SRID_FLAG;
}
 
 
private int readTypeCode(Bytes bytes) {
return (int)bytes.getUInt();
}
 
private void alignByteOrder(Bytes bytes) {
byte orderByte = bytes.get();
WKBByteOrder byteOrder = WKBByteOrder.valueOf(orderByte);
bytes.setWKBByteOrder(byteOrder);
}
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/UnsupportedConversionException.java
New file
0,0 → 1,8
package org.hibernatespatial.wkb;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 12, 2010
*/
public class UnsupportedConversionException extends Exception {
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/wkb/WKBPointEncoder.java
New file
0,0 → 1,37
package org.hibernatespatial.wkb;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Point;
import org.hibernatespatial.jts.geom.CoordinateDimension;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 11, 2010
*/
public class WKBPointEncoder extends WKBEncoder<Point> {
 
protected long getGeometryType() {
return WKBGeometryType.POINT.getTypeCode();
}
 
@Override
protected void writeNumComponents(Point geometry, Bytes output) {
}
 
@Override
protected void writeComponents(Point geometry, CoordinateDimension dim, Bytes output) {
Coordinate coordinate = geometry.getCoordinate();
writeCoordinate(coordinate, dim, output);
}
 
 
@Override
protected int calculateSize(Point geom, CoordinateDimension dimension) {
int size = 21; //minimal size
if (geom.getSRID() > 0) size += 4;
if (dimension.hasZ()) size += 8;
if (dimension.hasM()) size += 8;
return size;
}
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/geom/MeasuredCoordinateArraySequence.java
New file
0,0 → 1,183
package org.hibernatespatial.jts.geom;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
 
import java.util.Arrays;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 12, 2010
*/
public class MeasuredCoordinateArraySequence implements MeasuredCoordinateSequence {
 
private final double[] measures;
private final CoordinateSequence coordinates;
 
public MeasuredCoordinateArraySequence(int size){
this.coordinates = new CoordinateArraySequence(size);
this.measures = initMeasures(size);
}
 
public MeasuredCoordinateArraySequence(Coordinate[] coordinates) {
if (coordinates == null)
coordinates = new Coordinate[0];
this.coordinates = new CoordinateArraySequence(coordinates);
this.measures = initMeasures(coordinates.length);
}
 
public MeasuredCoordinateArraySequence(CoordinateSequence coordinateSequence){
this.coordinates = new CoordinateArraySequence(coordinateSequence);
if (coordinateSequence instanceof MeasuredCoordinateSequence){
MeasuredCoordinateSequence sequence = (MeasuredCoordinateSequence) coordinateSequence;
this.measures = copyMeasures(sequence);
} else {
this.measures = initMeasures(this.coordinates.size());
}
}
 
public MeasuredCoordinateArraySequence(CoordinateSequence coordinateSequence, double[] measures){
this.coordinates = new CoordinateArraySequence(coordinateSequence);
if (measures == null) {
measures = initMeasures(0);
}
if (this.coordinates.size() != measures.length) {
throw new IllegalArgumentException("CoordinateSequence and measure-array must have the same length.");
}
this.measures = measures;
}
 
public MeasuredCoordinateArraySequence(MeasuredCoordinateSequence sequence){
this.coordinates = new CoordinateArraySequence(sequence);
this.measures = copyMeasures(sequence);
}
 
public MeasuredCoordinateArraySequence(MeasuredCoordinate[] measuredCoordinates){
this(measuredCoordinates == null ? 0 : measuredCoordinates.length);
for (int i = 0; i < this.coordinates.size(); i++) {
setMeasuredCoordinate(i, measuredCoordinates[i]);
}
}
 
private double[] copyMeasures(MeasuredCoordinateSequence sequence) {
if (sequence == null){
return new double[0];
}
double[] measures = sequence.toMeasureArray();
double[] copied = new double[measures.length];
System.arraycopy(measures, 0, copied, 0, measures.length);
return copied;
}
 
public MeasuredCoordinateArraySequence(Coordinate[] coordinates, double[] mvalues) {
this.coordinates = new CoordinateArraySequence(coordinates);
if (mvalues == null){
mvalues = initMeasures(0);
}
if (this.coordinates.size() != mvalues.length) {
throw new IllegalArgumentException("Coordinate array and value array must be of the same length");
}
this.measures = mvalues;
}
 
public int getDimension() {
return 4;
}
 
public Coordinate getCoordinate(int i) {
return this.coordinates.getCoordinate(i);
}
 
public Coordinate getCoordinateCopy(int i) {
return this.coordinates.getCoordinateCopy(i);
}
 
public void getCoordinate(int index, Coordinate coord) {
this.coordinates.getCoordinate(index, coord);
}
 
//Keep this??
public void getMeasuredCoordinate(int index, MeasuredCoordinate measuredCoordinate) {
for (int i = 0; i < getDimension(); i++){
measuredCoordinate.setOrdinate(i, this.getOrdinate(index, i));
}
}
 
public double getX(int index) {
return this.coordinates.getX(index);
}
 
public double getY(int index) {
return this.coordinates.getY(index);
}
 
public double getZ(int index){
return this.coordinates.getCoordinate(index).z;
}
 
public double getM(int index){
return this.measures[index];
}
 
public double getOrdinate(int index, int ordinateIndex) {
if (ordinateIndex == CoordinateSequence.M){
return measures[index];
}
return this.coordinates.getOrdinate(index, ordinateIndex);
}
 
public int size() {
return this.coordinates.size();
}
 
public void setOrdinate(int index, int ordinateIndex, double value) {
if (ordinateIndex == CoordinateSequence.M){
measures[index] = value;
return;
}
this.coordinates.setOrdinate(index,ordinateIndex, value);
}
 
public Coordinate[] toCoordinateArray() {
return this.coordinates.toCoordinateArray();
}
 
public double[] toMeasureArray() {
return this.measures;
}
 
public Envelope expandEnvelope(Envelope env) {
return this.coordinates.expandEnvelope(env);
}
 
public void setMeasuredCoordinate(int index, MeasuredCoordinate coordinate) {
setOrdinate(index, CoordinateSequence.M, coordinate.getM());
setOrdinate(index, CoordinateSequence.X, coordinate.getX());
setOrdinate(index, CoordinateSequence.Y, coordinate.getY());
setOrdinate(index, CoordinateSequence.Z, coordinate.getZ());
}
 
public MeasuredCoordinate getMeasuredCoordinate(int index) {
return MeasuredCoordinate.from(this.coordinates.getCoordinate(index), this.measures[index]);
}
 
private double[] initMeasures(int size) {
double[] measures = new double[size];
for (int i = 0; i < size; i++) {
measures[i] = Double.NaN;
}
return measures;
}
 
@Override
public Object clone() {
CoordinateSequence seq = (CoordinateSequence)this.coordinates.clone();
if (measures == null){
return new MeasuredCoordinateArraySequence(seq);
}
return new MeasuredCoordinateArraySequence(seq, Arrays.copyOf(measures, measures.length));
}
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/geom/MeasuredCoordinateSequence.java
New file
0,0 → 1,22
package org.hibernatespatial.jts.geom;
 
import com.vividsolutions.jts.geom.CoordinateSequence;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 24, 2010
*/
public interface MeasuredCoordinateSequence extends CoordinateSequence {
 
public double getZ(int index);
 
public double getM(int index);
 
public void setMeasuredCoordinate(int index, MeasuredCoordinate coordinate);
 
public MeasuredCoordinate getMeasuredCoordinate(int index);
 
public void getMeasuredCoordinate(int index, MeasuredCoordinate measuredCoordinate);
 
public double[] toMeasureArray();
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/geom/CoordinateDimension.java
New file
0,0 → 1,43
package org.hibernatespatial.jts.geom;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 12, 2010
*/
//TODO -- move to top-level
public enum CoordinateDimension {
 
XY (false, false),
XYZ (true, false),
XYM (false, true),
XYZM(true, true);
 
private final boolean hasZ;
private final boolean hasM;
 
private CoordinateDimension(boolean hasZ, boolean hasM){
this.hasZ = hasZ;
this.hasM = hasM;
}
 
public boolean hasZ(){
return hasZ;
}
 
public boolean hasM(){
return hasM;
}
 
public int getNumDimensions(){
if (hasM() && hasZ()) return 4;
if (hasZ() || hasM()) return 3;
return 2;
}
 
public static CoordinateDimension valueFor(boolean hasZ, boolean hasM){
for(CoordinateDimension dim : values()){
if (dim.hasZ == hasZ && dim.hasM == hasM) return dim;
}
throw new IllegalStateException("Non-existent CoordinateDimension"); //this is never thrown.
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/geom/MeasuredCoordinate.java
New file
0,0 → 1,206
package org.hibernatespatial.jts.geom;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
 
/**
* A wrapper for <code>Coordinate</code>s that associates it with a measure value.
*
* @author Karel Maesen, Geovise BVBA
*
*/
public class MeasuredCoordinate {
 
private double m = Coordinate.NULL_ORDINATE;
private Coordinate coordinate;
 
public MeasuredCoordinate(Coordinate c, double m) {
this.coordinate = c;
this.m = m;
}
 
public MeasuredCoordinate() {
this.coordinate = new Coordinate();
this.m = Double.NaN;
}
 
public Coordinate getCoordinate(){
return this.coordinate;
}
 
public double getX() {
return this.coordinate.x;
}
 
public double getY() {
return this.coordinate.y;
}
 
public double getZ() {
return this.coordinate.z;
}
 
public double getM() {
return m;
}
 
/**
* Returns the ordinate value specified in this Coordinate instance. The
* index of the desired ordinates are specified in the CoordinateSequence
* class; hence CoodinateSequence.X returns the x ordinate,
* CoodinateSequence.Y the y ordinate, CoodinateSequence.Z the z ordinate,
* and CoodinateSequence.M the m ordinate. Note that the dimension may not
* imply the desired ordinate in the case where one is using a 2 dimensional
* geometry with a measure value. Therefore, these constants are highly
* recommended.
*
* @param ordinateIndex the desired ordinate index.
* @return the value of stored in the ordinate index. Incorrect or unused
* indexes shall return Double.NaN
*/
public double getOrdinate(int ordinateIndex) {
switch (ordinateIndex) {
case CoordinateSequence.X:
return this.coordinate.x;
case CoordinateSequence.Y:
return this.coordinate.y;
case CoordinateSequence.Z:
return this.coordinate.z;
case CoordinateSequence.M:
return this.m;
}
return Double.NaN;
}
 
/**
* the value for a given ordinate. This should be specified using the
* CoordinateSequence ordinate index constants.
*
* @param ordinateIndex the desired ordinate index.
* @param value the new ordinate value
* @throws IllegalArgumentException if the ordinateIndex value is incorrect
* @see #getOrdinate(int)
*/
public void setOrdinate(int ordinateIndex, double value) {
switch (ordinateIndex) {
case CoordinateSequence.X:
this.coordinate.x = value;
break;
case CoordinateSequence.Y:
this.coordinate.y = value;
break;
case CoordinateSequence.Z:
this.coordinate.z = value;
break;
case CoordinateSequence.M:
this.m = value;
break;
default:
throw new IllegalArgumentException("invalid ordinateIndex");
}
}
 
public boolean equals2DWithMeasure(MeasuredCoordinate other) {
return this.coordinate.equals2D(other.coordinate) && (Double.compare(this.m, other.m) == 0) ;
}
 
public boolean equals3DWithMeasure(MeasuredCoordinate other) {
return this.coordinate.equals3D(other.coordinate) &&
(Double.compare(this.m, other.m) == 0);
}
 
public String toString() {
return "(" + this.coordinate.x + "," + this.coordinate.y + "," + this.coordinate.z + "," + " m=" + this.m + ")";
}
 
public static MeasuredCoordinate from(Coordinate c) {
return new MeasuredCoordinate(c, Double.NaN);
}
 
public static MeasuredCoordinate from(Coordinate c, double m){
return new MeasuredCoordinate(c, m);
}
 
/**
* A convenience method for creating a MeasuredCoordinate instance where there are
* only 2 coordinates and an lrs measure value. The z value of the
* coordinate shall be set to Double.NaN
*
* @param x the x coordinate value
* @param y the y coordinate value
* @param m the lrs measure value
* @return The constructed MCoordinate value
*/
public static MeasuredCoordinate to2DM(double x, double y, double m) {
Coordinate c = new Coordinate(x,y);
return new MeasuredCoordinate(c, m);
}
 
/**
* A convenience method for creating a MCoordinate instance where there are
* only 2 coordinates and an lrs measure value. The z and m value of the
* coordinate shall be set to Double.NaN
*
* @param x the x coordinate value
* @param y the y coordinate value
* @return The constructed MCoordinate value
*/
public static MeasuredCoordinate to2D(double x, double y) {
Coordinate c = new Coordinate(x,y);
return new MeasuredCoordinate(c, Double.NaN);
}
 
/**
* A convenience method for creating a MCoordinate instance where there are
* 3 coordinates and an lrs measure value.
*
* @param x the x coordinate value
* @param y the y coordinate value
* @param z the z coordinate value
* @param m the lrs measure value
* @return The constructed MCoordinate value
*/
public static MeasuredCoordinate to3DM(double x, double y, double z,
double m) {
Coordinate c = new Coordinate(x, y, z);
return new MeasuredCoordinate(c, m);
}
 
/**
* A convenience method for creating a MCoordinate instance where there are
* 3 coordinates but no lrs measure value. The m value of the coordinate
* shall be set to Double.NaN
*
* @param x the x coordinate value
* @param y the y coordinate value
* @param z the z coordinate value
* @return The constructed MCoordinate value
*/
public static MeasuredCoordinate to3D(double x, double y, double z) {
Coordinate c = new Coordinate(x,y,z);
return new MeasuredCoordinate(c, Double.NaN);
}
 
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
 
MeasuredCoordinate that = (MeasuredCoordinate) o;
 
if (Double.compare(that.m, m) != 0) return false;
if (coordinate != null ? !coordinate.equals(that.coordinate) : that.coordinate != null) return false;
 
return true;
}
 
@Override
public int hashCode() {
int result;
long temp;
temp = m != +0.0d ? Double.doubleToLongBits(m) : 0L;
result = (int) (temp ^ (temp >>> 32));
result = 31 * result + (coordinate != null ? coordinate.hashCode() : 0);
return result;
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/geom/MeasuredCoordinateSequenceFactory.java
New file
0,0 → 1,33
package org.hibernatespatial.jts.geom;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 22, 2010
*/
public class MeasuredCoordinateSequenceFactory implements CoordinateSequenceFactory {
public MeasuredCoordinateSequence create(Coordinate[] coordinates) {
return new MeasuredCoordinateArraySequence(coordinates);
}
 
public MeasuredCoordinateSequence create(CoordinateSequence coordSeq) {
return new MeasuredCoordinateArraySequence(coordSeq);
}
 
public MeasuredCoordinateSequence create(int size, int dimension) {
return new MeasuredCoordinateArraySequence(size);
}
 
public MeasuredCoordinateSequence create(Coordinate[] coordinates, double[] measures){
return new MeasuredCoordinateArraySequence(coordinates, measures);
}
 
public MeasuredCoordinateSequence create(MeasuredCoordinate[] measuredCoordinates){
return new MeasuredCoordinateArraySequence(measuredCoordinates);
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/linearref/MeasuredLineSegment.java
New file
0,0 → 1,146
package org.hibernatespatial.jts.linearref;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineSegment;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Dec 1, 2010
*/
public class MeasuredLineSegment {
 
private final LineSegment segment;
private final MeasuredCoordinate start;
private final MeasuredCoordinate end;
 
MeasuredLineSegment(MeasuredCoordinate start, MeasuredCoordinate end) {
this.start = start;
this.end = end;
this.segment = new LineSegment(start.getCoordinate(), end.getCoordinate());
}
 
public double getStartM() {
return start.getM();
}
 
public double getEndM() {
return end.getM();
}
 
public double measureAlong(double factor) {
return getStartM() + factor * (getEndM() - getStartM());
}
 
public double segmentFraction(double measure) {
double fraction = (measure - getStartM()) / (getEndM() - getStartM());
 
if (fraction < 0d) return 0d;
if (fraction > 1d) return 1d;
if (Double.isNaN(fraction)) return 1d; //ensures that if measure=startM and endM == startM, the end measure is selected
return fraction;
}
 
public MeasuredCoordinate getMeasuredCoordinate(double measure) {
double fraction = this.segmentFraction(measure);
return measuredPointAlong(fraction);
}
 
public MeasuredCoordinate measuredPointAlong(double fraction) {
Coordinate co = this.pointAlong(fraction);
double measure = this.measureAlong(fraction);
return new MeasuredCoordinate(co, measure);
}
 
public boolean measureInSegment(double measure) {
return (getStartM() <= measure && measure <= getEndM()) ||
getStartM() >= measure && measure >= getEndM();
}
 
 
//Delegated methods.
 
public Coordinate intersection(LineSegment line) {
return segment.intersection(line);
}
 
public double getLength() {
return segment.getLength();
}
 
public boolean isHorizontal() {
return segment.isHorizontal();
}
 
public boolean isVertical() {
return segment.isVertical();
}
 
public int orientationIndex(LineSegment seg) {
return segment.orientationIndex(seg);
}
 
public int orientationIndex(Coordinate p) {
return segment.orientationIndex(p);
}
 
public void reverse() {
segment.reverse();
}
 
public void normalize() {
segment.normalize();
}
 
public double angle() {
return segment.angle();
}
 
public Coordinate midPoint() {
return segment.midPoint();
}
 
public double distance(LineSegment ls) {
return segment.distance(ls);
}
 
public double distance(Coordinate p) {
return segment.distance(p);
}
 
public double distancePerpendicular(Coordinate p) {
return segment.distancePerpendicular(p);
}
 
public Coordinate pointAlong(double segmentLengthFraction) {
return segment.pointAlong(segmentLengthFraction);
}
 
public Coordinate pointAlongOffset(double segmentLengthFraction, double offsetDistance) {
return segment.pointAlongOffset(segmentLengthFraction, offsetDistance);
}
 
public double projectionFactor(Coordinate p) {
return segment.projectionFactor(p);
}
 
public double segmentFraction(Coordinate inputPt) {
return segment.segmentFraction(inputPt);
}
 
public Coordinate project(Coordinate p) {
return segment.project(p);
}
 
public LineSegment project(LineSegment seg) {
return segment.project(seg);
}
 
public Coordinate closestPoint(Coordinate p) {
return segment.closestPoint(p);
}
 
public Coordinate[] closestPoints(LineSegment line) {
return segment.closestPoints(line);
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/linearref/MeasuredGeometry.java
New file
0,0 → 1,46
package org.hibernatespatial.jts.linearref;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import org.hibernatespatial.jts.geom.CoordinateDimension;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequence;
 
import java.lang.reflect.Method;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 29, 2010
*/
public interface MeasuredGeometry {
 
 
 
public Geometry locateAlong(double measure);
 
public Geometry locateBetween();
 
public double getMeasureAtCoordinate(Coordinate c, double tolerance);
 
/**
* Returns the <code>MeasuredCoordinate</code> at the specified measure.
*
* <p>In case of lineal <code>MeasuredGeometry</code>s, the coordinate is determined by interpolation on the linesegments
* of the curve, or (a curve in case of multi-curve geometries).
* </p>
* <p>When the measures are non-monotonic, the first interpolated coordinate will be returned.</p>
* @param measure
* @return
*/
public MeasuredCoordinate getCoordinateAtMeasure(double measure);
 
MeasuredCoordinateSequence[] getCoordinatesBetween(double begin, double end);
 
public double getMinMeasure();
 
public double getMaxMeasure();
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/linearref/MeasuredLineString.java
New file
0,0 → 1,187
package org.hibernatespatial.jts.linearref;
 
import com.vividsolutions.jts.geom.*;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
import org.hibernatespatial.jts.geom.MeasuredCoordinateArraySequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequence;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequenceFactory;
 
import java.util.ArrayList;
import java.util.List;
 
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: Nov 29, 2010
*/
public class MeasuredLineString implements MeasuredGeometry {
 
 
private final MeasuredCoordinateSequence coordinates;
private final GeometryFactory geometryFactory;
private final MeasuredCoordinateSequenceFactory coordinateSequenceFactory;
 
public MeasuredLineString(LineString lineString) {
if (lineString == null) {
throw new IllegalArgumentException("Non-null LineString required.");
}
this.geometryFactory = lineString.getFactory();
if (geometryFactory.getCoordinateSequenceFactory() instanceof MeasuredCoordinateSequenceFactory){
this.coordinateSequenceFactory = (MeasuredCoordinateSequenceFactory) geometryFactory.getCoordinateSequenceFactory();
} else {
this.coordinateSequenceFactory = new MeasuredCoordinateSequenceFactory();
}
 
CoordinateSequence sequence = lineString.getCoordinateSequence();
if (!(sequence instanceof MeasuredCoordinateSequence)) {
coordinates = new MeasuredCoordinateArraySequence(sequence);
return;
}
this.coordinates = (MeasuredCoordinateSequence) sequence;
}
 
 
public Geometry locateAlong(double measure) {
MeasuredCoordinate measuredCoordinate = getCoordinateAtMeasure(measure);
MeasuredCoordinate[] mca = null;
if (measuredCoordinate != null)
mca = new MeasuredCoordinate[]{measuredCoordinate};
MeasuredCoordinateSequence seq = this.coordinateSequenceFactory.create(mca);
return geometryFactory.createPoint(seq);
}
 
public Geometry locateBetween() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
 
public double getMeasureAtCoordinate(Coordinate c, double tolerance) {
MeasuredCoordinate measuredCoordinate = getClosestCoordinateAt(c, tolerance);
if (measuredCoordinate == null)
return Double.NaN;
return measuredCoordinate.getM();
}
 
public MeasuredCoordinate getCoordinateAtMeasure(double measure) {
 
for (MeasuredLineSegment segment : new MeasuredLineSegments(this.coordinates)) {
if (segment.measureInSegment(measure)) {
return segment.getMeasuredCoordinate(measure);
}
}
return null;
}
 
public MeasuredCoordinateSequence[] getCoordinatesBetween(double begin, double end) {
if (begin == end)
return new MeasuredCoordinateSequence[]{new MeasuredCoordinateArraySequence(0)};
List<MeasuredCoordinate> coordinates = new ArrayList<MeasuredCoordinate>();
boolean segmentStartVertexAdded = false;
double previousSegmentDirection = 0; //measure direction (increasing/decreasing) of previous segment
for (MeasuredLineSegment segment : new MeasuredLineSegments(this.coordinates)) {
previousSegmentDirection = getMeasureDirectionAndCompare(previousSegmentDirection, segment);
//Stop iterating if the begin/end measures are smaller(larger) than the segment's measures if increasing (decreasing)
if (stopIterating(begin, end, previousSegmentDirection, segment))
break;
double beginFraction = segment.segmentFraction(begin);
double endFraction = segment.segmentFraction(end);
if (beginFraction == endFraction) { //begin & end both to the left or right of current segment
continue;
}
addCoordinates(coordinates, segmentStartVertexAdded, segment, beginFraction, endFraction);
//start vertex of next segment already added?
segmentStartVertexAdded = (beginFraction == 1.0d || endFraction == 1.0d);
}
return new MeasuredCoordinateSequence[]{new MeasuredCoordinateArraySequence(coordinates.toArray(new MeasuredCoordinate[]{}))};
}
 
private boolean stopIterating(double begin, double end, double previousSegmentDirection, MeasuredLineSegment segment) {
return (previousSegmentDirection > 0 && begin < segment.getStartM() && end < segment.getStartM()) ||
(previousSegmentDirection < 0 && begin > segment.getStartM() && end > segment.getStartM());
}
 
private double getMeasureDirectionAndCompare(double previousSegmentDirection, MeasuredLineSegment segment) {
double segmentDirection = getSegmentDirection(segment);
if (!segmentDirectionSameAsPreviousSegment(previousSegmentDirection, segmentDirection))
throw new IllegalStateException("Measures needs to be monotonic increasing/decreasing for this method.");
if (segmentDirection != 0.0d) return segmentDirection;
return previousSegmentDirection;
}
 
private boolean segmentDirectionSameAsPreviousSegment(double previousSegmentDirection, double segmentDirection) {
return segmentDirection == 0 || previousSegmentDirection == 0 || segmentDirection == previousSegmentDirection;
}
 
/**
* Returns the segment direction: -1 when decreasing, +1 when increasing or 0 when constant.
*/
private double getSegmentDirection(MeasuredLineSegment segment) {
return Math.signum(segment.getEndM() - segment.getStartM());
}
 
private void addCoordinates(List<MeasuredCoordinate> coordinates, boolean startVertexIncluded, MeasuredLineSegment segment, double beginFraction, double endFraction) {
if (beginFraction < endFraction) {
addCoordinate(coordinates, startVertexIncluded, segment, beginFraction);
addCoordinate(coordinates, startVertexIncluded, segment, endFraction);
} else {
addCoordinate(coordinates, startVertexIncluded, segment, endFraction);
addCoordinate(coordinates, startVertexIncluded, segment, beginFraction);
}
}
 
/**
* Add coordinate. If it is the start-vertex of the segment, only add if it has not already been added (indicated by startVertexIncluded).
*/
private void addCoordinate(List<MeasuredCoordinate> coordinates, boolean startVertexIncluded, MeasuredLineSegment segment, double fraction) {
if (!startVertexIncluded || fraction != 0.0d)
coordinates.add(segment.measuredPointAlong(fraction));
}
 
public double getMinMeasure() {
double minX = Double.POSITIVE_INFINITY;
boolean hasMeasures = false;
for (int i = 0; i < this.coordinates.size(); i++) {
double m = this.coordinates.getOrdinate(i, CoordinateSequence.M);
if (Double.isNaN(m)) {
continue;
}
hasMeasures = true;
minX = Math.min(minX, m);
}
return hasMeasures ? minX : Double.NaN;
}
 
public double getMaxMeasure() {
double minX = Double.NEGATIVE_INFINITY;
boolean hasMeasures = false;
for (int i = 0; i < this.coordinates.size(); i++) {
double m = this.coordinates.getOrdinate(i, CoordinateSequence.M);
if (Double.isNaN(m)) {
continue;
}
hasMeasures = true;
minX = Math.max(minX, m);
}
return hasMeasures ? minX : Double.NaN;
}
 
protected MeasuredCoordinate getClosestCoordinateAt(Coordinate c, double tolerance) {
Coordinate currentClosest = null;
double currentMeasure = Double.NaN;
double currentMinimum = Double.POSITIVE_INFINITY;
for (MeasuredLineSegment segment : new MeasuredLineSegments(this.coordinates)) {
Coordinate closest = segment.closestPoint(c);
double distance = closest.distance(c);
if (distance < currentMinimum) {
currentMinimum = distance;
currentClosest = closest;
double factor = segment.projectionFactor(closest);
currentMeasure = segment.measureAlong(factor);
}
}
if (currentMinimum > tolerance || currentClosest == null) {
return null;
}
return new MeasuredCoordinate(currentClosest, currentMeasure);
}
 
 
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/jts/linearref/LineSegments.java
New file
0,0 → 1,61
package org.hibernatespatial.jts.linearref;
 
import com.vividsolutions.jts.geom.LineSegment;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;
import org.hibernatespatial.jts.geom.MeasuredCoordinateSequence;
 
import java.util.Iterator;
 
/**
* Allows fast iteration over the <code>LineSegment</code>s of a LineString.
*
* The <code>LineSegment</code> instance returned by the <code>Iterator</code> is re-used
* on each iteration.
*
* @author Karel Maesen, Geovise BVBA
* creation-date: Dec 1, 2010
*/
class MeasuredLineSegments implements Iterable<MeasuredLineSegment> {
 
private final MeasuredCoordinateSequence coordinates;
 
MeasuredLineSegments(MeasuredCoordinateSequence coordinates) {
this.coordinates = coordinates;
if (this.coordinates == null || this.coordinates.size() < 2)
throw new IllegalArgumentException("Coordinate sequence needs to be at least of length 2.");
}
 
 
public MeasuredLineSegmentIterator iterator() {
return new MeasuredLineSegmentIterator(coordinates);
}
 
private static class MeasuredLineSegmentIterator implements Iterator<MeasuredLineSegment> {
 
private final MeasuredCoordinateSequence coordinates;
private final MeasuredLineSegment currentSegment;
private final MeasuredCoordinate p0 = new MeasuredCoordinate();
private final MeasuredCoordinate p1 = new MeasuredCoordinate();
private int index = 0;
 
public MeasuredLineSegmentIterator(MeasuredCoordinateSequence coordinates) {
this.currentSegment = new MeasuredLineSegment(p0, p1);
this.coordinates = coordinates;
}
 
 
public boolean hasNext() {
return (index < coordinates.size() - 1 );
}
 
public MeasuredLineSegment next() {
this.coordinates.getMeasuredCoordinate(this.index++, p0);
this.coordinates.getMeasuredCoordinate(this.index, p1);
return this.currentSegment;
}
 
public void remove() {
throw new UnsupportedOperationException("Remove operation not supported.");
}
}
}
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MGeometryFactory.java
29,6 → 29,7
package org.hibernatespatial.mgeom;
 
import com.vividsolutions.jts.geom.*;
import org.hibernatespatial.mgeom.MCoordinate;
 
/**
* Extension of the GeometryFactory for constructing Geometries with Measure
36,6 → 37,7
*
* @see com.vividsolutions.jts.geom.GeometryFactory
*/
@Deprecated
public class MGeometryFactory extends GeometryFactory {
 
/**
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MCoordinate.java
43,6 → 43,7
*
* @see com.vividsolutions.jts.geom.Coordinate
*/
@Deprecated
public class MCoordinate extends Coordinate {
/**
*
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/EventLocator.java
32,6 → 32,7
import java.util.ArrayList;
import java.util.List;
 
@Deprecated
public class EventLocator {
 
/**
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MCoordinateSequenceFactory.java
33,11 → 33,13
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
import org.hibernatespatial.mgeom.MCoordinate;
 
/**
* Creates MCoordinateSequenceFactory internally represented as an array of
* Creates MeasuredCoordinateSequenceFactory internally represented as an array of
* {@link MCoordinate}s.
*/
@Deprecated
public class MCoordinateSequenceFactory implements CoordinateSequenceFactory,
Serializable {
 
52,7 → 54,7
}
 
/**
* Returns the singleton instance of MCoordinateSequenceFactory
* Returns the singleton instance of MeasuredCoordinateSequenceFactory
*/
public static MCoordinateSequenceFactory instance() {
return instance;
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MultiMLineString.java
25,7 → 25,9
package org.hibernatespatial.mgeom;
 
import com.vividsolutions.jts.geom.*;
import org.hibernatespatial.mgeom.MCoordinate;
 
@Deprecated
public class MultiMLineString extends MultiLineString implements MGeometry {
 
/**
139,7 → 141,7
 
com.vividsolutions.jts.geom.Point p = this.getFactory().createPoint(co);
 
// find points within tolerance for getMatCoordinate
// find points within tolerance for getMeasureatCoordinate
for (int i = 0; i < this.getNumGeometries(); i++) {
MLineString ml = (MLineString) this.getGeometryN(i);
// go to next MLineString if the input point is beyond tolerance
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MGeometryException.java
33,6 → 33,7
*
*
*/
@Deprecated
public class MGeometryException extends Exception {
 
/**
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MGeometry.java
41,6 → 41,7
* @author Karel Maesen
*/
 
@Deprecated
public interface MGeometry extends Cloneable, Serializable {
 
/**
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MLineString.java
29,6 → 29,7
package org.hibernatespatial.mgeom;
 
import com.vividsolutions.jts.geom.*;
import org.hibernatespatial.mgeom.MCoordinate;
 
import java.util.*;
 
40,6 → 41,7
*
* @author Karel Maesen
*/
@Deprecated
public class MLineString extends LineString implements MGeometry {
 
/**
199,7 → 201,7
/*
* (non-Javadoc)
*
* @see com.vividsolutions.jts.geom.Geometry#getMatCoordinate(com.vividsolutions.jts.geom.Coordinate,
* @see com.vividsolutions.jts.geom.Geometry#getMeasureatCoordinate(com.vividsolutions.jts.geom.Coordinate,
* double)
*/
public double getMatCoordinate(Coordinate c, double tolerance)
/branches/jts_measured_geometry/hibernate-spatial/src/main/java/org/hibernatespatial/mgeom/MCoordinateSequence.java
39,6 → 39,7
* Coordinates returned by #toArray and #get are live -- parties that change
* them are actually changing the MCoordinateSequence's underlying data.
*/
@Deprecated
public class MCoordinateSequence implements CoordinateSequence, Serializable {
/**
*