|
From: <svn...@os...> - 2012-01-09 03:10:49
|
Author: jdeolive
Date: 2012-01-08 19:10:43 -0800 (Sun, 08 Jan 2012)
New Revision: 38470
Modified:
trunk/modules/unsupported/geojson/src/main/java/org/geotools/geojson/geom/GeometryJSON.java
trunk/modules/unsupported/geojson/src/test/java/org/geotools/geojson/GeometryJSONTest.java
Log:
GEOT-3990, 3d writing support for for geojson encoder, patch submitted by James Chamberlain
Modified: trunk/modules/unsupported/geojson/src/main/java/org/geotools/geojson/geom/GeometryJSON.java
===================================================================
--- trunk/modules/unsupported/geojson/src/main/java/org/geotools/geojson/geom/GeometryJSON.java 2012-01-09 03:10:16 UTC (rev 38469)
+++ trunk/modules/unsupported/geojson/src/main/java/org/geotools/geojson/geom/GeometryJSON.java 2012-01-09 03:10:43 UTC (rev 38470)
@@ -16,6 +16,7 @@
*/
package org.geotools.geojson.geom;
+import com.vividsolutions.jts.geom.Coordinate;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -28,7 +29,6 @@
import org.geotools.geojson.GeoJSONUtil;
import org.geotools.geojson.IContentHandler;
-import org.json.simple.JSONArray;
import org.json.simple.JSONAware;
import org.opengis.geometry.BoundingBox;
@@ -713,12 +713,17 @@
}
for (int i = 0; i < seq.size(); i++) {
+ Coordinate coord = seq.getCoordinate(i);
sb.append("[");
- formatDecimal(seq.getX(i), sb);
+ formatDecimal(coord.x, sb);
sb.append(",");
- formatDecimal(seq.getY(i), sb);
+ formatDecimal(coord.y, sb);
+ if (!Double.isNaN(coord.z)) {
+ sb.append(",");
+ formatDecimal(coord.z, sb);
+ }
sb.append("],");
}
sb.setLength(sb.length()-1);
@@ -738,10 +743,15 @@
}
for (int i = 0; i < seq.size(); i++) {
+ Coordinate coord = seq.getCoordinate(i);
out.write("[");
- out.write(String.valueOf(seq.getX(i)));
+ out.write(String.valueOf(coord.x));
out.write(",");
- out.write(String.valueOf(seq.getY(i)));
+ out.write(String.valueOf(coord.y));
+ if (!Double.isNaN(coord.z)){
+ out.write(",");
+ out.write(String.valueOf(coord.z));
+ }
out.write("]");
if (i < seq.size()-1) {
out.write(",");
Modified: trunk/modules/unsupported/geojson/src/test/java/org/geotools/geojson/GeometryJSONTest.java
===================================================================
--- trunk/modules/unsupported/geojson/src/test/java/org/geotools/geojson/GeometryJSONTest.java 2012-01-09 03:10:16 UTC (rev 38469)
+++ trunk/modules/unsupported/geojson/src/test/java/org/geotools/geojson/GeometryJSONTest.java 2012-01-09 03:10:43 UTC (rev 38470)
@@ -43,8 +43,9 @@
public void testPointWrite() throws Exception {
assertEquals(pointText(), gjson.toString(point()));
+ assertEquals(point3dText(), gjson.toString(point3d()));
}
-
+
String pointText() {
return strip("{'type': 'Point','coordinates':[100.1,0.1]}");
}
@@ -54,14 +55,25 @@
return p;
}
+ String point3dText() {
+ return strip("{'type': 'Point','coordinates':[100.1,0.1,10.2]}");
+ }
+
+ Point point3d() {
+ Point p = gf.createPoint(new Coordinate(100.1, 0.1, 10.2));
+ return p;
+ }
+
public void testPointRead() throws Exception {
assertTrue(point().equals(gjson.readPoint(reader(pointText()))));
+ assertTrue(point3d().equals(gjson.readPoint(reader(point3dText()))));
}
-
+
public void testLineWrite() throws Exception {
assertEquals(lineText(), gjson.toString(line()));
+ assertEquals(line3dText(), gjson.toString(line3d()));
}
-
+
String lineText() {
return strip(
"{'type': 'LineString', 'coordinates': [[100.1,0.1],[101.1,1.1]]}");
@@ -72,13 +84,25 @@
return l;
}
+ String line3dText() {
+ return strip(
+ "{'type': 'LineString', 'coordinates': [[100.1,0.1,10.2],[101.1,1.1,10.2]]}");
+ }
+
+ LineString line3d() {
+ LineString l = gf.createLineString(array(new double[][]{{100.1, 0.1, 10.2},{101.1,1.1, 10.2}}));
+ return l;
+ }
+
public void testLineRead() throws Exception {
assertTrue(line().equals(gjson.readLine(reader(lineText()))));
+ assertTrue(line3d().equals(gjson.readLine(reader(line3dText()))));
}
-
+
public void testPolyWrite() throws Exception {
assertEquals(polygonText1(), gjson.toString(polygon1()));
assertEquals(polygonText2(), gjson.toString(polygon2()));
+ assertEquals(polygonText3(), gjson.toString(polygon3()));
}
Polygon polygon2() {
@@ -90,6 +114,25 @@
{100.2, 0.2}, {100.8, 0.2}, {100.8, 0.8}, {100.2, 0.8}, {100.2, 0.2}}))});
return poly;
}
+
+ Polygon polygon3() {
+ Polygon poly;
+ poly = gf.createPolygon(gf.createLinearRing(
+ array(new double[][]{
+ {100.1, 0.1, 10.2}, {101.1, 0.1, 11.2}, {101.1, 1.1, 11.2}, {100.1, 1.1, 10.2}, {100.1, 0.1, 10.2}})),
+ new LinearRing[]{ gf.createLinearRing(array(new double[][]{
+ {100.2, 0.2, 10.2}, {100.8, 0.2, 11.2}, {100.8, 0.8, 11.2}, {100.2, 0.8, 10.2}, {100.2, 0.2, 10.2}}))});
+ return poly;
+ }
+
+ String polygonText3() {
+ return strip("{ 'type': 'Polygon',"+
+ " 'coordinates': ["+
+ " [ [100.1, 0.1, 10.2], [101.1, 0.1, 11.2], [101.1, 1.1, 11.2], [100.1, 1.1, 10.2], [100.1, 0.1, 10.2] ],"+
+ " [ [100.2, 0.2, 10.2], [100.8, 0.2, 11.2], [100.8, 0.8, 11.2], [100.2, 0.8, 10.2], [100.2, 0.2, 10.2] ]"+
+ " ]"+
+ " }");
+ }
String polygonText1() {
return strip("{ 'type': 'Polygon',"+
@@ -118,10 +161,12 @@
public void testPolyRead() throws Exception {
assertTrue(polygon1().equals(gjson.readPolygon(reader(polygonText1()))));
assertTrue(polygon2().equals(gjson.readPolygon(reader(polygonText2()))));
+ assertTrue(polygon3().equals(gjson.readPolygon(reader(polygonText3()))));
}
public void testMultiPointWrite() throws Exception {
assertEquals(multiPointText(), gjson.toString(multiPoint()));
+ assertEquals(multiPoint3dText(), gjson.toString(multiPoint3d()));
}
String multiPointText() {
@@ -136,12 +181,26 @@
return mpoint;
}
+ String multiPoint3dText() {
+ return strip(
+ "{ 'type': 'MultiPoint',"+
+ "'coordinates': [ [100.1, 0.1, 10.2], [101.1, 1.1, 11.2] ]"+
+ "}");
+ }
+
+ MultiPoint multiPoint3d() {
+ MultiPoint mpoint = gf.createMultiPoint(array(new double[][]{{100.1, 0.1, 10.2}, {101.1, 1.1, 11.2}}));
+ return mpoint;
+ }
+
public void testMultiPointRead() throws Exception {
assertTrue(multiPoint().equals(gjson.readMultiPoint(reader(multiPointText()))));
+ assertTrue(multiPoint3d().equals(gjson.readMultiPoint(reader(multiPoint3dText()))));
}
public void testMultiLineWrite() throws Exception {
assertEquals(multiLineText(), gjson.toString(multiLine()));
+ assertEquals(multiLine3dText(), gjson.toString(multiLine3d()));
}
String multiLineText() {
@@ -162,12 +221,32 @@
return mline;
}
+ String multiLine3dText() {
+ return strip(
+ "{ 'type': 'MultiLineString',"+
+ " 'coordinates': ["+
+ " [ [100.1, 0.1, 10.2], [101.1, 1.1, 10.2] ],"+
+ " [ [102.1, 2.1, 11.2], [103.1, 3.1, 11.2] ]"+
+ " ]"+
+ " }");
+ }
+
+ MultiLineString multiLine3d() {
+ MultiLineString mline = gf.createMultiLineString(new LineString[]{
+ gf.createLineString(array(new double[][]{{100.1, 0.1, 10.2}, {101.1, 1.1, 10.2}})),
+ gf.createLineString(array(new double[][]{{102.1, 2.1, 11.2}, {103.1, 3.1, 11.2}}))
+ });
+ return mline;
+ }
+
public void testMultiLineRead() throws Exception {
assertTrue(multiLine().equals(gjson.readMultiLine(reader(multiLineText()))));
+ assertTrue(multiLine3d().equals(gjson.readMultiLine(reader(multiLine3dText()))));
}
public void testMultiPolygonWrite() throws Exception {
assertEquals(multiPolygonText(), gjson.toString(multiPolygon()));
+ assertEquals(multiPolygon3dText(), gjson.toString(multiPolygon3d()));
}
String multiPolygonText() {
@@ -193,12 +272,37 @@
return mpoly;
}
+ String multiPolygon3dText() {
+ return strip(
+ "{ 'type': 'MultiPolygon',"+
+ " 'coordinates': ["+
+ " [[[102.1, 2.1, 10.2], [103.1, 2.1, 10.2], [103.1, 3.1, 10.2], [102.1, 3.1, 10.2], [102.1, 2.1, 10.2]]],"+
+ " [[[100.1, 0.1, 10.2], [101.1, 0.1, 10.2], [101.1, 1.1, 10.2], [100.1, 1.1, 10.2], [100.1, 0.1, 10.2]],"+
+ " [[100.2, 0.2, 10.2], [100.8, 0.2, 10.2], [100.8, 0.8, 10.2], [100.2, 0.8, 10.2], [100.2, 0.2, 10.2]]]"+
+ " ]"+
+ " }");
+ }
+
+ MultiPolygon multiPolygon3d() {
+ MultiPolygon mpoly = gf.createMultiPolygon(new Polygon[]{
+ gf.createPolygon(gf.createLinearRing(
+ array(new double[][]{{102.1, 2.1, 10.2}, {103.1, 2.1, 10.2}, {103.1, 3.1, 10.2}, {102.1, 3.1, 10.2}, {102.1, 2.1, 10.2}})),null),
+ gf.createPolygon(gf.createLinearRing(
+ array(new double[][]{{100.1, 0.1, 10.2}, {101.1, 0.1, 10.2}, {101.1, 1.1, 10.2}, {100.1, 1.1, 10.2}, {100.1, 0.1, 10.2}})),
+ new LinearRing[]{gf.createLinearRing(
+ array(new double[][]{{100.2, 0.2, 10.2}, {100.8, 0.2, 10.2}, {100.8, 0.8, 10.2}, {100.2, 0.8, 10.2}, {100.2, 0.2, 10.2}}))})
+ });
+ return mpoly;
+ }
+
public void testMultiPolygonRead() throws IOException {
assertTrue(multiPolygon().equals(gjson.readMultiPolygon(reader(multiPolygonText()))));
+ assertTrue(multiPolygon3d().equals(gjson.readMultiPolygon(reader(multiPolygon3dText()))));
}
public void testGeometryCollectionWrite() throws Exception {
assertEquals(collectionText(), gjson.toString(collection()));
+ assertEquals(collection3dText(), gjson.toString(collection3d()));
}
private String collectionText() {
@@ -223,21 +327,52 @@
return gcol;
}
+ private String collection3dText() {
+ return strip(
+ "{ 'type': 'GeometryCollection',"+
+ " 'geometries': ["+
+ " { 'type': 'Point',"+
+ " 'coordinates': [100.1, 0.1, 10.2]"+
+ " },"+
+ " { 'type': 'LineString',"+
+ " 'coordinates': [ [101.1, 0.1, 10.2], [102.1, 1.1, 11.2] ]"+
+ " }"+
+ " ]"+
+ " }");
+ }
+
+ GeometryCollection collection3d() {
+ GeometryCollection gcol = gf.createGeometryCollection(new Geometry[]{
+ gf.createPoint(new Coordinate(100.1,0.1, 10.2)),
+ gf.createLineString(array(new double[][]{{101.1, 0.1, 10.2}, {102.1, 1.1, 11.2}}))
+ });
+ return gcol;
+ }
+
public void testGeometryCollectionRead() throws Exception {
assertEqual(collection(),
(GeometryCollection)gjson.readGeometryCollection(reader(collectionText())));
+ assertEqual(collection3d(),
+ (GeometryCollection)gjson.readGeometryCollection(reader(collection3dText())));
}
public void testRead() throws Exception {
assertTrue(point().equals(gjson.read(reader(pointText()))));
+ assertTrue(point3d().equals(gjson.read(reader(point3dText()))));
assertTrue(line().equals(gjson.read(reader(lineText()))));
+ assertTrue(line3d().equals(gjson.read(reader(line3dText()))));
assertTrue(polygon1().equals(gjson.read(reader(polygonText1()))));
assertTrue(polygon2().equals(gjson.read(reader(polygonText2()))));
+ assertTrue(polygon3().equals(gjson.read(reader(polygonText3()))));
assertTrue(multiPoint().equals(gjson.read(reader(multiPointText()))));
+ assertTrue(multiPoint3d().equals(gjson.read(reader(multiPoint3dText()))));
assertTrue(multiLine().equals(gjson.read(reader(multiLineText()))));
+ assertTrue(multiLine3d().equals(gjson.read(reader(multiLine3dText()))));
assertTrue(multiPolygon().equals(gjson.read(reader(multiPolygonText()))));
+ assertTrue(multiPolygon3d().equals(gjson.read(reader(multiPolygon3dText()))));
assertEqual(collection(), (GeometryCollection) gjson.read(reader(collectionText())));
+ assertEqual(collection3d(), (GeometryCollection) gjson.read(reader(collection3dText())));
}
void assertEqual(GeometryCollection col1, GeometryCollection col2) {
|