org.locationtech.jts.io.geojson.GeoJsonWriter Maven / Gradle / Ivy
/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.io.geojson;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.util.Assert;
/**
* Writes {@link Geometry}s as JSON fragments in GeoJson format.
*
* @author Martin Davis
* @author Paul Howells, Vivid Solutions
*/
public class GeoJsonWriter {
/**
* The prefix for EPSG codes in the crs
property.
*/
public static final String EPSG_PREFIX = "EPSG:";
private double scale;
private boolean isEncodeCRS = true;
/**
* Constructs a GeoJsonWriter instance.
*/
public GeoJsonWriter() {
this(8);
}
/**
* Constructs a GeoJsonWriter instance specifying the number of decimals to
* use when encoding floating point numbers.
*
* @param decimals the number of decimal places to output
*/
public GeoJsonWriter(int decimals) {
this.scale = Math.pow(10, decimals);
}
/**
* Sets whether the GeoJSON crs
property should
* be output.
* The value of the property is taken from geometry SRID.
*
* @param isEncodeCRS true if the crs property should be output
*/
public void setEncodeCRS(boolean isEncodeCRS) {
this.isEncodeCRS = isEncodeCRS;
}
/**
* Writes a {@link Geometry} in GeoJson format to a String.
*
* @param geometry the geometry to write
* @return String GeoJson Encoded Geometry
*/
public String write(Geometry geometry) {
StringWriter writer = new StringWriter();
try {
write(geometry, writer);
} catch (IOException ex) {
Assert.shouldNeverReachHere();
}
return writer.toString();
}
/**
* Writes a {@link Geometry} in GeoJson format into a {@link Writer}.
*
* @param geometry
* Geometry to encode
* @param writer
* Stream to encode to.
* @throws IOException
* throws an IOException when unable to write the JSON string
*/
public void write(Geometry geometry, Writer writer) throws IOException {
Map map = create(geometry, isEncodeCRS);
JSONObject.writeJSONString(map, writer);
writer.flush();
}
private Map create(Geometry geometry, boolean encodeCRS) {
Map result = new LinkedHashMap();
result.put(GeoJsonConstants.NAME_TYPE, geometry.getGeometryType());
if (geometry instanceof Point) {
Point point = (Point) geometry;
final String jsonString = getJsonString(point.getCoordinateSequence());
result.put(GeoJsonConstants.NAME_COORDINATES, new JSONAware() {
public String toJSONString() {
return jsonString;
}
});
} else if (geometry instanceof LineString) {
LineString lineString = (LineString) geometry;
final String jsonString = getJsonString(lineString
.getCoordinateSequence());
result.put(GeoJsonConstants.NAME_COORDINATES, new JSONAware() {
public String toJSONString() {
return jsonString;
}
});
} else if (geometry instanceof Polygon) {
Polygon polygon = (Polygon) geometry;
result.put(GeoJsonConstants.NAME_COORDINATES, makeJsonAware(polygon));
} else if (geometry instanceof MultiPoint) {
MultiPoint multiPoint = (MultiPoint) geometry;
result.put(GeoJsonConstants.NAME_COORDINATES, makeJsonAware(multiPoint));
} else if (geometry instanceof MultiLineString) {
MultiLineString multiLineString = (MultiLineString) geometry;
result.put(GeoJsonConstants.NAME_COORDINATES, makeJsonAware(multiLineString));
} else if (geometry instanceof MultiPolygon) {
MultiPolygon multiPolygon = (MultiPolygon) geometry;
result.put(GeoJsonConstants.NAME_COORDINATES, makeJsonAware(multiPolygon));
} else if (geometry instanceof GeometryCollection) {
GeometryCollection geometryCollection = (GeometryCollection) geometry;
ArrayList