@mark1russell7/cartography 0.1.1 → 0.1.2
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.
- package/README.md +30 -1
- package/dist/index.d.ts +199 -83
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +307 -72
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -15,71 +15,6 @@ var MapError = class extends Error {
|
|
|
15
15
|
this.code = code;
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
-
// src/filter.ts
|
|
19
|
-
/** This function gives the kind of a geometry. */
|
|
20
|
-
function geometryKind(geometry) {
|
|
21
|
-
switch (geometry.type) {
|
|
22
|
-
case "Point":
|
|
23
|
-
case "MultiPoint": return "point";
|
|
24
|
-
case "LineString":
|
|
25
|
-
case "MultiLineString": return "line";
|
|
26
|
-
case "Polygon":
|
|
27
|
-
case "MultiPolygon": return "area";
|
|
28
|
-
case "GeometryCollection": {
|
|
29
|
-
const kinds = new Set(geometry.geometries.map(geometryKind));
|
|
30
|
-
return kinds.size === 1 ? [...kinds][0] : "mixed";
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/** This function tells if a feature matches a filter. A feature with no geometry matches no `{ geometry }` filter. */
|
|
35
|
-
function matchesFilter(filter, feature) {
|
|
36
|
-
if ("all" in filter) return filter.all.every((item) => matchesFilter(item, feature));
|
|
37
|
-
if ("any" in filter) return filter.any.some((item) => matchesFilter(item, feature));
|
|
38
|
-
if ("not" in filter) return !matchesFilter(filter.not, feature);
|
|
39
|
-
if ("geometry" in filter) return feature.geometry !== null && geometryKind(feature.geometry) === filter.geometry;
|
|
40
|
-
const properties = feature.properties ?? {};
|
|
41
|
-
const has = Object.hasOwn(properties, filter.property);
|
|
42
|
-
if ("exists" in filter) return has === filter.exists;
|
|
43
|
-
return has && filter.in.includes(properties[filter.property]);
|
|
44
|
-
}
|
|
45
|
-
const KINDS = [
|
|
46
|
-
"point",
|
|
47
|
-
"line",
|
|
48
|
-
"area",
|
|
49
|
-
"mixed"
|
|
50
|
-
];
|
|
51
|
-
/**
|
|
52
|
-
* This function refuses a value that is not a `Filter`, for example a filter from JSON.
|
|
53
|
-
*
|
|
54
|
-
* @throws MapError - The code is `filter`.
|
|
55
|
-
*/
|
|
56
|
-
function checkFilter(filter, path = "filter") {
|
|
57
|
-
const fail = (reason) => {
|
|
58
|
-
throw new MapError("filter", `${path}: ${reason}`);
|
|
59
|
-
};
|
|
60
|
-
if (typeof filter !== "object" || filter === null || Array.isArray(filter)) fail("a filter must be an object");
|
|
61
|
-
const value = filter;
|
|
62
|
-
const keys = Object.keys(value).toSorted().join(",");
|
|
63
|
-
if (keys === "all" || keys === "any") {
|
|
64
|
-
const items = value[keys];
|
|
65
|
-
if (!Array.isArray(items)) fail(`the member ${keys} must be an array`);
|
|
66
|
-
items.forEach((item, i) => checkFilter(item, `${path}.${keys}[${i}]`));
|
|
67
|
-
} else if (keys === "not") checkFilter(value["not"], `${path}.not`);
|
|
68
|
-
else if (keys === "geometry") {
|
|
69
|
-
if (!KINDS.includes(value["geometry"])) fail(`the geometry must be one of ${KINDS.join(", ")}`);
|
|
70
|
-
} else if (keys === "in,property" || keys === "exists,property") {
|
|
71
|
-
if (typeof value["property"] !== "string") fail("the property must be a string");
|
|
72
|
-
if (keys === "exists,property" && typeof value["exists"] !== "boolean") fail("the member exists must be true or false");
|
|
73
|
-
if (keys === "in,property") {
|
|
74
|
-
const items = value["in"];
|
|
75
|
-
if (!Array.isArray(items) || !items.every((item) => [
|
|
76
|
-
"string",
|
|
77
|
-
"number",
|
|
78
|
-
"boolean"
|
|
79
|
-
].includes(typeof item))) fail("the member in must be an array of strings, numbers and booleans");
|
|
80
|
-
}
|
|
81
|
-
} else fail(`the members ${keys === "" ? "(none)" : keys} are not a form of a filter`);
|
|
82
|
-
}
|
|
83
18
|
// src/frame.ts
|
|
84
19
|
/**
|
|
85
20
|
* The frame of a map: the part of the Earth that the map shows, its scale and its size on the paper.
|
|
@@ -165,6 +100,237 @@ function frameBBox(frame) {
|
|
|
165
100
|
}
|
|
166
101
|
return bboxOfPoints(points);
|
|
167
102
|
}
|
|
103
|
+
// src/atlas.ts
|
|
104
|
+
/** The default width of the overlap of neighbor pages, in millimetres. */
|
|
105
|
+
const ATLAS_OVERLAP_MM = 10;
|
|
106
|
+
/**
|
|
107
|
+
* This function gives the name of a column: A to Z, then AA, AB and the next names.
|
|
108
|
+
*
|
|
109
|
+
* @throws MapError - The code is `atlas`. The index is not an integer of 0 or more.
|
|
110
|
+
*/
|
|
111
|
+
function columnName(index) {
|
|
112
|
+
if (!(Number.isInteger(index) && index >= 0)) throw new MapError("atlas", `The column ${index} is not an integer of 0 or more`);
|
|
113
|
+
let name = "";
|
|
114
|
+
for (let rest = index + 1; rest > 0; rest = Math.floor((rest - 1) / 26)) name = String.fromCharCode(65 + (rest - 1) % 26) + name;
|
|
115
|
+
return name;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* This function gives the pages of an atlas: a grid of frames on the plane of the projection.
|
|
119
|
+
* The middle of the grid is at the center of the area.
|
|
120
|
+
*
|
|
121
|
+
* - Neighbor pages share a strip of `overlapMm` at their common edge.
|
|
122
|
+
* - The grid has the smallest number of columns and rows that shows the full circle.
|
|
123
|
+
* - Each page has the scale of the plan at its own center. For `azimuthalEquidistant` over 50 km, the edges of neighbor pages agree to 0.01 mm.
|
|
124
|
+
*
|
|
125
|
+
* @throws MapError - The code is `frame` or `atlas`.
|
|
126
|
+
*/
|
|
127
|
+
function atlasPages(plan) {
|
|
128
|
+
const overlapMm = plan.overlapMm ?? 10;
|
|
129
|
+
checkFrame(plan);
|
|
130
|
+
if (!(Number.isFinite(plan.radiusMeters) && plan.radiusMeters > 0)) throw new MapError("atlas", `The radius ${plan.radiusMeters} of the atlas is not a finite number more than 0`);
|
|
131
|
+
if (!(Number.isFinite(overlapMm) && overlapMm >= 0 && overlapMm < Math.min(plan.widthMm, plan.heightMm) / 2)) throw new MapError("atlas", `The overlap ${overlapMm} of the atlas is not from 0 to less than half of the smaller side of the page`);
|
|
132
|
+
const middle = frameView(plan);
|
|
133
|
+
const origin = plan.projection.toPlane(plan.center);
|
|
134
|
+
const radiusMm = plan.radiusMeters * middle.mmPerMeter;
|
|
135
|
+
const stepX = plan.widthMm - overlapMm;
|
|
136
|
+
const stepY = plan.heightMm - overlapMm;
|
|
137
|
+
const columns = Math.max(1, Math.ceil((2 * radiusMm - plan.widthMm) / stepX) + 1);
|
|
138
|
+
const rows = Math.max(1, Math.ceil((2 * radiusMm - plan.heightMm) / stepY) + 1);
|
|
139
|
+
const offsetX = (column) => (column - (columns - 1) / 2) * stepX;
|
|
140
|
+
const offsetY = (row) => ((rows - 1) / 2 - row) * stepY;
|
|
141
|
+
const shows = (column, row) => {
|
|
142
|
+
if (column < 0 || column >= columns || row < 0 || row >= rows) return false;
|
|
143
|
+
const dx = Math.max(Math.abs(offsetX(column)) - plan.widthMm / 2, 0);
|
|
144
|
+
const dy = Math.max(Math.abs(offsetY(row)) - plan.heightMm / 2, 0);
|
|
145
|
+
return dx * dx + dy * dy < radiusMm * radiusMm;
|
|
146
|
+
};
|
|
147
|
+
const name = (column, row) => `${columnName(column)}${row + 1}`;
|
|
148
|
+
const pages = [];
|
|
149
|
+
for (let row = 0; row < rows; row += 1) for (let column = 0; column < columns; column += 1) {
|
|
150
|
+
if (!shows(column, row)) continue;
|
|
151
|
+
const center = plan.projection.fromPlane({
|
|
152
|
+
x: origin.x + offsetX(column) / middle.mmPerPlaneMeter,
|
|
153
|
+
y: origin.y + offsetY(row) / middle.mmPerPlaneMeter
|
|
154
|
+
});
|
|
155
|
+
const neighbors = {};
|
|
156
|
+
for (const [edge, dc, dr] of [
|
|
157
|
+
[
|
|
158
|
+
"top",
|
|
159
|
+
0,
|
|
160
|
+
-1
|
|
161
|
+
],
|
|
162
|
+
[
|
|
163
|
+
"right",
|
|
164
|
+
1,
|
|
165
|
+
0
|
|
166
|
+
],
|
|
167
|
+
[
|
|
168
|
+
"bottom",
|
|
169
|
+
0,
|
|
170
|
+
1
|
|
171
|
+
],
|
|
172
|
+
[
|
|
173
|
+
"left",
|
|
174
|
+
-1,
|
|
175
|
+
0
|
|
176
|
+
]
|
|
177
|
+
]) if (shows(column + dc, row + dr)) neighbors[edge] = name(column + dc, row + dr);
|
|
178
|
+
const frame = {
|
|
179
|
+
projection: plan.projection,
|
|
180
|
+
center,
|
|
181
|
+
scale: plan.scale,
|
|
182
|
+
widthMm: plan.widthMm,
|
|
183
|
+
heightMm: plan.heightMm
|
|
184
|
+
};
|
|
185
|
+
pages.push({
|
|
186
|
+
id: name(column, row),
|
|
187
|
+
column,
|
|
188
|
+
row,
|
|
189
|
+
frame,
|
|
190
|
+
neighbors
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
plan,
|
|
195
|
+
columns,
|
|
196
|
+
rows,
|
|
197
|
+
pages
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* This function gives the outline of each page of an atlas as a polygon feature, with the name of the page in the property `id`.
|
|
202
|
+
* A layer of an index map draws them. Each edge has `pointsPerEdge` points, because the edges of the pages are curves on the Earth.
|
|
203
|
+
*
|
|
204
|
+
* @throws MapError - The code is `atlas`. The number of points is not an integer of 1 or more.
|
|
205
|
+
*/
|
|
206
|
+
function atlasOutlines(atlas, pointsPerEdge = 16) {
|
|
207
|
+
if (!(Number.isInteger(pointsPerEdge) && pointsPerEdge >= 1)) throw new MapError("atlas", `The number of points ${pointsPerEdge} of an edge is not an integer of 1 or more`);
|
|
208
|
+
return atlas.pages.map((page) => {
|
|
209
|
+
const view = frameView(page.frame);
|
|
210
|
+
const { widthMm: w, heightMm: h } = page.frame;
|
|
211
|
+
const ring = [];
|
|
212
|
+
for (let i = 0; i < pointsPerEdge; i += 1) ring.push(lonLat(view.fromPaper({
|
|
213
|
+
x: i / pointsPerEdge * w,
|
|
214
|
+
y: 0
|
|
215
|
+
})));
|
|
216
|
+
for (let i = 0; i < pointsPerEdge; i += 1) ring.push(lonLat(view.fromPaper({
|
|
217
|
+
x: w,
|
|
218
|
+
y: i / pointsPerEdge * h
|
|
219
|
+
})));
|
|
220
|
+
for (let i = 0; i < pointsPerEdge; i += 1) ring.push(lonLat(view.fromPaper({
|
|
221
|
+
x: w - i / pointsPerEdge * w,
|
|
222
|
+
y: h
|
|
223
|
+
})));
|
|
224
|
+
for (let i = 0; i < pointsPerEdge; i += 1) ring.push(lonLat(view.fromPaper({
|
|
225
|
+
x: 0,
|
|
226
|
+
y: h - i / pointsPerEdge * h
|
|
227
|
+
})));
|
|
228
|
+
ring.reverse();
|
|
229
|
+
ring.push(ring[0]);
|
|
230
|
+
return {
|
|
231
|
+
type: "Feature",
|
|
232
|
+
geometry: {
|
|
233
|
+
type: "Polygon",
|
|
234
|
+
coordinates: [ring]
|
|
235
|
+
},
|
|
236
|
+
properties: { id: page.id }
|
|
237
|
+
};
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* This function gives the frame of an index map of an atlas. The frame shows all the pages of the grid in the space, at the largest scale.
|
|
242
|
+
* Its center is the middle of the grid.
|
|
243
|
+
*
|
|
244
|
+
* @throws MapError - The code is `frame`.
|
|
245
|
+
*/
|
|
246
|
+
function atlasIndexFrame(atlas, widthMm, heightMm) {
|
|
247
|
+
const { plan } = atlas;
|
|
248
|
+
checkFrame({
|
|
249
|
+
...plan,
|
|
250
|
+
widthMm,
|
|
251
|
+
heightMm
|
|
252
|
+
});
|
|
253
|
+
const overlapMm = plan.overlapMm ?? 10;
|
|
254
|
+
const gridWidthMm = (atlas.columns - 1) * (plan.widthMm - overlapMm) + plan.widthMm;
|
|
255
|
+
const gridHeightMm = (atlas.rows - 1) * (plan.heightMm - overlapMm) + plan.heightMm;
|
|
256
|
+
const scale = plan.scale * Math.max(gridWidthMm / widthMm, gridHeightMm / heightMm);
|
|
257
|
+
return {
|
|
258
|
+
projection: plan.projection,
|
|
259
|
+
center: plan.center,
|
|
260
|
+
scale,
|
|
261
|
+
widthMm,
|
|
262
|
+
heightMm
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/** This function gives the GeoJSON position `[lon, lat]` of a point. */
|
|
266
|
+
function lonLat(point) {
|
|
267
|
+
return [point.lon, point.lat];
|
|
268
|
+
}
|
|
269
|
+
// src/filter.ts
|
|
270
|
+
/** This function gives the kind of a geometry. */
|
|
271
|
+
function geometryKind(geometry) {
|
|
272
|
+
switch (geometry.type) {
|
|
273
|
+
case "Point":
|
|
274
|
+
case "MultiPoint": return "point";
|
|
275
|
+
case "LineString":
|
|
276
|
+
case "MultiLineString": return "line";
|
|
277
|
+
case "Polygon":
|
|
278
|
+
case "MultiPolygon": return "area";
|
|
279
|
+
case "GeometryCollection": {
|
|
280
|
+
const kinds = new Set(geometry.geometries.map(geometryKind));
|
|
281
|
+
return kinds.size === 1 ? [...kinds][0] : "mixed";
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/** This function tells if a feature matches a filter. A feature with no geometry matches no `{ geometry }` filter. */
|
|
286
|
+
function matchesFilter(filter, feature) {
|
|
287
|
+
if ("all" in filter) return filter.all.every((item) => matchesFilter(item, feature));
|
|
288
|
+
if ("any" in filter) return filter.any.some((item) => matchesFilter(item, feature));
|
|
289
|
+
if ("not" in filter) return !matchesFilter(filter.not, feature);
|
|
290
|
+
if ("geometry" in filter) return feature.geometry !== null && geometryKind(feature.geometry) === filter.geometry;
|
|
291
|
+
const properties = feature.properties ?? {};
|
|
292
|
+
const has = Object.hasOwn(properties, filter.property);
|
|
293
|
+
if ("exists" in filter) return has === filter.exists;
|
|
294
|
+
return has && filter.in.includes(properties[filter.property]);
|
|
295
|
+
}
|
|
296
|
+
const KINDS = [
|
|
297
|
+
"point",
|
|
298
|
+
"line",
|
|
299
|
+
"area",
|
|
300
|
+
"mixed"
|
|
301
|
+
];
|
|
302
|
+
/**
|
|
303
|
+
* This function refuses a value that is not a `Filter`, for example a filter from JSON.
|
|
304
|
+
*
|
|
305
|
+
* @throws MapError - The code is `filter`.
|
|
306
|
+
*/
|
|
307
|
+
function checkFilter(filter, path = "filter") {
|
|
308
|
+
const fail = (reason) => {
|
|
309
|
+
throw new MapError("filter", `${path}: ${reason}`);
|
|
310
|
+
};
|
|
311
|
+
if (typeof filter !== "object" || filter === null || Array.isArray(filter)) fail("a filter must be an object");
|
|
312
|
+
const value = filter;
|
|
313
|
+
const keys = Object.keys(value).toSorted().join(",");
|
|
314
|
+
if (keys === "all" || keys === "any") {
|
|
315
|
+
const items = value[keys];
|
|
316
|
+
if (!Array.isArray(items)) fail(`the member ${keys} must be an array`);
|
|
317
|
+
items.forEach((item, i) => checkFilter(item, `${path}.${keys}[${i}]`));
|
|
318
|
+
} else if (keys === "not") checkFilter(value["not"], `${path}.not`);
|
|
319
|
+
else if (keys === "geometry") {
|
|
320
|
+
if (!KINDS.includes(value["geometry"])) fail(`the geometry must be one of ${KINDS.join(", ")}`);
|
|
321
|
+
} else if (keys === "in,property" || keys === "exists,property") {
|
|
322
|
+
if (typeof value["property"] !== "string") fail("the property must be a string");
|
|
323
|
+
if (keys === "exists,property" && typeof value["exists"] !== "boolean") fail("the member exists must be true or false");
|
|
324
|
+
if (keys === "in,property") {
|
|
325
|
+
const items = value["in"];
|
|
326
|
+
if (!Array.isArray(items) || !items.every((item) => [
|
|
327
|
+
"string",
|
|
328
|
+
"number",
|
|
329
|
+
"boolean"
|
|
330
|
+
].includes(typeof item))) fail("the member in must be an array of strings, numbers and booleans");
|
|
331
|
+
}
|
|
332
|
+
} else fail(`the members ${keys === "" ? "(none)" : keys} are not a form of a filter`);
|
|
333
|
+
}
|
|
168
334
|
// src/style.ts
|
|
169
335
|
/**
|
|
170
336
|
* The black and white styles of the layers: lines with dashes, areas with gray levels and hatches, and point symbols.
|
|
@@ -525,7 +691,7 @@ function areaOnFrame(area, view) {
|
|
|
525
691
|
* - Each point symbol of the layers and the north arrow are obstacles too.
|
|
526
692
|
* - A text with a part on the frame and no free place is in `skipped`. A text with no part on the frame is in neither list.
|
|
527
693
|
*
|
|
528
|
-
* @param obstacles - The boxes on the paper that no label covers, as the middle and the half size in millimetres.
|
|
694
|
+
* @param obstacles - The boxes on the paper that no label covers, as the middle and the half size in millimetres. A box with `halfHeight` has the half width `half`.
|
|
529
695
|
* @throws MapError - The code is `label`: the width of a text from `measure` is not a finite number of 0 or more.
|
|
530
696
|
*/
|
|
531
697
|
function placeLabels(layers, view, font, obstacles = []) {
|
|
@@ -534,7 +700,7 @@ function placeLabels(layers, view, font, obstacles = []) {
|
|
|
534
700
|
x: o.x,
|
|
535
701
|
y: o.y,
|
|
536
702
|
hw: o.half,
|
|
537
|
-
hh: o.half,
|
|
703
|
+
hh: o.halfHeight ?? o.half,
|
|
538
704
|
angle: 0
|
|
539
705
|
});
|
|
540
706
|
for (const layer of layers) {
|
|
@@ -650,7 +816,7 @@ function labelsSvg(placement, font) {
|
|
|
650
816
|
}
|
|
651
817
|
// src/marks.ts
|
|
652
818
|
/**
|
|
653
|
-
* The marks of a map: the scale bar and the
|
|
819
|
+
* The marks of a map: the scale bar, the north arrow and the texts at the edges of the frame.
|
|
654
820
|
*
|
|
655
821
|
* @packageDocumentation
|
|
656
822
|
*/
|
|
@@ -711,6 +877,57 @@ function scaleBarSvg(x, y, length, mmPerMeter) {
|
|
|
711
877
|
function northArrowSvg(x, y, northDegrees) {
|
|
712
878
|
return `<g class="north-arrow" transform="translate(${mm(x)} ${mm(y)}) rotate(${mm(northDegrees)})"><circle r="4.5" fill="#ffffff" stroke="#000000" stroke-width="0.2"/><path d="M0 -3.6L1.6 1.2L0 0.4L-1.6 1.2Z" fill="#000000"/><text y="3.9" font-size="2.2" text-anchor="middle">N</text></g>`;
|
|
713
879
|
}
|
|
880
|
+
/** The four edges of a frame, in the sequence of the marks. */
|
|
881
|
+
const FRAME_EDGES = [
|
|
882
|
+
"top",
|
|
883
|
+
"right",
|
|
884
|
+
"bottom",
|
|
885
|
+
"left"
|
|
886
|
+
];
|
|
887
|
+
/** The size of the font of a text at an edge of the frame, in millimetres. */
|
|
888
|
+
const EDGE_TEXT_MM = 2.6;
|
|
889
|
+
/** The space in the box of an edge text around the arrow and the text, and the size of the arrow, in millimetres. */
|
|
890
|
+
const EDGE = {
|
|
891
|
+
pad: .8,
|
|
892
|
+
arrow: 1.8,
|
|
893
|
+
gap: .7
|
|
894
|
+
};
|
|
895
|
+
/** The angle of the arrow of each edge. The arrow points out of the frame. */
|
|
896
|
+
const EDGE_TURN = {
|
|
897
|
+
top: 0,
|
|
898
|
+
right: 90,
|
|
899
|
+
bottom: 180,
|
|
900
|
+
left: 270
|
|
901
|
+
};
|
|
902
|
+
/**
|
|
903
|
+
* This function gives the marks of the texts at the middle of the edges of a frame, in the sequence top, right, bottom and left.
|
|
904
|
+
* An edge with no text has no mark.
|
|
905
|
+
*
|
|
906
|
+
* @param measure - This function gives the width of a text in millimetres at the size `EDGE_TEXT_MM`.
|
|
907
|
+
*/
|
|
908
|
+
function edgeMarks(widthMm, heightMm, texts, measure) {
|
|
909
|
+
return FRAME_EDGES.flatMap((edge) => {
|
|
910
|
+
const text = texts[edge];
|
|
911
|
+
if (text === void 0) return [];
|
|
912
|
+
const boxWidth = 2 * EDGE.pad + EDGE.arrow + EDGE.gap + measure(text);
|
|
913
|
+
const boxHeight = 2 * EDGE.pad + EDGE_TEXT_MM;
|
|
914
|
+
const x = edge === "left" ? boxWidth / 2 : edge === "right" ? widthMm - boxWidth / 2 : widthMm / 2;
|
|
915
|
+
const y = edge === "top" ? boxHeight / 2 : edge === "bottom" ? heightMm - boxHeight / 2 : heightMm / 2;
|
|
916
|
+
const left = x - boxWidth / 2;
|
|
917
|
+
const top = y - boxHeight / 2;
|
|
918
|
+
const arrowX = left + EDGE.pad + EDGE.arrow / 2;
|
|
919
|
+
const half = EDGE.arrow / 2;
|
|
920
|
+
const svg = `<g class="edge-${edge}"><rect x="${mm(left)}" y="${mm(top)}" width="${mm(boxWidth)}" height="${mm(boxHeight)}" fill="#ffffff" stroke="#000000" stroke-width="0.2"/><path transform="translate(${mm(arrowX)} ${mm(y)}) rotate(${EDGE_TURN[edge]})" d="M0 ${mm(-half)}L${mm(half)} ${mm(half)}L${mm(-half)} ${mm(half)}Z" fill="#000000"/><text x="${mm(arrowX + half + EDGE.gap)}" y="${mm(y + EDGE_TEXT_MM * .35)}" font-size="${EDGE_TEXT_MM}" font-weight="bold">${escapeHtml(text)}</text></g>`;
|
|
921
|
+
return [{
|
|
922
|
+
edge,
|
|
923
|
+
x,
|
|
924
|
+
y,
|
|
925
|
+
halfWidth: boxWidth / 2,
|
|
926
|
+
halfHeight: boxHeight / 2,
|
|
927
|
+
svg
|
|
928
|
+
}];
|
|
929
|
+
});
|
|
930
|
+
}
|
|
714
931
|
// src/render.ts
|
|
715
932
|
/**
|
|
716
933
|
* A map as data, and its render to SVG.
|
|
@@ -748,6 +965,13 @@ function checkMap(map) {
|
|
|
748
965
|
if (layer.label !== void 0) checkLabelRule(layer.label, layer.id);
|
|
749
966
|
if (layer.simplifyMm !== void 0 && !(Number.isFinite(layer.simplifyMm) && layer.simplifyMm > 0)) throw new MapError("style", `The layer "${layer.id}": the tolerance ${layer.simplifyMm} is not a finite number more than 0`);
|
|
750
967
|
}
|
|
968
|
+
if (map.edges !== void 0) {
|
|
969
|
+
if (typeof map.edges !== "object" || map.edges === null) throw new MapError("label", "The edge texts of the map are not an object");
|
|
970
|
+
for (const [edge, text] of Object.entries(map.edges)) {
|
|
971
|
+
if (!FRAME_EDGES.includes(edge)) throw new MapError("label", `The edge "${edge}" is not top, right, bottom or left`);
|
|
972
|
+
if (text !== void 0 && (typeof text !== "string" || text.trim() === "")) throw new MapError("label", `The text of the edge "${edge}" is not a text that is not empty`);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
751
975
|
}
|
|
752
976
|
/** The box of the points on the paper that a feature uses. */
|
|
753
977
|
var PaperBox = class {
|
|
@@ -832,12 +1056,22 @@ const NORTH_ARROW = {
|
|
|
832
1056
|
inset: 6.5,
|
|
833
1057
|
half: 4.7
|
|
834
1058
|
};
|
|
1059
|
+
/** This function gives the marks of the edge texts of a map. Without a font, the width of a text is an estimate. */
|
|
1060
|
+
function edgeMarksOf(map, font) {
|
|
1061
|
+
const measure = (text) => font === void 0 ? text.length * EDGE_TEXT_MM * CHARACTER_WIDTH : font.measure(text, EDGE_TEXT_MM);
|
|
1062
|
+
return edgeMarks(map.frame.widthMm, map.frame.heightMm, map.edges ?? {}, measure);
|
|
1063
|
+
}
|
|
835
1064
|
function placementOf(map, features, view, font) {
|
|
836
|
-
const obstacles = map.northArrow === false ? [] : [{
|
|
1065
|
+
const obstacles = [...map.northArrow === false ? [] : [{
|
|
837
1066
|
x: map.frame.widthMm - NORTH_ARROW.inset,
|
|
838
1067
|
y: NORTH_ARROW.inset,
|
|
839
1068
|
half: NORTH_ARROW.half
|
|
840
|
-
}]
|
|
1069
|
+
}], ...edgeMarksOf(map, font).map((mark) => ({
|
|
1070
|
+
x: mark.x,
|
|
1071
|
+
y: mark.y,
|
|
1072
|
+
half: mark.halfWidth,
|
|
1073
|
+
halfHeight: mark.halfHeight
|
|
1074
|
+
}))];
|
|
841
1075
|
return placeLabels(map.layers.map((layer, i) => ({
|
|
842
1076
|
id: layer.id,
|
|
843
1077
|
features: features[i],
|
|
@@ -1055,8 +1289,9 @@ function renderMap(map, options = {}) {
|
|
|
1055
1289
|
const total = firstLine + (lines.length - 1) * 3 + 1.5;
|
|
1056
1290
|
const width = side ? sideX + layout.columnMm : w;
|
|
1057
1291
|
const arrow = map.northArrow === false ? "" : northArrowSvg(w - 6.5, 6.5, view.northDegrees);
|
|
1058
|
-
|
|
1292
|
+
const edges = edgeMarksOf(map, measureFontOf(options)).map((mark) => mark.svg).join("");
|
|
1293
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${mm(width)}mm" height="${mm(total)}mm" viewBox="0 0 ${mm(width)} ${mm(total)}" role="img" aria-label="${escapeAttribute(map.title)}" font-family="sans-serif"><title>${escapeHtml(map.title)}</title><defs>${defs.join("")}</defs><rect width="${mm(width)}" height="${mm(total)}" fill="#ffffff"/><g clip-path="url(#${map.id}-clip)">${layers.join("")}</g>${labels}<rect width="${mm(w)}" height="${mm(h)}" fill="none" stroke="#000000" stroke-width="0.3"/>${arrow}${edges}<g class="legend">${scaleBars}${legend}</g><g class="attribution">${attribution}</g></svg>`;
|
|
1059
1294
|
}
|
|
1060
|
-
export { MapError, SCALE_BAR_ROW_MM, attributionLines, checkFilter, checkFrame, checkLabelRule, checkMap, checkStyle, fitFrame, frameBBox, frameView, geometryKind, grayColor, hatchPattern, labelText, labelsSvg, mapLabels, mapMargins, matchesFilter, mm, niceNumber, northArrowSvg, placeLabels, pointSymbol, renderMap, scaleBarLength, scaleBarSvg, strokeAttributes };
|
|
1295
|
+
export { ATLAS_OVERLAP_MM, EDGE_TEXT_MM, FRAME_EDGES, MapError, SCALE_BAR_ROW_MM, atlasIndexFrame, atlasOutlines, atlasPages, attributionLines, checkFilter, checkFrame, checkLabelRule, checkMap, checkStyle, columnName, edgeMarks, fitFrame, frameBBox, frameView, geometryKind, grayColor, hatchPattern, labelText, labelsSvg, mapLabels, mapMargins, matchesFilter, mm, niceNumber, northArrowSvg, placeLabels, pointSymbol, renderMap, scaleBarLength, scaleBarSvg, strokeAttributes };
|
|
1061
1296
|
|
|
1062
1297
|
//# sourceMappingURL=index.js.map
|