forked from arthur-e/Wicket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wicket-arcgis.js
392 lines (336 loc) · 12.8 KB
/
wicket-arcgis.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
/** @license
*
* Copyright (C) 2012 K. Arthur Endsley ([email protected])
* 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/>.
*
*/
if (!Array.prototype.map) {
Array.prototype.map = function (fun /* thisArg? */) {
'use strict';
var t, len, res, thisArg;
if (this === void 0 || this === null) {
throw new TypeError();
}
t = Object(this);
len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
res = new Array(len);
thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
// NOTE: Absolute correctness would demand Object.defineProperty
// be used. But this method is fairly new, and failure is
// possible only if Object.prototype or Array.prototype
// has a property |i| (very unlikely), so use a less-correct
// but more portable alternative.
if (i in t) {
res[i] = fun.call(thisArg, t[i], i, t);
}
}
return res;
};
}
/**
* @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
* 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 {esri.geometry.Point}
*/
point: function (config, component) {
var coord = component || this.components;
if (coord instanceof Array) {
coord = coord[0];
}
if (config) {
// Allow the specification of a coordinate system
coord.spatialReference = config.spatialReference || config.srs;
}
return new esri.geometry.Point(coord);
},
/**
* Creates the framework's equivalent multipoint geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {esri.geometry.Multipoint}
*/
multipoint: function (config) {
config = config || {};
if (!config.spatialReference && config.srs) {
config.spatialReference = config.srs;
}
return new esri.geometry.Multipoint({
// Create an Array of [x, y] coords from each point among the components
points: this.components.map(function (i) {
if (Wkt.isArray(i)) {
i = i[0]; // Unwrap coords
}
return [i.x, i.y];
}),
spatialReference: config.spatialReference
});
},
/**
* Creates the framework's equivalent linestring geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {esri.geometry.Polyline}
*/
linestring: function (config) {
config = config || {};
if (!config.spatialReference && config.srs) {
config.spatialReference = config.srs;
}
return new esri.geometry.Polyline({
// Create an Array of paths...
paths: [
this.components.map(function (i) {
return [i.x, i.y];
})
],
spatialReference: config.spatialReference
});
},
/**
* Creates the framework's equivalent multilinestring geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {esri.geometry.Polyline}
*/
multilinestring: function (config) {
config = config || {};
if (!config.spatialReference && config.srs) {
config.spatialReference = config.srs;
}
return new esri.geometry.Polyline({
// Create an Array of paths...
paths: this.components.map(function (i) {
// ...Within which are Arrays of coordinate pairs (vertices)
return i.map(function (j) {
return [j.x, j.y];
});
}),
spatialReference: config.spatialReference
});
},
/**
* Creates the framework's equivalent polygon geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {esri.geometry.Polygon}
*/
polygon: function (config) {
config = config || {};
if (!config.spatialReference && config.srs) {
config.spatialReference = config.srs;
}
return new esri.geometry.Polygon({
// Create an Array of rings...
rings: this.components.map(function (i) {
// ...Within which are Arrays of coordinate pairs (vertices)
return i.map(function (j) {
return [j.x, j.y];
});
}),
spatialReference: config.spatialReference
});
},
/**
* Creates the framework's equivalent multipolygon geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {esri.geometry.Polygon}
*/
multipolygon: function (config) {
var that = this;
config = config || {};
if (!config.spatialReference && config.srs) {
config.spatialReference = config.srs;
}
return new esri.geometry.Polygon({
// Create an Array of rings...
rings: (function () {
var i, j, holey, newRings, rings;
holey = false; // Assume there are no inner rings (holes)
rings = that.components.map(function (i) {
// ...Within which are Arrays of (outer) rings (polygons)
var rings = i.map(function (j) {
// ...Within which are (possibly) Arrays of (inner) rings (holes)
return j.map(function (k) {
return [k.x, k.y];
});
});
holey = (rings.length > 1);
return rings;
});
if (!holey && rings[0].length > 1) { // Easy, if there are no inner rings (holes)
// But we add the second condition to check that we're not too deeply nested
return rings;
}
newRings = [];
for (i = 0; i < rings.length; i += 1) {
if (rings[i].length > 1) {
for (j = 0; j < rings[i].length; j += 1) {
newRings.push(rings[i][j]);
}
} else {
newRings.push(rings[i][0]);
}
}
return newRings;
}()),
spatialReference: config.spatialReference
});
}
};
/**
* A test for determining whether one ring is an inner ring of another; tests
* to see whether the first argument (ring1) is an inner ring of the second
* (ring2) argument
* @param ring1 {Array} An Array of vertices that describe a ring in an esri.geometry.Polygon instance
* @param ring2 {Array} An Array of vertices that describe a ring in an esri.geometry.Polygon instance
* @param srs {esri.SpatialReference} The SRS to conduct this test within
* @return {Boolean}
*/
Wkt.isInnerRingOf = function (ring1, ring2, srs) {
var contained, i, ply, pnt;
// Though less common, we assume that the first ring is an inner ring of the
// second as this is a stricter case (all vertices must be contained);
// we'll test this against the contrary where at least one vertex of the
// first ring is not contained by the second ring (ergo, not an inner ring)
contained = true;
ply = new esri.geometry.Polygon({ // Create single polygon from second ring
rings: ring2.map(function (i) {
// ...Within which are Arrays of coordinate pairs (vertices)
return i.map(function (j) {
return [j.x, j.y];
});
}),
spatialReference: srs
});
for (i = 0; i < ring1.length; i += 1) {
// Sample a vertex of the first ring
pnt = new esri.geometry.Point(ring1[i].x, ring1[i].y, srs);
// Now we have a test for inner rings: if the second ring does not
// contain every vertex of the first, then the first ring cannot be
// an inner ring of the second
if (!ply.contains(pnt)) {
contained = false;
break;
}
}
return contained;
};
/**
* @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 i, j, paths, rings, verts;
// esri.geometry.Point /////////////////////////////////////////////////////
if (obj.constructor === esri.geometry.Point) {
return {
type: 'point',
components: [{
x: obj.x,
y: obj.y
}]
};
}
// esri.geometry.Multipoint ////////////////////////////////////////////////
if (obj.constructor === esri.geometry.Multipoint) {
verts = [];
for (i = 0; i < obj.points.length; i += 1) {
verts.push([{
x: obj.points[i][0],
y: obj.points[i][1]
}]);
}
return {
type: 'multipoint',
components: verts
};
}
// esri.geometry.Polyline //////////////////////////////////////////////////
if (obj.constructor === esri.geometry.Polyline) {
paths = [];
for (i = 0; i < obj.paths.length; i += 1) {
verts = [];
for (j = 0; j < obj.paths[i].length; j += 1) {
verts.push({
x: obj.paths[i][j][0], // First item is longitude, second is latitude
y: obj.paths[i][j][1]
});
}
paths.push(verts);
}
if (obj.paths.length > 1) { // More than one path means more than one linestring
return {
type: 'multilinestring',
components: paths
};
}
return {
type: 'linestring',
components: verts
};
}
// esri.geometry.Polygon ///////////////////////////////////////////////////
if (obj.constructor === esri.geometry.Polygon || obj.constructor === esri.geometry.Circle) {
rings = [];
for (i = 0; i < obj.rings.length; i += 1) {
verts = [];
for (j = 0; j < obj.rings[i].length; j += 1) {
verts.push({
x: obj.rings[i][j][0], // First item is longitude, second is latitude
y: obj.rings[i][j][1]
});
}
if (i > 0) {
if (Wkt.isInnerRingOf(verts, rings[rings.length - 1], obj.spatialReference)) {
rings[rings.length - 1].push(verts);
} else {
rings.push([verts]);
}
} else {
rings.push([verts]);
}
}
if (rings.length > 1) {
return {
type: 'multipolygon',
components: rings
};
}
return {
type: 'polygon',
components: rings[0]
};
}
};