jsonWriter.key("features");
jsonWriter.array();
CoordinateReferenceSystem crs = null;
for (int i = 0; i < resultsList.size(); i++) {
FeatureCollection collection = (FeatureCollection) resultsList
.get(i);
FeatureIterator iterator = collection.features();
try {
SimpleFeatureType fType;
List<AttributeDescriptor> types;
while (iterator.hasNext()) {
SimpleFeature feature = (SimpleFeature) iterator.next();
jsonWriter.object();
jsonWriter.key("type").value("Feature");
jsonWriter.key("id").value(feature.getID());
fType = feature.getFeatureType();
types = fType.getAttributeDescriptors();
GeometryDescriptor defaultGeomType = fType.getGeometryDescriptor();
if (crs == null && defaultGeomType != null)
crs = fType.getGeometryDescriptor().getCoordinateReferenceSystem();
jsonWriter.key("geometry");
Geometry aGeom = (Geometry) feature.getDefaultGeometry();
if (aGeom == null) {
// In case the default geometry is not set, we will
// just use the first geometry we find
for (int j = 0; j < types.size() && aGeom == null; j++) {
Object value = feature.getAttribute(j);
if (value != null && value instanceof Geometry) {
aGeom = (Geometry) value;
}
}
}
// Write the geometry, whether it is a null or not
if (aGeom != null) {
jsonWriter.writeGeom(aGeom);
hasGeom = true;
} else {
jsonWriter.value(null);
}
if (defaultGeomType != null)
jsonWriter.key("geometry_name").value(
defaultGeomType.getLocalName());
jsonWriter.key("properties");
jsonWriter.object();
for (int j = 0; j < types.size(); j++) {
Object value = feature.getAttribute(j);
AttributeDescriptor ad = types.get(j);
if (value != null) {
if (value instanceof Geometry) {
// This is an area of the spec where they
// decided to 'let convention evolve',
// that is how to handle multiple
// geometries. My take is to print the
// geometry here if it's not the default.
// If it's the default that you already
// printed above, so you don't need it here.
if (ad.equals(defaultGeomType)) {
// Do nothing, we wrote it above
// jsonWriter.value("geometry_name");
} else {
jsonWriter.key(ad.getLocalName());
jsonWriter.writeGeom((Geometry) value);
}
} else {
jsonWriter.key(ad.getLocalName());
jsonWriter.value(value);
}
} else {
jsonWriter.key(ad.getLocalName());
jsonWriter.value(null);
}
}
// Bounding box for feature in properties
ReferencedEnvelope refenv = new ReferencedEnvelope(feature.getBounds());
if (featureBounding && !refenv.isEmpty())
jsonWriter.writeBoundingBox(refenv);
jsonWriter.endObject(); // end the properties
jsonWriter.endObject(); // end the feature
}
} // catch an exception here?
finally {
collection.close(iterator);
}
}
jsonWriter.endArray(); // end features
// Coordinate Referense 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
}
}
}
// Bounding box for featurecollection
if (hasGeom) {
ReferencedEnvelope e = null;
for (int i = 0; i < resultsList.size(); i++) {
FeatureCollection collection = (FeatureCollection) resultsList
.get(i);
if (e == null) {
e = collection.getBounds();
} else {
e.expandToInclude(collection.getBounds());
}
}
if (e != null) {