geogrid 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,159 +0,0 @@
1
- /**
2
- * The code in this file rewinds GeoJSON polygons and multipolygons to fit d3's expectations.
3
- * This is adapted by Matthieu Viry from a notebook of Philippe Rivière: https://observablehq.com/@fil/rewind
4
- * which is licensed under the ISC license.
5
- */
6
-
7
- import d3 from './d3-custom';
8
- import { GeoJSONFeature, GeoJSONFeatureCollection } from '../global';
9
-
10
- const {
11
- geoTransform,
12
- geoStream,
13
- geoContains,
14
- geoArea,
15
- } = d3;
16
-
17
- function projectPolygons(o, stream) {
18
- let coordinates = [];
19
- let polygon; let
20
- line;
21
- geoStream(
22
- o,
23
- stream({
24
- polygonStart() {
25
- coordinates.push((polygon = []));
26
- },
27
- polygonEnd() {},
28
- lineStart() {
29
- polygon.push((line = []));
30
- },
31
- lineEnd() {
32
- line.push(line[0].slice());
33
- },
34
- point(x, y) {
35
- line.push([x, y]);
36
- },
37
- }),
38
- );
39
- if (o.type === 'Polygon') {
40
- // eslint-disable-next-line prefer-destructuring
41
- coordinates = coordinates[0];
42
- }
43
- return { ...o, coordinates, rewind: true };
44
- }
45
-
46
- function projectGeometry(o, stream) {
47
- // eslint-disable-next-line no-nested-ternary
48
- return !o
49
- ? null
50
- : o.type === 'GeometryCollection' // eslint-disable-line no-nested-ternary
51
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
52
- ? projectGeometryCollection(o, stream)
53
- : o.type === 'Polygon' || o.type === 'MultiPolygon'
54
- ? projectPolygons(o, stream)
55
- : o;
56
- }
57
-
58
- function projectFeature(o, stream) {
59
- return { ...o, geometry: projectGeometry(o.geometry, stream) };
60
- }
61
-
62
- function projectFeatureCollection(o, stream) {
63
- return { ...o, features: o.features.map((f) => projectFeature(f, stream)) };
64
- }
65
-
66
- function projectGeometryCollection(obj, stream) {
67
- return {
68
- ...obj,
69
- geometries: obj.geometries.map((o) => projectGeometry(o, stream)),
70
- };
71
- }
72
-
73
- const geoProjectSimple = function (object, projection) {
74
- const { stream } = projection;
75
- let project;
76
- if (!stream) throw new Error('invalid projection');
77
- switch (object && object.type) {
78
- case 'Feature':
79
- project = projectFeature;
80
- break;
81
- case 'FeatureCollection':
82
- project = projectFeatureCollection;
83
- break;
84
- default:
85
- project = projectGeometry;
86
- break;
87
- }
88
- return project(object, stream);
89
- };
90
-
91
- function geoRewindStream(simple = true) {
92
- let ring;
93
- let polygon;
94
- return geoTransform({
95
- polygonStart() {
96
- this.stream.polygonStart();
97
- polygon = [];
98
- },
99
- lineStart() {
100
- if (polygon) polygon.push((ring = []));
101
- else this.stream.lineStart();
102
- },
103
- lineEnd() {
104
- if (!polygon) this.stream.lineEnd();
105
- },
106
- point(x, y) {
107
- if (polygon) ring.push([x, y]);
108
- else this.stream.point(x, y);
109
- },
110
- polygonEnd() {
111
- // eslint-disable-next-line no-restricted-syntax
112
- for (const [i, rring] of polygon.entries()) {
113
- rring.push(rring[0].slice());
114
- if (
115
- i // eslint-disable-line no-nested-ternary
116
- // a hole must contain the first point of the polygon
117
- ? !geoContains(
118
- { type: 'Polygon', coordinates: [rring] },
119
- polygon[0][0],
120
- )
121
- : polygon[1]
122
- // the outer ring must contain the first point of its first hole (if any)
123
- ? !geoContains(
124
- { type: 'Polygon', coordinates: [rring] },
125
- polygon[1][0],
126
- // eslint-disable-next-line @typescript-eslint/no-loop-func
127
- ) && !rring.some((p) => p[0] === polygon[1][0][0] && p[1] === polygon[1][0][1])
128
- // a single ring polygon must be smaller than a hemisphere (optional)
129
- : simple && geoArea({ type: 'Polygon', coordinates: [rring] }) > 2 * Math.PI
130
- ) {
131
- rring.reverse();
132
- }
133
-
134
- this.stream.lineStart();
135
- rring.pop();
136
- // eslint-disable-next-line no-restricted-syntax
137
- for (const [x, y] of rring) this.stream.point(x, y);
138
- this.stream.lineEnd();
139
- }
140
- this.stream.polygonEnd();
141
- polygon = null;
142
- },
143
- });
144
- }
145
-
146
- const rewindFeature = (
147
- feature: GeoJSONFeature,
148
- simple: boolean,
149
- ) => geoProjectSimple(feature, geoRewindStream(simple));
150
-
151
- const rewindLayer = (
152
- layer: GeoJSONFeatureCollection,
153
- simple: boolean = true,
154
- ): GeoJSONFeatureCollection => {
155
- const features = layer.features.map((feature) => rewindFeature(feature, simple));
156
- return { ...layer, features };
157
- };
158
-
159
- export default rewindLayer;