Skip to content

Commit

Permalink
Fixed GeoJSON output not containing geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed May 24, 2024
1 parent 5cde0b3 commit b51f060
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Improved generated queries when fetching entities over a one-to-many relation.
* Fixed deep select for complex properties in GeoJSON and CSV output.
* Fixed automatic generation of Features when Feature and Location use different idTypes.
* Fixed GeoJSON output not containing geometries.


## Release version 2.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.util;

import com.fasterxml.jackson.core.TreeNode;
import java.io.IOException;
import org.geojson.GeoJsonObject;
import org.geojson.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author jab
*/
public class GeoHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(GeoHelper.class.getName());

private GeoHelper() {
// Not for instantiation.
}
Expand All @@ -44,4 +49,13 @@ public static <T extends Number> Point getPoint(T... values) {
public static GeoJsonObject parseGeoJson(String geoJsonString) throws IOException {
return SimpleJsonMapper.getSimpleObjectMapper().readValue(geoJsonString, GeoJsonObject.class);
}

public static GeoJsonObject parseGeoJson(TreeNode tn) {
try {
return SimpleJsonMapper.getSimpleObjectMapper().treeToValue(tn, GeoJsonObject.class);
} catch (IOException ex) {
LOGGER.debug("Failed to convert to GeoJson: ", tn);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public void writeData(GjRowCollector collector, Entity source, String namePrefix
Object value = source.getProperty(property);
if (value instanceof ComplexValue complexValue) {
for (Map.Entry<String, String> entry : subProperties.entrySet()) {
collector.collectEntry(namePrefix + entry.getKey(), complexValue.getProperty(entry.getValue()));
collector.collectEntry(namePrefix + entry.getKey(), complexValue.getProperty(entry.getValue()), null);
}
} else if (value instanceof Map mapValue) {
for (Map.Entry<String, String> entry : subProperties.entrySet()) {
collector.collectEntry(namePrefix + entry.getKey(), mapValue.get(entry.getValue()));
collector.collectEntry(namePrefix + entry.getKey(), mapValue.get(entry.getValue()), null);
}
} else {
collector.collectEntry(namePrefix + name, property.getFrom(source));
collector.collectEntry(namePrefix + name, property.getFrom(source), null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void writeData(GjRowCollector collector, Entity source, String namePrefix
if (topLevel) {
collector.collectId(property.getFrom(source));
}
collector.collectEntry(namePrefix + name, property.getFrom(source));
collector.collectEntry(namePrefix + name, property.getFrom(source), property.getType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public GjEntityProperty(String name, Property property) {

@Override
public void writeData(GjRowCollector collector, Entity source, String namePrefix) {
collector.collectEntry(namePrefix + name, property.getFrom(source));
collector.collectEntry(namePrefix + name, property.getFrom(source), property.getType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.plugin.format.geojson.tools;

import com.fasterxml.jackson.core.TreeNode;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.ext.TimeValue;
import de.fraunhofer.iosb.ilt.frostserver.property.SpecialNames;
import de.fraunhofer.iosb.ilt.frostserver.property.type.PropertyType;
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeSimple;
import de.fraunhofer.iosb.ilt.frostserver.util.GeoHelper;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -92,8 +96,9 @@ public void collectId(Object value) {
*
* @param headerName The name of the element.
* @param value The value of the element for the current row.
* @param type The type of the property.
*/
public void collectEntry(String headerName, Object value) {
public void collectEntry(String headerName, Object value, PropertyType type) {
if (value == null) {
return;
}
Expand All @@ -108,9 +113,15 @@ public void collectEntry(String headerName, Object value) {
}
return;
}
if (value instanceof GeoJsonObject geoJsonObject) {
if (feature.getGeometry() == null) {
feature.setGeometry(geoJsonObject);
boolean isGeom = false;
if (type instanceof TypeSimple ts) {
if (ts.getUnderlyingType().getName().startsWith("Edm.Geo")) {
isGeom = true;
}
}
if (isGeom) {
if (feature.getGeometry() == null && value instanceof TreeNode tn) {
feature.setGeometry(GeoHelper.parseGeoJson(tn));
}
return;
}
Expand All @@ -134,15 +145,15 @@ private void flattenMap(Map<String, Object> map, String headerName) {
String key = entry.getKey();
String header = headerName + "/" + key;
Object value = entry.getValue();
collectEntry(header, value);
collectEntry(header, value, null);
}
}

private void flattenList(List<Object> list, String headerName) {
int idx = 0;
for (Object item : list) {
String header = headerName + "/" + idx;
collectEntry(header, item);
collectEntry(header, item, null);
idx++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public GjSelfLinkProperty(Query query, String serviceRootUrl, Version version, S

@Override
public void writeData(GjRowCollector collector, Entity source, String namePrefix) {
collector.collectEntry(namePrefix + name, UrlHelper.generateSelfLink(serviceRootUrl, version, source));
collector.collectEntry(namePrefix + name, UrlHelper.generateSelfLink(serviceRootUrl, version, source), null);
}

}

0 comments on commit b51f060

Please sign in to comment.