Skip to content

Commit 554dbec

Browse files
author
hackermd
committed
Update coordinates of polygon upon modification
1 parent f19149e commit 554dbec

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/api.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,27 @@ class VLWholeSlideMicroscopyImageViewer {
743743
});
744744

745745
this[_drawingSource].on(VectorEventType.CHANGEFEATURE, (e) => {
746+
const geometry = e.feature.getGeometry();
747+
const type = geometry.getType();
748+
// The first and last point of a polygon must be identical. The last point
749+
// is an implmentation detail and is hidden from the user in the graphical
750+
// interface. However, we must update the last point in case the first
751+
// piont has been modified by the user.
752+
if (type === 'Polygon') {
753+
// NOTE: Polyon in GeoJSON format contains an array of geometries,
754+
// where the first element represents the coordinates of the outer ring
755+
// and the second element represents the coordinates of the inner ring
756+
// (in our case the inner ring should not be present).
757+
const layout = geometry.getLayout();
758+
const coordinates = geometry.getCoordinates();
759+
const firstPoint = coordinates[0][0];
760+
const lastPoint = coordinates[0][coordinates[0].length-1];
761+
if (firstPoint[0] !== lastPoint[0] || firstPoint[1] !== lastPoint[1]) {
762+
coordinates[0][coordinates[0].length-1] = firstPoint;
763+
geometry.setCoordinates(coordinates, layout);
764+
e.feature.setGeometry(geometry);
765+
}
766+
}
746767
publish(container, EVENT.ROI_MODIFIED, _getROIFromFeature(e.feature, this[_pyramidMetadata]));
747768
});
748769

0 commit comments

Comments
 (0)