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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
}
</script>
<script src="./src/apis.js"></script>
<script type="module" src="./src/apis.js"></script>
<script type="module" src="./src/index.js"></script>
<div id="errorBox" style="position:absolute; top:10px; display:block; z-index:100; background-color: #ffffff; white-space:pre-line"></div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/apis.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const osmApiUrl = new URLSearchParams(location.search).get('osmApiUrl') || 'https://api.openstreetmap.org/api/0.6';
const apis = {
export const apis = {
bounding: {
api: osmApiUrl + '/map?bbox=',
url: (left, bottom, right, top) => {
Expand Down
32 changes: 24 additions & 8 deletions src/building.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {apis} from './apis.js';
import {BuildingShapeUtils} from './extras/BuildingShapeUtils.js';
import {BuildingPart} from './buildingpart.js';
import {MultiBuildingPart} from './multibuildingpart.js';
Expand Down Expand Up @@ -192,24 +193,39 @@ class Building {
static async getWayData(id) {
let restPath = apis.getWay.url(id);
let response = await fetch(restPath);
let text = await response.text();
return text;
if (response.status === 404) {
throw `The way ${id} was not found on the server.\nURL: ${restPath}`;
} else if (response.status === 410) {
throw `The way ${id} was deleted.\nURL: ${restPath}`;
} else if (response.status !== 200) {
throw `HTTP ${response.status}.\nURL: ${restPath}`;
}
return await response.text();
}

static async getRelationData(id) {
let restPath = apis.getRelation.url(id);
let response = await fetch(restPath);
let text = await response.text();
return text;
if (response.status === 404) {
throw `The relation ${id} was not found on the server.\nURL: ${restPath}`;
} else if (response.status === 410) {
throw `The relation ${id} was deleted.\nURL: ${restPath}`;
} else if (response.status !== 200) {
throw `HTTP ${response.status}.\nURL: ${restPath}`;
}
return await response.text();
}

/**
* Fetch way data from OSM
* Fetch map data data from OSM
*/
static async getInnerData(left, bottom, right, top) {
let response = await fetch(apis.bounding.url(left, bottom, right, top));
let res = await response.text();
return res;
let url = apis.bounding.url(left, bottom, right, top);
let response = await fetch(url);
if (response.status !== 200) {
throw `HTTP ${response.status}.\nURL: ${url}`;
}
return await response.text();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function init() {
createFolders(folder, part.options);
}
}
}).catch(err => {
window.printError(err);
alert(err);
});
camera = new PerspectiveCamera(
50,
Expand Down
33 changes: 17 additions & 16 deletions test/building.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@ expect.extend({toBeDeepCloseTo});

import { Shape, Mesh } from 'three';
import { TextEncoder } from 'node:util';
import {expect, test, beforeEach, describe} from '@jest/globals';
global.TextEncoder = TextEncoder;

let apis = {
bounding: {
api:'https://api.openstreetmap.org/api/0.6/map?bbox=',
url: (left, bottom, right, top) => {
return apis.bounding.api + left + ',' + bottom + ',' + right + ',' + top;
},
},
getRelation: {
api:'https://api.openstreetmap.org/api/0.6/relation/',
parameters:'/full',
url: (relationId) => {
return apis.getRelation.api + relationId + apis.getRelation.parameters;
},
},
};
import {apis} from '../src/apis.js';
global.apis = apis;

import { Building } from '../src/building.js';
Expand Down Expand Up @@ -55,10 +42,24 @@ const data = `
</osm>`;

beforeEach(() => {
fetch.resetMocks();
fetchMock.resetMocks();
errors = [];
});

describe.each([
[['way', -1], ['', { status: 404 }], /^The way -1 was not found on the server.\nURL: /],
[['way', -1], ['', { status: 410 }], /^The way -1 was deleted.\nURL: /],
[['way', -1], ['', { status: 509 }], /^HTTP 509.\nURL: /],
[['relation', -1], ['', { status: 404 }], /^The relation -1 was not found on the server.\nURL: /],
[['relation', -1], ['', { status: 410 }], /^The relation -1 was deleted.\nURL: /],
[['relation', -1], ['', { status: 509 }], /^HTTP 509.\nURL: /],
])('Test API error handling', (args, response, matcher) => {
test(`Test API error for ${args[0]} ${args[1]} with HTTP ${response[1].status}`, async() => {
fetch.mockResponses(response);
await expect(Building.downloadDataAroundBuilding(...args)).rejects.toMatch(matcher);
});
});

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