This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
lib.js
2288 lines (1966 loc) · 69.3 KB
/
lib.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
topojson = require("topojson");
},{"topojson":2}],2:[function(require,module,exports){
var topojson = module.exports = require("./topojson");
topojson.topology = require("./lib/topojson/topology");
topojson.simplify = require("./lib/topojson/simplify");
topojson.clockwise = require("./lib/topojson/clockwise");
topojson.filter = require("./lib/topojson/filter");
topojson.prune = require("./lib/topojson/prune");
topojson.bind = require("./lib/topojson/bind");
},{"./lib/topojson/bind":3,"./lib/topojson/clockwise":6,"./lib/topojson/filter":10,"./lib/topojson/prune":13,"./lib/topojson/simplify":15,"./lib/topojson/topology":18,"./topojson":29}],3:[function(require,module,exports){
var type = require("./type"),
topojson = require("../../");
module.exports = function(topology, propertiesById) {
var bind = type({
geometry: function(geometry) {
var properties0 = geometry.properties,
properties1 = propertiesById[geometry.id];
if (properties1) {
if (properties0) for (var k in properties1) properties0[k] = properties1[k];
else for (var k in properties1) { geometry.properties = properties1; break; }
}
this.defaults.geometry.call(this, geometry);
},
LineString: noop,
MultiLineString: noop,
Point: noop,
MultiPoint: noop,
Polygon: noop,
MultiPolygon: noop
});
for (var key in topology.objects) {
bind.object(topology.objects[key]);
}
};
function noop() {}
},{"../../":2,"./type":28}],4:[function(require,module,exports){
// Computes the bounding box of the specified hash of GeoJSON objects.
module.exports = function(objects) {
var x0 = Infinity,
y0 = Infinity,
x1 = -Infinity,
y1 = -Infinity;
function boundGeometry(geometry) {
if (geometry && boundGeometryType.hasOwnProperty(geometry.type)) boundGeometryType[geometry.type](geometry);
}
var boundGeometryType = {
GeometryCollection: function(o) { o.geometries.forEach(boundGeometry); },
Point: function(o) { boundPoint(o.coordinates); },
MultiPoint: function(o) { o.coordinates.forEach(boundPoint); },
LineString: function(o) { boundLine(o.coordinates); },
MultiLineString: function(o) { o.coordinates.forEach(boundLine); },
Polygon: function(o) { o.coordinates.forEach(boundLine); },
MultiPolygon: function(o) { o.coordinates.forEach(boundMultiLine); }
};
function boundPoint(coordinates) {
var x = coordinates[0],
y = coordinates[1];
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
function boundLine(coordinates) {
coordinates.forEach(boundPoint);
}
function boundMultiLine(coordinates) {
coordinates.forEach(boundLine);
}
for (var key in objects) {
boundGeometry(objects[key]);
}
return [x0, y0, x1, y1];
};
},{}],5:[function(require,module,exports){
exports.name = "cartesian";
exports.formatDistance = formatDistance;
exports.ringArea = ringArea;
exports.absoluteArea = Math.abs;
exports.triangleArea = triangleArea;
exports.distance = distance;
function formatDistance(d) {
return d.toString();
}
function ringArea(ring) {
var i = 0,
n = ring.length,
area = ring[n - 1][1] * ring[0][0] - ring[n - 1][0] * ring[0][1];
while (++i < n) {
area += ring[i - 1][1] * ring[i][0] - ring[i - 1][0] * ring[i][1];
}
return -area * .5; // ensure clockwise pixel areas are positive
}
function triangleArea(triangle) {
return Math.abs(
(triangle[0][0] - triangle[2][0]) * (triangle[1][1] - triangle[0][1])
- (triangle[0][0] - triangle[1][0]) * (triangle[2][1] - triangle[0][1])
);
}
function distance(x0, y0, x1, y1) {
var dx = x0 - x1, dy = y0 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
},{}],6:[function(require,module,exports){
var type = require("./type"),
systems = require("./coordinate-systems"),
topojson = require("../../");
module.exports = function(object, options) {
if (object.type === "Topology") clockwiseTopology(object, options);
else clockwiseGeometry(object, options);
};
function clockwiseGeometry(object, options) {
var system = null;
if (options)
"coordinate-system" in options && (system = systems[options["coordinate-system"]]);
var clockwisePolygon = clockwisePolygonSystem(system.ringArea, reverse);
type({
LineString: noop,
MultiLineString: noop,
Point: noop,
MultiPoint: noop,
Polygon: function(polygon) { clockwisePolygon(polygon.coordinates); },
MultiPolygon: function(multiPolygon) { multiPolygon.coordinates.forEach(clockwisePolygon); }
}).object(object);
function reverse(array) { array.reverse(); }
}
function clockwiseTopology(topology, options) {
var system = null;
if (options)
"coordinate-system" in options && (system = systems[options["coordinate-system"]]);
var clockwisePolygon = clockwisePolygonSystem(ringArea, reverse);
var clockwise = type({
LineString: noop,
MultiLineString: noop,
Point: noop,
MultiPoint: noop,
Polygon: function(polygon) { clockwisePolygon(polygon.arcs); },
MultiPolygon: function(multiPolygon) { multiPolygon.arcs.forEach(clockwisePolygon); }
});
for (var key in topology.objects) {
clockwise.object(topology.objects[key]);
}
function ringArea(ring) {
return system.ringArea(topojson.feature(topology, {type: "Polygon", arcs: [ring]}).geometry.coordinates[0]);
}
// TODO It might be slightly more compact to reverse the arc.
function reverse(ring) {
var i = -1, n = ring.length;
ring.reverse();
while (++i < n) ring[i] = ~ring[i];
}
};
function clockwisePolygonSystem(ringArea, reverse) {
return function(rings) {
if (!(n = rings.length)) return;
var n,
areas = new Array(n),
max = -Infinity,
best,
area,
t;
// Find the largest absolute ring area; this should be the exterior ring.
for (var i = 0; i < n; ++i) {
var area = Math.abs(areas[i] = ringArea(rings[i]));
if (area > max) max = area, best = i;
}
// Ensure the largest ring appears first.
if (best) {
t = rings[best], rings[best] = rings[0], rings[0] = t;
t = areas[best], areas[best] = areas[0], areas[0] = t;
}
if (areas[0] < 0) reverse(rings[0]);
for (var i = 1; i < n; ++i) {
if (areas[i] > 0) reverse(rings[i]);
}
};
}
function noop() {}
},{"../../":2,"./coordinate-systems":8,"./type":28}],7:[function(require,module,exports){
// Given a hash of GeoJSON objects and an id function, invokes the id function
// to compute a new id for each object that is a feature. The function is passed
// the feature and is expected to return the new feature id, or null if the
// feature should not have an id.
module.exports = function(objects, id) {
if (arguments.length < 2) id = function(d) { return d.id; };
function idObject(object) {
if (object && idObjectType.hasOwnProperty(object.type)) idObjectType[object.type](object);
}
function idFeature(feature) {
var i = id(feature);
if (i == null) delete feature.id;
else feature.id = i;
}
var idObjectType = {
Feature: idFeature,
FeatureCollection: function(collection) { collection.features.forEach(idFeature); }
};
for (var key in objects) {
idObject(objects[key]);
}
return objects;
};
},{}],8:[function(require,module,exports){
module.exports = {
cartesian: require("./cartesian"),
spherical: require("./spherical")
};
},{"./cartesian":5,"./spherical":16}],9:[function(require,module,exports){
// Given a TopoJSON topology in absolute (quantized) coordinates,
// converts to fixed-point delta encoding.
// This is a destructive operation that modifies the given topology!
module.exports = function(topology) {
var arcs = topology.arcs,
i = -1,
n = arcs.length;
while (++i < n) {
var arc = arcs[i],
j = 0,
m = arc.length,
point = arc[0],
x0 = point[0],
y0 = point[1],
x1,
y1;
while (++j < m) {
point = arc[j];
x1 = point[0];
y1 = point[1];
arc[j] = [x1 - x0, y1 - y0];
x0 = x1;
y0 = y1;
}
}
return topology;
};
},{}],10:[function(require,module,exports){
var type = require("./type"),
prune = require("./prune"),
clockwise = require("./clockwise"),
systems = require("./coordinate-systems"),
topojson = require("../../");
module.exports = function(topology, options) {
var system = null,
forceClockwise = true, // force exterior rings to be clockwise?
minimumArea;
if (options)
"coordinate-system" in options && (system = systems[options["coordinate-system"]]),
"minimum-area" in options && (minimumArea = +options["minimum-area"]),
"force-clockwise" in options && (forceClockwise = !!options["force-clockwise"]);
if (forceClockwise) clockwise(topology, options); // deprecated; for backwards-compatibility
if (!(minimumArea > 0)) minimumArea = Number.MIN_VALUE;
var filter = type({
LineString: noop, // TODO remove empty lines
MultiLineString: noop,
Point: noop,
MultiPoint: noop,
Polygon: function(polygon) {
polygon.arcs = polygon.arcs.filter(ringArea);
if (!polygon.arcs.length) {
polygon.type = null;
delete polygon.arcs;
}
},
MultiPolygon: function(multiPolygon) {
multiPolygon.arcs = multiPolygon.arcs.map(function(polygon) {
return polygon.filter(ringArea);
}).filter(function(polygon) {
return polygon.length;
});
if (!multiPolygon.arcs.length) {
multiPolygon.type = null;
delete multiPolygon.arcs;
}
},
GeometryCollection: function(collection) {
this.defaults.GeometryCollection.call(this, collection);
collection.geometries = collection.geometries.filter(function(geometry) { return geometry.type != null; });
if (!collection.geometries.length) {
collection.type = null;
delete collection.geometries;
}
}
});
for (var key in topology.objects) {
filter.object(topology.objects[key]);
}
prune(topology, options);
function ringArea(ring) {
var topopolygon = {type: "Polygon", arcs: [ring]},
geopolygon = topojson.feature(topology, topopolygon),
exterior = geopolygon.geometry.coordinates[0],
exteriorArea = system.absoluteArea(system.ringArea(exterior));
return exteriorArea >= minimumArea;
}
};
function noop() {}
},{"../../":2,"./clockwise":6,"./coordinate-systems":8,"./prune":13,"./type":28}],11:[function(require,module,exports){
// Given a hash of GeoJSON objects, replaces Features with geometry objects.
// This is a destructive operation that modifies the input objects!
module.exports = function(objects) {
function geomifyObject(object) {
return (object && geomifyObjectType.hasOwnProperty(object.type)
? geomifyObjectType[object.type]
: geomifyGeometry)(object);
}
function geomifyFeature(feature) {
var geometry = feature.geometry;
if (geometry == null) {
feature.type = null;
} else {
geomifyGeometry(geometry);
feature.type = geometry.type;
if (geometry.geometries) feature.geometries = geometry.geometries;
else if (geometry.coordinates) feature.coordinates = geometry.coordinates;
}
delete feature.geometry;
return feature;
}
function geomifyGeometry(geometry) {
if (!geometry) return {type: null};
if (geomifyGeometryType.hasOwnProperty(geometry.type)) geomifyGeometryType[geometry.type](geometry);
return geometry;
}
var geomifyObjectType = {
Feature: geomifyFeature,
FeatureCollection: function(collection) {
collection.type = "GeometryCollection";
collection.geometries = collection.features;
collection.features.forEach(geomifyFeature);
delete collection.features;
return collection;
}
};
var geomifyGeometryType = {
GeometryCollection: function(o) {
var geometries = o.geometries, i = -1, n = geometries.length;
while (++i < n) geometries[i] = geomifyGeometry(geometries[i]);
},
MultiPoint: function(o) {
if (!o.coordinates.length) {
o.type = null;
delete o.coordinates;
} else if (o.coordinates.length < 2) {
o.type = "Point";
o.coordinates = o.coordinates[0];
}
},
LineString: function(o) {
if (!o.coordinates.length) {
o.type = null;
delete o.coordinates;
}
},
MultiLineString: function(o) {
for (var lines = o.coordinates, i = 0, N = 0, n = lines.length; i < n; ++i) {
var line = lines[i];
if (line.length) lines[N++] = line;
}
if (!N) {
o.type = null;
delete o.coordinates;
} else if (N < 2) {
o.type = "LineString";
o.coordinates = lines[0];
} else {
o.coordinates.length = N;
}
},
Polygon: function(o) {
for (var rings = o.coordinates, i = 0, N = 0, n = rings.length; i < n; ++i) {
var ring = rings[i];
if (ring.length) rings[N++] = ring;
}
if (!N) {
o.type = null;
delete o.coordinates;
} else {
o.coordinates.length = N;
}
},
MultiPolygon: function(o) {
for (var polygons = o.coordinates, j = 0, M = 0, m = polygons.length; j < m; ++j) {
for (var rings = polygons[j], i = 0, N = 0, n = rings.length; i < n; ++i) {
var ring = rings[i];
if (ring.length) rings[N++] = ring;
}
if (N) {
rings.length = N;
polygons[M++] = rings;
}
}
if (!M) {
o.type = null;
delete o.coordinates;
} else if (M < 2) {
o.type = "Polygon";
o.coordinates = polygons[0];
} else {
polygons.length = M;
}
}
};
for (var key in objects) {
objects[key] = geomifyObject(objects[key]);
}
return objects;
};
},{}],12:[function(require,module,exports){
module.exports = function(objects, filter) {
function prefilterGeometry(geometry) {
if (!geometry) return {type: null};
if (prefilterGeometryType.hasOwnProperty(geometry.type)) prefilterGeometryType[geometry.type](geometry);
return geometry;
}
var prefilterGeometryType = {
GeometryCollection: function(o) {
var geometries = o.geometries, i = -1, n = geometries.length;
while (++i < n) geometries[i] = prefilterGeometry(geometries[i]);
},
Polygon: function(o) {
for (var rings = o.coordinates, i = 0, N = 0, n = rings.length; i < n; ++i) {
var ring = rings[i];
if (filter(ring)) rings[N++] = ring;
}
if (!N) {
o.type = null;
delete o.coordinates;
} else {
o.coordinates.length = N;
}
},
MultiPolygon: function(o) {
for (var polygons = o.coordinates, j = 0, M = 0, m = polygons.length; j < m; ++j) {
for (var rings = polygons[j], i = 0, N = 0, n = rings.length; i < n; ++i) {
var ring = rings[i];
if (filter(ring)) rings[N++] = ring;
}
if (N) {
rings.length = N;
polygons[M++] = rings;
}
}
if (!M) {
o.type = null;
delete o.coordinates;
} else if (M < 2) {
o.type = "Polygon";
o.coordinates = polygons[0];
} else {
polygons.length = M;
}
}
};
for (var key in objects) {
objects[key] = prefilterGeometry(objects[key]);
}
return objects;
};
},{}],13:[function(require,module,exports){
module.exports = function(topology, options) {
var verbose = false,
objects = topology.objects,
oldArcs = topology.arcs,
oldArcCount = oldArcs.length,
newArcs = topology.arcs = [],
newArcCount = 0,
newIndexByOldIndex = new Array(oldArcs.length);
if (options)
"verbose" in options && (verbose = !!options["verbose"]);
function pruneGeometry(geometry) {
if (geometry && pruneGeometryType.hasOwnProperty(geometry.type)) pruneGeometryType[geometry.type](geometry);
}
var pruneGeometryType = {
GeometryCollection: function(o) { o.geometries.forEach(pruneGeometry); },
LineString: function(o) { pruneArcs(o.arcs); },
MultiLineString: function(o) { o.arcs.forEach(pruneArcs); },
Polygon: function(o) { o.arcs.forEach(pruneArcs); },
MultiPolygon: function(o) { o.arcs.forEach(pruneMultiArcs); }
};
function pruneArcs(arcs) {
for (var i = 0, m = 0, n = arcs.length; i < n; ++i) {
var oldIndex = arcs[i],
oldReverse = oldIndex < 0 && (oldIndex = ~oldIndex, true),
oldArc = oldArcs[oldIndex],
newIndex;
// Skip collapsed arc segments.
if (oldArc.length < 3 && !oldArc[1][0] && !oldArc[1][1]) continue;
// If this is the first instance of this arc,
// record it under its new index.
if ((newIndex = newIndexByOldIndex[oldIndex]) == null) {
newIndexByOldIndex[oldIndex] = newIndex = newArcCount++;
newArcs[newIndex] = oldArcs[oldIndex];
}
arcs[m++] = oldReverse ? ~newIndex : newIndex;
}
// If all were collapsed, restore the last arc to avoid collapsing the line.
if (!(arcs.length = m) && n) {
// If this is the first instance of this arc,
// record it under its new index.
if ((newIndex = newIndexByOldIndex[oldIndex]) == null) {
newIndexByOldIndex[oldIndex] = newIndex = newArcCount++;
newArcs[newIndex] = oldArcs[oldIndex];
}
arcs[0] = oldReverse ? ~newIndex : newIndex;
}
}
function pruneMultiArcs(arcs) {
arcs.forEach(pruneArcs);
}
for (var key in objects) {
pruneGeometry(objects[key]);
}
if (verbose) console.warn("prune: retained " + newArcCount + " / " + oldArcCount + " arcs (" + Math.round(newArcCount / oldArcCount * 100) + "%)");
return topology;
};
function noop() {}
},{}],14:[function(require,module,exports){
module.exports = function(objects, bbox, Q) {
var x0 = isFinite(bbox[0]) ? bbox[0] : 0,
y0 = isFinite(bbox[1]) ? bbox[1] : 0,
x1 = isFinite(bbox[2]) ? bbox[2] : 0,
y1 = isFinite(bbox[3]) ? bbox[3] : 0,
kx = x1 - x0 ? (Q - 1) / (x1 - x0) : 1,
ky = y1 - y0 ? (Q - 1) / (y1 - y0) : 1;
function quantizeGeometry(geometry) {
if (geometry && quantizeGeometryType.hasOwnProperty(geometry.type)) quantizeGeometryType[geometry.type](geometry);
}
var quantizeGeometryType = {
GeometryCollection: function(o) { o.geometries.forEach(quantizeGeometry); },
Point: function(o) { quantizePoint(o.coordinates); },
MultiPoint: function(o) { o.coordinates.forEach(quantizePoint); },
LineString: function(o) {
var line = o.coordinates;
quantizeLine(line);
if (line.length < 2) line[1] = line[0]; // must have 2+
},
MultiLineString: function(o) {
for (var lines = o.coordinates, i = 0, n = lines.length; i < n; ++i) {
var line = lines[i];
quantizeLine(line);
if (line.length < 2) line[1] = line[0]; // must have 2+
}
},
Polygon: function(o) {
for (var rings = o.coordinates, i = 0, n = rings.length; i < n; ++i) {
var ring = rings[i];
quantizeLine(ring);
while (ring.length < 4) ring.push(ring[0]); // must have 4+
}
},
MultiPolygon: function(o) {
for (var polygons = o.coordinates, i = 0, n = polygons.length; i < n; ++i) {
for (var rings = polygons[i], j = 0, m = rings.length; j < m; ++j) {
var ring = rings[j];
quantizeLine(ring);
while (ring.length < 4) ring.push(ring[0]); // must have 4+
}
}
}
};
function quantizePoint(coordinates) {
coordinates[0] = Math.round((coordinates[0] - x0) * kx);
coordinates[1] = Math.round((coordinates[1] - y0) * ky);
}
function quantizeLine(coordinates) {
var i = 0,
j = 1,
n = coordinates.length,
pi = coordinates[0],
pj,
px = pi[0] = Math.round((pi[0] - x0) * kx),
py = pi[1] = Math.round((pi[1] - y0) * ky),
x,
y;
while (++i < n) {
pi = coordinates[i];
x = Math.round((pi[0] - x0) * kx);
y = Math.round((pi[1] - y0) * ky);
if (x !== px || y !== py) { // skip coincident points
pj = coordinates[j++];
pj[0] = px = x;
pj[1] = py = y;
}
}
coordinates.length = j;
}
for (var key in objects) {
quantizeGeometry(objects[key]);
}
return {
scale: [1 / kx, 1 / ky],
translate: [x0, y0]
};
};
},{}],15:[function(require,module,exports){
var topojson = require("../../"),
systems = require("./coordinate-systems");
module.exports = function(topology, options) {
var minimumArea = 0,
retainProportion,
verbose = false,
system = null,
N = topology.arcs.reduce(function(p, v) { return p + v.length; }, 0),
M = 0;
if (options)
"minimum-area" in options && (minimumArea = +options["minimum-area"]),
"coordinate-system" in options && (system = systems[options["coordinate-system"]]),
"retain-proportion" in options && (retainProportion = +options["retain-proportion"]),
"verbose" in options && (verbose = !!options["verbose"]);
topojson.presimplify(topology, system.triangleArea);
if (retainProportion) {
var areas = [];
topology.arcs.forEach(function(arc) {
arc.forEach(function(point) {
areas.push(point[2]);
});
});
options["minimum-area"] = minimumArea = N ? areas.sort(function(a, b) { return b - a; })[Math.ceil((N - 1) * retainProportion)] : 0;
if (verbose) console.warn("simplification: effective minimum area " + minimumArea.toPrecision(3));
}
topology.arcs.forEach(topology.transform ? function(arc) {
var dx = 0,
dy = 0, // accumulate removed points
i = -1,
j = -1,
n = arc.length,
source,
target;
while (++i < n) {
source = arc[i];
if (source[2] >= minimumArea) {
target = arc[++j];
target[0] = source[0] + dx;
target[1] = source[1] + dy;
dx = dy = 0;
} else {
dx += source[0];
dy += source[1];
}
}
arc.length = ++j;
} : function(arc) {
var i = -1,
j = -1,
n = arc.length,
point;
while (++i < n) {
point = arc[i];
if (point[2] >= minimumArea) {
arc[++j] = point;
}
}
arc.length = ++j;
});
// Remove computed area (z) for each point.
// This is done as a separate pass because some coordinates may be shared
// between arcs (such as the last point and first point of a cut line).
topology.arcs.forEach(function(arc) {
var i = -1, n = arc.length;
while (++i < n) arc[i].length = 2;
M += arc.length;
});
if (verbose) console.warn("simplification: retained " + M + " / " + N + " points (" + Math.round((M / N) * 100) + "%)");
return topology;
};
},{"../../":2,"./coordinate-systems":8}],16:[function(require,module,exports){
var π = Math.PI,
π_4 = π / 4,
radians = π / 180;
exports.name = "spherical";
exports.formatDistance = formatDistance;
exports.ringArea = ringArea;
exports.absoluteArea = absoluteArea;
exports.triangleArea = triangleArea;
exports.distance = haversinDistance; // XXX why two implementations?
function formatDistance(radians) {
var km = radians * 6371;
return (km > 1 ? km.toFixed(3) + "km" : (km * 1000).toPrecision(3) + "m")
+ " (" + (radians * 180 / Math.PI).toPrecision(3) + "°)";
}
function ringArea(ring) {
if (!ring.length) return 0;
var area = 0,
p = ring[0],
λ = p[0] * radians,
φ = p[1] * radians / 2 + π_4,
λ0 = λ,
cosφ0 = Math.cos(φ),
sinφ0 = Math.sin(φ);
for (var i = 1, n = ring.length; i < n; ++i) {
p = ring[i], λ = p[0] * radians, φ = p[1] * radians / 2 + π_4;
// Spherical excess E for a spherical triangle with vertices: south pole,
// previous point, current point. Uses a formula derived from Cagnoli’s
// theorem. See Todhunter, Spherical Trig. (1871), Sec. 103, Eq. (2).
var dλ = λ - λ0,
cosφ = Math.cos(φ),
sinφ = Math.sin(φ),
k = sinφ0 * sinφ,
u = cosφ0 * cosφ + k * Math.cos(dλ),
v = k * Math.sin(dλ);
area += Math.atan2(v, u);
// Advance the previous point.
λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ;
}
return 2 * (area > π ? area - 2 * π : area < -π ? area + 2 * π : area);
}
function absoluteArea(a) {
return a < 0 ? a + 4 * π : a;
}
function triangleArea(t) {
var a = distance(t[0], t[1]),
b = distance(t[1], t[2]),
c = distance(t[2], t[0]),
s = (a + b + c) / 2;
return 4 * Math.atan(Math.sqrt(Math.max(0, Math.tan(s / 2) * Math.tan((s - a) / 2) * Math.tan((s - b) / 2) * Math.tan((s - c) / 2))));
}
function distance(a, b) {
var Δλ = (b[0] - a[0]) * radians,
sinΔλ = Math.sin(Δλ),
cosΔλ = Math.cos(Δλ),
sinφ0 = Math.sin(a[1] * radians),
cosφ0 = Math.cos(a[1] * radians),
sinφ1 = Math.sin(b[1] * radians),
cosφ1 = Math.cos(b[1] * radians),
_;
return Math.atan2(Math.sqrt((_ = cosφ1 * sinΔλ) * _ + (_ = cosφ0 * sinφ1 - sinφ0 * cosφ1 * cosΔλ) * _), sinφ0 * sinφ1 + cosφ0 * cosφ1 * cosΔλ);
}
function haversinDistance(x0, y0, x1, y1) {
x0 *= radians, y0 *= radians, x1 *= radians, y1 *= radians;
return 2 * Math.asin(Math.sqrt(haversin(y1 - y0) + Math.cos(y0) * Math.cos(y1) * haversin(x1 - x0)));
}
function haversin(x) {
return (x = Math.sin(x / 2)) * x;
}
},{}],17:[function(require,module,exports){
var type = require("./type");
module.exports = function(objects, transform) {
var ε = 1e-2,
x0 = -180, x0e = x0 + ε,
x1 = 180, x1e = x1 - ε,
y0 = -90, y0e = y0 + ε,
y1 = 90, y1e = y1 - ε,
fragments = [];
if (transform) {
var kx = transform.scale[0],
ky = transform.scale[1],
dx = transform.translate[0],
dy = transform.translate[1];
x0 = Math.round((x0 - dx) / kx);
x1 = Math.round((x1 - dx) / kx);
y0 = Math.round((y0 - dy) / ky);
y1 = Math.round((y1 - dy) / ky);
x0e = Math.round((x0e - dx) / kx);
x1e = Math.round((x1e - dx) / kx);
y0e = Math.round((y0e - dy) / ky);
y1e = Math.round((y1e - dy) / ky);
}
function normalizePoint(y) {
return y <= y0e ? [0, y0] // south pole
: y >= y1e ? [0, y1] // north pole
: [x0, y]; // antimeridian
}
var stitch = type({
polygon: function(polygon) {
var rings = [];
// For each ring, detect where it crosses the antimeridian or pole.
for (var j = 0, m = polygon.length; j < m; ++j) {
var ring = polygon[j],
fragments = [];
// By default, assume that this ring doesn’t need any stitching.
fragments.push(ring);
for (var i = 0, n = ring.length; i < n; ++i) {
var point = ring[i],
x = point[0],
y = point[1];
// If this is an antimeridian or polar point…
if (x <= x0e || x >= x1e || y <= y0e || y >= y1e) {
// Advance through any antimeridian or polar points…
for (var k = i + 1; k < n; ++k) {
var pointk = ring[k],
xk = pointk[0],
yk = pointk[1];
if (xk > x0e && xk < x1e && yk > y0e && yk < y1e) break;
}
// If this was just a single antimeridian or polar point,
// we don’t need to cut this ring into a fragment;
// we can just leave it as-is.
if (k === i + 1) continue;
// Otherwise, if this is not the first point in the ring,
// cut the current fragment so that it ends at the current point.
// The current point is also normalized for later joining.
if (i) {
var fragmentBefore = ring.slice(0, i + 1);
fragmentBefore[fragmentBefore.length - 1] = normalizePoint(y);
fragments[fragments.length - 1] = fragmentBefore;
}
// If the ring started with an antimeridian fragment,
// we can ignore that fragment entirely.
else {
fragments.pop();
}
// If the remainder of the ring is an antimeridian fragment,
// move on to the next ring.
if (k >= n) break;
// Otherwise, add the remaining ring fragment and continue.
fragments.push(ring = ring.slice(k - 1));
ring[0] = normalizePoint(ring[0][1]);
i = -1;
n = ring.length;
}
}
// Now stitch the fragments back together into rings.
// To connect the fragments start-to-end, create a simple index by end.
var fragmentByStart = {},
fragmentByEnd = {};
// For each fragment…
for (var i = 0, n = fragments.length; i < n; ++i) {
var fragment = fragments[i],
start = fragment[0],
end = fragment[fragment.length - 1];
// If this fragment is closed, add it as a standalone ring.
if (start[0] === end[0] && start[1] === end[1]) {
rings.push(fragment);
fragments[i] = null;
continue;
}
fragment.index = i;
fragmentByStart[start] = fragmentByEnd[end] = fragment;
}
// For each open fragment…
for (var i = 0; i < n; ++i) {
var fragment = fragments[i];
if (fragment) {
var start = fragment[0],
end = fragment[fragment.length - 1],
startFragment = fragmentByEnd[start],
endFragment = fragmentByStart[end];
delete fragmentByStart[start];
delete fragmentByEnd[end];
// If this fragment is closed, add it as a standalone ring.
if (start[0] === end[0] && start[1] === end[1]) {
rings.push(fragment);
continue;
}
if (startFragment) {
delete fragmentByEnd[start];
delete fragmentByStart[startFragment[0]];
startFragment.pop(); // drop the shared coordinate
fragments[startFragment.index] = null;
fragment = startFragment.concat(fragment);
if (startFragment === endFragment) {
// Connect both ends to this single fragment to create a ring.
rings.push(fragment);
} else {
fragment.index = n++;