Skip to content

Commit

Permalink
fix: Fix determining annotation correspondence.
Browse files Browse the repository at this point in the history
In some instances, annotations with different holes could be considered
identical.

Also, don't reuse annotation ids.
  • Loading branch information
manthey committed Jun 21, 2022
1 parent 965d40f commit aa973ea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ var geo_action = {
zoomrotate: 'geo_action_zoom_rotate',
zoomselect: 'geo_action_zoomselect',

// annotation actions
annotation_line: 'geo_annotation_line',
annotation_polygon: 'geo_annotation_polygon',
annotation_rectangle: 'geo_annotation_rectangle',
// annotation actions -- some are also added by the registry
annotation_edit_handle: 'geo_annotation_edit_handle'
};

Expand Down
8 changes: 8 additions & 0 deletions src/annotation/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ var annotation = function (type, args) {
return m_id;
};

/**
* Assign a new id to this annotation.
*/
this.newId = function () {
annotationId += 1;
m_id = annotationId;
};

/**
* Get or set the name of this annotation.
*
Expand Down
22 changes: 15 additions & 7 deletions src/annotationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ var annotationLayer = function (arg) {
this.addAnnotation = function (annotation, gcs, update) {
var pos = $.inArray(annotation, m_annotations);
if (pos < 0) {
while (m_this.annotationById(annotation.id())) {
annotation.newId();
}
m_this.geoTrigger(geo_event.annotation.add_before, {
annotation: annotation
});
Expand Down Expand Up @@ -1258,11 +1261,15 @@ var annotationLayer = function (arg) {
this.toPolygonList = function (opts) {
const poly = [];
opts = opts || {};
opts.annotationIndices = [];
const indices = [];
if (!opts.annotationIndices) {
opts.annotationIndices = {};
}
opts.annotationIndices[m_this.id()] = indices;
m_annotations.forEach((annotation, idx) => {
if (annotation.toPolygonList) {
annotation.toPolygonList(opts).forEach((p) => poly.push(p));
opts.annotationIndices.push(idx);
indices.push(idx);
}
});
return poly;
Expand All @@ -1285,14 +1292,15 @@ var annotationLayer = function (arg) {
const keepIds = {};
const reusedIds = {};
let correspond, exact, annot;
if (opts.annotationIndices && opts.correspond && opts.correspond.poly1 && opts.annotationIndices.length === opts.correspond.poly1.length) {
const indices = (opts.annotationIndices || {})[m_this.id()];
if (indices && opts.correspond && opts.correspond.poly1 && indices.length === opts.correspond.poly1.length) {
correspond = opts.correspond.poly1;
exact = opts.correspond.exact1;
annot = m_this.annotations();
}
if (keep !== 'all' && keep !== 'none') {
if (keep !== 'all' && keep !== 'none' && annot) {
annot.forEach((oldAnnot, idx) => {
if (opts.annotationIndices.indexOf(idx) < 0) {
if (indices.indexOf(idx) < 0) {
keepIds[oldAnnot.id()] = true;
}
});
Expand All @@ -1306,7 +1314,7 @@ var annotationLayer = function (arg) {
if (correspond) {
for (let i = 0; i < correspond.length; i += 1) {
if (correspond[i] && correspond[i].indexOf(idx) >= 0) {
const orig = annot[opts.annotationIndices[i]];
const orig = annot[indices[i]];
if (keep !== 'all' && keep !== 'none' && exact && exact[i] && exact[i].indexOf(idx) >= 0) {
keepIds[orig.id()] = true;
return;
Expand Down Expand Up @@ -1343,7 +1351,7 @@ var annotationLayer = function (arg) {
}
// add new annotations
polyAnnot.forEach((p) => {
m_this.addAnnotation(registry.createAnnotation('polygon', p), m_this.gcs(), false);
m_this.addAnnotation(registry.createAnnotation('polygon', p), m_this.map().gcs(), false);
update = true;
});
if (update) {
Expand Down
3 changes: 3 additions & 0 deletions src/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var $ = require('jquery');
var geo_action = require('./action');

var widgets = {
dom: {}
};
Expand Down Expand Up @@ -398,6 +400,7 @@ util.registerAnnotation = function (name, func, features) {
console.warn('The ' + name + ' annotation is already registered');
}
annotations[name] = {func: func, features: features || {}};
geo_action['annotation_' + name] = 'geo_annotation_' + name;
return old;
};

Expand Down
5 changes: 4 additions & 1 deletion src/util/polyops.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,19 @@ function generateCorrespondence(poly1, poly2, newpoly, results) {
results[ekey] = Array(poly.length);
poly.forEach((p, idx) => {
const found = {};
let missed = 0;
p.forEach((h) => h.forEach((pt) => {
const key = '_' + pt[0] + '_' + pt[1];
if (pts[key]) {
pts[key].forEach((val) => {
found[val] = (found[val] || 0) + 1;
});
} else {
missed += 1;
}
}));
Object.keys(found).forEach((nidx) => {
if (found[nidx] === counts[+nidx]) {
if (found[nidx] === counts[+nidx] && !missed && p.length === newpoly[+nidx].length) {
if (!results[ekey][idx]) {
results[ekey][idx] = [];
}
Expand Down

0 comments on commit aa973ea

Please sign in to comment.