Debian Codegraph @

js/jquery.geo.core.js

raw symbols SCIP
1 (function ($, window, undefined) {
2 window.toStaticHTML = window.toStaticHTML || function ( x ) { return x; };
3
4 var pos_oo = Number.POSITIVE_INFINITY,
5 neg_oo = Number.NEGATIVE_INFINITY;
6
7 $.geo = {
8 //
9 // utility functions
10 //
11
12 _allCoordinates: function (geom) {
13 // return array of all positions in all geometries of geom
14 // not in JTS
15 var geometries = this._flatten(geom),
16 curGeom = 0,
17 result = [];
18
19 for (; curGeom < geometries.length; curGeom++) {
20 var coordinates = geometries[curGeom].coordinates,
21 isArray = coordinates && $.isArray(coordinates[0]),
22 isDblArray = isArray && $.isArray(coordinates[0][0]),
23 isTriArray = isDblArray && $.isArray(coordinates[0][0][0]),
24 i, j, k;
25
26 if (!isTriArray) {
27 if (!isDblArray) {
28 if (!isArray) {
29 coordinates = [coordinates];
30 }
31 coordinates = [coordinates];
32 }
33 coordinates = [coordinates];
34 }
35
36 for (i = 0; i < coordinates.length; i++) {
37 for (j = 0; j < coordinates[i].length; j++) {
38 for (k = 0; k < coordinates[i][j].length; k++) {
39 result.push(coordinates[i][j][k]);
40 }
41 }
42 }
43 }
44 return result;
45 },
46
47 _isGeodetic: function( coords ) {
48 // returns true if the first coordinate it can find is geodetic
49
50 while ( $.isArray( coords ) ) {
51 if ( coords.length > 1 && ! $.isArray( coords[ 0 ] ) ) {
52 return ( coords[ 0 ] >= -180.001 && coords[ 0 ] <= 180.001 && coords[ 1 ] >= -90.001 && coords[ 1 ] <= 90.001 );
53 } else {
54 coords = coords[ 0 ];
55 }
56 }
57
58 return false;
59 },
60
61 //
62 // bbox functions
63 //
64
65 center: function (bbox, _ignoreGeo /* Internal Use Only */) {
66 // Envelope.centre in JTS
67 // bbox only, use centroid for geom
68 var wasGeodetic = false;
69 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
70 wasGeodetic = true;
71 bbox = $.geo.proj.fromGeodetic(bbox);
72 }
73
74 var center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
75 return wasGeodetic ? $.geo.proj.toGeodetic(center) : center;
76 },
77
78 expandBy: function (bbox, dx, dy, _ignoreGeo /* Internal Use Only */) {
79 var wasGeodetic = false;
80 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
81 wasGeodetic = true;
82 bbox = $.geo.proj.fromGeodetic(bbox);
83 }
84
85 bbox = [bbox[0] - dx, bbox[1] - dy, bbox[2] + dx, bbox[3] + dy];
86 return wasGeodetic ? $.geo.proj.toGeodetic(bbox) : bbox;
87 },
88
89 height: function (bbox, _ignoreGeo /* Internal Use Only */ ) {
90 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
91 bbox = $.geo.proj.fromGeodetic(bbox);
92 }
93
94 return bbox[3] - bbox[1];
95 },
96
97 _in: function(bbox1, bbox2) {
98 return bbox1[0] <= bbox2[0] &&
99 bbox1[1] <= bbox2[1] &&
100 bbox1[2] >= bbox2[2] &&
101 bbox1[3] >= bbox2[3];
102 },
103
104 _bboxDisjoint: function( bbox1, bbox2 ) {
105 return bbox2[ 0 ] > bbox1[ 2 ] ||
106 bbox2[ 2 ] < bbox1[ 0 ] ||
107 bbox2[ 1 ] > bbox1[ 3 ] ||
108 bbox2[ 3 ] < bbox1[ 1 ];
109 },
110
111 include: function( bbox, value, _ignoreGeo /* Internal Use Only */ ) {
112 // similar to Envelope.expandToInclude in JTS
113 if ( !value || !$.isArray( value ) ) {
114 return bbox;
115 }
116
117 var wasGeodetic = false;
118 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox || value ) ) {
119 wasGeodetic = true;
120 }
121
122 if ( !bbox ) {
123 bbox = [ pos_oo, pos_oo, neg_oo, neg_oo ];
124 } else if ( wasGeodetic ) {
125 bbox = $.geo.proj.fromGeodetic( bbox );
126 }
127
128 if ( value.length === 2 ) {
129 value = [ value[ 0 ], value[ 1 ], value[ 0 ], value[ 1 ] ];
130 }
131
132 if ( $.geo.proj ) {
133 value = $.geo.proj.fromGeodetic( value );
134 }
135
136 bbox[0] = Math.min( value[ 0 ], bbox[ 0 ] );
137 bbox[1] = Math.min( value[ 1 ], bbox[ 1 ] );
138 bbox[2] = Math.max( value[ 2 ], bbox[ 2 ] );
139 bbox[3] = Math.max( value[ 3 ], bbox[ 3 ] );
140
141 return wasGeodetic ? $.geo.proj.toGeodetic( bbox ) : bbox;
142 },
143
144 polygonize: function( bbox, _ignoreGeo /* Internal Use Only */ ) {
145 // adaptation of Polygonizer class in JTS for use with bboxes
146 var wasGeodetic = false;
147 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
148 wasGeodetic = true;
149 bbox = $.geo.proj.fromGeodetic(bbox);
150 }
151
152 var polygon = {
153 type: "Polygon",
154 coordinates: [ [
155 [ bbox[ 0 ], bbox[ 1 ] ],
156 [ bbox[ 0 ], bbox[ 3 ] ],
157 [ bbox[ 2 ], bbox[ 3 ] ],
158 [ bbox[ 2 ], bbox[ 1 ] ],
159 [ bbox[ 0 ], bbox[ 1 ] ]
160 ] ]
161 };
162
163 if ( wasGeodetic ) {
164 polygon.coordinates = $.geo.proj.toGeodetic( polygon.coordinates );
165 }
166
167 return polygon;
168 },
169
170 reaspect: function (bbox, ratio, _ignoreGeo /* Internal Use Only */ ) {
171 // not in JTS
172 var wasGeodetic = false;
173 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
174 wasGeodetic = true;
175 bbox = $.geo.proj.fromGeodetic(bbox);
176 }
177
178 var width = this.width(bbox, true),
179 height = this.height(bbox, true),
180 center = this.center(bbox, true),
181 dx, dy;
182
183 if (width !== 0 && height !== 0 && ratio > 0) {
184 if (width / height > ratio) {
185 dx = width / 2;
186 dy = dx / ratio;
187 } else {
188 dy = height / 2;
189 dx = dy * ratio;
190 }
191
192 bbox = [center[0] - dx, center[1] - dy, center[0] + dx, center[1] + dy];
193 }
194
195 return wasGeodetic ? $.geo.proj.toGeodetic(bbox) : bbox;
196 },
197
198 recenter: function( bbox, center, _ignoreGeo /* Internal Use Only */ ) {
199 // not in JTS
200 var wasGeodetic = false;
201 if ( !_ignoreGeo && $.geo.proj ) {
202 if ( this._isGeodetic( bbox ) ) {
203 wasGeodetic = true;
204 bbox = $.geo.proj.fromGeodetic(bbox);
205 }
206
207 if ( this._isGeodetic( center ) ) {
208 center = $.geo.proj.fromGeodetic(center);
209 }
210 }
211
212 var halfWidth = ( bbox[ 2 ] - bbox[ 0 ] ) / 2,
213 halfHeight = ( bbox[ 3 ] - bbox[ 1 ] ) / 2;
214
215 bbox = [
216 center[ 0 ] - halfWidth,
217 center[ 1 ] - halfHeight,
218 center[ 0 ] + halfWidth,
219 center[ 1 ] + halfHeight
220 ];
221
222 return wasGeodetic ? $.geo.proj.toGeodetic(bbox) : bbox;
223 },
224
225 scaleBy: function ( bbox, scale, _ignoreGeo /* Internal Use Only */ ) {
226 // not in JTS
227 var wasGeodetic = false;
228 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
229 wasGeodetic = true;
230 bbox = $.geo.proj.fromGeodetic(bbox);
231 }
232
233 var c = this.center(bbox, true),
234 dx = (bbox[2] - bbox[0]) * scale / 2,
235 dy = (bbox[3] - bbox[1]) * scale / 2;
236
237 bbox = [c[0] - dx, c[1] - dy, c[0] + dx, c[1] + dy];
238
239 return wasGeodetic ? $.geo.proj.toGeodetic(bbox) : bbox;
240 },
241
242 width: function (bbox, _ignoreGeo /* Internal Use Only */ ) {
243 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( bbox ) ) {
244 bbox = $.geo.proj.fromGeodetic(bbox);
245 }
246
247 return bbox[2] - bbox[0];
248 },
249
250 //
251 // geometry functions
252 //
253
254 // bbox (Geometry.getEnvelope in JTS)
255
256 bbox: function ( geom, _ignoreGeo /* Internal Use Only */ ) {
257 var result, wasGeodetic = false;
258 if ( !geom ) {
259 return undefined;
260 } else if ( geom.bbox ) {
261 result = ( !_ignoreGeo && $.geo.proj && this._isGeodetic( geom.bbox ) ) ? $.geo.proj.fromGeodetic( geom.bbox ) : geom.bbox;
262 } else {
263 result = [ pos_oo, pos_oo, neg_oo, neg_oo ];
264
265 var coordinates = this._allCoordinates( geom ),
266 curCoord = 0;
267
268 if ( coordinates.length === 0 ) {
269 return undefined;
270 }
271
272 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( coordinates ) ) {
273 wasGeodetic = true;
274 coordinates = $.geo.proj.fromGeodetic( coordinates );
275 }
276
277 for ( ; curCoord < coordinates.length; curCoord++ ) {
278 result[0] = Math.min(coordinates[curCoord][0], result[0]);
279 result[1] = Math.min(coordinates[curCoord][1], result[1]);
280 result[2] = Math.max(coordinates[curCoord][0], result[2]);
281 result[3] = Math.max(coordinates[curCoord][1], result[3]);
282 }
283 }
284
285 return wasGeodetic ? $.geo.proj.toGeodetic(result) : result;
286 },
287
288 // centroid
289
290 centroid: function( geom, _ignoreGeo /* Internal Use Only */ ) {
291 var c; //< temp storage for any coordinate during centroid op
292
293 switch (geom.type) {
294 case "Point":
295 return $.extend({}, geom);
296
297 case "LineString":
298 case "Polygon":
299 var a = 0,
300 coords = $.merge( [ ], geom.type === "Polygon" ? geom.coordinates[0] : geom.coordinates ),
301 i = 1, j, n,
302 bbox = [ pos_oo, pos_oo, neg_oo, neg_oo ];
303
304 c = [ 0, 0 ];
305
306 var wasGeodetic = false;
307 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( coords ) ) {
308 wasGeodetic = true;
309 coords = $.geo.proj.fromGeodetic(coords);
310 }
311
312 //if (coords[0][0] != coords[coords.length - 1][0] || coords[0][1] != coords[coords.length - 1][1]) {
313 // coords.push(coords[0]);
314 //}
315
316 for (; i <= coords.length; i++) {
317 j = i % coords.length;
318
319 bbox[0] = Math.min(coords[j][0], bbox[0]);
320 bbox[1] = Math.min(coords[j][1], bbox[1]);
321 bbox[2] = Math.max(coords[j][0], bbox[2]);
322 bbox[3] = Math.max(coords[j][1], bbox[3]);
323
324 n = (coords[i - 1][0] * coords[j][1]) - (coords[j][0] * coords[i - 1][1]);
325 a += n;
326 c[0] += (coords[i - 1][0] + coords[j][0]) * n;
327 c[1] += (coords[i - 1][1] + coords[j][1]) * n;
328 }
329
330 if (a === 0) {
331 if (coords.length > 0) {
332 c[0] = Math.min( Math.max( coords[0][0], bbox[ 0 ] ), bbox[ 2 ] );
333 c[1] = Math.min( Math.max( coords[0][1], bbox[ 1 ] ), bbox[ 3 ] );
334 return { type: "Point", coordinates: wasGeodetic ? $.geo.proj.toGeodetic(c) : c };
335 } else {
336 return undefined;
337 }
338 }
339
340 a *= 3;
341 //c[0] /= a;
342 //c[1] /= a;
343
344 c[0] = Math.min( Math.max( c[0] / a, bbox[ 0 ] ), bbox[ 2 ] );
345 c[1] = Math.min( Math.max( c[1] / a, bbox[ 1 ] ), bbox[ 3 ] );
346
347 return { type: "Point", coordinates: wasGeodetic ? $.geo.proj.toGeodetic(c) : c };
348
349 case "MultiPoint":
350 // should return center of mass for point cluster but just return first point for now
351 if ( geom.coordinates.length > 0 ) {
352 c = geom.coordinates[ 0 ];
353 return {
354 type: "Point",
355 coordinates: [ c[ 0 ], c[ 1 ] ]
356 };
357 }
358 break;
359
360 case "MultiLineString":
361 case "MultiPolygon":
362 if ( geom.coordinates.length > 0 ) {
363 return this.centroid( {
364 type: geom.type.substr( 5 ),
365 coordinates: geom.coordinates[ 0 ]
366 }, _ignoreGeo );
367 }
368 break;
369
370 }
371 return undefined;
372 },
373
374 // contains
375
376 contains: function (geom1, geom2) {
377 if (geom1.type !== "Polygon") {
378 return false;
379 }
380
381 switch (geom2.type) {
382 case "Point":
383 return this._containsPolygonPoint(geom1.coordinates, geom2.coordinates);
384
385 case "LineString":
386 return this._containsPolygonLineString(geom1.coordinates, geom2.coordinates);
387
388 case "Polygon":
389 return this._containsPolygonLineString(geom1.coordinates, geom2.coordinates[0]);
390
391 default:
392 return false;
393 }
394 },
395
396 _containsPolygonPoint: function (polygonCoordinates, pointCoordinate) {
397 if (polygonCoordinates.length === 0 || polygonCoordinates[0].length < 4) {
398 return false;
399 }
400
401 var rayCross = 0,
402 a = polygonCoordinates[0][0],
403 i = 1,
404 b,
405 x;
406
407 for (; i < polygonCoordinates[0].length; i++) {
408 b = polygonCoordinates[0][i];
409
410 if ((a[1] <= pointCoordinate[1] && pointCoordinate[1] < b[1]) || (b[1] <= pointCoordinate[1] && pointCoordinate[1] < a[1]) && (pointCoordinate[0] < a[0] || pointCoordinate[0] < b[0])) {
411 x = a[0] + (b[0] - a[0]) * (pointCoordinate[1] - a[1]) / (b[1] - a[1]);
412
413 if (x > pointCoordinate[0]) {
414 rayCross++;
415 }
416 }
417
418 a = b;
419 }
420
421 return rayCross % 2 === 1;
422 },
423
424 _containsPolygonLineString: function (polygonCoordinates, lineStringCoordinates) {
425 for (var i = 0; i < lineStringCoordinates.length; i++) {
426 if (!this._containsPolygonPoint(polygonCoordinates, lineStringCoordinates[i])) {
427 return false;
428 }
429 }
430 return true;
431 },
432
433 // distance
434
435 distance: function ( geom1, geom2, _ignoreGeo /* Internal Use Only */ ) {
436 var geom1CoordinatesProjected = ( !_ignoreGeo && $.geo.proj && this._isGeodetic( geom1.coordinates ) ) ? $.geo.proj.fromGeodetic(geom1.coordinates) : geom1.coordinates,
437 geom2CoordinatesProjected = ( !_ignoreGeo && $.geo.proj && this._isGeodetic( geom2.coordinates ) ) ? $.geo.proj.fromGeodetic(geom2.coordinates) : geom2.coordinates;
438
439 switch (geom1.type) {
440 case "Point":
441 switch (geom2.type) {
442 case "Point":
443 return this._distancePointPoint(geom2CoordinatesProjected, geom1CoordinatesProjected);
444 case "LineString":
445 return this._distanceLineStringPoint(geom2CoordinatesProjected, geom1CoordinatesProjected);
446 case "Polygon":
447 return this._containsPolygonPoint(geom2CoordinatesProjected, geom1CoordinatesProjected) ? 0 : this._distanceLineStringPoint(geom2CoordinatesProjected[0], geom1CoordinatesProjected);
448 default:
449 return undefined;
450 }
451 break;
452
453 case "LineString":
454 switch (geom2.type) {
455 case "Point":
456 return this._distanceLineStringPoint(geom1CoordinatesProjected, geom2CoordinatesProjected);
457 case "LineString":
458 return this._distanceLineStringLineString(geom1CoordinatesProjected, geom2CoordinatesProjected);
459 case "Polygon":
460 return this._containsPolygonLineString(geom2CoordinatesProjected, geom1CoordinatesProjected) ? 0 : this._distanceLineStringLineString(geom2CoordinatesProjected[0], geom1CoordinatesProjected);
461 default:
462 return undefined;
463 }
464 break;
465
466 case "Polygon":
467 switch (geom2.type) {
468 case "Point":
469 return this._containsPolygonPoint(geom1CoordinatesProjected, geom2CoordinatesProjected) ? 0 : this._distanceLineStringPoint(geom1CoordinatesProjected[0], geom2CoordinatesProjected);
470 case "LineString":
471 return this._containsPolygonLineString(geom1CoordinatesProjected, geom2CoordinatesProjected) ? 0 : this._distanceLineStringLineString(geom1CoordinatesProjected[0], geom2CoordinatesProjected);
472 case "Polygon":
473 return this._containsPolygonLineString(geom1CoordinatesProjected, geom2CoordinatesProjected[0]) ? 0 : this._distanceLineStringLineString(geom1CoordinatesProjected[0], geom2CoordinatesProjected[0]);
474 default:
475 return undefined;
476 }
477 break;
478 }
479 },
480
481 _distancePointPoint: function (coordinate1, coordinate2) {
482 var dx = coordinate2[0] - coordinate1[0],
483 dy = coordinate2[1] - coordinate1[1];
484 return Math.sqrt((dx * dx) + (dy * dy));
485 },
486
487 _distanceLineStringPoint: function (lineStringCoordinates, pointCoordinate) {
488 var minDist = pos_oo;
489
490 if (lineStringCoordinates.length > 0) {
491 var a = lineStringCoordinates[0],
492
493 apx = pointCoordinate[0] - a[0],
494 apy = pointCoordinate[1] - a[1];
495
496 if (lineStringCoordinates.length === 1) {
497 return Math.sqrt(apx * apx + apy * apy);
498 } else {
499 for (var i = 1; i < lineStringCoordinates.length; i++) {
500 var b = lineStringCoordinates[i],
501
502 abx = b[0] - a[0],
503 aby = b[1] - a[1],
504 bpx = pointCoordinate[0] - b[0],
505 bpy = pointCoordinate[1] - b[1],
506
507 d = this._distanceSegmentPoint(abx, aby, apx, apy, bpx, bpy);
508
509 if (d === 0) {
510 return 0;
511 }
512
513 if (d < minDist) {
514 minDist = d;
515 }
516
517 a = b;
518 apx = bpx;
519 apy = bpy;
520 }
521 }
522 }
523
524 return Math.sqrt(minDist);
525 },
526
527 _distanceSegmentPoint: function (abx, aby, apx, apy, bpx, bpy) {
528 var dot1 = abx * apx + aby * apy;
529
530 if (dot1 <= 0) {
531 return apx * apx + apy * apy;
532 }
533
534 var dot2 = abx * abx + aby * aby;
535
536 if (dot1 >= dot2) {
537 return bpx * bpx + bpy * bpy;
538 }
539
540 return apx * apx + apy * apy - dot1 * dot1 / dot2;
541 },
542
543 _distanceLineStringLineString: function (lineStringCoordinates1, lineStringCoordinates2) {
544 var minDist = pos_oo;
545 for (var i = 0; i < lineStringCoordinates2.length; i++) {
546 minDist = Math.min(minDist, this._distanceLineStringPoint(lineStringCoordinates1, lineStringCoordinates2[i]));
547 }
548 return minDist;
549 },
550
551 // buffer
552
553 buffer: function( geom, distance, _ignoreGeo /* Internal Use Only */ ) {
554 return this._buffer( geom, distance, _ignoreGeo );
555 },
556
557 _buffer: function( geom, distance, _ignoreGeo /* Internal Use Only */ ) {
558 var wasGeodetic = false,
559 coords = geom.coordinates;
560
561 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( geom.coordinates ) ) {
562 wasGeodetic = true;
563 coords = $.geo.proj.fromGeodetic( geom.coordinates );
564 }
565
566 if ( geom.type === "Point" ) {
567 var resultCoords = [],
568 slices = 180,
569 i = 0,
570 a;
571
572 for ( ; i <= slices; i++ ) {
573 a = ( i * 360 / slices ) * ( Math.PI / 180 );
574 resultCoords.push( [
575 coords[ 0 ] + Math.cos( a ) * distance,
576 coords[ 1 ] + Math.sin( a ) * distance
577 ] );
578 }
579
580 return {
581 type: "Polygon",
582 coordinates: [ ( wasGeodetic ? $.geo.proj.toGeodetic( resultCoords ) : resultCoords ) ]
583 };
584 } else {
585 return undefined;
586 }
587 },
588
589
590 //
591 // feature
592 //
593
594 _basic: function( geom ) {
595 // return an array of all basic geometries
596 // e.g., MultiPolygons become multiple Polygons
597 // coordinate arrays are kept as references for speed & should not be altered
598 // not in JTS
599 var geometries = [ ];
600 var multiType;
601 var i = 0;
602 var j;
603
604 var flat = this._flatten( geom );
605
606 for ( ; i < flat.length; i++ ) {
607 if ( flat[ i ].type.substring( 0, 5 ) === "Multi" ) {
608 multiType = flat[ i ].type.substring( 5 );
609
610 for ( j = 0; j < flat[ i ].coordinates.length; j++ ) {
611 geometries.push( {
612 type: multiType,
613 coordinates: flat[ i ].coordinates[ j ]
614 } );
615 }
616 } else {
617 geometries.push( flat[ i ] );
618 }
619 }
620
621 return geometries;
622 },
623
624 _flatten: function (geom) {
625 // return an array of only geometries
626 // will extract geometries from Feature, FeatureCollection, & GeometryCollection
627 // not in JTS
628 var geometries = [],
629 curGeom = 0;
630 switch (geom.type) {
631 case "Feature":
632 $.merge(geometries, this._flatten(geom.geometry));
633 break;
634
635 case "FeatureCollection":
636 for (; curGeom < geom.features.length; curGeom++) {
637 $.merge(geometries, this._flatten(geom.features[curGeom].geometry));
638 }
639 break;
640
641 case "GeometryCollection":
642 for (; curGeom < geom.geometries.length; curGeom++) {
643 $.merge(geometries, this._flatten(geom.geometries[curGeom]));
644 }
645 break;
646
647 default:
648 geometries[0] = geom;
649 break;
650 }
651 return geometries;
652 },
653
654 length: function( geom, _ignoreGeo /* Internal Use Only */ ) {
655 var sum = 0,
656 lineStringCoordinates,
657 i = 1, dx, dy;
658
659 switch ( geom.type ) {
660 case "Point":
661 return 0;
662
663 case "LineString":
664 lineStringCoordinates = geom.coordinates;
665 break;
666
667 case "Polygon":
668 lineStringCoordinates = geom.coordinates[ 0 ];
669 break;
670 }
671
672 if ( lineStringCoordinates ) {
673 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( lineStringCoordinates ) ) {
674 lineStringCoordinates = $.geo.proj.fromGeodetic( lineStringCoordinates );
675 }
676
677 for ( ; i < lineStringCoordinates.length; i++ ) {
678 dx = lineStringCoordinates[ i ][0] - lineStringCoordinates[ i - 1 ][0];
679 dy = lineStringCoordinates[ i ][1] - lineStringCoordinates[ i - 1 ][1];
680 sum += Math.sqrt((dx * dx) + (dy * dy));
681 }
682
683 return sum;
684 }
685
686 // return undefined;
687 },
688
689 area: function( geom, _ignoreGeo /* Internal Use Only */ ) {
690 var sum = 0,
691 polygonCoordinates,
692 i = 1, j;
693
694 switch ( geom.type ) {
695 case "Point":
696 case "LineString":
697 return 0;
698
699 case "Polygon":
700 polygonCoordinates = geom.coordinates[ 0 ];
701 break;
702 }
703
704 if ( polygonCoordinates ) {
705 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( polygonCoordinates ) ) {
706 polygonCoordinates = $.geo.proj.fromGeodetic( polygonCoordinates );
707 }
708
709 for ( ; i <= polygonCoordinates.length; i++) {
710 j = i % polygonCoordinates.length;
711 sum += ( polygonCoordinates[ i - 1 ][ 0 ] - polygonCoordinates[ j ][ 0 ] ) * ( polygonCoordinates[ i - 1 ][ 1 ] + polygonCoordinates[ j ][ 1 ] ) / 2;
712 }
713
714 return Math.abs( sum );
715 }
716 },
717
718 pointAlong: function( geom, percentage, _ignoreGeo /* Internal Use Only */ ) {
719 var totalLength = 0,
720 previousPercentageSum = 0,
721 percentageSum = 0,
722 remainderPercentageSum,
723 len,
724 lineStringCoordinates,
725 segmentLengths = [],
726 i = 1, dx, dy,
727 c, c0, c1,
728 wasGeodetic = false;
729
730 switch ( geom.type ) {
731 case "Point":
732 return $.extend( { }, geom );
733
734 case "LineString":
735 lineStringCoordinates = geom.coordinates;
736 break;
737
738 case "Polygon":
739 lineStringCoordinates = geom.coordinates[ 0 ];
740 break;
741 }
742
743 if ( lineStringCoordinates ) {
744 if ( percentage === 0 ) {
745 return {
746 type: "Point",
747 coordinates: [ lineStringCoordinates[ 0 ][ 0 ], lineStringCoordinates[ 0 ][ 1 ] ]
748 };
749 } else if ( percentage === 1 ) {
750 i = lineStringCoordinates.length - 1;
751 return {
752 type: "Point",
753 coordinates: [ lineStringCoordinates[ i ][ 0 ], lineStringCoordinates[ i ][ 1 ] ]
754 };
755 } else {
756 if ( !_ignoreGeo && $.geo.proj && this._isGeodetic( lineStringCoordinates ) ) {
757 wasGeodetic = true;
758 lineStringCoordinates = $.geo.proj.fromGeodetic( lineStringCoordinates );
759 }
760
761 for ( ; i < lineStringCoordinates.length; i++ ) {
762 dx = lineStringCoordinates[ i ][ 0 ] - lineStringCoordinates[ i - 1 ][ 0 ];
763 dy = lineStringCoordinates[ i ][ 1 ] - lineStringCoordinates[ i - 1 ][ 1 ];
764 len = Math.sqrt((dx * dx) + (dy * dy));
765 segmentLengths.push( len );
766 totalLength += len;
767 }
768
769 for ( i = 0; i < segmentLengths.length && percentageSum < percentage; i++ ) {
770 previousPercentageSum = percentageSum;
771 percentageSum += ( segmentLengths[ i ] / totalLength );
772 }
773
774 remainderPercentageSum = percentage - previousPercentageSum;
775
776 c0 = lineStringCoordinates[ i - 1 ];
777 c1 = lineStringCoordinates[ i ];
778
779 c = [
780 c0[ 0 ] + ( remainderPercentageSum * ( c1[ 0 ] - c0[ 0 ] ) ),
781 c0[ 1 ] + ( remainderPercentageSum * ( c1[ 1 ] - c0[ 1 ] ) )
782 ];
783
784 return {
785 type: "Point",
786 coordinates: wasGeodetic ? $.geo.proj.toGeodetic(c) : c
787 };
788 }
789 }
790 },
791
792 //
793 // WKT functions
794 //
795
796 WKT: (function () {
797 function pointToString(value) {
798 return "POINT " + pointToUntaggedString(value.coordinates);
799 }
800
801 function pointToUntaggedString(coordinates) {
802 if (!(coordinates && coordinates.length)) {
803 return "EMPTY";
804 } else {
805 return "(" + coordinates.join(" ") + ")";
806 }
807 }
808
809 function lineStringToString(value) {
810 return "LINESTRING " + lineStringToUntaggedString(value.coordinates);
811 }
812
813 function lineStringToUntaggedString(coordinates) {
814 if (!(coordinates && coordinates.length)) {
815 return "EMPTY";
816 } else {
817 var points = [];
818
819 for (var i = 0; i < coordinates.length; i++) {
820 points.push(coordinates[i].join(" "));
821 }
822
823 return "(" + points + ")";
824 }
825 }
826
827 function polygonToString(value) {
828 return "POLYGON " + polygonToUntaggedString(value.coordinates);
829 }
830
831 function polygonToUntaggedString(coordinates) {
832 if (!(coordinates && coordinates.length)) {
833 return "EMTPY";
834 } else {
835 var lineStrings = [];
836
837 for (var i = 0; i < coordinates.length; i++) {
838 lineStrings.push(lineStringToUntaggedString(coordinates[i]));
839 }
840
841 return "(" + lineStrings + ")";
842 }
843 }
844
845 function multiPointToString(value) {
846 return "MULTIPOINT " + lineStringToUntaggedString(value.coordinates);
847 }
848
849 function multiLineStringToString(value) {
850 return "MULTILINSTRING " + polygonToUntaggedString(value.coordinates);
851 }
852
853 function multiPolygonToString(value) {
854 return "MULTIPOLYGON " + multiPolygonToUntaggedString(value.coordinates);
855 }
856
857 function multiPolygonToUntaggedString(coordinates) {
858 if (!(coordinates && coordinates.length)) {
859 return "EMPTY";
860 } else {
861 var polygons = [];
862 for (var i = 0; i < coordinates.length; i++) {
863 polygons.push(polygonToUntaggedString(coordinates[i]));
864 }
865 return "(" + polygons + ")";
866 }
867 }
868
869 function geometryCollectionToString(value) {
870 return "GEOMETRYCOLLECTION " + geometryCollectionToUntaggedString(value.geometries);
871 }
872
873 function geometryCollectionToUntaggedString(geometries) {
874 if (!(geometries && geometries.length)) {
875 return "EMPTY";
876 } else {
877 var geometryText = [];
878 for (var i = 0; i < geometries.length; i++) {
879 geometryText.push(stringify(geometries[i]));
880 }
881 return "(" + geometries + ")";
882 }
883 }
884
885 function stringify(value) {
886 if (!(value && value.type)) {
887 return "";
888 } else {
889 switch (value.type) {
890 case "Point":
891 return pointToString(value);
892
893 case "LineString":
894 return lineStringToString(value);
895
896 case "Polygon":
897 return polygonToString(value);
898
899 case "MultiPoint":
900 return multiPointToString(value);
901
902 case "MultiLineString":
903 return multiLineStringToString(value);
904
905 case "MultiPolygon":
906 return multiPolygonToString(value);
907
908 case "GeometryCollection":
909 return geometryCollectionToString(value);
910
911 default:
912 return "";
913 }
914 }
915 }
916
917 function pointParseUntagged(wkt) {
918 var pointString = wkt.match( /\(\s*([\d\.\-]+)\s+([\d\.\-]+)\s*\)/ );
919 return pointString && pointString.length > 2 ? {
920 type: "Point",
921 coordinates: [
922 parseFloat(pointString[1]),
923 parseFloat(pointString[2])
924 ]
925 } : null;
926 }
927
928 function lineStringParseUntagged(wkt) {
929 var lineString = wkt.match( /\s*\((.*)\)/ ),
930 coords = [],
931 pointStrings,
932 pointParts,
933 i = 0;
934
935 if ( lineString && lineString.length > 1 ) {
936 pointStrings = lineString[ 1 ].match( /[\d\.\-]+\s+[\d\.\-]+/g );
937
938 for ( ; i < pointStrings.length; i++ ) {
939 pointParts = pointStrings[ i ].match( /\s*([\d\.\-]+)\s+([\d\.\-]+)\s*/ );
940 coords[ i ] = [ parseFloat( pointParts[ 1 ] ), parseFloat( pointParts[ 2 ] ) ];
941 }
942
943 return {
944 type: "LineString",
945 coordinates: coords
946 };
947 } else {
948 return null;
949 }
950 }
951
952 function polygonParseUntagged(wkt) {
953 var polygon = wkt.match( /\s*\(\s*\((.*)\)\s*\)/ ),
954 coords = [],
955 pointStrings,
956 pointParts,
957 i = 0;
958
959 if ( polygon && polygon.length > 1 ) {
960 pointStrings = polygon[ 1 ].match( /[\d\.\-]+\s+[\d\.\-]+/g );
961
962 for ( ; i < pointStrings.length; i++ ) {
963 pointParts = pointStrings[ i ].match( /\s*([\d\.\-]+)\s+([\d\.\-]+)\s*/ );
964 coords[ i ] = [ parseFloat( pointParts[ 1 ] ), parseFloat( pointParts[ 2 ] ) ];
965 }
966
967 return {
968 type: "Polygon",
969 coordinates: [ coords ]
970 };
971 } else {
972 return null;
973 }
974 }
975
976 function multiPointParseUntagged(wkt) {
977 var multiSomething;
978
979 if ( wkt.indexOf( "((" ) === -1 ) {
980 multiSomething = lineStringParseUntagged( wkt );
981 } else {
982 multiSomething = multiLineStringParseUntagged( wkt );
983 multiSomething.coordinates = $.geo._allCoordinates( multiSomething );
984 }
985
986 multiSomething.type = "MultiPoint";
987
988 return multiSomething;
989 }
990
991 function multiLineStringParseUntagged(wkt) {
992 var lineStringsWkt = wkt.substr( 2, wkt.length - 4 ),
993 lineString,
994 lineStrings = lineStringsWkt.split( /\),\s*\(/ ),
995 i = 0,
996 multiLineString = {
997 type: "MultiLineString",
998 coordinates: [ ]
999 };
1000
1001 for ( ; i < lineStrings.length; i++ ) {
1002 lineString = lineStringParseUntagged( "(" + lineStrings[ i ] + ")" );
1003 if ( lineString ) {
1004 multiLineString.coordinates.push( lineString.coordinates );
1005 }
1006 }
1007
1008 return multiLineString;
1009 }
1010
1011 function multiPolygonParseUntagged(wkt) {
1012 var polygonsWkt = wkt.substr( 1, wkt.length - 2 ),
1013 polygon,
1014 polygons = polygonsWkt.split( /\),\s*\(/ ),
1015 i = 0,
1016 multiPolygon = {
1017 type: "MultiPolygon",
1018 coordinates: [ ]
1019 };
1020
1021 for ( ; i < polygons.length; i++ ) {
1022 polygon = polygonParseUntagged( "(" + polygons[ i ] + ")" );
1023 if ( polygon ) {
1024 multiPolygon.coordinates.push( polygon.coordinates );
1025 }
1026 }
1027
1028 return multiPolygon;
1029 }
1030
1031 function geometryCollectionParseUntagged( wkt ) {
1032 var geometriesWkt = wkt.substr( 1, wkt.length - 2 ),
1033 geometries = geometriesWkt.match( /\),[a-zA-Z]/g ),
1034 geometryCollection = {
1035 type: "GeometryCollection",
1036 geometries: [ ]
1037 },
1038 curGeom,
1039 i = 0, curStart = 0, curLen;
1040
1041 if ( geometries && geometries.length > 0 ) {
1042 for ( ; i < geometries.length; i++ ) {
1043 curLen = geometriesWkt.indexOf( geometries[ i ], curStart ) - curStart + 1;
1044 curGeom = parse( geometriesWkt.substr( curStart, curLen ) );
1045 if ( curGeom ) {
1046 geometryCollection.geometries.push( curGeom );
1047 }
1048 curStart += curLen + 1;
1049 }
1050
1051 // one more
1052 curGeom = parse( geometriesWkt.substr( curStart ) );
1053 if ( curGeom ) {
1054 geometryCollection.geometries.push( curGeom );
1055 }
1056
1057 return geometryCollection;
1058 } else {
1059 return null;
1060 }
1061 }
1062
1063 function parse(wkt) {
1064 wkt = $.trim(wkt);
1065
1066 var typeIndex = wkt.indexOf( "(" ),
1067 untagged = wkt.substr( typeIndex );
1068
1069 switch ($.trim(wkt.substr(0, typeIndex)).toUpperCase()) {
1070 case "POINT":
1071 return pointParseUntagged( untagged );
1072
1073 case "LINESTRING":
1074 return lineStringParseUntagged( untagged );
1075
1076 case "POLYGON":
1077 return polygonParseUntagged( untagged );
1078
1079 case "MULTIPOINT":
1080 return multiPointParseUntagged( untagged );
1081
1082 case "MULTILINESTRING":
1083 return multiLineStringParseUntagged( untagged );
1084
1085 case "MULTIPOLYGON":
1086 return multiPolygonParseUntagged( untagged );
1087
1088 case "GEOMETRYCOLLECTION":
1089 return geometryCollectionParseUntagged( untagged );
1090
1091 default:
1092 return null;
1093 }
1094 }
1095
1096 return {
1097 stringify: stringify,
1098
1099 parse: parse
1100 };
1101 }()),
1102
1103 //
1104 // projection functions
1105 //
1106
1107 proj: (function () {
1108 var halfPi = 1.5707963267948966192,
1109 quarterPi = 0.7853981633974483096,
1110 radiansPerDegree = 0.0174532925199432958,
1111 degreesPerRadian = 57.295779513082320877,
1112 semiMajorAxis = 6378137;
1113
1114 return {
1115 fromGeodeticPos: function (coordinate) {
1116 return [
1117 semiMajorAxis * coordinate[ 0 ] * radiansPerDegree,
1118 semiMajorAxis * Math.log(Math.tan(quarterPi + coordinate[ 1 ] * radiansPerDegree / 2))
1119 ];
1120 },
1121
1122 fromGeodetic: function ( coordinates ) {
1123 if ( ! $.geo._isGeodetic( coordinates ) ) {
1124 return coordinates;
1125 }
1126
1127 var isMultiPointOrLineString = $.isArray(coordinates[ 0 ]),
1128 fromGeodeticPos = this.fromGeodeticPos;
1129
1130 if (!isMultiPointOrLineString && coordinates.length === 4) {
1131 // bbox
1132 var min = fromGeodeticPos([ coordinates[ 0 ], coordinates[ 1 ] ]),
1133 max = fromGeodeticPos([ coordinates[ 2 ], coordinates[ 3 ] ]);
1134 return [ min[ 0 ], min[ 1 ], max[ 0 ], max[ 1 ] ];
1135 } else {
1136 // geometry
1137 var isMultiLineStringOrPolygon = isMultiPointOrLineString && $.isArray(coordinates[ 0 ][ 0 ]),
1138 isMultiPolygon = isMultiLineStringOrPolygon && $.isArray(coordinates[ 0 ][ 0 ][ 0 ]),
1139 result = [ ],
1140 i, j, k;
1141
1142 if (!isMultiPolygon) {
1143 if (!isMultiLineStringOrPolygon) {
1144 if (!isMultiPointOrLineString) {
1145 coordinates = [ coordinates ];
1146 }
1147 coordinates = [ coordinates ];
1148 }
1149 coordinates = [ coordinates ];
1150 }
1151
1152 for ( i = 0; i < coordinates.length; i++ ) {
1153 result[ i ] = [ ];
1154 for ( j = 0; j < coordinates[ i ].length; j++ ) {
1155 result[ i ][ j ] = [ ];
1156 for ( k = 0; k < coordinates[ i ][ j ].length; k++ ) {
1157 result[ i ][ j ][ k ] = fromGeodeticPos(coordinates[ i ][ j ][ k ]);
1158 }
1159 }
1160 }
1161
1162 return isMultiPolygon ? result : isMultiLineStringOrPolygon ? result[ 0 ] : isMultiPointOrLineString ? result[ 0 ][ 0 ] : result[ 0 ][ 0 ][ 0 ];
1163 }
1164 },
1165
1166 toGeodeticPos: function (coordinate) {
1167 return [
1168 (coordinate[ 0 ] / semiMajorAxis) * degreesPerRadian,
1169 (halfPi - 2 * Math.atan(1 / Math.exp(coordinate[ 1 ] / semiMajorAxis))) * degreesPerRadian
1170 ];
1171 },
1172
1173 toGeodetic: function (coordinates) {
1174 if ( $.geo._isGeodetic( coordinates ) ) {
1175 return coordinates;
1176 }
1177
1178 var isMultiPointOrLineString = $.isArray(coordinates[ 0 ]),
1179 toGeodeticPos = this.toGeodeticPos;
1180
1181 if (!isMultiPointOrLineString && coordinates.length === 4) {
1182 // bbox
1183 var min = toGeodeticPos([ coordinates[ 0 ], coordinates[ 1 ] ]),
1184 max = toGeodeticPos([ coordinates[ 2 ], coordinates[ 3 ] ]);
1185 return [ min[ 0 ], min[ 1 ], max[ 0 ], max[ 1 ] ];
1186 } else {
1187 // geometry
1188 var isMultiLineStringOrPolygon = isMultiPointOrLineString && $.isArray(coordinates[ 0 ][ 0 ]),
1189 isMultiPolygon = isMultiLineStringOrPolygon && $.isArray(coordinates[ 0 ][ 0 ][ 0 ]),
1190 result = [ ],
1191 i, j, k;
1192
1193 if (!isMultiPolygon) {
1194 if (!isMultiLineStringOrPolygon) {
1195 if (!isMultiPointOrLineString) {
1196 coordinates = [ coordinates ];
1197 }
1198 coordinates = [ coordinates ];
1199 }
1200 coordinates = [ coordinates ];
1201 }
1202
1203 for ( i = 0; i < coordinates.length; i++ ) {
1204 result[ i ] = [ ];
1205 for ( j = 0; j < coordinates[ i ].length; j++ ) {
1206 result[ i ][ j ] = [ ];
1207 for ( k = 0; k < coordinates[ i ][ j ].length; k++ ) {
1208 result[ i ][ j ][ k ] = toGeodeticPos(coordinates[ i ][ j ][ k ]);
1209 }
1210 }
1211 }
1212
1213 return isMultiPolygon ? result : isMultiLineStringOrPolygon ? result[ 0 ] : isMultiPointOrLineString ? result[ 0 ][ 0 ] : result[ 0 ][ 0 ][ 0 ];
1214 }
1215 }
1216 };
1217 }()),
1218
1219 //
1220 // service types (defined in other files)
1221 //
1222
1223 _serviceTypes: {}
1224 };
1225 }(jQuery, this));