I have a workshop application users are using to select items, explore … but as well to
create new “shapes” (Polygons) that I want to write back to some newly created object instances or to existing object instances.
How can I save the polygon drawn by a user to an object instance ?
I’m using the Workshop’s map’s widget, but I know alternative exist.
There are multiple alternatives:
- Use the Map application to draw the shapes and use Action. There is one special configuration on the action, namely adding 2 type-classes on the Action’s field containing the geoshape, documented here and here so that the Action can write back the drawn polygon to an Object’s Geoshape property. From there you can just call the Action from the map itself after drawing a polygon.
Example below Create a new area
is an Action
Note: If you go for the map application, you can then embed the Map In Workshop, and even specify which actions should be displayed to end users.
- Use the Workshop’s Map’s Widget output called
DRAWN GEOMETRY
. This is stored in a string variable, which you can process via a Foundry Function to extract the polygon (or any geo shape really) of interest. You can then call your Action with the output of this function. The Action should also have type-classes etc. as above. It can even be the same action
Example function
import { Function } from "@foundry/functions-api";
interface Geometry {
type: string;
coordinates: number[][][];
}
interface Feature {
id: string;
type: string;
geometry: Geometry;
properties: object;
}
interface FeatureCollection {
type: string;
features: Feature[];
}
export class MyFunctions {
@Function()
public transformFeatureCollectionInPolygon(geoShapeFeatureCollection: string): string {
// Parse the string as a javascript object
var parsed_geojson: FeatureCollection = JSON.parse(geoShapeFeatureCollection)
// Keep only the polygons
const polygons = parsed_geojson.features.filter((feature) => feature.geometry.type === "Polygon").map((feature) => feature.geometry);
// Keep only the first polygon and serialize it
if (polygons.length > 0) {
const firstPolygon = polygons[0];
const output = {
type: firstPolygon.type,
coordinates: firstPolygon.coordinates,
};
return JSON.stringify(output);
} else {
// If no polygon is found, return an empty string. Alternative: Throw an error
return ""
}
}
2 Likes