Skip to content

Commit c24b9ff

Browse files
committed
Merge pull request #8 from ngageoint/develop
Develop to Master, 1.0.2 updates
2 parents dc67b30 + a347f75 commit c24b9ff

File tree

3 files changed

+235
-4
lines changed

3 files changed

+235
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Adheres to [Semantic Versioning](http://semver.org/).
44

55
---
66

7-
## 1.0.2 (TBD)
7+
## [1.0.2](https://github.com/ngageoint/geopackage-wkb-java/releases/tag/1.0.2) (04-18-2016)
88

9-
* TBD
9+
* Geometry to JSON Compatible object utility
1010

1111
## [1.0.1](https://github.com/ngageoint/geopackage-wkb-java/releases/tag/1.0.1) (11-20-2015)
1212

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ View the latest [Javadoc](http://ngageoint.github.io/geopackage-wkb-java/docs/ap
3838

3939
### Installation ###
4040

41-
Pull from the [Maven Central Repository](http://search.maven.org/#artifactdetails|mil.nga|wkb|1.0.1|jar) (JAR, POM, Source, Javadoc)
41+
Pull from the [Maven Central Repository](http://search.maven.org/#artifactdetails|mil.nga|wkb|1.0.2|jar) (JAR, POM, Source, Javadoc)
4242

4343
<dependency>
4444
<groupId>mil.nga</groupId>
4545
<artifactId>wkb</artifactId>
46-
<version>1.0.1</version>
46+
<version>1.0.2</version>
4747
</dependency>
4848

4949
### Build ###
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
package mil.nga.wkb.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import mil.nga.wkb.geom.CircularString;
9+
import mil.nga.wkb.geom.CompoundCurve;
10+
import mil.nga.wkb.geom.Geometry;
11+
import mil.nga.wkb.geom.GeometryCollection;
12+
import mil.nga.wkb.geom.GeometryType;
13+
import mil.nga.wkb.geom.LineString;
14+
import mil.nga.wkb.geom.MultiLineString;
15+
import mil.nga.wkb.geom.MultiPoint;
16+
import mil.nga.wkb.geom.MultiPolygon;
17+
import mil.nga.wkb.geom.Point;
18+
import mil.nga.wkb.geom.Polygon;
19+
import mil.nga.wkb.geom.PolyhedralSurface;
20+
import mil.nga.wkb.geom.TIN;
21+
import mil.nga.wkb.geom.Triangle;
22+
23+
/**
24+
* JSON compatible object representation of a Geometry
25+
*
26+
* @author osbornb
27+
* @since 1.0.2
28+
*/
29+
public class GeometryJSONCompatible {
30+
31+
/**
32+
* Get a Geometry object that is JSON compatible
33+
*
34+
* @param geometry
35+
* geometry
36+
* @return geometry JSON object
37+
*/
38+
public static Object getJSONCompatibleGeometry(Geometry geometry) {
39+
40+
Map<String, Object> jsonObject = new HashMap<>();
41+
42+
Object geometryObject = null;
43+
44+
GeometryType geometryType = geometry.getGeometryType();
45+
switch (geometryType) {
46+
case POINT:
47+
geometryObject = getPoint((Point) geometry);
48+
break;
49+
case LINESTRING:
50+
geometryObject = getLineString((LineString) geometry);
51+
break;
52+
case POLYGON:
53+
geometryObject = getPolygon((Polygon) geometry);
54+
break;
55+
case MULTIPOINT:
56+
geometryObject = getMultiPoint((MultiPoint) geometry);
57+
break;
58+
case MULTILINESTRING:
59+
geometryObject = getMultiLineString((MultiLineString) geometry);
60+
break;
61+
case MULTIPOLYGON:
62+
geometryObject = getMultiPolygon((MultiPolygon) geometry);
63+
break;
64+
case CIRCULARSTRING:
65+
geometryObject = getLineString((CircularString) geometry);
66+
break;
67+
case COMPOUNDCURVE:
68+
geometryObject = getCompoundCurve((CompoundCurve) geometry);
69+
break;
70+
case POLYHEDRALSURFACE:
71+
geometryObject = getPolyhedralSurface((PolyhedralSurface) geometry);
72+
break;
73+
case TIN:
74+
geometryObject = getPolyhedralSurface((TIN) geometry);
75+
break;
76+
case TRIANGLE:
77+
geometryObject = getPolygon((Triangle) geometry);
78+
break;
79+
case GEOMETRYCOLLECTION:
80+
List<Object> jsonGeoCollectionObject = new ArrayList<>();
81+
@SuppressWarnings("unchecked")
82+
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
83+
List<Geometry> geometries = geomCollection.getGeometries();
84+
for (int i = 0; i < geometries.size(); i++) {
85+
Geometry subGeometry = geometries.get(i);
86+
jsonGeoCollectionObject
87+
.add(getJSONCompatibleGeometry(subGeometry));
88+
}
89+
geometryObject = jsonGeoCollectionObject;
90+
break;
91+
default:
92+
}
93+
94+
if (geometryObject != null) {
95+
jsonObject.put(geometryType.getName(), geometryObject);
96+
}
97+
98+
return jsonObject;
99+
}
100+
101+
/**
102+
* Get Point object
103+
*
104+
* @param point
105+
* @return point object
106+
*/
107+
private static Object getPoint(Point point) {
108+
Map<String, Double> jsonObject = new HashMap<>();
109+
jsonObject.put("x", point.getX());
110+
jsonObject.put("y", point.getY());
111+
if (point.hasZ()) {
112+
jsonObject.put("z", point.getZ());
113+
}
114+
if (point.hasM()) {
115+
jsonObject.put("m", point.getM());
116+
}
117+
return jsonObject;
118+
}
119+
120+
/**
121+
* Get MultiPoint object
122+
*
123+
* @param multiPoint
124+
* @return multi point object
125+
*/
126+
private static Object getMultiPoint(MultiPoint multiPoint) {
127+
List<Object> jsonObject = new ArrayList<>();
128+
List<Point> points = multiPoint.getPoints();
129+
for (int i = 0; i < points.size(); i++) {
130+
Point point = points.get(i);
131+
jsonObject.add(getPoint(point));
132+
}
133+
return jsonObject;
134+
}
135+
136+
/**
137+
* Get LineString object
138+
*
139+
* @param lineString
140+
* @return line string object
141+
*/
142+
private static Object getLineString(LineString lineString) {
143+
List<Object> jsonObject = new ArrayList<>();
144+
for (Point point : lineString.getPoints()) {
145+
jsonObject.add(getPoint(point));
146+
}
147+
return jsonObject;
148+
}
149+
150+
/**
151+
* Get MultiLineString object
152+
*
153+
* @param multiLineString
154+
* @return multi line string object
155+
*/
156+
private static Object getMultiLineString(MultiLineString multiLineString) {
157+
List<Object> jsonObject = new ArrayList<>();
158+
List<LineString> lineStrings = multiLineString.getLineStrings();
159+
for (int i = 0; i < lineStrings.size(); i++) {
160+
LineString lineString = lineStrings.get(i);
161+
jsonObject.add(getLineString(lineString));
162+
}
163+
return jsonObject;
164+
}
165+
166+
/**
167+
* Get Polygon object
168+
*
169+
* @param polygon
170+
* @return polygon object
171+
*/
172+
private static Object getPolygon(Polygon polygon) {
173+
List<Object> jsonObject = new ArrayList<>();
174+
List<LineString> rings = polygon.getRings();
175+
for (int i = 0; i < rings.size(); i++) {
176+
LineString ring = rings.get(i);
177+
jsonObject.add(getLineString(ring));
178+
}
179+
return jsonObject;
180+
}
181+
182+
/**
183+
* Get MultiPolygon object
184+
*
185+
* @param multiPolygon
186+
* @return multi polygon object
187+
*/
188+
private static Object getMultiPolygon(MultiPolygon multiPolygon) {
189+
List<Object> jsonObject = new ArrayList<>();
190+
List<Polygon> polygons = multiPolygon.getPolygons();
191+
for (int i = 0; i < polygons.size(); i++) {
192+
Polygon polygon = polygons.get(i);
193+
jsonObject.add(getPolygon(polygon));
194+
}
195+
return jsonObject;
196+
}
197+
198+
/**
199+
* Get CompoundCurve object
200+
*
201+
* @param compoundCurve
202+
* @return compound curve object
203+
*/
204+
private static Object getCompoundCurve(CompoundCurve compoundCurve) {
205+
List<Object> jsonObject = new ArrayList<>();
206+
List<LineString> lineStrings = compoundCurve.getLineStrings();
207+
for (int i = 0; i < lineStrings.size(); i++) {
208+
LineString lineString = lineStrings.get(i);
209+
jsonObject.add(getLineString(lineString));
210+
}
211+
return jsonObject;
212+
}
213+
214+
/**
215+
* Get PolyhedralSurface object
216+
*
217+
* @param polyhedralSurface
218+
* @return polyhedral surface object
219+
*/
220+
private static Object getPolyhedralSurface(
221+
PolyhedralSurface polyhedralSurface) {
222+
List<Object> jsonObject = new ArrayList<>();
223+
List<Polygon> polygons = polyhedralSurface.getPolygons();
224+
for (int i = 0; i < polygons.size(); i++) {
225+
Polygon polygon = polygons.get(i);
226+
jsonObject.add(getPolygon(polygon));
227+
}
228+
return jsonObject;
229+
}
230+
231+
}

0 commit comments

Comments
 (0)