Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 26 additions & 28 deletions src/building.js
Comment thread
deevroman marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,30 @@ class Building {
} else {
this.type = 'relation';
}
if (this.isValidData(outerElementXml)) {
this.nodelist = Building.buildNodeList(this.fullXmlData);
this.setHome();
this.repositionNodes();
if (this.type === 'way') {
try {
this.validateData(outerElementXml);
} catch (e) {
throw new Error(`Rendering of ${outerElementXml.tagName.toLowerCase()} ${id} is not is not possible. ${e}`);
}

this.nodelist = Building.buildNodeList(this.fullXmlData);
this.setHome();
this.repositionNodes();
if (this.type === 'way') {
this.outerElement = new BuildingPart(id, this.fullXmlData, this.nodelist);
} else if (this.type === 'multipolygon') {
this.outerElement = new MultiBuildingPart(id, this.fullXmlData, this.nodelist);
} else {
const outlineRef = outerElementXml.querySelector('member[role="outline"]').getAttribute('ref');
const outline = this.fullXmlData.getElementById(outlineRef);
const outlineType = outline.tagName.toLowerCase();
if (outlineType === 'way') {
this.outerElement = new BuildingPart(id, this.fullXmlData, this.nodelist);
} else if (this.type === 'multipolygon') {
this.outerElement = new MultiBuildingPart(id, this.fullXmlData, this.nodelist);
} else {
const outlineRef = outerElementXml.querySelector('member[role="outline"]').getAttribute('ref');
const outline = this.fullXmlData.getElementById(outlineRef);
const outlineType = outline.tagName.toLowerCase();
if (outlineType === 'way') {
this.outerElement = new BuildingPart(id, this.fullXmlData, this.nodelist);
} else {
this.outerElement = new MultiBuildingPart(outlineRef, this.fullXmlData, this.nodelist);
}
this.outerElement = new MultiBuildingPart(outlineRef, this.fullXmlData, this.nodelist);
}
this.addParts();
} else {
window.printError('XML Not Valid');
throw new Error('invalid XML');
}
this.addParts();
}

/**
Expand Down Expand Up @@ -248,15 +249,15 @@ class Building {
/**
* validate that we have the ID of a building way.
*/
isValidData(xmlData) {
validateData(xmlData) {
// Check that it is a building (<tag k="building" v="*"/> exists)
const buildingType = xmlData.querySelector('[k="building"]');
const ways = [];
if (xmlData.tagName === 'relation') {
// get all building relation parts
// todo: multipolygon inner and outer roles.
let parts = xmlData.querySelectorAll('member[role="part"]');
var ref = 0;
let ref = 0;
for (let i = 0; i < parts.length; i++) {
ref = parts[i].getAttribute('ref');
const part = this.fullXmlData.getElementById(ref);
Expand All @@ -268,8 +269,7 @@ class Building {
}
} else {
if (!buildingType) {
window.printError('Outer way is not a building');
return false;
throw new Error('Outer way is not a building');
}
ways.push(xmlData);
}
Expand All @@ -282,16 +282,14 @@ class Building {
const firstRef = nodes[0].getAttribute('ref');
const lastRef = nodes[nodes.length - 1].getAttribute('ref');
if (firstRef !== lastRef) {
window.printError('Way ' + way.getAttribute('id') + ' is not a closed way. ' + firstRef + ' !== ' + lastRef + '.');
return false;
throw new Error('Way ' + way.getAttribute('id') + ' is not a closed way. ' + firstRef + ' !== ' + lastRef + '.');
}
} else {
window.printError('Way ' + way.getAttribute('id') + ' has no nodes.');
return false;
throw new Error('Way ' + way.getAttribute('id') + ' has no nodes.');
}
} else {
let parts = way.querySelectorAll('member[role="part"]');
var ref = 0;
let ref = 0;
for (let i = 0; i < parts.length; i++) {
ref = parts[i].getAttribute('ref');
const part = this.fullXmlData.getElementById(ref);
Expand Down
34 changes: 34 additions & 0 deletions test/building.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,40 @@ describe.each([
});
});

test('Test data validation open outline', () => {
const data = `
<way id="1">
<nd ref="2"/>
<nd ref="3"/>
<nd ref="4"/>
<tag k="building" v="yes"/>
</way>`;
expect(() => new Building('1', data))
.toThrow(new Error('Rendering of way 1 is not is not possible. Error: Way 1 is not a closed way. 2 !== 4.'));
});

test('Test data validation with not building', () => {
const data = `
<way id="1">
<nd ref="2"/>
<nd ref="3"/>
<nd ref="4"/>
<nd ref="2"/>
<tag k="not:building" v="yes"/>
</way>`;
expect(() => new Building('1', data))
.toThrow(new Error('Rendering of way 1 is not is not possible. Error: Outer way is not a building'));
});

test('Test data validation with empty way', () => {
const data = `
<way id="1">
<tag k="building" v="yes"/>
</way>`;
expect(() => new Building('1', data))
.toThrow(new Error('Rendering of way 1 is not is not possible. Error: Way 1 has no nodes.'));
});

test('Test Constructor', async() => {
const bldg = new Building('31361386', data);
expect(bldg.home).toBeDeepCloseTo([11.015512, 49.5833659], 10);
Expand Down