Skip to content

Commit cc4d5d7

Browse files
committed
This commit causes a pretty major regression, probably due to incomplete
implementation of Util.getBounds et al, tbd.
1 parent 2ed9031 commit cc4d5d7

File tree

13 files changed

+189
-192
lines changed

13 files changed

+189
-192
lines changed

src/map-feature.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class MapFeature extends HTMLElement {
7878
if (mapmlvectors?._staticFeature) {
7979
this._removeInFeatureList(oldValue);
8080
let native = this._getNativeZoomAndCS(layer._content);
81-
mapmlvectors.zoomBounds = mapmlvectors._getZoomBounds(
81+
mapmlvectors.zoomBounds = M.getZoomBounds(
8282
layerEl.shadowRoot || layerEl,
8383
native.zoom
8484
);
@@ -159,7 +159,7 @@ export class MapFeature extends HTMLElement {
159159
}
160160
let container = this._layer.shadowRoot || this._layer._layerEl;
161161
// update zoom bounds of vector layer
162-
mapmlvectors.zoomBounds = mapmlvectors._getZoomBounds(
162+
mapmlvectors.zoomBounds = M.getZoomBounds(
163163
container,
164164
this._getNativeZoomAndCS(this._layer._content).zoom
165165
);
@@ -194,7 +194,6 @@ export class MapFeature extends HTMLElement {
194194
if (!this.querySelector('map-geometry')) return;
195195
if (!this._parentEl._layer._mapmlvectors) {
196196
// if vector layer has not yet created (i.e. the layer- is not yet rendered on the map / layer is empty)
197-
let layerEl = this._layer._layerEl;
198197
this._layer.once('add', this._setUpEvents, this);
199198
return;
200199
} else if (!this._featureGroup) {
@@ -222,7 +221,7 @@ export class MapFeature extends HTMLElement {
222221
if (mapmlvectors._staticFeature) {
223222
let container = this._layer.shadowRoot || this._layer._layerEl;
224223
// update zoom bounds of vector layer
225-
mapmlvectors.zoomBounds = mapmlvectors._getZoomBounds(
224+
mapmlvectors.zoomBounds = M.getZoomBounds(
226225
container,
227226
this._getNativeZoomAndCS(this._layer._content).zoom
228227
);
@@ -298,7 +297,7 @@ export class MapFeature extends HTMLElement {
298297
// feature attaches to layer- or layer-'s shadow
299298
if (content.nodeType === Node.DOCUMENT_NODE) {
300299
// for features migrated from mapml, read native zoom and cs from the remote mapml
301-
return this._layer._mapmlvectors._getNativeVariables(content);
300+
return M.getNativeVariables(content);
302301
} else if (content.nodeName.toUpperCase() === 'LAYER-') {
303302
// for inline features, read native zoom and cs from inline map-meta
304303
let zoomMeta = this._parentEl.querySelectorAll('map-meta[name=zoom]'),

src/mapml-viewer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ export class MapViewer extends HTMLElement {
351351
this._map.options.crs = M[newValue];
352352
this._map.options.projection = newValue;
353353
let layersReady = [];
354+
this._map.announceMovement.disable();
354355
for (let layer of this.querySelectorAll('layer-')) {
355356
layer.removeAttribute('disabled');
356357
let reAttach = this.removeChild(layer);
@@ -360,6 +361,7 @@ export class MapViewer extends HTMLElement {
360361
Promise.allSettled(layersReady).then(() => {
361362
this.zoomTo(lat, lon, zoom);
362363
this._resetHistory();
364+
this._map.announceMovement.enable();
363365
});
364366
}
365367
};

src/mapml/features/path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export var Path = L.Path.extend({
142142
},
143143
this
144144
);
145-
leafletLayer.on('add', addMouseHandler, this);
145+
leafletLayer.on('add', addMouseHandler, leafletLayer);
146146
function handleMouse(e) {
147147
//adds a lot of event handlers
148148
if (!container.parentElement) return;
@@ -157,7 +157,7 @@ export var Path = L.Path.extend({
157157
this
158158
);
159159
}
160-
leafletLayer.on('remove', removeMouseHandler, this);
160+
leafletLayer.on('remove', removeMouseHandler, leafletLayer);
161161
function removeMouseHandler() {
162162
L.DomEvent.off(this._map.getContainer(), {
163163
mouseout: handleMouse,

src/mapml/handlers/AnnounceMovement.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ export var AnnounceMovement = L.Handler.extend({
55
layerremove: this.totalBounds
66
});
77

8-
this._map.options.mapEl.addEventListener(
9-
'map-moveend',
10-
this.announceBounds
11-
);
8+
this._map.on('moveend', this.announceBounds, this._map.options.mapEl);
129
this._map.dragging._draggable.addEventListener('dragstart', this.dragged);
1310
this._map.options.mapEl.addEventListener(
1411
'mapfocused',
@@ -21,10 +18,7 @@ export var AnnounceMovement = L.Handler.extend({
2118
layerremove: this.totalBounds
2219
});
2320

24-
this._map.options.mapEl.removeEventListener(
25-
'map-moveend',
26-
this.announceBounds
27-
);
21+
this._map.off('moveend', this.announceBounds, this._map.options.mapEl);
2822
this._map.dragging._draggable.removeEventListener(
2923
'dragstart',
3024
this.dragged

src/mapml/handlers/QueryHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export var QueryHandler = L.Handler.extend({
253253
}
254254
function displayFeaturesPopup(features, loc) {
255255
if (features.length === 0) return;
256-
let f = M.featureLayer(features, {
256+
let f = M.featureLayer(null, {
257257
// pass the vector layer a renderer of its own, otherwise leaflet
258258
// puts everything into the overlayPane
259259
renderer: M.featureRenderer(),

src/mapml/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,9 @@ import {
827827
M._pcrsToGcrs = Util._pcrsToGcrs;
828828
M.mapml2geojson = Util.mapml2geojson;
829829
M.getMaxZoom = Util.getMaxZoom;
830+
M.getBounds = Util.getBounds;
831+
M.getZoomBounds = Util.getZoomBounds;
832+
M.getNativeVariables = Util.getNativeVariables;
830833

831834
M.QueryHandler = QueryHandler;
832835
M.ContextMenu = ContextMenu;

src/mapml/layers/FeatureLayer.js

Lines changed: 9 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ export var FeatureLayer = L.FeatureGroup.extend({
2828
);
2929
L.setOptions(this.options.renderer, { pane: this._container });
3030
}
31-
3231
this._layers = {};
3332
if (this.options.query) {
3433
this._mapmlFeatures = mapml.features ? mapml.features : mapml;
3534
this.isVisible = true;
36-
let native = this._getNativeVariables(mapml);
35+
let native = M.getNativeVariables(mapml);
3736
this.options.nativeZoom = native.zoom;
3837
this.options.nativeCS = native.cs;
39-
}
40-
if (mapml && !this.options.query) {
41-
let native = this._getNativeVariables(mapml);
38+
} else if (mapml) {
39+
let native = M.getNativeVariables(mapml);
4240
//needed to check if the feature is static or not, since this method is used by templated also
4341
if (
4442
!mapml.querySelector('map-extent') &&
@@ -48,11 +46,15 @@ export var FeatureLayer = L.FeatureGroup.extend({
4846
this._features = {};
4947
this._staticFeature = true;
5048
this.isVisible = true; //placeholder for when this actually gets updated in the future
51-
this.zoomBounds = this._getZoomBounds(mapml, native.zoom);
52-
this.layerBounds = this._getLayerBounds(mapml);
49+
this.zoomBounds = M.getZoomBounds(mapml, native.zoom);
50+
this.layerBounds = M.getLayerBounds(mapml);
5351
L.extend(this.options, this.zoomBounds);
5452
}
5553
this.addData(mapml, native.cs, native.zoom);
54+
} else if (!mapml) {
55+
this.isVisible = true;
56+
this.layerBounds = this.options.extent;
57+
this.zoomBounds = this.options.zoomBounds;
5658
}
5759
},
5860

@@ -131,54 +133,6 @@ export var FeatureLayer = L.FeatureGroup.extend({
131133
}
132134
},
133135

134-
// _getNativeVariables: returns an object with the native zoom and CS,
135-
// based on the map-metas that are available within
136-
// the layer or the fallback default values.
137-
// _getNativeVariables: mapml-||layer-||null||[map-feature,...] -> {zoom: _, val: _}
138-
// mapml can be a mapml- element, layer- element, null, or an array of map-features
139-
_getNativeVariables: function (mapml) {
140-
let nativeZoom, nativeCS;
141-
// when mapml is an array of features provided by the query
142-
if (
143-
mapml.length &&
144-
mapml[0].parentElement.parentElement &&
145-
mapml[0].parentElement.parentElement.tagName === 'mapml-'
146-
) {
147-
let mapmlEl = mapml[0].parentElement.parentElement;
148-
nativeZoom =
149-
(mapmlEl.querySelector &&
150-
mapmlEl.querySelector('map-meta[name=zoom]') &&
151-
+M._metaContentToObject(
152-
mapmlEl.querySelector('map-meta[name=zoom]').getAttribute('content')
153-
).value) ||
154-
0;
155-
nativeCS =
156-
(mapmlEl.querySelector &&
157-
mapmlEl.querySelector('map-meta[name=cs]') &&
158-
M._metaContentToObject(
159-
mapmlEl.querySelector('map-meta[name=cs]').getAttribute('content')
160-
).content) ||
161-
'GCRS';
162-
} else {
163-
// when mapml is null or a layer-/mapml- element
164-
nativeZoom =
165-
(mapml.querySelector &&
166-
mapml.querySelector('map-meta[name=zoom]') &&
167-
+M._metaContentToObject(
168-
mapml.querySelector('map-meta[name=zoom]').getAttribute('content')
169-
).value) ||
170-
0;
171-
nativeCS =
172-
(mapml.querySelector &&
173-
mapml.querySelector('map-meta[name=cs]') &&
174-
M._metaContentToObject(
175-
mapml.querySelector('map-meta[name=cs]').getAttribute('content')
176-
).content) ||
177-
'GCRS';
178-
}
179-
return { zoom: nativeZoom, cs: nativeCS };
180-
},
181-
182136
_handleMoveEnd: function () {
183137
let mapZoom = this._map.getZoom(),
184138
withinZoom =
@@ -210,60 +164,6 @@ export var FeatureLayer = L.FeatureGroup.extend({
210164
this._resetFeatures();
211165
},
212166

213-
//sets default if any are missing, better to only replace ones that are missing
214-
_getLayerBounds: function (container) {
215-
if (!container) return null;
216-
let cs = FALLBACK_CS,
217-
projection =
218-
(container.querySelector('map-meta[name=projection]') &&
219-
M._metaContentToObject(
220-
container
221-
.querySelector('map-meta[name=projection]')
222-
.getAttribute('content')
223-
).content.toUpperCase()) ||
224-
FALLBACK_PROJECTION;
225-
try {
226-
let meta =
227-
container.querySelector('map-meta[name=extent]') &&
228-
M._metaContentToObject(
229-
container
230-
.querySelector('map-meta[name=extent]')
231-
.getAttribute('content')
232-
);
233-
234-
let zoom = meta.zoom || 0;
235-
236-
let metaKeys = Object.keys(meta);
237-
for (let i = 0; i < metaKeys.length; i++) {
238-
if (!metaKeys[i].includes('zoom')) {
239-
cs = M.axisToCS(metaKeys[i].split('-')[2]);
240-
break;
241-
}
242-
}
243-
let axes = M.csToAxes(cs);
244-
return M.boundsToPCRSBounds(
245-
L.bounds(
246-
L.point(+meta[`top-left-${axes[0]}`], +meta[`top-left-${axes[1]}`]),
247-
L.point(
248-
+meta[`bottom-right-${axes[0]}`],
249-
+meta[`bottom-right-${axes[1]}`]
250-
)
251-
),
252-
zoom,
253-
projection,
254-
cs
255-
);
256-
} catch (error) {
257-
//if error then by default set the layer to osm and bounds to the entire map view
258-
return M.boundsToPCRSBounds(
259-
M[projection].options.crs.tilematrix.bounds(0),
260-
0,
261-
projection,
262-
cs
263-
);
264-
}
265-
},
266-
267167
_resetFeatures: function () {
268168
this.clearLayers();
269169
// since features are removed and re-added by zoom level, need to clean the feature index before re-adding
@@ -300,45 +200,6 @@ export var FeatureLayer = L.FeatureGroup.extend({
300200
}
301201
},
302202

303-
_getZoomBounds: function (container, nativeZoom) {
304-
if (!container) return null;
305-
let nMin = 100,
306-
nMax = 0,
307-
features = container.querySelectorAll('map-feature'),
308-
meta,
309-
projection;
310-
for (let i = 0; i < features.length; i++) {
311-
let lZoom = +features[i].getAttribute('zoom');
312-
if (!features[i].getAttribute('zoom')) lZoom = nativeZoom;
313-
nMax = Math.max(nMax, lZoom);
314-
nMin = Math.min(nMin, lZoom);
315-
}
316-
try {
317-
projection = M._metaContentToObject(
318-
container
319-
.querySelector('map-meta[name=projection]')
320-
.getAttribute('content')
321-
).content;
322-
meta = M._metaContentToObject(
323-
container.querySelector('map-meta[name=zoom]').getAttribute('content')
324-
);
325-
} catch (error) {
326-
return {
327-
minZoom: 0,
328-
maxZoom:
329-
M[projection || FALLBACK_PROJECTION].options.resolutions.length - 1,
330-
minNativeZoom: nMin,
331-
maxNativeZoom: nMax
332-
};
333-
}
334-
return {
335-
minZoom: +meta.min,
336-
maxZoom: +meta.max,
337-
minNativeZoom: nMin,
338-
maxNativeZoom: nMax
339-
};
340-
},
341-
342203
addData: function (mapml, nativeCS, nativeZoom) {
343204
var features =
344205
mapml.nodeType === Node.DOCUMENT_NODE || mapml.nodeName === 'LAYER-'

src/mapml/layers/MapMLLayer.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ export var MapMLLayer = L.Layer.extend({
152152
}
153153
this._map = map;
154154
if (this._mapmlvectors) map.addLayer(this._mapmlvectors);
155-
this._setLayerElExtent();
156155

157156
if (!this._imageLayer) {
158157
this._imageLayer = L.layerGroup();
@@ -175,7 +174,6 @@ export var MapMLLayer = L.Layer.extend({
175174
tileSize: map.options.crs.options.crs.tile.bounds.max.x
176175
});
177176
map.addLayer(this._staticTileLayer);
178-
this._setLayerElExtent();
179177
}
180178

181179
const createAndAdd = createAndAddTemplatedLayers.bind(this);
@@ -249,7 +247,6 @@ export var MapMLLayer = L.Layer.extend({
249247
);
250248
}
251249
}
252-
this._setLayerElExtent();
253250
}
254251
}
255252
},
@@ -881,6 +878,7 @@ export var MapMLLayer = L.Layer.extend({
881878
// if layer does not have a parent Element, do not need to set Controls
882879
layer._layerEl.parentElement._toggleControls();
883880
}
881+
layer._setLayerElExtent();
884882
layer.fire('foo', layer, false);
885883
// local functions
886884
// sets layer._properties.projection. Supposed to replace / simplify
@@ -943,6 +941,15 @@ export var MapMLLayer = L.Layer.extend({
943941
})
944942
);
945943
return true;
944+
//if this is the only layer, but the projection doesn't match,
945+
// set the map's projection to that of the layer
946+
} else if (
947+
layer._properties.projection !== layer.options.mapprojection &&
948+
layer._layerEl.parentElement.layers.length === 1
949+
) {
950+
layer._layerEl.parentElement.projection =
951+
layer._properties.projection;
952+
return true;
946953
}
947954
} catch (error) {}
948955
return false;
@@ -1486,7 +1493,8 @@ export var MapMLLayer = L.Layer.extend({
14861493
}
14871494
}
14881495
function processFeatures() {
1489-
layer._mapmlvectors = M.featureLayer(layer._content, {
1496+
let native = M.getNativeVariables(layer._content);
1497+
layer._mapmlvectors = M.featureLayer(null, {
14901498
// pass the vector layer a renderer of its own, otherwise leaflet
14911499
// puts everything into the overlayPane
14921500
renderer: M.featureRenderer(),
@@ -1495,6 +1503,9 @@ export var MapMLLayer = L.Layer.extend({
14951503
pane: layer._container,
14961504
opacity: layer.options.opacity,
14971505
projection: layer._properties.projection,
1506+
extent: M.getBounds(layer._content),
1507+
native: native,
1508+
zoomBounds: M.getZoomBounds(layer._content, native.zoom),
14981509
// each owned child layer gets a reference to the root layer
14991510
_leafletLayer: layer,
15001511
static: true,

src/mapml/layers/TemplatedTileLayer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export var TemplatedTileLayer = L.TileLayer.extend({
163163
xOffset = coords.x * tileSize,
164164
yOffset = coords.y * tileSize;
165165

166-
let tileFeatures = M.featureLayer(markup, {
166+
let tileFeatures = M.featureLayer(null, {
167167
projection: this._map.options.projection,
168168
static: false,
169169
interactive: false

0 commit comments

Comments
 (0)