Skip to content

Commit 3e6757a

Browse files
committed
support map data override from .osm via URL or Drag&Drop
1 parent 810298d commit 3e6757a

File tree

3 files changed

+102
-10
lines changed

3 files changed

+102
-10
lines changed

src/building.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Building {
5050
/**
5151
* Create new building
5252
*/
53-
static async create(type, id) {
53+
static async create(type, id, overrideInnerData=null) {
5454
let data;
5555
if (type === 'way') {
5656
data = await Building.getWayData(id);
@@ -60,7 +60,7 @@ class Building {
6060
let xmlData = new window.DOMParser().parseFromString(data, 'text/xml');
6161
const nodelist = Building.buildNodeList(xmlData);
6262
const extents = Building.getExtents(id, xmlData, nodelist);
63-
const innerData = await Building.getInnerData(...extents);
63+
const innerData = overrideInnerData ?? await Building.getInnerData(...extents);
6464
return new Building(id, innerData);
6565
}
6666

src/index.js

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,95 @@ var errorBox = false;
2929

3030
var gui;
3131

32+
async function getFileFromForm() {
33+
return new Promise(resolve => {
34+
const overlay = document.createElement('div');
35+
overlay.id = 'overlay';
36+
Object.assign(overlay.style, {
37+
position: 'fixed',
38+
inset: '0',
39+
background: '#222',
40+
display: 'flex',
41+
alignItems: 'center',
42+
justifyContent: 'center',
43+
zIndex: '9999',
44+
});
45+
46+
const container = document.createElement('div');
47+
Object.assign(container.style, {
48+
display: 'flex',
49+
alignItems: 'center',
50+
gap: '1rem',
51+
});
52+
53+
const text = document.createElement('p');
54+
text.textContent = 'Select .osm file:';
55+
Object.assign(text.style, {
56+
color: '#eee',
57+
margin: '0',
58+
fontSize: '1.5rem',
59+
fontFamily: 'Arial',
60+
});
61+
62+
const input = document.createElement('input');
63+
input.type = 'file';
64+
Object.assign(input.style, {
65+
fontSize: '1.5rem',
66+
padding: '0.5rem 1rem',
67+
borderRadius: '0.5rem',
68+
cursor: 'pointer',
69+
backgroundColor: '#333',
70+
color: '#eee',
71+
border: '1px solid #555',
72+
});
73+
74+
container.appendChild(text);
75+
container.appendChild(input);
76+
overlay.appendChild(container);
77+
document.body.appendChild(overlay);
78+
79+
input.addEventListener('change', event => {
80+
const file = event.target.files[0];
81+
if (file) {
82+
const reader = new FileReader();
83+
reader.onload = () => resolve(reader.result);
84+
reader.readAsText(file);
85+
overlay.remove();
86+
}
87+
});
88+
89+
overlay.addEventListener('dragover', (event) => {
90+
event.preventDefault();
91+
overlay.style.cursor = 'copy';
92+
});
93+
94+
overlay.addEventListener('dragleave', () => {
95+
overlay.style.cursor = '';
96+
});
97+
98+
overlay.addEventListener('drop', async(event) => {
99+
event.preventDefault();
100+
overlay.style.cursor = '';
101+
102+
const file = event.dataTransfer.files[0];
103+
if (file) {
104+
const reader = new FileReader();
105+
reader.onload = () => resolve(reader.result);
106+
reader.readAsText(file);
107+
overlay.remove();
108+
}
109+
});
110+
});
111+
}
112+
32113
/**
33114
* Initialize the screen
34115
*/
35-
function init() {
36-
var type = 'way';
37-
var id = 66418809;
116+
async function init() {
117+
let type = 'way';
118+
let id = 66418809;
38119

39-
var displayInfo = false;
120+
let displayInfo = false;
40121

41122
window.printError = printError;
42123

@@ -53,7 +134,16 @@ function init() {
53134
if (params.has('errorBox')) {
54135
errorBox = true;
55136
}
56-
Building.create(type, id).then(function(myObj){
137+
const fileUrl = new URLSearchParams(location.search).get('fromFile');
138+
let innerData = null;
139+
if (fileUrl === '') {
140+
innerData = await getFileFromForm();
141+
} else if (fileUrl !== null) {
142+
printError('Loading map data from URL');
143+
innerData = await (await fetch(new URLSearchParams(location.search).get('fromFile'))).text();
144+
}
145+
146+
Building.create(type, id, innerData).then(function(myObj){
57147
mainBuilding = myObj;
58148
const helperSize = myObj.outerElement.getWidth();
59149
const helper = new GridHelper(helperSize / 0.9, helperSize / 9);
@@ -193,9 +283,10 @@ function addLights() {
193283
scene.add( dirLight );
194284
}
195285

196-
init();
197-
createScene();
198-
window.addEventListener('resize', resize, false);
286+
init().then(() => {
287+
createScene();
288+
window.addEventListener('resize', resize, false);
289+
});
199290

200291
/**
201292
* Set the camera position

style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
body {
1414
margin: 0;
15+
background-color: #222;
1516
}
1617

1718
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */

0 commit comments

Comments
 (0)