Skip to content

Commit

Permalink
Update FeatureLayer to dynamically update .layerBounds if initialized
Browse files Browse the repository at this point in the history
without a options.extent bounds
  • Loading branch information
prushforth committed Sep 11, 2023
1 parent 57d3ecf commit 633c4ae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/mapml/layers/FeatureLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export var FeatureLayer = L.FeatureGroup.extend({
2. for static templated feature: null
3. for non-templated feature: layer- (with no src) or mapml file (with src)
*/
// options.extent: when you use a FeatureLayer, you can either get it to calculate the
// .layerBounds dynamically (the default), based on adds/removes of features from the layer/
// or you can construct it with a bounds (via options.extent),
// which will then remain static for the lifetime of the layer

L.setOptions(this, options);
if (this.options.static) {
this._container = L.DomUtil.create(
Expand Down Expand Up @@ -53,7 +58,7 @@ export var FeatureLayer = L.FeatureGroup.extend({
this.addData(mapml, native.cs, native.zoom);
} else if (!mapml) {
this.isVisible = false;
this.layerBounds = null;
this.layerBounds = this.options.extent ? this.options.extent : null;
this.zoomBounds = this.options.zoomBounds;
}
},
Expand Down Expand Up @@ -273,14 +278,16 @@ export var FeatureLayer = L.FeatureGroup.extend({
}

let layer = this.geometryToLayer(mapml, options, nativeCS, +zoom, title);
let ext = mapml.extent,
xmin = ext.topLeft.pcrs.horizontal,
ymin = ext.bottomRight.pcrs.vertical,
xmax = ext.bottomRight.pcrs.horizontal,
ymax = ext.topLeft.pcrs.vertical,
bnd = L.bounds(L.point(xmin, ymin), L.point(xmax, ymax));
if (!this.options.extent) {
let ext = mapml.extent,
xmin = ext.topLeft.pcrs.horizontal,
ymin = ext.bottomRight.pcrs.vertical,
xmax = ext.bottomRight.pcrs.horizontal,
ymax = ext.topLeft.pcrs.vertical,
bnd = L.bounds(L.point(xmin, ymin), L.point(xmax, ymax));

this.layerBounds = this.layerBounds ? this.layerBounds.extend(bnd) : bnd;
this.layerBounds = this.layerBounds ? this.layerBounds.extend(bnd) : bnd;
}
if (layer) {
// if the layer is being used as a query handler output, it will have
// a color option set. Otherwise, copy classes from the feature
Expand Down
3 changes: 3 additions & 0 deletions src/mapml/layers/MapMLLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,9 @@ export var MapMLLayer = L.Layer.extend({
pane: layer._container,
opacity: layer.options.opacity,
projection: layer._properties.projection,
// by NOT passing options.extent, we are asking the FeatureLayer
// to dynamically update its .layerBounds property as features are
// added or removed from it
native: native,
zoomBounds: M.getZoomBounds(layer._content, native.zoom),
// each owned child layer gets a reference to the root layer
Expand Down
2 changes: 2 additions & 0 deletions src/mapml/layers/TemplatedFeaturesLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export var TemplatedFeaturesLayer = L.Layer.extend({
// pass the vector layer the container for the parent into which
// it will append its own container for rendering into
pane: container,
// the extent will be static, fixed, constant for the lifetime of the layer
extent: this.extentBounds,
opacity: opacity,
projection: map.options.projection,
static: true,
Expand Down

0 comments on commit 633c4ae

Please sign in to comment.