Skip to content

Commit

Permalink
feat: CSV, Excel and JSON writer - reduce to single geometries if dat…
Browse files Browse the repository at this point in the history
…a does not contain multi geometry

CSV, Excel and JSON writer have been reduced to single geometries if data contains single geometry.

ING-3570
Closes halestudio#986
  • Loading branch information
emanuelaepure10 committed Jun 3, 2024
1 parent ffc4dfe commit 3aa38bf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Import-Package: au.com.bytecode.opencsv;version="2.3.0",
eu.esdihumboldt.hale.common.lookup,
eu.esdihumboldt.hale.common.lookup.impl,
eu.esdihumboldt.hale.common.schema,
eu.esdihumboldt.hale.common.schema.geometry,
eu.esdihumboldt.hale.common.schema.io,
eu.esdihumboldt.hale.common.schema.io.impl,
eu.esdihumboldt.hale.common.schema.model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@

import javax.xml.namespace.QName;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;

import eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty;
import eu.esdihumboldt.hale.common.instance.io.impl.AbstractInstanceWriter;
import eu.esdihumboldt.hale.common.instance.model.Group;
import eu.esdihumboldt.hale.common.instance.model.Instance;
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.TypeFilter;
import eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty;
import eu.esdihumboldt.hale.common.schema.model.DefinitionUtil;
import eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition;
import eu.esdihumboldt.hale.common.schema.model.TypeDefinition;
Expand Down Expand Up @@ -57,6 +62,7 @@ public boolean isCancelable() {
* @param useSchema <code>true</code> if properties should be defined from
* the schema, otherwise <code>false</code> and properties are
* defined from the Instances
* @param reporter IOReporter
* @return a map of properties with string of localpart of the QName of the
* property as key
*/
Expand Down Expand Up @@ -229,6 +235,18 @@ private void addProperty(List<String> headerRow, Map<String, Object> row, Object
if (!headerRow.contains(propertyTypeName)) {
headerRow.add(propertyTypeName);
}

if (property instanceof GeometryProperty<?>) {
GeometryProperty<?> geometryProperty = (GeometryProperty<?>) property;

Geometry geometry = geometryProperty.getGeometry();
if (geometry instanceof GeometryCollection
&& ((GeometryCollection) geometry).getNumGeometries() == 1) {
property = new DefaultGeometryProperty<Geometry>(
geometryProperty.getCRSDefinition(), geometry.getGeometryN(0));
}
}

row.put(propertyTypeName, property);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)

File outputFile = new File(filenameWithType);
try (FileOutputStream fos = new FileOutputStream(filePath)) {
addCSVFileByQName(reporter, new FileOutputStream(outputFile), instances);
addCSVFileByQName(new FileOutputStream(outputFile), instances);
fos.close();
} catch (FileNotFoundException fnfe) {
reporter.error(new IOMessageImpl("Cannot create csv file", fnfe));
Expand All @@ -121,7 +121,7 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
}
else {
try (OutputStream os = getTarget().getOutput()) {
addCSVFileByQName(reporter, os, instances);
addCSVFileByQName(os, instances);
os.close();
} catch (FileNotFoundException fnfe) {
reporter.error(new IOMessageImpl("Cannot create csv file", fnfe));
Expand All @@ -144,8 +144,8 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
* @throws IOException
* @throws FileNotFoundException
*/
private void addCSVFileByQName(IOReporter reporter, OutputStream outputStream,
InstanceCollection instances) throws IOException, FileNotFoundException {
private void addCSVFileByQName(OutputStream outputStream, InstanceCollection instances)
throws IOException, FileNotFoundException {

boolean solveNestedProperties = getParameter(
InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES).as(Boolean.class, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
49.8728337,
8.651
]
49.8728337,
8.651
]
],
properties: [
Expand All @@ -200,12 +198,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
48.13722,
11.5755
]
48.13722,
11.5755
]
],
properties: [
Expand Down Expand Up @@ -272,12 +268,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
49.8728337891123,
8.65122278912346
]
49.8728337891123,
8.65122278912346
]
],
properties: [
Expand All @@ -296,12 +290,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
48.13722289876544,
11.57555687654323
]
48.13722289876544,
11.57555687654323
]
],
properties: [
Expand Down Expand Up @@ -368,12 +360,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
49.87283378911236,
8.65122278912346
]
49.87283378911236,
8.65122278912346
]
],
properties: [
Expand All @@ -392,12 +382,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
48.13722289876544,
11.57555687654323
]
48.13722289876544,
11.57555687654323
]
],
properties: [
Expand Down Expand Up @@ -464,12 +452,10 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
type: "Point",
coordinates: [
[
49.8728337891123,
8.651222789123
]
49.8728337891123,
8.651222789123
]
],
properties: [
Expand All @@ -488,8 +474,8 @@ class InstanceToJsonTest {
type: "Feature",
"@type": "city",
geometry: [
type: "MultiPoint",
coordinates: [[48.137222, 11.575556]]
type: "Point",
coordinates: [48.137222, 11.575556]
],
properties: [
population: 1471508,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.opengis.referencing.operation.MathTransform;

import com.fasterxml.jackson.core.JsonFactory;
Expand Down Expand Up @@ -646,10 +647,16 @@ protected void writeGeometryValue(JsonGenerator jsonGen, GeometryProperty<?> geo
}
}

Geometry geometry = geomProp.getGeometry();
if (geometry instanceof GeometryCollection
&& ((GeometryCollection) geometry).getNumGeometries() == 1) {
geometry = geometry.getGeometryN(0);
}

// correct winding order as per right-hand rule, i.e.,
// exterior rings are counterclockwise, and holes are
// clockwise.
geom = WindingOrder.unifyWindingOrder(geomProp.getGeometry(), true,
geom = WindingOrder.unifyWindingOrder(geometry, true,
geomProp.getCRSDefinition().getCRS());

// FIXME what to do in case of an invalid geometry?
Expand Down

0 comments on commit 3aa38bf

Please sign in to comment.