-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathlayout_defaults.js
More file actions
176 lines (144 loc) · 4.73 KB
/
layout_defaults.js
File metadata and controls
176 lines (144 loc) · 4.73 KB
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
'use strict';
var Lib = require('../../lib');
var handleSubplotDefaults = require('../subplot_defaults');
var handleArrayContainerDefaults = require('../array_container_defaults');
var layoutAttributes = require('./layout_attributes');
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
handleSubplotDefaults(layoutIn, layoutOut, fullData, {
type: 'map',
attributes: layoutAttributes,
handleDefaults: handleDefaults,
partition: 'y',
fullData
});
};
function handleDefaults(containerIn, containerOut, coerce, opts) {
coerce('style');
coerce('center.lon');
coerce('center.lat');
coerce('zoom');
coerce('bearing');
coerce('pitch');
// dynamically set center/zoom if neither param provided
if (!containerIn?.center && !containerIn?.zoom) {
var [{ lon, lat }] = opts.fullData;
var { minLon, maxLon } = getMinBoundLon(lon);
var { minLat, maxLat } = getMinBoundLat(lat);
// this param is called bounds in mapLibre ctor
// not to be confused with maxBounds aliased below
containerOut.fitBounds = {
west: minLon,
east: maxLon,
south: minLat,
north: maxLat,
};
}
// bounds is really for setting maxBounds in mapLibre ctor
var west = coerce('bounds.west');
var east = coerce('bounds.east');
var south = coerce('bounds.south');
var north = coerce('bounds.north');
if(
west === undefined ||
east === undefined ||
south === undefined ||
north === undefined
) {
delete containerOut.bounds;
}
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'layers',
handleItemDefaults: handleLayerDefaults
});
// copy ref to input container to update 'center' and 'zoom' on map move
containerOut._input = containerIn;
}
function getMinBoundLon(lon) {
if (!lon.length) return { minLon: 0, maxLon: 0 };
// normalize to [0, 360)
const norm = lon.map(to360).sort((a, b) => a - b);
let maxGap = -1;
let gapIndex = 0;
// find largest gap
for (let i = 0; i < norm.length; i++) {
const curr = norm[i];
const next = norm[(i + 1) % norm.length];
const gap = (next - curr + 360) % 360;
if (gap > maxGap) {
maxGap = gap;
gapIndex = i;
}
}
// take complement of largest gap
let minLon = norm[(gapIndex + 1) % norm.length];
let maxLon = norm[gapIndex];
minLon = to180(minLon)
maxLon = to180(maxLon)
return { minLon, maxLon };
// https://gis.stackexchange.com/questions/201789/verifying-formula-that-will-convert-longitude-0-360-to-180-to-180
function to180(deg) {
return ((deg + 180) % 360) - 180
}
function to360(deg) {
return ((deg % 360) + 360) % 360;
}
}
function getMinBoundLat(lat) {
return {
minLat: Math.min(...lat),
maxLat: Math.max(...lat)
};
}
function handleLayerDefaults(layerIn, layerOut) {
function coerce(attr, dflt) {
return Lib.coerce(layerIn, layerOut, layoutAttributes.layers, attr, dflt);
}
var visible = coerce('visible');
if(visible) {
var sourceType = coerce('sourcetype');
var mustBeRasterLayer = sourceType === 'raster' || sourceType === 'image';
coerce('source');
coerce('sourceattribution');
if(sourceType === 'vector') {
coerce('sourcelayer');
}
if(sourceType === 'image') {
coerce('coordinates');
}
var typeDflt;
if(mustBeRasterLayer) typeDflt = 'raster';
var type = coerce('type', typeDflt);
if(mustBeRasterLayer && type !== 'raster') {
type = layerOut.type = 'raster';
Lib.log('Source types *raster* and *image* must drawn *raster* layer type.');
}
coerce('below');
coerce('color');
coerce('opacity');
coerce('minzoom');
coerce('maxzoom');
if(type === 'circle') {
coerce('circle.radius');
}
if(type === 'line') {
coerce('line.width');
coerce('line.dash');
}
if(type === 'fill') {
coerce('fill.outlinecolor');
}
if(type === 'symbol') {
coerce('symbol.icon');
coerce('symbol.iconsize');
coerce('symbol.text');
Lib.coerceFont(coerce, 'symbol.textfont', undefined, {
noFontVariant: true,
noFontShadow: true,
noFontLineposition: true,
noFontTextcase: true,
});
coerce('symbol.textposition');
coerce('symbol.placement');
}
}
}