Skip to content

Commit

Permalink
Merge pull request #8 from ngageoint/develop
Browse files Browse the repository at this point in the history
Develop to Master, 1.0.2 updates
  • Loading branch information
bosborn committed Apr 18, 2016
2 parents dc67b30 + a347f75 commit c24b9ff
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Adheres to [Semantic Versioning](http://semver.org/).

---

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

* TBD
* Geometry to JSON Compatible object utility

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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ View the latest [Javadoc](http://ngageoint.github.io/geopackage-wkb-java/docs/ap

### Installation ###

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

<dependency>
<groupId>mil.nga</groupId>
<artifactId>wkb</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>

### Build ###
Expand Down
231 changes: 231 additions & 0 deletions src/main/java/mil/nga/wkb/util/GeometryJSONCompatible.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package mil.nga.wkb.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mil.nga.wkb.geom.CircularString;
import mil.nga.wkb.geom.CompoundCurve;
import mil.nga.wkb.geom.Geometry;
import mil.nga.wkb.geom.GeometryCollection;
import mil.nga.wkb.geom.GeometryType;
import mil.nga.wkb.geom.LineString;
import mil.nga.wkb.geom.MultiLineString;
import mil.nga.wkb.geom.MultiPoint;
import mil.nga.wkb.geom.MultiPolygon;
import mil.nga.wkb.geom.Point;
import mil.nga.wkb.geom.Polygon;
import mil.nga.wkb.geom.PolyhedralSurface;
import mil.nga.wkb.geom.TIN;
import mil.nga.wkb.geom.Triangle;

/**
* JSON compatible object representation of a Geometry
*
* @author osbornb
* @since 1.0.2
*/
public class GeometryJSONCompatible {

/**
* Get a Geometry object that is JSON compatible
*
* @param geometry
* geometry
* @return geometry JSON object
*/
public static Object getJSONCompatibleGeometry(Geometry geometry) {

Map<String, Object> jsonObject = new HashMap<>();

Object geometryObject = null;

GeometryType geometryType = geometry.getGeometryType();
switch (geometryType) {
case POINT:
geometryObject = getPoint((Point) geometry);
break;
case LINESTRING:
geometryObject = getLineString((LineString) geometry);
break;
case POLYGON:
geometryObject = getPolygon((Polygon) geometry);
break;
case MULTIPOINT:
geometryObject = getMultiPoint((MultiPoint) geometry);
break;
case MULTILINESTRING:
geometryObject = getMultiLineString((MultiLineString) geometry);
break;
case MULTIPOLYGON:
geometryObject = getMultiPolygon((MultiPolygon) geometry);
break;
case CIRCULARSTRING:
geometryObject = getLineString((CircularString) geometry);
break;
case COMPOUNDCURVE:
geometryObject = getCompoundCurve((CompoundCurve) geometry);
break;
case POLYHEDRALSURFACE:
geometryObject = getPolyhedralSurface((PolyhedralSurface) geometry);
break;
case TIN:
geometryObject = getPolyhedralSurface((TIN) geometry);
break;
case TRIANGLE:
geometryObject = getPolygon((Triangle) geometry);
break;
case GEOMETRYCOLLECTION:
List<Object> jsonGeoCollectionObject = new ArrayList<>();
@SuppressWarnings("unchecked")
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
List<Geometry> geometries = geomCollection.getGeometries();
for (int i = 0; i < geometries.size(); i++) {
Geometry subGeometry = geometries.get(i);
jsonGeoCollectionObject
.add(getJSONCompatibleGeometry(subGeometry));
}
geometryObject = jsonGeoCollectionObject;
break;
default:
}

if (geometryObject != null) {
jsonObject.put(geometryType.getName(), geometryObject);
}

return jsonObject;
}

/**
* Get Point object
*
* @param point
* @return point object
*/
private static Object getPoint(Point point) {
Map<String, Double> jsonObject = new HashMap<>();
jsonObject.put("x", point.getX());
jsonObject.put("y", point.getY());
if (point.hasZ()) {
jsonObject.put("z", point.getZ());
}
if (point.hasM()) {
jsonObject.put("m", point.getM());
}
return jsonObject;
}

/**
* Get MultiPoint object
*
* @param multiPoint
* @return multi point object
*/
private static Object getMultiPoint(MultiPoint multiPoint) {
List<Object> jsonObject = new ArrayList<>();
List<Point> points = multiPoint.getPoints();
for (int i = 0; i < points.size(); i++) {
Point point = points.get(i);
jsonObject.add(getPoint(point));
}
return jsonObject;
}

/**
* Get LineString object
*
* @param lineString
* @return line string object
*/
private static Object getLineString(LineString lineString) {
List<Object> jsonObject = new ArrayList<>();
for (Point point : lineString.getPoints()) {
jsonObject.add(getPoint(point));
}
return jsonObject;
}

/**
* Get MultiLineString object
*
* @param multiLineString
* @return multi line string object
*/
private static Object getMultiLineString(MultiLineString multiLineString) {
List<Object> jsonObject = new ArrayList<>();
List<LineString> lineStrings = multiLineString.getLineStrings();
for (int i = 0; i < lineStrings.size(); i++) {
LineString lineString = lineStrings.get(i);
jsonObject.add(getLineString(lineString));
}
return jsonObject;
}

/**
* Get Polygon object
*
* @param polygon
* @return polygon object
*/
private static Object getPolygon(Polygon polygon) {
List<Object> jsonObject = new ArrayList<>();
List<LineString> rings = polygon.getRings();
for (int i = 0; i < rings.size(); i++) {
LineString ring = rings.get(i);
jsonObject.add(getLineString(ring));
}
return jsonObject;
}

/**
* Get MultiPolygon object
*
* @param multiPolygon
* @return multi polygon object
*/
private static Object getMultiPolygon(MultiPolygon multiPolygon) {
List<Object> jsonObject = new ArrayList<>();
List<Polygon> polygons = multiPolygon.getPolygons();
for (int i = 0; i < polygons.size(); i++) {
Polygon polygon = polygons.get(i);
jsonObject.add(getPolygon(polygon));
}
return jsonObject;
}

/**
* Get CompoundCurve object
*
* @param compoundCurve
* @return compound curve object
*/
private static Object getCompoundCurve(CompoundCurve compoundCurve) {
List<Object> jsonObject = new ArrayList<>();
List<LineString> lineStrings = compoundCurve.getLineStrings();
for (int i = 0; i < lineStrings.size(); i++) {
LineString lineString = lineStrings.get(i);
jsonObject.add(getLineString(lineString));
}
return jsonObject;
}

/**
* Get PolyhedralSurface object
*
* @param polyhedralSurface
* @return polyhedral surface object
*/
private static Object getPolyhedralSurface(
PolyhedralSurface polyhedralSurface) {
List<Object> jsonObject = new ArrayList<>();
List<Polygon> polygons = polyhedralSurface.getPolygons();
for (int i = 0; i < polygons.size(); i++) {
Polygon polygon = polygons.get(i);
jsonObject.add(getPolygon(polygon));
}
return jsonObject;
}

}

0 comments on commit c24b9ff

Please sign in to comment.