/** * Write a java.util.List out as a JSON Array. The values of the array will be converted using * ike standard primitive conversions. If the list contains List or Map objects, they will be * serialized as JSON Arrays and JSON Objects respectively. * * @param list a java.util.List to be serialized as JSON Array */ public JSONBuilder writeList(final List list) { this.array(); for (final Object o : list) { this.value(o); } return this.endArray(); }
/** Encodes linked features as a JSON array. */ private void encodeLinkedFeatures( String attributeName, List<Map<NameImpl, String>> linkedFeatures) { // start the JSON object jsonWriter.key(attributeName); jsonWriter.array(); // encode each linked feature for (Map<NameImpl, String> feature : linkedFeatures) { encodeAttributes(feature); } // end the linked features JSON array jsonWriter.endArray(); }
private JSONBuilder writeGeomCollection(GeometryCollection collection) { this.key("geometries"); this.array(); for (int i = 0, n = collection.getNumGeometries(); i < n; i++) { writeGeom(collection.getGeometryN(i)); } return this.endArray(); }
/** Encodes a list of features (chained features) as a JSON array. */ private void encodeChainedFeatures(String attributeName, List<Feature> chainedFeatures) { // start the JSON object jsonWriter.key(attributeName); jsonWriter.array(); for (Feature feature : chainedFeatures) { // encode each chained feature jsonWriter.object(); encodeProperties(null, feature.getType(), feature.getProperties()); jsonWriter.endObject(); } // end the JSON chained features array jsonWriter.endArray(); }
/** Encode a complex attribute as a JSON object. */ private void encodeComplexAttribute( ComplexAttribute attribute, Map<NameImpl, String> attributes) { // get the attribute name and start a JSON object String name = attribute.getName().getLocalPart(); jsonWriter.key(name); if (attributes != null && !attributes.isEmpty()) { // we have some attributes to encode jsonWriter.array(); } // let's see if we have actually some properties to encode if (attribute.getProperties() != null && !attribute.getProperties().isEmpty()) { jsonWriter.object(); // encode the object properties, since this is not a top feature or a // chained feature we don't need to explicitly handle the geometry attribute encodeProperties(null, attribute.getType(), attribute.getProperties()); // end the attribute JSON object jsonWriter.endObject(); } if (attributes != null && !attributes.isEmpty()) { // encode the attributes list encodeAttributes(attributes); jsonWriter.endArray(); } }
/** * Writes a polygon * * @param geometry The polygon to write * @throws JSONException */ private void writePolygon(Polygon geometry) throws JSONException { this.array(); writeCoordinates(geometry.getExteriorRing().getCoordinateSequence()); for (int i = 0, ii = geometry.getNumInteriorRing(); i < ii; i++) { writeCoordinates(geometry.getInteriorRingN(i).getCoordinateSequence()); } this.endArray(); // end the linear ring // this.endObject(); //end the }
/** * Helper method that encodes a sequence of coordinates to the JSON output as an array. This * method will respect the configured axis order. If activated, coordinates measures (M) will be * encoded, otherwise measures will be ignored. * * @param coordinates the coordinates sequence that will be encoded * @return the JSON builder instance, this allow chained calls */ private JSONBuilder writeCoordinates(CoordinateSequence coordinates) throws JSONException { // start encoding the JSON array of coordinates this.array(); // each coordinate will be encoded has an array of ordinates for (int i = 0; i < coordinates.size(); i++) { // let's see if we need to encode measures, NaN values will not be encoded double m = encodeMeasures ? coordinates.getM(i) : Double.NaN; // encode the coordinate ordinates to the JSON output writeCoordinate(coordinates.getX(i), coordinates.getY(i), coordinates.getZ(i), m); } // we are done with the array return this.endArray(); }
jsonWriter.object().key("type").value("FeatureCollection"); jsonWriter.key("features"); jsonWriter.array(); List<FeatureCollection> resultsList = featureCollection.getFeature(); FeaturesInfo featuresInfo =
public GeoJsonWMSBuilder(Rectangle mapSize, ReferencedEnvelope mapArea) { final int memotyBufferThreshold = 8096; out = new DeferredFileOutputStream(memotyBufferThreshold, "geojson", ".geojson", null); writer = new OutputStreamWriter(out, Charsets.UTF_8); jsonWriter = new org.geoserver.wfs.json.GeoJSONBuilder(writer); jsonWriter.object(); // start root object jsonWriter.key("type").value("FeatureCollection"); jsonWriter.key("totalFeatures").value("unknown"); jsonWriter.key("features"); jsonWriter.array(); CoordinateReferenceSystem mapCrs = mapArea.getCoordinateReferenceSystem(); jsonWriter.setAxisOrder(CRS.getAxisOrder(mapCrs)); Unit<?> unit = mapCrs.getCoordinateSystem().getAxis(0).getUnit(); Unit<?> standardUnit = unit.getSystemUnit(); PrecisionModel pm = null; if (SI.RADIAN.equals(standardUnit)) { pm = new PrecisionModel(1e6); // truncate coords at 6 decimals } else if (SI.METRE.equals(standardUnit)) { pm = new PrecisionModel(100); // truncate coords at 2 decimals } if (pm != null) { precisionReducerFilter = new CoordinatePrecisionReducerFilter(pm); } }
/** * Writes WFS3 compliant paging links * * @param response * @param operation * @param jw */ protected void writePagingLinks( FeatureCollectionResponse response, Operation operation, GeoJSONBuilder jw) { if (response.getPrevious() != null || response.getNext() != null) { jw.key("links"); jw.array(); String mimeType = getMimeType(response, operation); writeLink(jw, "previous page", mimeType, "previous", response.getPrevious()); writeLink(jw, "next page", mimeType, "next", response.getNext()); jw.endArray(); } }
jw.key("featureTypes"); jw.array(); for (int i = 0; i < featureTypeInfos.length; i++) { FeatureTypeInfo ft = featureTypeInfos[i]; SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType(); jw.key("properties"); jw.array(); for (AttributeDescriptor ad : schema.getAttributeDescriptors()) { if (ad == schema.getGeometryDescriptor()) {
String baseUrl = request.getBaseUrl(); jw.key("links"); jw.array();
public GeoJsonWMSBuilder(Rectangle mapSize, ReferencedEnvelope mapArea) { final int memotyBufferThreshold = 8096; out = new DeferredFileOutputStream(memotyBufferThreshold, "geojson", ".geojson", null); writer = new OutputStreamWriter(out, Charsets.UTF_8); jsonWriter = new org.geoserver.wfs.json.GeoJSONBuilder(writer); jsonWriter.object();// start root object jsonWriter.key("type").value("FeatureCollection"); jsonWriter.key("totalFeatures").value("unknown"); jsonWriter.key("features"); jsonWriter.array(); CoordinateReferenceSystem mapCrs = mapArea.getCoordinateReferenceSystem(); jsonWriter.setAxisOrder(CRS.getAxisOrder(mapCrs)); Unit<?> unit = mapCrs.getCoordinateSystem().getAxis(0).getUnit(); Unit<?> standardUnit = unit.getStandardUnit(); PrecisionModel pm = null; if (SI.RADIAN.equals(standardUnit)) { pm = new PrecisionModel(1e6);// truncate coords at 6 decimals } else if (SI.METRE.equals(standardUnit)) { pm = new PrecisionModel(100);// truncate coords at 2 decimals } if (pm != null) { precisionReducerFilter = new CoordinatePrecisionReducerFilter(pm); } }
/** * Turns an envelope into an array [minX,minY,maxX,maxY] * * @param env envelope representing bounding box * @return this */ protected JSONBuilder writeBoundingBox(Envelope env) { this.key("bbox"); this.array(); if (axisOrder == CRS.AxisOrder.NORTH_EAST) { roundedValue(env.getMinY()); roundedValue(env.getMinX()); roundedValue(env.getMaxY()); roundedValue(env.getMaxX()); } else { roundedValue(env.getMinX()); roundedValue(env.getMinY()); roundedValue(env.getMaxX()); roundedValue(env.getMaxY()); } return this.endArray(); }