Skip to content

Commit fc9a08b

Browse files
authored
color split in xml2geo.js
1 parent 9c89e9d commit fc9a08b

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

apps/port/xml2geo.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
parser = new DOMParser();
22

3+
function generateRandomId(length = 6) {
4+
return Math.random().toString(36).substr(2, length);
5+
}
6+
37
var template = {
48
'provenance': {
59
'image': {
@@ -20,39 +24,50 @@ var template = {
2024
},
2125
'geometries': {
2226
'type': 'FeatureCollection',
27+
'features': [],
2328
},
2429
};
2530

2631
var aperioMap = {
2732
'0': 'Polygon',
2833
'1': 'Polygon',
29-
'2': 'Polygon', // rectangle but should work?? haven't seen one yet
34+
'2': 'Polygon',
3035
'4': 'Polyline',
3136
};
3237

3338
function xml2geo() {
34-
let features = [];
3539
let input = document.getElementById('xml_in').value;
3640
xmlDoc = parser.parseFromString(input, 'text/xml');
37-
let annotations = xmlDoc.getElementsByTagName('Annotation'); // Assuming regions are inside 'Annotation' elements
41+
let annotations = xmlDoc.getElementsByTagName('Annotation');
42+
let slideId = document.getElementById('slide_id').value;
43+
let annotName = document.getElementById('annot_name').value;
44+
45+
// Store objects per unique color
46+
let outputMap = {};
3847

3948
for (let annotation of annotations) {
40-
let annotationType = annotation.getAttribute('Type') || '0'; // Default to '0' if Type is not provided
41-
let annotationLineColor = annotation.getAttribute('LineColor'); // Get LineColor from the parent annotation
49+
let annotationType = annotation.getAttribute('Type') || '0';
50+
let annotationLineColor = annotation.getAttribute('LineColor') || '0';
4251
let annotationId = annotation.getAttribute('Id');
4352

44-
console.log('Processing Annotation ID:', annotationId, 'with Type:', annotationType);
53+
let hexColor = `#${parseInt(annotationLineColor).toString(16).padStart(6, '0')}`;
54+
if (!outputMap[hexColor]) {
55+
let randomId = generateRandomId();
56+
outputMap[hexColor] = JSON.parse(JSON.stringify(template));
57+
outputMap[hexColor]['provenance']['image']['slide'] = slideId;
58+
outputMap[hexColor]['provenance']['analysis']['execution_id'] = randomId;
59+
outputMap[hexColor]['provenance']['analysis']['name'] = `${annotName}_${hexColor}`;
60+
outputMap[hexColor]['properties']['annotations']['name'] = `${annotName}_${hexColor}`;
61+
}
4562

46-
let regions = annotation.getElementsByTagName('Region'); // Get regions within this annotation
63+
let regions = annotation.getElementsByTagName('Region');
4764
for (let region of regions) {
4865
let regionId = region.getAttribute('Id');
49-
regionType = annotationType || region.getAttribute('Type'); // parent annotation type if present, else own (odd?)
50-
regionType = aperioMap[regionType];
51-
console.log('Processing Region ID:', regionId, 'as', regionType);
66+
let regionType = aperioMap[annotationType || region.getAttribute('Type')] || 'Polygon';
5267

5368
let vertices = region.getElementsByTagName('Vertex');
5469
let coordinates = [];
55-
let minX = 99e99; let maxX = 0; let minY = 99e99; let maxY = 0;
70+
let minX = 99e99, maxX = 0, minY = 99e99, maxY = 0;
5671

5772
for (let vertex of vertices) {
5873
let x = parseFloat(vertex.getAttribute('X'));
@@ -63,18 +78,15 @@ function xml2geo() {
6378
maxY = Math.max(maxY, y);
6479
coordinates.push([x, y]);
6580
}
81+
6682
let isFill = false;
67-
// **Detect Polygon vs. Polyline**
6883
if (regionType === 'Polygon') {
69-
coordinates.push(coordinates[0]); // Close the polygon by repeating the first point
84+
coordinates.push(coordinates[0]);
7085
isFill = true;
7186
}
7287

7388
let boundRect = [[minX, minY], [minX, maxY], [maxX, maxY], [maxX, minY], [minX, minY]];
7489

75-
// **Detect Color**
76-
let hexColor = annotationLineColor ? `#${parseInt(annotationLineColor).toString(16).padStart(6, '0')}` : '#000000';
77-
7890
let feature = {
7991
'type': 'Feature',
8092
'geometry': {
@@ -96,17 +108,11 @@ function xml2geo() {
96108
},
97109
};
98110

99-
features.push(feature);
111+
outputMap[hexColor]['geometries']['features'].push(feature);
100112
}
101113
}
102114

103-
let output = Object.assign({}, template);
104-
output['geometries']['features'] = features;
105-
output['provenance']['image']['slide'] = document.getElementById('slide_id').value;
106-
output['provenance']['analysis']['execution'] = document.getElementById('annot_name').value;
107-
output['properties']['annotations']['name'] = document.getElementById('annot_name').value;
108-
output['provenance']['analysis']['name'] = document.getElementById('annot_name').value;
109-
output['provenance']['analysis']['execution_id'] = document.getElementById('annot_name').value;
110-
111-
document.getElementById('output').textContent = JSON.stringify(output);
115+
// Show all color-specific outputs
116+
let finalOutput = Object.values(outputMap);
117+
document.getElementById('output').textContent = JSON.stringify(finalOutput, null, 2);
112118
}

0 commit comments

Comments
 (0)