/** * Writes the collection counts (if available): * * <ul> * <li>GeoServer legacy's totalFeatures * <li>WFS 3 numberMatched (same as totalFeatures) * <li>WFS 3 numberReturned * </ul> * * @param featureCount * @param numberReturned * @param jsonWriter */ protected void writeCollectionCounts( BigInteger featureCount, long numberReturned, GeoJSONBuilder jsonWriter) { // counts if (featureCount != null) { jsonWriter.key("totalFeatures").value(featureCount); // WFS3 suggested name for the same concept as totalFeatures jsonWriter.key("numberMatched").value(featureCount); } else { jsonWriter.key("totalFeatures").value("unknown"); // in WFS3 the suggestion is not to include the element } jsonWriter.key("numberReturned").value(numberReturned); }
/** * Write a java.util.Map out as a JSON Object. Keys are serialized using the toString method of * the object and values are serialized using primitives conversions. If a value in the map is a * List or Map object, it will be serialized as JSON Array or JSON Object respectively. * * @param map a java.util.Map object to be serialized as a JSON Object */ public JSONBuilder writeMap(final Map map) { this.object(); for (final Object k : map.keySet()) { this.key(k.toString()); this.value(map.get(k)); } return this.endObject(); }
/** * Writes a WFS3 compliant timeStamp collection attribute * * @param jw */ protected void writeCollectionTimeStamp(GeoJSONBuilder jw) { jw.key("timeStamp").value(new ISO8601Formatter().format(new Date())); }
private void writeCrsLegacy(final GeoJSONBuilder jsonWriter, CoordinateReferenceSystem crs) { // Coordinate Reference System, currently only if the namespace is // EPSG if (crs != null) { Set<ReferenceIdentifier> ids = crs.getIdentifiers(); // WKT defined crs might not have identifiers at all if (ids != null && ids.size() > 0) { NamedIdentifier namedIdent = (NamedIdentifier) ids.iterator().next(); String csStr = namedIdent.getCodeSpace().toUpperCase(); if (csStr.equals("EPSG")) { jsonWriter.key("crs"); jsonWriter.object(); jsonWriter.key("type").value(csStr); jsonWriter.key("properties"); jsonWriter.object(); jsonWriter.key("code"); jsonWriter.value(namedIdent.getCode()); jsonWriter.endObject(); // end properties jsonWriter.endObject(); // end crs } } } }
private static void describeProperty( String name, AttributeDescriptor ad, GeoJSONBuilder jw, boolean isGeometry) { jw.object(); jw.key("name").value(name); jw.key("maxOccurs").value(ad.getMaxOccurs()); jw.key("minOccurs").value(ad.getMinOccurs()); jw.key("nillable").value((ad.getMinOccurs() > 0) ? false : true); Class<?> binding = ad.getType().getBinding(); if (isGeometry) { jw.key("type").value("gml:" + mapToJsonType(binding)); } else { jw.key("type").value("xsd:" + mapToJsonType(binding)); } jw.key("localType").value(mapToJsonType(binding)); jw.endObject(); // end of attribute description }
@Override public void addFeature(String layerName, String featureId, String geometryName, Geometry aGeom, Map<String, Object> properties) { if (precisionReducerFilter != null) { aGeom.apply(precisionReducerFilter); } jsonWriter.object(); jsonWriter.key("type").value("Feature"); jsonWriter.key("id").value(featureId); jsonWriter.key("geometry"); // Write the geometry, whether it is a null or not jsonWriter.writeGeom(aGeom); jsonWriter.key("geometry_name").value(geometryName); jsonWriter.key("properties"); jsonWriter.object(); for (Map.Entry<String, Object> e : properties.entrySet()) { String attributeName = e.getKey(); Object value = e.getValue(); jsonWriter.key(attributeName); if (value == null) { jsonWriter.value(null); } else { jsonWriter.value(value); } } jsonWriter.endObject(); // end the properties jsonWriter.endObject(); // end the feature }
/** * Encode a simple attribute, this means that this property will be encoded as a simple JSON * attribute if no attributes are available, otherwise it will be encoded as an array containing * the value and attributes values. */ private void encodeSimpleAttribute( String name, Object value, Map<NameImpl, String> attributes) { // let's see if we need to encode attributes or simple value if (attributes == null || attributes.isEmpty()) { // add a simple JSON attribute to the current object jsonWriter.key(name).value(value); return; } // we need to encode a list of attributes, let's first encode the main value jsonWriter.key(name).array(); jsonWriter.value(value); // encode the attributes list encodeAttributes(attributes); // close the values \ attributes array jsonWriter.endArray(); }
private void writeCrs(final GeoJSONBuilder jsonWriter, CoordinateReferenceSystem crs) throws FactoryException { if (crs != null) { String identifier = null; Integer code = CRS.lookupEpsgCode(crs, true); if (code != null) { if (code != null) { identifier = SrsSyntax.OGC_URN.getPrefix() + code; } } else { identifier = CRS.lookupIdentifier(crs, true); } jsonWriter.key("crs"); jsonWriter.object(); jsonWriter.key("type").value("name"); jsonWriter.key("properties"); jsonWriter.object(); jsonWriter.key("name"); jsonWriter.value(identifier); jsonWriter.endObject(); // end properties jsonWriter.endObject(); // end crs } else { jsonWriter.key("crs"); jsonWriter.value(null); } }
/** 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(); }
/** * Utility method that encode an attributes map, only the attribute name local part will be * used. Attributes with a NULL value will not be encoded. This method assumes that it is * already in an array context. */ private void encodeAttributes(Map<NameImpl, String> attributes) { attributes.forEach( (name, value) -> { if (value != null) { // encode attribute, we don't take namespace into account jsonWriter.object(); jsonWriter.key(name.getLocalPart()).value(value); jsonWriter.endObject(); } }); }
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(); }
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); } }
/** Encode a feature in GeoJSON. */ protected void encodeFeature(Feature feature) { // start the feature JSON object jsonWriter.object(); jsonWriter.key("type").value("Feature"); // encode the feature identifier if available Identifier identifier = feature.getIdentifier(); if (identifier != null) { jsonWriter.key("id").value(identifier.getID()); } // geometry attribute has some special handling Property geometryAttribute = encodeGeometry(feature); // start the JSON object that will contain all the others properties jsonWriter.key("properties"); jsonWriter.object(); // encode object properties, we pass the geometry attribute to avoid duplicate encodings encodeProperties(geometryAttribute, feature.getType(), feature.getProperties()); // close the feature JSON object jsonWriter.endObject(); // close the properties JSON object jsonWriter.endObject(); }
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); } }
/** 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(); } }
/** 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(); }
/** * 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(); } }
/** * 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(); }