@@ -75,29 +75,30 @@ class Building {
7575 } else {
7676 this . type = 'relation' ;
7777 }
78- if ( this . isValidData ( outerElementXml ) ) {
79- this . nodelist = Building . buildNodeList ( this . fullXmlData ) ;
80- this . setHome ( ) ;
81- this . repositionNodes ( ) ;
82- if ( this . type === 'way' ) {
78+ try {
79+ this . validateData ( outerElementXml ) ;
80+ } catch ( e ) {
81+ throw new Error ( `Rendering of ${ outerElementXml . tagName . toLowerCase ( ) } ${ id } is not possible. ${ e } ` ) ;
82+ }
83+
84+ this . nodelist = Building . buildNodeList ( this . fullXmlData ) ;
85+ this . setHome ( ) ;
86+ this . repositionNodes ( ) ;
87+ if ( this . type === 'way' ) {
88+ this . outerElement = new BuildingPart ( id , this . fullXmlData , this . nodelist ) ;
89+ } else if ( this . type === 'multipolygon' ) {
90+ this . outerElement = new MultiBuildingPart ( id , this . fullXmlData , this . nodelist ) ;
91+ } else {
92+ const outlineRef = outerElementXml . querySelector ( 'member[role="outline"]' ) . getAttribute ( 'ref' ) ;
93+ const outline = this . fullXmlData . getElementById ( outlineRef ) ;
94+ const outlineType = outline . tagName . toLowerCase ( ) ;
95+ if ( outlineType === 'way' ) {
8396 this . outerElement = new BuildingPart ( id , this . fullXmlData , this . nodelist ) ;
84- } else if ( this . type === 'multipolygon' ) {
85- this . outerElement = new MultiBuildingPart ( id , this . fullXmlData , this . nodelist ) ;
8697 } else {
87- const outlineRef = outerElementXml . querySelector ( 'member[role="outline"]' ) . getAttribute ( 'ref' ) ;
88- const outline = this . fullXmlData . getElementById ( outlineRef ) ;
89- const outlineType = outline . tagName . toLowerCase ( ) ;
90- if ( outlineType === 'way' ) {
91- this . outerElement = new BuildingPart ( id , this . fullXmlData , this . nodelist ) ;
92- } else {
93- this . outerElement = new MultiBuildingPart ( outlineRef , this . fullXmlData , this . nodelist ) ;
94- }
98+ this . outerElement = new MultiBuildingPart ( outlineRef , this . fullXmlData , this . nodelist ) ;
9599 }
96- this . addParts ( ) ;
97- } else {
98- window . printError ( 'XML Not Valid' ) ;
99- throw new Error ( 'invalid XML' ) ;
100100 }
101+ this . addParts ( ) ;
101102 }
102103
103104 /**
@@ -248,15 +249,15 @@ class Building {
248249 /**
249250 * validate that we have the ID of a building way.
250251 */
251- isValidData ( xmlData ) {
252+ validateData ( xmlData ) {
252253 // Check that it is a building (<tag k="building" v="*"/> exists)
253254 const buildingType = xmlData . querySelector ( '[k="building"]' ) ;
254255 const ways = [ ] ;
255256 if ( xmlData . tagName === 'relation' ) {
256257 // get all building relation parts
257258 // todo: multipolygon inner and outer roles.
258259 let parts = xmlData . querySelectorAll ( 'member[role="part"]' ) ;
259- var ref = 0 ;
260+ let ref = 0 ;
260261 for ( let i = 0 ; i < parts . length ; i ++ ) {
261262 ref = parts [ i ] . getAttribute ( 'ref' ) ;
262263 const part = this . fullXmlData . getElementById ( ref ) ;
@@ -268,8 +269,7 @@ class Building {
268269 }
269270 } else {
270271 if ( ! buildingType ) {
271- window . printError ( 'Outer way is not a building' ) ;
272- return false ;
272+ throw new Error ( 'Outer way is not a building' ) ;
273273 }
274274 ways . push ( xmlData ) ;
275275 }
@@ -282,16 +282,14 @@ class Building {
282282 const firstRef = nodes [ 0 ] . getAttribute ( 'ref' ) ;
283283 const lastRef = nodes [ nodes . length - 1 ] . getAttribute ( 'ref' ) ;
284284 if ( firstRef !== lastRef ) {
285- window . printError ( 'Way ' + way . getAttribute ( 'id' ) + ' is not a closed way. ' + firstRef + ' !== ' + lastRef + '.' ) ;
286- return false ;
285+ throw new Error ( 'Way ' + way . getAttribute ( 'id' ) + ' is not a closed way. ' + firstRef + ' !== ' + lastRef + '.' ) ;
287286 }
288287 } else {
289- window . printError ( 'Way ' + way . getAttribute ( 'id' ) + ' has no nodes.' ) ;
290- return false ;
288+ throw new Error ( 'Way ' + way . getAttribute ( 'id' ) + ' has no nodes.' ) ;
291289 }
292290 } else {
293291 let parts = way . querySelectorAll ( 'member[role="part"]' ) ;
294- var ref = 0 ;
292+ let ref = 0 ;
295293 for ( let i = 0 ; i < parts . length ; i ++ ) {
296294 ref = parts [ i ] . getAttribute ( 'ref' ) ;
297295 const part = this . fullXmlData . getElementById ( ref ) ;
0 commit comments