forked from arthur-e/Wicket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wicket-leaflet.js
408 lines (346 loc) · 13.4 KB
/
wicket-leaflet.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/** @license
*
* Copyright (C) 2012 K. Arthur Endsley (kaendsle@mtu.edu)
* Michigan Tech Research Institute (MTRI)
* 3600 Green Court, Suite 100, Ann Arbor, MI, 48105
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @augments Wkt.Wkt
* A framework-dependent flag, set for each Wkt.Wkt() instance, that indicates
* whether or not a closed polygon geometry should be interpreted as a rectangle.
*/
Wkt.Wkt.prototype.isRectangle = false;
/**
* @augments Wkt.Wkt
* Truncates an Array of coordinates by the closing coordinate when it is
* equal to the first coordinate given--this is only to be used for closed
* geometries in order to provide merely an "implied" closure to Leaflet.
* @param coords {Array} An Array of x,y coordinates (objects)
* @return {Array}
*/
Wkt.Wkt.prototype.trunc = function (coords) {
var i, verts = [];
for (i = 0; i < coords.length; i += 1) {
if (Wkt.isArray(coords[i])) {
verts.push(this.trunc(coords[i]));
} else {
// Add the first coord, but skip the last if it is identical
if (i === 0 || !this.sameCoords(coords[0], coords[i])) {
verts.push(coords[i]);
}
}
}
return verts;
};
/**
* @augments Wkt.Wkt
* An object of framework-dependent construction methods used to generate
* objects belonging to the various geometry classes of the framework.
*/
Wkt.Wkt.prototype.construct = {
/**
* Creates the framework's equivalent point geometry object.
* @param config {Object} An optional properties hash the object should use
* @param component {Object} An optional component to build from
* @return {L.marker}
*/
point: function (config, component) {
var coord = component || this.components;
if (coord instanceof Array) {
coord = coord[0];
}
return L.marker(this.coordsToLatLng(coord), config);
},
/**
* Creates the framework's equivalent multipoint geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {L.featureGroup}
*/
multipoint: function (config) {
var i,
layers = [],
coords = this.components;
for (i = 0; i < coords.length; i += 1) {
layers.push(this.construct.point.call(this, config, coords[i]));
}
return L.featureGroup(layers, config);
},
/**
* Creates the framework's equivalent linestring geometry object.
* @param config {Object} An optional properties hash the object should use
* @param component {Object} An optional component to build from
* @return {L.polyline}
*/
linestring: function (config, component) {
var coords = component || this.components,
latlngs = this.coordsToLatLngs(coords);
return L.polyline(latlngs, config);
},
/**
* Creates the framework's equivalent multilinestring geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {L.multiPolyline}
*/
multilinestring: function (config) {
var coords = this.components,
latlngs = this.coordsToLatLngs(coords, 1);
return L.multiPolyline(latlngs, config);
},
/**
* Creates the framework's equivalent polygon geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {L.multiPolygon}
*/
polygon: function (config) {
// Truncate the coordinates to remove the closing coordinate
var coords = this.trunc(this.components),
latlngs = this.coordsToLatLngs(coords, 1);
return L.polygon(latlngs, config);
},
/**
* Creates the framework's equivalent multipolygon geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {L.multiPolygon}
*/
multipolygon: function (config) {
// Truncate the coordinates to remove the closing coordinate
var coords = this.trunc(this.components),
latlngs = this.coordsToLatLngs(coords, 2);
return L.multiPolygon(latlngs, config);
},
/**
* Creates the framework's equivalent collection of geometry objects.
* @param config {Object} An optional properties hash the object should use
* @return {L.featureGroup}
*/
geometrycollection: function (config) {
var comps, i, layers;
comps = this.trunc(this.components);
layers = [];
for (i = 0; i < this.components.length; i += 1) {
layers.push(this.construct[comps[i].type].call(this, comps[i]));
}
return L.featureGroup(layers, config);
}
};
L.Util.extend(Wkt.Wkt.prototype, {
coordsToLatLngs: L.GeoJSON.coordsToLatLngs,
// TODO Why doesn't the coordsToLatLng function in L.GeoJSON already suffice?
coordsToLatLng: function (coords, reverse) {
var lat = reverse ? coords.x : coords.y,
lng = reverse ? coords.y : coords.x;
return L.latLng(lat, lng, true);
}
});
/**
* @augments Wkt.Wkt
* A framework-dependent deconstruction method used to generate internal
* geometric representations from instances of framework geometry. This method
* uses object detection to attempt to classify members of framework geometry
* classes into the standard WKT types.
* @param obj {Object} An instance of one of the framework's geometry classes
* @return {Object} A hash of the 'type' and 'components' thus derived
*/
Wkt.Wkt.prototype.deconstruct = function (obj) {
var attr, coordsFromLatLngs, features, i, verts, rings, tmp;
/**
* Accepts an Array (arr) of LatLngs from which it extracts each one as a
* vertex; calls itself recursively to deal with nested Arrays.
*/
coordsFromLatLngs = function (arr) {
var i, coords;
coords = [];
for (i = 0; i < arr.length; i += 1) {
if (Wkt.isArray(arr[i])) {
coords.push(coordsFromLatLngs(arr[i]));
} else {
coords.push({
x: arr[i].lng,
y: arr[i].lat
});
}
}
return coords;
};
// L.Marker ////////////////////////////////////////////////////////////////
if (obj.constructor === L.Marker || obj.constructor === L.marker) {
return {
type: 'point',
components: [{
x: obj.getLatLng().lng,
y: obj.getLatLng().lat
}]
};
}
// L.Rectangle /////////////////////////////////////////////////////////////
if (obj.constructor === L.Rectangle || obj.constructor === L.rectangle) {
tmp = obj.getBounds(); // L.LatLngBounds instance
return {
type: 'polygon',
isRectangle: true,
components: [
[
{ // NW corner
x: tmp.getSouthWest().lng,
y: tmp.getNorthEast().lat
},
{ // NE corner
x: tmp.getNorthEast().lng,
y: tmp.getNorthEast().lat
},
{ // SE corner
x: tmp.getNorthEast().lng,
y: tmp.getSouthWest().lat
},
{ // SW corner
x: tmp.getSouthWest().lng,
y: tmp.getSouthWest().lat
},
{ // NW corner (again, for closure)
x: tmp.getSouthWest().lng,
y: tmp.getNorthEast().lat
}
]
]
};
}
// L.Polyline //////////////////////////////////////////////////////////////
if (obj.constructor === L.Polyline || obj.constructor === L.polyline) {
verts = [];
tmp = obj.getLatLngs();
if (!tmp[0].equals(tmp[tmp.length - 1])) {
for (i = 0; i < tmp.length; i += 1) {
verts.push({
x: tmp[i].lng,
y: tmp[i].lat
});
}
return {
type: 'linestring',
components: verts
};
}
}
// L.Polygon ///////////////////////////////////////////////////////////////
if (obj.constructor === L.Polygon || obj.constructor === L.polygon) {
rings = [];
verts = [];
tmp = obj.getLatLngs();
// First, we deal with the boundary points
for (i = 0; i < obj._latlngs.length; i += 1) {
verts.push({ // Add the first coordinate again for closure
x: tmp[i].lng,
y: tmp[i].lat
});
}
verts.push({ // Add the first coordinate again for closure
x: tmp[0].lng,
y: tmp[0].lat
});
rings.push(verts);
// Now, any holes
if (obj._holes && obj._holes.length > 0) {
// Reworked to support holes properly
verts = coordsFromLatLngs(obj._holes);
for (i=0; i < verts.length;i++) {
verts[i].push(verts[i][0]); // Copy the beginning coords again for closure
rings.push(verts[i]);
}
}
return {
type: 'polygon',
components: rings
};
}
// L.MultiPolyline /////////////////////////////////////////////////////////
// L.MultiPolygon //////////////////////////////////////////////////////////
// L.LayerGroup ////////////////////////////////////////////////////////////
// L.FeatureGroup //////////////////////////////////////////////////////////
if (obj.constructor === L.MultiPolyline || obj.constructor === L.MultiPolygon
|| obj.constructor === L.LayerGroup || obj.constructor === L.FeatureGroup) {
features = [];
tmp = obj._layers;
for (attr in tmp) {
if (tmp.hasOwnProperty(attr)) {
if (tmp[attr].getLatLngs || tmp[attr].getLatLng) {
// Recursively deconstruct each layer
features.push(this.deconstruct(tmp[attr]));
}
}
}
return {
type: (function () {
switch (obj.constructor) {
case L.MultiPolyline:
return 'multilinestring';
case L.MultiPolygon:
return 'multipolygon';
case L.FeatureGroup:
return (function () {
var i, mpgon, mpline, mpoint;
// Assume that all layers are of one type (any one type)
mpgon = true;
mpline = true;
mpoint = true;
for (i in obj._layers) {
if (obj._layers.hasOwnProperty(i)) {
if (obj._layers[i].constructor !== L.Marker) {
mpoint = false;
}
if (obj._layers[i].constructor !== L.Polyline) {
mpline = false;
}
if (obj._layers[i].constructor !== L.Polygon) {
mpgon = false;
}
}
}
if (mpoint) {
return 'multipoint';
}
if (mpline) {
return 'multilinestring';
}
if (mpgon) {
return 'multipolygon';
}
return 'geometrycollection';
}());
default:
return 'geometrycollection';
}
}()),
components: (function () {
// Pluck the components from each Wkt
var i, comps;
comps = [];
for (i = 0; i < features.length; i += 1) {
if (features[i].components) {
comps.push(features[i].components);
}
}
return comps;
}())
};
}
// L.Circle ////////////////////////////////////////////////////////////////
if (obj.constructor === L.Rectangle || obj.constructor === L.rectangle) {
console.log('Deconstruction of L.Circle objects is not yet supported');
} else {
console.log('The passed object does not have any recognizable properties.');
}
};