/** * 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(); }
/** * 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 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 } } } }
@Test public void testWriteTime() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.HOUR, 15); cal.set(Calendar.MINUTE, 48); cal.set(Calendar.SECOND, 5); java.sql.Time date = new java.sql.Time(cal.getTimeInMillis()); builder.object().key("time").value(date).endObject(); assertEquals("{\"time\":\"15:48:05Z\"}", writer.toString()); }
@Test public void testWriteDateTime() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); cal.set(Calendar.HOUR, 15); cal.set(Calendar.MINUTE, 48); cal.set(Calendar.SECOND, 5); java.sql.Timestamp date = new java.sql.Timestamp(cal.getTimeInMillis()); builder.object().key("timestamp").value(date).endObject(); assertEquals("{\"timestamp\":\"2011-10-25T15:48:05Z\"}", writer.toString()); }
@Test public void testWriteJavaDate() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); cal.set(Calendar.HOUR, 15); cal.set(Calendar.MINUTE, 48); cal.set(Calendar.SECOND, 5); cal.set(Calendar.MILLISECOND, 223); java.util.Date date = new java.util.Date(cal.getTimeInMillis()); builder.object().key("date").value(date).endObject(); assertEquals("{\"date\":\"2011-10-25T15:48:05.223Z\"}", writer.toString()); }
@Test public void testWriteCalendar() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); builder.object().key("cal").value(cal).endObject(); assertEquals("{\"cal\":\"2011-10-25T00:00:00Z\"}", writer.toString()); }
@Test public void testWriteDate() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); java.sql.Date date = new java.sql.Date(cal.getTimeInMillis()); builder.object().key("date").value(date).endObject(); assertEquals("{\"date\":\"2011-10-25Z\"}", writer.toString()); }
@Test public void testWriteCalendarTZ() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-05:00")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); builder.object().key("cal").value(cal).endObject(); assertEquals("{\"cal\":\"2011-10-25T00:00:00-05:00\"}", writer.toString()); }
@Test public void testWriteDateTimeMillis() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); cal.set(Calendar.HOUR, 15); cal.set(Calendar.MINUTE, 48); cal.set(Calendar.SECOND, 5); cal.set(Calendar.MILLISECOND, 223); java.sql.Timestamp date = new java.sql.Timestamp(cal.getTimeInMillis()); builder.object().key("timestamp").value(date).endObject(); assertEquals("{\"timestamp\":\"2011-10-25T15:48:05.223Z\"}", writer.toString()); }
@Test public void testWriteCalendarFull() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.clear(); cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 9); cal.set(Calendar.DAY_OF_MONTH, 25); cal.set(Calendar.HOUR, 15); cal.set(Calendar.MINUTE, 48); cal.set(Calendar.SECOND, 5); cal.set(Calendar.MILLISECOND, 223); builder.object().key("cal").value(cal).endObject(); assertEquals("{\"cal\":\"2011-10-25T15:48:05.223Z\"}", writer.toString()); }
/** 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(); } }
/** 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(); }
protected void writeLink( GeoJSONBuilder jw, String title, String mimeType, String rel, String href) { if (href != null) { jw.object(); jw.key("title").value(title); jw.key("type").value(mimeType); jw.key("rel").value(rel); jw.key("href").value(href); jw.endObject(); } }
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); } }
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); } }
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); } }
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 }