-
Notifications
You must be signed in to change notification settings - Fork 5
/
lunar_assembler_helpful_functions_for_map_styles_apply_patterns_to_areas.js
158 lines (147 loc) · 6.06 KB
/
lunar_assembler_helpful_functions_for_map_styles_apply_patterns_to_areas.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
/*
lunar_assembler - tool for generating SVG files from OpenStreetMap data. Available as a website.
Copyright (C) 2021 Mateusz Konieczny
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, under version 3 of the
License only.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
function intersectGeometryWithHorizontalStripes(feature, stripeSizeInDegrees, distanceBetweenStripesInDegrees) {
bbox = turf.bbox(feature);
var minLongitude = bbox[0];
var minLatitude = bbox[1];
var maxLongitude = bbox[2];
var maxLatitude = bbox[3];
if (!isMultipolygonAsExpected(feature)) {
return null;
}
var collected = [];
// gathering horizontal stripes
var minLatitudeForStripe = minLatitude;
while (minLatitudeForStripe < maxLatitude) {
var maxLatitudeForStripe = minLatitudeForStripe + stripeSizeInDegrees;
var stripeRing = [
[minLongitude, minLatitudeForStripe],
[maxLongitude, minLatitudeForStripe],
[maxLongitude, maxLatitudeForStripe],
[minLongitude, maxLatitudeForStripe],
[minLongitude, minLatitudeForStripe],
];
var stripe = [stripeRing];
var intersectedStripe = polygonClipping.intersection(feature.geometry.coordinates, stripe);
if (intersectedStripe != []) {
collected.push(intersectedStripe);
}
minLatitudeForStripe += stripeSizeInDegrees + distanceBetweenStripesInDegrees;
}
if (collected.length == 1) {
console.warn("one element! Is spread working as expected? See #68"); // TODO - trigger and debug it
}
var generated = polygonClipping.union(...collected);
var cloned = JSON.parse(JSON.stringify(feature));
cloned.geometry.coordinates = generated;
return cloned;
}
function intersectGeometryWithVerticalStripes(feature, stripeSizeInDegrees, distanceBetweenStripesInDegrees) {
bbox = turf.bbox(feature);
var minLongitude = bbox[0];
var minLatitude = bbox[1];
var maxLongitude = bbox[2];
var maxLatitude = bbox[3];
if (!isMultipolygonAsExpected(feature)) {
return null;
}
var collected = [];
// gathering horizontal stripes
var minLongitudeForStripe = minLongitude;
while (minLongitudeForStripe < maxLongitude) {
var maxLongitudeForStripe = minLongitudeForStripe + stripeSizeInDegrees;
var stripeRing = [
[minLongitudeForStripe, minLatitude],
[maxLongitudeForStripe, minLatitude],
[maxLongitudeForStripe, maxLatitude],
[minLongitudeForStripe, maxLatitude],
[minLongitudeForStripe, minLatitude],
];
var stripe = [stripeRing];
var intersectedStripe = polygonClipping.intersection(feature.geometry.coordinates, stripe);
if (intersectedStripe != []) {
collected.push(intersectedStripe);
}
minLongitudeForStripe += stripeSizeInDegrees + distanceBetweenStripesInDegrees;
}
if (collected.length == 1) {
console.warn("one element! Is spread working as expected? See #68"); // TODO - trigger and debug it
}
var generated = polygonClipping.union(...collected);
var cloned = JSON.parse(JSON.stringify(feature));
cloned.geometry.coordinates = generated;
return cloned;
}
function intersectGeometryWithPlaneHavingRectangularHoles(feature, holeVerticalInDegrees, holeHorizontalInDegrees, spaceVerticalInDegrees, spaceHorizontalInDegrees) {
bbox = turf.bbox(feature);
var minLongitude = bbox[0];
var minLatitude = bbox[1];
var maxLongitude = bbox[2];
var maxLatitude = bbox[3];
if (!isMultipolygonAsExpected(feature)) {
return null;
}
var collected = [];
// gathering horizontal stripes
var minLatitudeForStripe = minLatitude;
while (minLatitudeForStripe < maxLatitude) {
var maxLatitudeForStripe = minLatitudeForStripe + spaceVerticalInDegrees;
var stripeRing = [
[minLongitude, minLatitudeForStripe],
[maxLongitude, minLatitudeForStripe],
[maxLongitude, maxLatitudeForStripe],
[minLongitude, maxLatitudeForStripe],
[minLongitude, minLatitudeForStripe],
];
var stripe = [stripeRing];
var intersectedStripe = polygonClipping.intersection(feature.geometry.coordinates, stripe);
if (intersectedStripe.length > 0) {
collected.push(intersectedStripe);
}
minLatitudeForStripe += spaceVerticalInDegrees + holeVerticalInDegrees;
}
if (collected.length == 1) {
console.warn("one element! Is spread working as expected? See #68"); // TODO - trigger and debug it
}
// split in pairs due to https://github.com/mfogel/polygon-clipping/issues/118
var generatedHorizontal = polygonClipping.union(...collected);
collected = [];
// gathering vertical stripes
var minLongitudeForStripe = minLongitude;
while (minLongitudeForStripe < maxLongitude) {
var maxLongitudeForStripe = minLongitudeForStripe + spaceHorizontalInDegrees;
var stripeRing = [
[minLongitudeForStripe, minLatitude],
[maxLongitudeForStripe, minLatitude],
[maxLongitudeForStripe, maxLatitude],
[minLongitudeForStripe, maxLatitude],
[minLongitudeForStripe, minLatitude],
];
var stripe = [stripeRing];
var intersectedStripe = polygonClipping.intersection(feature.geometry.coordinates, stripe);
if (intersectedStripe.length > 0) {
collected.push(intersectedStripe);
}
minLongitudeForStripe += spaceHorizontalInDegrees + holeHorizontalInDegrees;
}
if (collected.length == 1) {
console.warn("one element! Is spread working as expected? See #68"); // TODO - trigger and debug it
}
var generatedVertical = polygonClipping.union(...collected);
var generated = polygonClipping.union(generatedHorizontal, generatedVertical);
var cloned = JSON.parse(JSON.stringify(feature));
cloned.geometry.coordinates = generated;
return cloned;
}