Skip to content

Commit

Permalink
Allow export of vectors as GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
inigo-cobian committed Nov 12, 2024
1 parent 76d4073 commit d956d11
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.integratedmodelling.klab.ogc.vector.files;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
Expand Down Expand Up @@ -97,6 +98,7 @@ public List<Triple<String, String, String>> getExportCapabilities(IObservation o
(!observation.getScale().getSpace().isRegular()||
(observation.getScale().getSpace().getShape()!= null && !observation.getScale().getSpace().getShape().isRegular()))) {
ret.add(new Triple<>("shp", "ESRI Shapefile", "zip"));
ret.add(new Triple<>("json", "GeoJSON", "json"));
}
}
return ret;
Expand Down Expand Up @@ -232,16 +234,12 @@ public Pair<SimpleFeatureType, FeatureCollection<SimpleFeatureType, SimpleFeatur
public File exportObservation(File file, IObservation observation, ILocator locator, String format,
IMonitor monitor) {

if (format.equals("shp")) {
Pair<SimpleFeatureType, FeatureCollection<SimpleFeatureType, SimpleFeature>> collected = getFeatureCollection(
observation, locator, monitor);
Pair<SimpleFeatureType, FeatureCollection<SimpleFeatureType, SimpleFeature>> collected = getFeatureCollection(
observation, locator, monitor);
File dir = new File(MiscUtilities.changeExtension(file.toString(), "dir"));
dir.mkdirs();

/*
* create a directory from the file name and write the file there, along with
* all the sidecar BS.
*/
File dir = new File(MiscUtilities.changeExtension(file.toString(), "dir"));
dir.mkdirs();
if (format.equals("shp")) {
File out = new File(dir + File.separator + MiscUtilities.getFileName(file));
File zip = new File(MiscUtilities.changeExtension(file.toString(), "zip"));

Expand Down Expand Up @@ -276,8 +274,20 @@ public File exportObservation(File file, IObservation observation, ILocator loca

file = zip;
} else if (format.equals("json")) {

// TODO GeoJson output
File json = new File(MiscUtilities.changeExtension(file.toString(), "json"));
FeatureJSON fjson = new FeatureJSON();
String geojsonString = null;
try {
geojsonString = fjson.toString(collected.getSecond());
} catch (IOException e) {
throw new KlabIOException(e);
}
try (Writer writer = new FileWriter(json)) {
writer.write(geojsonString);
} catch (IOException e) {
throw new KlabIOException(e);
}
file = json;
}

return file;
Expand All @@ -288,6 +298,7 @@ public File exportObservation(File file, IObservation observation, ILocator loca
public Map<String, String> getExportCapabilities(IResource resource) {
Map<String, String> ret = new HashMap<>();
ret.put("shp", "ESRI shapefile");
ret.put("json", "GeoJSON file");
return ret;
}

Expand Down

0 comments on commit d956d11

Please sign in to comment.