Skip to content

Commit

Permalink
fix(home): home button works for extent and ids (Canadian-Geospatial-…
Browse files Browse the repository at this point in the history
…Platform#2504)

Also fixes sublayer entry configs not being deleted when parent was removed
  • Loading branch information
DamonU2 authored Sep 24, 2024
1 parent 70c56b4 commit 1debc9a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ export class MapFeatureConfig {
// Default map config depends on map projection.
defaultsDeep(gvMap, MapFeatureConfig.#getDefaultMapConfig(gvMap?.viewSettings?.projection as TypeValidMapProjectionCodes))
);

// Above code will add default zoomAndCenter, remove if other initial view is provided
if (this.map.viewSettings.initialView?.extent || this.map.viewSettings.initialView?.layerIds)
delete this.map.viewSettings.initialView.zoomAndCenter;

this.map.listOfGeoviewLayerConfig = this.map.listOfGeoviewLayerConfig
.map((geoviewLayerConfig) => {
return MapFeatureConfig.nodeFactory(toJsonObject(geoviewLayerConfig), this.#language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,16 +916,10 @@ export class MapEventProcessor extends AbstractEventProcessor {
}

// If extent is in config, use it
if (getGeoViewStore(mapId).getState().mapConfig!.map.viewSettings.initialView?.extent)
extent = getGeoViewStore(mapId).getState().mapConfig!.map.viewSettings.initialView!.extent as Extent;

// Get extents of provided layer IDs if available
if (getGeoViewStore(mapId).getState().mapConfig!.map.viewSettings.initialView?.layerIds) {
const layerExtents = api.maps[mapId].layer.getExtentOfMultipleLayers(
getGeoViewStore(mapId).getState().mapConfig!.map.viewSettings.initialView?.layerIds
);

if (layerExtents) extent = layerExtents;
if (getGeoViewStore(mapId).getState().mapConfig!.map.viewSettings.initialView?.extent) {
const lnglatExtent = getGeoViewStore(mapId).getState().mapConfig!.map.viewSettings.initialView!.extent as Extent;
extent = Projection.transformExtent(lnglatExtent, Projection.PROJECTION_NAMES.LNGLAT, `EPSG:${currProjection}`);
options.padding = [0, 0, 0, 0];
}

return this.zoomToExtent(mapId, extent, options);
Expand Down
21 changes: 12 additions & 9 deletions packages/geoview-core/src/geo/layer/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ export class LayerApi {
// Unregister layer
this.unregisterLayerConfig(this.getLayerEntryConfig(registeredLayerPath)!);
// Remove from registered layers
delete this.#layerEntryConfigs[layerPath];
delete this.#layerEntryConfigs[registeredLayerPath];
}
});

Expand Down Expand Up @@ -1334,14 +1334,17 @@ export class LayerApi {
// Get sublayerpaths and layerpaths from layer IDs.
const subLayerPaths = Object.keys(this.#layerEntryConfigs).filter((layerPath) => layerPath.startsWith(layerId));

// Get max extents from all selected layers.
subLayerPaths.forEach((layerPath) => {
// Get the bounds for the layer path
const layerBounds = LegendEventProcessor.getLayerBounds(this.getMapId(), layerPath);
// If bounds has not yet been defined, set to this layers bounds.
if (!bounds.length && layerBounds) bounds = layerBounds;
else if (layerBounds) bounds = getMinOrMaxExtents(bounds, layerBounds);
});
if (subLayerPaths.length) {
// Get max extents from all selected layers.
subLayerPaths.forEach((layerPath) => {
// Get the bounds for the layer path
const layerBounds = LegendEventProcessor.getLayerBounds(this.getMapId(), layerPath);

// If bounds has not yet been defined, set to this layers bounds.
if (!bounds.length && layerBounds) bounds = layerBounds;
else if (layerBounds) bounds = getMinOrMaxExtents(bounds, layerBounds);
});
}
});

return bounds;
Expand Down
31 changes: 20 additions & 11 deletions packages/geoview-core/src/geo/map/map-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
TypeDisplayLanguage,
TypeDisplayTheme,
} from '@config/types/map-schema-types';
import { removeGeoviewStore } from '@/core/stores/stores-managers';
import { getGeoViewStore, removeGeoviewStore } from '@/core/stores/stores-managers';

import { Basemap } from '@/geo/layer/basemap/basemap';
import { LayerApi } from '@/geo/layer/layer';
Expand Down Expand Up @@ -612,14 +612,9 @@ export class MapViewer {
setTimeout(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
() =>
this.zoomToExtent(
Projection.transformExtent(
this.mapFeaturesConfig.map.viewSettings.initialView?.extent as Extent,
Projection.PROJECTION_NAMES.LNGLAT,
`EPSG:${this.mapFeaturesConfig.map.viewSettings.projection}`
),
{ padding: [0, 0, 0, 0] }
).catch((error) => logger.logPromiseFailed('promiseMapLayers in #checkMapLayersProcessed in map-viewer', error)),
this.zoomToExtent(this.convertExtentLngLatToMapProj(this.mapFeaturesConfig.map.viewSettings.initialView!.extent as Extent), {
padding: [0, 0, 0, 0],
}).catch((error) => logger.logPromiseFailed('promiseMapLayers in #checkMapLayersProcessed in map-viewer', error)),
200
);

Expand All @@ -638,16 +633,30 @@ export class MapViewer {
layerExtents = this.convertExtentLngLatToMapProj(CV_MAP_EXTENTS[this.mapFeaturesConfig.map.viewSettings.projection]);

// Zoom to calculated extent
if (layerExtents.length)
if (layerExtents.length) {
// GV Breaking the rule to not change the config here to prevent later issues
// GV Here we replace the ids in the config with an extent in case the layers are removed
// Replace layerIds with extent in configs
delete this.mapFeaturesConfig.map.viewSettings.initialView!.layerIds;

// The conversions may cause a small amount of inaccuracy as we go to lon/lat for config and convert back when zooming
const lnglatExtent = this.convertExtentMapProjToLngLat(layerExtents);
this.mapFeaturesConfig.map.viewSettings.initialView!.extent = lnglatExtent;

const storeConfig = getGeoViewStore(this.mapId).getState().mapConfig;
delete storeConfig!.map.viewSettings.initialView!.layerIds;
storeConfig!.map.viewSettings.initialView!.extent = lnglatExtent;

// TODO: Timeout allows for map height to be set before zoom happens, so padding is applied properly
setTimeout(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
() =>
this.zoomToExtent(layerExtents).catch((error) =>
logger.logPromiseFailed('promiseMapLayers in #checkMapLayersProcessed in map-viewer', error)
logger.logPromiseFailed('zoomtToExtent in #checkMapReadyGo in map-viewer', error)
),
200
);
}
});
}
}
Expand Down

0 comments on commit 1debc9a

Please sign in to comment.