@@ -28,14 +28,16 @@ function xml2geo() {
2828 let input = document . getElementById ( 'xml_in' ) . value ;
2929 xmlDoc = parser . parseFromString ( input , 'text/xml' ) ;
3030 let regions = xmlDoc . getElementsByTagName ( 'Region' ) ;
31+
3132 for ( let i of regions ) {
32- console . log ( 'Looking at Region ID' , i . getAttribute ( 'Id' ) ) ;
33+ let regionId = i . getAttribute ( 'Id' ) ;
34+ let regionType = i . getAttribute ( 'Type' ) || 'Polygon' ; // Default to Polygon if Type is missing
35+ console . log ( 'Processing Region ID:' , regionId , 'as' , regionType ) ;
36+
3337 let vertices = i . getElementsByTagName ( 'Vertex' ) ;
3438 let coordinates = [ ] ;
35- let minX = 99e99 ;
36- let maxX = 0 ;
37- let minY = 99e99 ;
38- let maxY = 0 ;
39+ let minX = 99e99 ; let maxX = 0 ; let minY = 99e99 ; let maxY = 0 ;
40+
3941 for ( let j of vertices ) {
4042 let x = parseFloat ( j . getAttribute ( 'X' ) ) ;
4143 let y = parseFloat ( j . getAttribute ( 'Y' ) ) ;
@@ -45,24 +47,45 @@ function xml2geo() {
4547 maxY = Math . max ( maxY , y ) ;
4648 coordinates . push ( [ x , y ] ) ;
4749 }
48- coordinates . push ( coordinates [ 0 ] ) ;
50+
51+ // **Detect Polygon vs. Polyline**
52+ if ( regionType === 'Polygon' ) {
53+ coordinates . push ( coordinates [ 0 ] ) ; // Close the polygon by repeating the first point
54+ }
55+
4956 let boundRect = [ [ minX , minY ] , [ minX , maxY ] , [ maxX , maxY ] , [ maxX , minY ] , [ minX , minY ] ] ;
50- let feature = { } ;
51- feature [ 'type' ] = 'Feature' ;
52- feature [ 'geometry' ] = { } ;
53- feature [ 'geometry' ] [ 'type' ] = 'Polygon' ;
54- feature [ 'geometry' ] [ 'coordinates' ] = [ coordinates ] ;
55- feature [ 'bound' ] = { } ;
56- feature [ 'bound' ] [ 'type' ] = 'Polygon' ;
57- feature [ 'bound' ] [ 'coordinates' ] = [ boundRect ] ;
57+
58+ // **Detect Color**
59+ let colorValue = i . getAttribute ( 'LineColor' ) ;
60+ let hexColor = colorValue ? `#${ parseInt ( colorValue ) . toString ( 16 ) . padStart ( 6 , '0' ) } ` : '#000000' ;
61+
62+ let feature = {
63+ 'type' : 'Feature' ,
64+ 'geometry' : {
65+ 'type' : regionType === 'Polyline' ? 'LineString' : 'Polygon' ,
66+ 'coordinates' : [ coordinates ] ,
67+ } ,
68+ 'properties' : {
69+ 'regionId' : regionId ,
70+ 'lineColor' : hexColor ,
71+ 'group' : i . parentNode . getAttribute ( 'Name' ) || 'Ungrouped' ,
72+ } ,
73+ 'bound' : {
74+ 'type' : 'BoundingBox' ,
75+ 'coordinates' : [ [ minX , minY ] , [ maxX , maxY ] ] ,
76+ } ,
77+ } ;
78+
5879 features . push ( feature ) ;
5980 }
81+
6082 let output = Object . assign ( { } , template ) ;
6183 output [ 'geometries' ] [ 'features' ] = features ;
6284 output [ 'provenance' ] [ 'image' ] [ 'slide' ] = document . getElementById ( 'slide_id' ) . value ;
6385 output [ 'provenance' ] [ 'analysis' ] [ 'execution' ] = document . getElementById ( 'annot_name' ) . value ;
6486 output [ 'properties' ] [ 'annotations' ] [ 'name' ] = document . getElementById ( 'annot_name' ) . value ;
6587 output [ 'provenance' ] [ 'analysis' ] [ 'name' ] = document . getElementById ( 'annot_name' ) . value ;
6688 output [ 'provenance' ] [ 'analysis' ] [ 'execution_id' ] = document . getElementById ( 'annot_name' ) . value ;
67- document . getElementById ( 'output' ) . innerHTML = JSON . stringify ( output ) ;
89+
90+ document . getElementById ( 'output' ) . textContent = JSON . stringify ( output ) ;
6891}
0 commit comments