GeoJSONBuilder jw = new GeoJSONBuilder(outWriter); jw.object(); jw.key("elementFormDefault"); jw.value("qualified"); jw.key("targetNamespace"); NamespaceInfo nsInfo = featureTypeInfos[0].getNamespace(); jw.value(nsInfo.getURI()); jw.key("targetPrefix"); jw.value(nsInfo.getName()); jw.key("featureTypes"); jw.array(); for (int i = 0; i < featureTypeInfos.length; i++) { FeatureTypeInfo ft = featureTypeInfos[i]; jw.object(); jw.key("typeName").value(ft.getName()); SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType(); jw.key("properties"); jw.array(); for (AttributeDescriptor ad : schema.getAttributeDescriptors()) { if (ad == schema.getGeometryDescriptor()) { jw.endArray(); jw.endObject(); // end of the feature type schema jw.endArray(); jw.endObject();
jsonWriter.object(); jsonWriter.key("type").value("Feature"); jsonWriter.key("id").value(featureId); jsonWriter.key("geometry"); jsonWriter.writeGeom(aGeom); jsonWriter.key("geometry_name").value(geometryName); jsonWriter.key("properties"); jsonWriter.object(); 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
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); } }
/** * 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(); }
/** * 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(); }
/** 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(); }
this.object(); this.key("type"); this.value(getGeometryName(geometry)); final int geometryType = getGeometryType(geometry); this.key("coordinates"); writeCoordinate(point); break; case LINESTRING: writeCoordinates(((LineString) geometry).getCoordinateSequence()); break; case MULTIPOINT: this.array(); for (int i = 0, n = geometry.getNumGeometries(); i < n; i++) { writeCoordinate((Point) geometry.getGeometryN(i)); this.endArray(); break; case POLYGON: writePolygon((Polygon) geometry); this.array(); writeCoordinates( ((LineString) geometry.getGeometryN(i)).getCoordinateSequence()); 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(); }
jsonWriter.object(); jsonWriter.key("type").value("Feature"); fType = simpleFeature.getFeatureType(); types = fType.getAttributeDescriptors(); jsonWriter.key("id").value(simpleFeature.getID()); } else if (id_option.length() != 0) { jsonWriter.key("id").value(value); CoordinateReferenceSystem featureCrs = defaultGeomType.getCoordinateReferenceSystem(); jsonWriter.setAxisOrder(CRS.getAxisOrder(featureCrs)); if (crs == null) { crs = featureCrs; jsonWriter.setAxisOrder(CRS.AxisOrder.EAST_NORTH); jsonWriter.key("geometry"); Geometry aGeom = (Geometry) simpleFeature.getDefaultGeometry(); jsonWriter.writeGeom(aGeom); hasGeom = true; } else { jsonWriter.value(null); jsonWriter.key("geometry_name").value(defaultGeomType.getLocalName()); jsonWriter.key("properties");
@Test public void testWriteGeometrySubclass() throws Exception { builder.writeGeom(new MyPoint(1, 2)); assertEquals("{\"type\":\"Point\",\"coordinates\":[1,2]}", writer.toString()); }
jsonWriter.setAxisOrder(CRS.getAxisOrder(crs)); if (crs != null) { } else { jsonWriter.setAxisOrder(CRS.AxisOrder.EAST_NORTH); jsonWriter.key("geometry"); if (geometry != null) { jsonWriter.writeGeom(geometry); jsonWriter.value(null);
/** * 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(); }
/** * 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(); }
@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()); }
this.array(); roundedValue(y); roundedValue(x); } else { roundedValue(x); roundedValue(y); roundedValue(z); roundedValue(m); return this.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 }
@Test public void testNumberOfDecimalsFor3dPolygon() throws Exception { builder.setNumberOfDecimals(0); Geometry g = new WKTReader() .read( "POLYGON((0.1 0.2 0.3, 0.1 10.1 1.1, 10.2 10.3 2.4, 9.5 0.4 3, 0.1 0.2 0.3),(1 1 4, 1 2 5, 2 2 6, 2 1 7, 1 1 4))"); builder.writeGeom(g); assertEquals( "{\"type\":\"Polygon\",\"coordinates\":[[[0,0,0],[0,10,1],[10,10,2],[10,0,3],[0,0,0]],[[1,1,4],[1,2,5],[2,2,6],[2,1,7],[1,1,4]]]}", writer.toString()); }
/** * 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(); }