Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ Roof Types:
* Dome
* Pyramidal
* Gabled
* Hipped

Examples:
* Simple building with no parts - [Washington Monument](https://beakerboy.github.io/OSMBuilding/index.html?id=766761337)
* Glass - [Petronas Towers](https://beakerboy.github.io/OSMBuilding/index.html?id=279944536)
* Dome roof, Gabled roof, and Skillion ramp - [Jefferson Memorial](https://beakerboy.github.io/OSMBuilding/index.html?type=relation&id=3461570)
* Dome, Gabled, and Pyramidal Roof - [US Capitol](https://beakerboy.github.io/OSMBuilding/index.html?type=relation&id=12286916)
* Dome, Gabled, Hipped, and Pyramidal Roof - [US Capitol](https://beakerboy.github.io/OSMBuilding/index.html?type=relation&id=12286916)
* [Chrysler Building](https://beakerboy.github.io/OSMBuilding/index.html?id=42500770)
* Building Relation [Burj Khalifa](https://beakerboy.github.io/OSMBuilding/index.html?type=relation&id=7584462)
* Multipolygon with no parts - [Freer Art Gallery](https://beakerboy.github.io/OSMBuilding/index.html?type=relation&id=1029355)
Expand Down
28 changes: 24 additions & 4 deletions src/building.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,13 @@ class Building {
} else {
// Filter to all ways
var parts = this.fullXmlData.getElementsByTagName('way');
for (let j = 0; j < parts.length; j++) {
if (parts[j].querySelector('[k="building:part"]')) {
const id = parts[j].getAttribute('id');
this.parts.push(new BuildingPart(id, this.fullXmlData, this.nodelist, this.outerElement.options));
for (const xmlPart of parts) {
if (xmlPart.querySelector('[k="building:part"]')) {
const id = xmlPart.getAttribute('id');
const part = new BuildingPart(id, this.fullXmlData, this.nodelist, this.outerElement.options);
if (this.partIsInside(part)) {
this.parts.push(part);
}
}
}
// Filter all relations
Expand Down Expand Up @@ -408,5 +411,22 @@ class Building {
}
}
}

/**
* Check if any point in a part is within this building's outline.
* It only checknof points are inside, not if crossing events occur, or
* if the part completly surrounds the building.
* @param {BuildingPart} part - the part to be tested
* @returns {bool} is it?
*/
partIsInside(part) {
const shape = part.shape;
for (const vector of shape.extractPoints().shape) {
if (BuildingShapeUtils.surrounds(this.outerElement.shape, [vector.x, vector.y])) {
return true;
}
}
return false;
}
}
export {Building};
4 changes: 2 additions & 2 deletions src/buildingpart.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class BuildingPart {
const options = {
center: center,
angle: angle / 180 * Math.PI,
depth: this.options.roof.height,
depth: this.options.roof.height ?? 3,
};
const geometry = new WedgeGeometry(this.shape, options);

Expand Down Expand Up @@ -434,7 +434,7 @@ class BuildingPart {

/**
* OSM compass degrees are 0-360 clockwise.
*
* 0 degrees is North.
* @return {number} degrees
*/
static atanRadToCompassDeg(rad) {
Expand Down
9 changes: 1 addition & 8 deletions src/extras/BuildingShapeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class BuildingShapeUtils extends ShapeUtils {
const elements = way.getElementsByTagName('nd');

// Get the coordinates of all the nodes and add them to the shape outline.

for (const element of elements) {
ref = element.getAttribute('ref');
nodes.push(nodelist[ref]);
Expand All @@ -45,7 +44,6 @@ class BuildingShapeUtils extends ShapeUtils {
}
return shape;
}

/**
* Check if a way is a closed shape.
*
Expand Down Expand Up @@ -288,7 +286,6 @@ class BuildingShapeUtils extends ShapeUtils {
*/
static combineCoordinates(shape) {
const points = shape.extractPoints().shape;
points.pop();
var x = [];
var y = [];
var vec;
Expand Down Expand Up @@ -338,7 +335,6 @@ class BuildingShapeUtils extends ShapeUtils {
*/
static edgeLength(shape) {
const points = shape.extractPoints().shape;
points.pop();
const lengths = [];
var p1;
var p2;
Expand All @@ -360,7 +356,6 @@ class BuildingShapeUtils extends ShapeUtils {
*/
static vertexAngle(shape) {
const points = shape.extractPoints().shape;
points.pop();
const angles = [];
var p0;
var p1;
Expand Down Expand Up @@ -395,7 +390,6 @@ class BuildingShapeUtils extends ShapeUtils {
*/
static edgeDirection(shape) {
const points = shape.extractPoints().shape;
points.pop();
const angles = [];
var p1;
var p2;
Expand All @@ -419,7 +413,6 @@ class BuildingShapeUtils extends ShapeUtils {
static surrounds(shape, point) {
var count = 0;
const vecs = shape.extractPoints().shape;
vecs.pop();
var vec;
var nextvec;
for (let i = 0; i < vecs.length; i++) {
Expand Down Expand Up @@ -469,7 +462,7 @@ class BuildingShapeUtils extends ShapeUtils {
* Return the angle of the longest side of a shape with 90° vertices.
*
* @param {THREE.Shape} shape - the shape
* @return {number}
* @return {number} in radians from Pi > x > -Pi
*/
static longestSideAngle(shape) {
const lengths = BuildingShapeUtils.edgeLength(shape);
Expand Down
34 changes: 34 additions & 0 deletions test/building.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,40 @@ test('Test downloading type=building with multipolygon outline and multiple inne
const building = new Building('42', innerData);
expect(building.id).toBe('42');
expect(building.outerElement.shape.holes.length).toBe(1);
const urlBase = 'https://api.openstreetmap.org/api/0.6/';
expect(global.fetch.mock.calls[0][0]).toBe(urlBase + 'relation/42/full');
expect(global.fetch.mock.calls[1][0]).toBe(urlBase + 'relation/40/full');
expect(global.fetch.mock.calls[2][0]).toBe(urlBase + 'map?bbox=30.4980057,59.9380365,30.4993839,59.9385087');
});

test('Part must be within outline', () => {
const data = `<?xml version="1.0" encoding="UTF-8"?>
<osm>
<node id="1" lat="0.001" lon="0.001"/>
<node id="2" lat="0.001" lon="0"/>
<node id="3" lat="0" lon="0"/>
<node id="4" lat="0" lon=".0005"/>
<node id="5" lat="0" lon=".001"/>
<node id="6" lat=".0001" lon=".001"/>
<node id="7" lat=".0001" lon="0.005"/>
<way id="11">
<nd ref="1"/>
<nd ref="2"/>
<nd ref="3"/>
<nd ref="1"/>
<tag k="building" v="apartments"/>
</way>
<way id="22">
<nd ref="4"/>
<nd ref="5"/>
<nd ref="6"/>
<nd ref="7"/>
<nd ref="4"/>
<tag k="building:part" v="yes"/>
</way>
</osm>
`;
expect(new Building('11', data).parts.length).toBe(0);
});

window.printError = printError;
Expand Down
2 changes: 2 additions & 0 deletions test/buildingpart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ test('Constructor', () => {
expect(part.options.roof.orientation).toBe('along');

// toDo: Mock BuildingShapeUtils and test options
expect(BuildingShapeUtils.edgeDirection(part.shape)).toStrictEqual([1.5707963267948966, 0, -1.5707963267948966, 3.141592653589793]);
expect(BuildingShapeUtils.longestSideAngle(part.shape)).toBe(1.5707963267948966);
expect(part.options.roof.direction).toBe(90);
expect(errors.length).toBe(0);
});
Expand Down
11 changes: 7 additions & 4 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ test('', () => {
3: [-1, 1],
};
const shape = BuildingShapeUtils.createShape(xmlData, nodelist);
expect(shape.extractPoints().shape.length).toBe(4);
expect(shape.extractPoints().shape.length).toBe(3);
const points = shape.extractPoints().shape;
expect([points[0].x, points[0].y]).toStrictEqual(nodelist[1]);
expect([points[1].x, points[1].y]).toStrictEqual(nodelist[2]);
expect([points[2].x, points[2].y]).toStrictEqual(nodelist[3]);
expect([points[0].x, points[0].y]).toStrictEqual(nodelist[1]);
});

/** Test isClosed */
Expand Down Expand Up @@ -116,13 +115,17 @@ const rightTriangle = new Shape();
rightTriangle.moveTo(1, 1);
rightTriangle.lineTo(1, -1);
rightTriangle.lineTo(-1, 1);
rightTriangle.lineTo(1, 1);

const rightTriangle2 = new Shape();
rightTriangle2.moveTo(1, 1);
rightTriangle2.lineTo(-1, 1);
rightTriangle2.lineTo(1, -1);
rightTriangle2.lineTo(1, 1);

const rectangle = new Shape();
rectangle.moveTo(-4.332738077015795, -5.882209888874915);
rectangle.lineTo(-4.332738077015795, 5.88221335051411);
rectangle.lineTo(4.332747472106493, 5.88221335051411);
rectangle.lineTo(4.332747472106493, -5.882209888874915);

const rectangle = new Shape();
rectangle.moveTo(-4.332738077015795, -5.882209888874915);
Expand Down