Blame | Last modification | View Log | RSS feed
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");}@Testpublic 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]);}@Testpublic 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@Testpublic 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)));}}