Search in sources :

Example 1 with FeatureCollection

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();
}
Also used : Arrays(java.util.Arrays) ImageServer(qupath.lib.images.servers.ImageServer) BufferedInputStream(java.io.BufferedInputStream) TypeToken(com.google.gson.reflect.TypeToken) FeatureCollection(qupath.lib.io.PathObjectTypeAdapters.FeatureCollection) ObjectInputStream(java.io.ObjectInputStream) LoggerFactory(org.slf4j.LoggerFactory) Category(java.util.Locale.Category) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) ImageServerProvider(qupath.lib.images.servers.ImageServerProvider) Collection(java.util.Collection) PathObjects(qupath.lib.objects.PathObjects) Set(java.util.Set) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) PathObject(qupath.lib.objects.PathObject) Objects(java.util.Objects) List(java.util.List) Entry(java.util.Map.Entry) ImagePlane(qupath.lib.regions.ImagePlane) Geometry(org.locationtech.jts.geom.Geometry) ZipOutputStream(java.util.zip.ZipOutputStream) HashMap(java.util.HashMap) PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) DefaultImageServerBuilder(qupath.lib.images.servers.ImageServerBuilder.DefaultImageServerBuilder) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JsonElement(com.google.gson.JsonElement) ColorDeconvolutionStains(qupath.lib.color.ColorDeconvolutionStains) OutputStreamWriter(java.io.OutputStreamWriter) ObjectOutputStream(java.io.ObjectOutputStream) GeometryTools(qupath.lib.roi.GeometryTools) LinkedHashSet(java.util.LinkedHashSet) OutputStream(java.io.OutputStream) ImageData(qupath.lib.images.ImageData) Logger(org.slf4j.Logger) Files(java.nio.file.Files) GeneralTools(qupath.lib.common.GeneralTools) Workflow(qupath.lib.plugins.workflow.Workflow) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ServerBuilder(qupath.lib.images.servers.ImageServerBuilder.ServerBuilder) InputStreamReader(java.io.InputStreamReader) File(java.io.File) PathObjectTools(qupath.lib.objects.PathObjectTools) Collections(java.util.Collections) FileSystems(java.nio.file.FileSystems) InputStream(java.io.InputStream) OutputStreamWriter(java.io.OutputStreamWriter) List(java.util.List) ArrayList(java.util.ArrayList) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 TypeToken (com.google.gson.reflect.TypeToken)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Serializable (java.io.Serializable)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1