-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm-tiled.mjs
More file actions
375 lines (356 loc) · 15.8 KB
/
Copy pathosm-tiled.mjs
File metadata and controls
375 lines (356 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import line from './line.mjs';
import fill from './fill.mjs';
import {analyzeTileSets, pickRandomTile} from './wang.mjs';
const DEBUG = false;
let debug = function () {};
if (DEBUG) {
debug = tiled.log;
}
const TOP_RIGHT = 1;
const TOP_LEFT = 7;
const BOTTOM_RIGHT = 3;
const BOTTOM_LEFT = 5;
// From https://stackoverflow.com/questions/17410809/how-to-calculate-rotation-in-2d-in-javascript
function rotate (cx, cy, x, y, angle) {
const radians = (Math.PI / 180) * angle,
cos = Math.cos (radians),
sin = Math.sin (radians),
nx = (cos * (x - cx)) + (sin * (y - cy)) + cx,
ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
return [nx, ny];
}
const plot = function (id) {
return function (x, y) {
if (x >= 0 && x < grid.length && y >= 0 && y < grid [0].length) {
//debug ("Plotting " + x + "," + y + " as " + id);
grid [x][y] = id;
}
}
}
let p;
let activeMap;
let plotTile;
let coordsCounter;
let grid;
let drawId; // This is either a tile ID or a wang color ID
let lineWidth = 1;
const importOSMAction = tiled.registerAction ("ImportOSM", function (action) {
const d = new Dialog ("OSM import");
const geoJsonFilePicker = d.addFilePicker ("OSM GEOJson File");
geoJsonFilePicker.fileUrl = "C:/Users/ROBIN/Downloads/export(3).geojson";
const useTerrainsCheckbox = d.addCheckBox ("Use Terrains");
useTerrainsCheckbox.checked = true;
const rotationNumber = d.addNumberInput ("Rotation angle");
rotationNumber.value = 0;
const fillPolygonsCheckbox = d.addCheckBox ("Fill polygons");
const respectOSMRatioCheckbox = d.addCheckBox ("Respect OSM XY ratio");
const emptyUndefinedTerrainsCheckbox = d.addCheckBox ("Empty undefined terrains");
emptyUndefinedTerrainsCheckbox.stateChanged.connect (function () {
if (emptyUndefinedTerrainsCheckbox.checked) {
fillUndefinedTerrainsCheckbox.checked = false;
}
});
const fillUndefinedTerrainsCheckbox = d.addCheckBox ("Fill undefined terrains");
fillUndefinedTerrainsCheckbox.stateChanged.connect (function () {
if (fillUndefinedTerrainsCheckbox.checked) {
emptyUndefinedTerrainsCheckbox.checked = false;
}
});
fillUndefinedTerrainsCheckbox.checked = true;
const lineWidthNumber = d.addNumberInput ("Line width");
lineWidthNumber.decimals = 0;
lineWidthNumber.minimum = 1;
const importButton = d.addButton ("Import OSM data");
importButton.clicked.connect (function () {
lineWidth = lineWidthNumber.value;
importOsm (geoJsonFilePicker.fileUrl, rotationNumber.value, fillPolygonsCheckbox.checked, useTerrainsCheckbox.checked, respectOSMRatioCheckbox.checked, emptyUndefinedTerrainsCheckbox.checked, fillUndefinedTerrainsCheckbox.checked);
});
d.show ();
});
const urlToLocalPath = function (url) {
return url
.replace ('file:///', '')
.replace (/\//g, '\\');
}
const importOsm = function (geoJsonFile, rotation, fillPolygons, useTerrains, respectOSMRatio, emptyUndefined, fillUndefined) {
coordsCounter = 10000; // DEBUG feature.
const f = new TextFile (urlToLocalPath ("" + geoJsonFile));
p = JSON.parse (f.readAll());
f.close ();
activeMap = tiled.activeAsset; // TODO check this is a map, and there is one layer selected
const tiles = tiled.mapEditor.tilesetsView.selectedTiles;
if (tiles.length !== 1) {
tiled.alert ("One tile must be selected in the tilesets view");
return;
}
const drawingTileId = tiles [0].id;
plotTile = activeMap.usedTilesets()[0].findTile (drawingTileId);
//debug ("plotTile="+plotTile);
const [wangIdsToTiles, tileById, tileIdToWang, tileProbabilities] = analyzeTileSets (activeMap);
if (DEBUG) debug ("wangIdsToTiles="+JSON.stringify (wangIdsToTiles));
if (DEBUG) debug ("tileProbabilities="+JSON.stringify (tileProbabilities));
const useWangCells = useTerrains;
if (useWangCells) {
const drawWangId = tiles [0].tileset.wangSets [0].wangId (tiles [0]);
if (!(drawWangId [TOP_RIGHT] === drawWangId [TOP_LEFT] && drawWangId [TOP_LEFT] === drawWangId [BOTTOM_LEFT] && drawWangId [BOTTOM_LEFT] === drawWangId [BOTTOM_RIGHT])) {
tiled.alert ("The selected tile must have only one terrain type");
return;
}
drawId = drawWangId [TOP_LEFT];
}
else {
drawId = drawingTileId; // Make this the selected tile in the selected tileset
}
let selectedWidth = activeMap.selectedArea.boundingRect.width;
let selectedHeight = activeMap.selectedArea.boundingRect.height;
const selectedX = activeMap.selectedArea.boundingRect.x;
const selectedY = activeMap.selectedArea.boundingRect.y;
let minLongitude;
let minLatitude;
let maxLongitude;
let maxLatitude;
let centerLongitude = 0;
let centerLatitude = 0;
let coordsCount = 0;
for (const feature of p.features) {
let geometry = feature.geometry;
const type = geometry.type;
let coordsSet;
switch (type) {
case "LineString":
coordsSet = [geometry.coordinates];
break;
case "MultiLineString":
case "Polygon":
coordsSet = geometry.coordinates;
break;
default:
tiled.warn ("geometry type " + type + " is unknown, skipping it");
break;
}
if (coordsSet === undefined) {
continue;
}
for (const coordinates of coordsSet) {
for (const coord of coordinates) {
const longitude = coord [0];
const latitude = coord [1];
centerLongitude += longitude;
centerLatitude += latitude;
coordsCount ++;
if (minLongitude === undefined) {
minLongitude = maxLongitude = longitude;
minLatitude = maxLatitude = latitude;
}
else {
minLongitude = Math.min (minLongitude, longitude);
minLatitude = Math.min (minLatitude, latitude);
maxLongitude = Math.max (maxLongitude, longitude);
maxLatitude = Math.max (maxLatitude, latitude);
}
}
}
}
centerLongitude = centerLongitude / coordsCount;
centerLatitude /= coordsCount;
debug ("rotation centerLongitude="+centerLongitude+", centerLatitude="+centerLatitude);
if (useWangCells) {
selectedWidth *= 2;
selectedHeight *= 2;
}
let xMapScaling = selectedWidth / (maxLongitude - minLongitude);
let yMapScaling = selectedHeight / (maxLatitude - minLatitude);
if (respectOSMRatio) {
xMapScaling = yMapScaling = Math.min (xMapScaling, yMapScaling);
}
debug ("xMapScaling="+xMapScaling);
debug ("yMapScaling="+yMapScaling);
const angle = rotation;
const editedLayer = activeMap.selectedLayers [0];
// Setup a grid for drawing and filling
grid = new Array (selectedWidth);
for (let x = 0; x < grid.length; x ++) {
grid [x] = new Array (selectedHeight);
}
for (const feature of p.features) {
let geometry = feature.geometry;
const type = geometry.type;
let coordsSet;
switch (type) {
case "LineString":
coordsSet = [geometry.coordinates];
break;
case "MultiLineString":
case "Polygon":
coordsSet = geometry.coordinates;
break;
default:
tiled.warn ("geometry type " + type + " is unknown, skipping it");
break;
}
if (coordsSet === undefined) {
continue;
}
for (const coordinates of coordsSet) {
let centerX = 0;
let centerY = 0;
let coordsCount = 0;
let previousX;
let previousY;
let currentX;
let currentY;
for (const coord of coordinates) {
if (coordsCounter < 0) break; // this is a DEBUG feature
let coordLongitude = coord [0];
let coordLatitude = coord [1];
if (angle !== 0) {
const rotatedCoords = rotate (
centerLongitude, centerLatitude,
coordLongitude, coordLatitude, angle);
debug ("Rotated coords="+rotatedCoords[0]+","+rotatedCoords[1] +" (from " + coordLongitude+","+coordLatitude+")");
coordLongitude = rotatedCoords [0];
coordLatitude = rotatedCoords [1];
}
currentX = /*selectedX + */ Math.floor ((coordLongitude - minLongitude) * xMapScaling);
//debug ("coord="+coord+", minLongitude="+minLongitude+",xMapScaling="+xMapScaling);
currentY = /*selectedY + */ selectedHeight - Math.floor ((coordLatitude - minLatitude) * yMapScaling); // Latitudes go up and map coordinates go down, so we need to take the opposite of the latitude
//debug ("currentX="+currentX+",currentY="+currentY);
centerX += coordLongitude;
centerY += coordLatitude;
coordsCount ++;
if (previousX !== undefined) {
if (previousX !== currentX || previousY !== currentY) {
coordsCounter --;
//debug ("Line " + previousX + "," + previousY + "-" + currentX + "," + currentY);
if (lineWidth === 1) {
line (previousX, previousY, currentX, currentY, plot (drawId), useWangCells);
}
else {
const dx = currentX - previousX;
const dy = currentY - previousY;
const d = Math.sqrt (dx*dx+dy*dy);
const bx = Math.floor (-lineWidth*dy/(d*2));
const by = Math.floor (lineWidth*dx/(d*2));
tiled.log ("border from " + (previousX+bx) + ","
+ (previousY+by) + " to " + (previousX-bx) + ","
+ (previousY-by));
line (previousX+bx, previousY+by,
previousX-bx, previousY-by,
function (x, y) {
line (x, y, x+dx, y+dy, plot (drawId), useWangCells);
}, useWangCells);
}
}
}
previousX = currentX;
previousY = currentY;
}
if (fillPolygons && type === "Polygon") {
centerX /= coordsCount;
centerY /= coordsCount;
centerX = /*selectedX +*/ Math.floor ((centerX - minLongitude) * xMapScaling);
centerY = /*selectedY +*/ selectedHeight - Math.floor ((centerY - minLatitude) * yMapScaling);
debug ("polygon centerX="+centerX+",centerY="+centerY+",drawId="+drawId);
fill (centerX, centerY,
grid.length, grid [0].length,
drawId, function (x, y) {
return grid [x] [y];
}, plot (drawId));
// TODO : utiliser une grille de wangCells
}
}
}
// Draw from the grid to the tiles
const edit = editedLayer.edit (); // TODO check only one layer is selected
const tileset = activeMap.usedTilesets()[0]; // TODO fix this (many tilesets are possible)
if (!useWangCells) {
for (let x = 0; x < grid.length; x ++) {
for (let y = 0; y < grid [0].length; y ++) {
if (grid [x] [y] !== undefined) {
edit.setTile (selectedX + x, selectedY + y, tileset.findTile (drawId));
}
}
}
}
else {
// We make sure the wang IDs will be ok for a Terrains Corner Set
for (let x = 0; x < grid.length; x ++) {
for (let y = 0; y < grid [0].length; y ++) {
if (grid [x] [y] !== undefined) {
const xSmallOdd = x - 1 | 1;
const ySmallOdd = y - 1 | 1;
//debug ("xSmallOdd="+xSmallOdd);
if (grid [xSmallOdd] === undefined) {
grid [xSmallOdd] = new Array (selectedHeight);
}
grid [xSmallOdd] [ySmallOdd] = grid [x] [y];
grid [xSmallOdd] [ySmallOdd + 1] = grid [x] [y];
if (grid [xSmallOdd + 1] === undefined) {
grid [xSmallOdd + 1] = new Array (selectedHeight);
}
grid [xSmallOdd + 1] [ySmallOdd] = grid [x] [y];
grid [xSmallOdd + 1] [ySmallOdd + 1] = grid [x] [y];
}
}
}
let stop = false; // DEBUG
let wangCounter = 10000;
// Now find tiles with the appropriate wang ids (=colors), matching also the colors on the map
for (let tileX = selectedX; !stop && tileX < selectedX + selectedWidth / 2; tileX ++) { // /2 because it has been multiplied by 2 earlier - FIXME make another variable
for (let tileY = selectedY; !stop && tileY < selectedY + selectedHeight / 2; tileY ++) { // See comment above
const tile = editedLayer.tileAt (tileX, tileY);
let wangId;
if (tile === null) {
wangId = [0, 0, 0, 0, 0, 0, 0, 0];
debug ("empty tile @"+tileX+","+tileY);
}
else {
wangId = tile.tileset.wangSets[0].wangId (tile); // TODO could be more than one wangsets
}
const x = (tileX - selectedX) * 2;
const y = (tileY - selectedY) * 2;
let changed = false;
if (grid [x] [y] !== undefined) {
wangId [TOP_LEFT] = grid [x] [y];
changed = true;
}
if (grid [x+1] [y] !== undefined) {
wangId [TOP_RIGHT] = grid [x+1] [y];
changed = true;
}
if (grid [x] [y+1] !== undefined) {
wangId [BOTTOM_LEFT] = grid [x] [y+1];
changed = true;
}
if (grid [x+1] [y+1] !== undefined) {
wangId [BOTTOM_RIGHT] = grid [x+1] [y+1];
changed = true;
}
if (changed) {
debug ("wangIdsToTiles="+wangIdsToTiles);
const newTile = pickRandomTile (wangId, wangIdsToTiles, tileProbabilities);
if (tile === null) debug ("newTile="+newTile);
if (newTile !== undefined) {
edit.setTile (tileX, tileY, tileById [newTile]);
}
else {
if (emptyUndefined) {
edit.setTile (tileX, tileY, null);
}
else {
if (fillUndefined) {
edit.setTile (tileX, tileY, tileById [pickRandomTile ([0, drawId, 0, drawId, 0, drawId, 0, drawId], wangIdsToTiles, tileProbabilities)]);
}
}
}
}
stop = (wangCounter--<0);
}
}
}
edit.apply ();
//const c = g.coordinates[0][0]; // Premier point (x, y) d'un MultiLineString
//g.coordinates[0]; // Premier point (x, y) d'un LineString
};
importOSMAction.text = "Import OSM";
tiled.extendMenu ("Map", [{action: "ImportOSM", before: "MapProperties"}, {separator: true}]);