use of qupath.lib.io.PathObjectTypeAdapters.FeatureCollection in project qupath by qupath.
the class PathIO method exportObjectsAsGeoJSON.
/**
* Export a collection of objects as a GeoJSON "FeatureCollection" to an output stream.
* @param stream
* @param pathObjects
* @param options
* @throws IOException
*/
public static void exportObjectsAsGeoJSON(OutputStream stream, Collection<? extends PathObject> pathObjects, GeoJsonExportOptions... options) throws IOException {
Collection<GeoJsonExportOptions> optionList = Arrays.asList(options);
// If exclude measurements, 'transform' each PathObject to get rid of measurements
if (optionList.contains(GeoJsonExportOptions.EXCLUDE_MEASUREMENTS))
pathObjects = pathObjects.stream().map(e -> PathObjectTools.transformObject(e, null, false)).collect(Collectors.toList());
var writer = new OutputStreamWriter(new BufferedOutputStream(stream), StandardCharsets.UTF_8);
var gson = GsonTools.getInstance(optionList.contains(GeoJsonExportOptions.PRETTY_JSON));
if (optionList.contains(GeoJsonExportOptions.FEATURE_COLLECTION))
gson.toJson(GsonTools.wrapFeatureCollection(pathObjects), writer);
else if (pathObjects.size() == 1) {
gson.toJson(pathObjects.iterator().next(), writer);
} else {
gson.toJson(pathObjects, new TypeToken<List<PathObject>>() {
}.getType(), writer);
}
writer.flush();
}
Aggregations