-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_annotations.groovy
More file actions
55 lines (46 loc) · 1.83 KB
/
Copy pathexport_annotations.groovy
File metadata and controls
55 lines (46 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* QuPath Annotation GeoJSON Export Script
* ---------------------------------------
* This script exports all annotation objects from the currently opened image in QuPath
* to a single GeoJSON file. The user is prompted to select an output directory, and the
* file is named after the image.
*
* Features:
* - Prompts the user to select a folder for saving the output.
* - Automatically names the output file using the current image name.
* - Exports all annotations in GeoJSON format using the "PRETTY_JSON" and "FEATURE_COLLECTION" options.
*
* Requirements:
* - An image must be open in QuPath.
* - The image must contain annotation objects.
*
* Output:
* - A `.geojson` file containing all annotations will be saved in the selected folder.
*
* Example output filename:
* - ImageName.geojson
*
* Notes:
* - If the user cancels the folder selection, the script will exit without exporting.
*/
import qupath.fx.dialogs.FileChoosers
import java.io.File
// =======================
// Prompt user to select output directory
// =======================
def defaultDir = new File(System.getProperty("user.home"))
def outputDirectory = FileChoosers.promptForDirectory("Please select a folder to save the GeoJSON annotation file", defaultDir)
// If the user cancels, stop the script
if (outputDirectory == null) {
print "Export cancelled by user."
return
}
// Get the name of the current image
def imageName = getCurrentImageData().getServer().getMetadata().getName()
// Construct the full output path
def outputPath = new File(outputDirectory, imageName + ".geojson").getAbsolutePath()
// Get the annotation objects
def annotations = getAnnotationObjects()
// Export the annotations to GeoJSON
exportObjectsToGeoJson(annotations, outputPath, "PRETTY_JSON", "FEATURE_COLLECTION")
print "Annotations saved to: " + outputPath