/** Extracts from the provided properties any chained features resolved as links. */ @SuppressWarnings("unchecked") private List<Map<NameImpl, String>> getLinkedFeatures(List<Property> properties) { List<Map<NameImpl, String>> linkedFeatures = new ArrayList<>(); for (Property property : properties) { // get the attributes (XML attributes) associated with the current property Map<NameImpl, String> attributes = (Map<NameImpl, String>) property.getUserData().get(org.xml.sax.Attributes.class); if (checkIfFeatureIsLinked(property, attributes)) { // we have a linked features linkedFeatures.add(attributes); } } return linkedFeatures; }
/** * Encode feature properties by type, we do this way so we can handle the case were these * properties should be encoded as a list or as elements that appear multiple times. */ private void encodePropertiesByType( PropertyType parentType, PropertyType type, List<Property> properties) { PropertyDescriptor multipleType = isMultipleType(parentType, type); if (multipleType == null) { // simple JSON objects properties.forEach(this::encodeProperty); } else { // possible chained features that need to be encoded as a list List<Feature> chainedFeatures = getChainedFeatures(properties); if (chainedFeatures == null || chainedFeatures.isEmpty()) { // let's check if we are in the presence of linked features List<Map<NameImpl, String>> linkedFeatures = getLinkedFeatures(properties); if (!linkedFeatures.isEmpty()) { // encode linked features encodeLinkedFeatures(multipleType.getName().getLocalPart(), linkedFeatures); } else { // no chained or linked features just encode each property properties.forEach(this::encodeProperty); } } else { // chained features so we need to encode the chained features as an array encodeChainedFeatures(multipleType.getName().getLocalPart(), chainedFeatures); } } }
Object simpleValue = getSimpleContent(complexAttribute); if (simpleValue != null) { encodeSimpleAttribute( complexAttribute.getName().getLocalPart(), simpleValue, attributes); } else { encodeComplexAttribute((ComplexAttribute) property, attributes); List<Feature> features = getFeatures((Attribute) property); if (features != null) { encodeChainedFeatures(property.getName().getLocalPart(), features); } else { encodeSimpleAttribute((Attribute) property, attributes);
/** Encode a feature properties. Geometry attribute will be ignored. */ private void encodeProperties( Property geometryAttribute, PropertyType parentType, Collection<Property> properties) { // index all the feature available properties by their type Map<PropertyType, List<Property>> index = indexPropertiesByType(geometryAttribute, properties); for (Map.Entry<PropertyType, List<Property>> entry : index.entrySet()) { // encode properties per type encodePropertiesByType(parentType, entry.getKey(), entry.getValue()); } }
/** 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(); }
@Override protected void encodeFeature(Feature feature) { super.encodeFeature(feature); writeExtraFeatureProperties(feature, operation, jsonWriter); } };
/** 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(); }
public void write(List<FeatureCollection> collections) { for (FeatureCollection collection : collections) { // encode the feature collection making sure that the collection is closed try (FeatureIterator iterator = collection.features()) { encodeFeatureCollection(iterator); } } }
/** 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 all available features by iterating over the iterator. */ private void encodeFeatureCollection(FeatureIterator iterator) { while (iterator.hasNext()) { // encode the next feature encodeFeature(iterator.next()); featuresCount++; } }
/** * 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(); }