Skip to content

Commit

Permalink
fix(layer): fix map layers ordering and removal
Browse files Browse the repository at this point in the history
  • Loading branch information
cbourget committed Mar 27, 2019
1 parent 30c6fe3 commit f88e03d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Directive, OnInit, OnDestroy, Optional } from '@angular/core';

import { Subscription } from 'rxjs';
import { skip, filter } from 'rxjs/operators';
import { Subscription, of, zip } from 'rxjs';
import { withLatestFrom, skip, filter } from 'rxjs/operators';

import { RouteService } from '@igo2/core';
import {
Expand Down Expand Up @@ -66,47 +66,33 @@ export class LayerContextDirective implements OnInit, OnDestroy {
this.map.removeLayers(this.contextLayers);
this.contextLayers = [];

context.layers.forEach((layerOptions: LayerOptions) => {
this.addLayerToMap(layerOptions);
const layersAndIndex$ = zip(...context.layers.map((layerOptions: LayerOptions, index: number) => {
return this.layerService.createAsyncLayer(layerOptions).pipe(
withLatestFrom(of(index))
);
}));

layersAndIndex$.subscribe((layersAndIndex: [Layer, number][]) => {
const layers = layersAndIndex.reduce((acc: Layer[], bunch: [Layer, number]) => {
const [layer, index] = bunch;
layer.visible = this.computeLayerVisibilityFromUrl(layer);
layer.zIndex = layer.zIndex || index + 1; // Map indexes start at 1
acc[index] = layer;
return acc;
}, new Array(layersAndIndex.length));
this.contextLayers = layers;
this.map.addLayers(layers);
});
}

private addLayerToMap(layerOptions: LayerOptions) {
// if (contextLayer.maxScaleDenom) {
// contextLayer.maxResolution = this.getResolutionFromScale(
// contextLayer.maxScaleDenom
// );
// }
// if (contextLayer.minScaleDenom) {
// contextLayer.minResolution = this.getResolutionFromScale(
// contextLayer.minScaleDenom
// );
// }

this.layerService
.createAsyncLayer(layerOptions)
.subscribe(layer => {
this.getLayerParamVisibilityUrl(
layer.id,
layer
);
this.contextLayers.push(layer);
this.map.addLayer(layer);
});
}

public getResolutionFromScale(scale: number): number {
const dpi = 25.4 / 0.28;
return scale / (39.37 * dpi);
}

private getLayerParamVisibilityUrl(id, layer) {
private computeLayerVisibilityFromUrl(layer: Layer): boolean {
const params = this.queryParams;
const currentContext = this.contextService.context$.value.uri;
const currentLayerid: string = id;
const currentLayerid: string = layer.id;

let visible = layer.visible;
if (!params || !currentLayerid) {
return;
return visible;
}

const contextParams = params[this.route.options.contextKey as string];
Expand Down Expand Up @@ -134,21 +120,23 @@ export class LayerContextDirective implements OnInit, OnDestroy {
/* This order is important because to control whichever
the order of * param. First whe open and close everything.*/
if (visibleOnLayersParams === '*') {
layer.visible = true;
visible = true;
}
if (visibleOffLayersParams === '*') {
layer.visible = false;
visible = false;
}

// After, managing named layer by id (context.json OR id from datasource)
visiblelayers = visibleOnLayersParams.split(',');
invisiblelayers = visibleOffLayersParams.split(',');
if (visiblelayers.indexOf(currentLayerid) > -1) {
layer.visible = true;
visible = true;
}
if (invisiblelayers.indexOf(currentLayerid) > -1) {
layer.visible = false;
visible = false;
}
}

return visible;
}
}
4 changes: 3 additions & 1 deletion packages/geo/src/lib/map/shared/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,16 @@ export class IgoMap {
*/
removeLayers(layers: Layer[]) {
const newLayers = this.layers$.value.slice(0);
const layersToRemove = [];
layers.forEach((layer: Layer) => {
const index = this.getLayerIndex(layer);
if (index >= 0) {
this.doRemoveLayer(layer);
layersToRemove.push(layer);
newLayers.splice(index, 1);
}
});

layersToRemove.forEach((layer: Layer) => this.doRemoveLayer(layer));
this.setLayers(newLayers);
}

Expand Down

0 comments on commit f88e03d

Please sign in to comment.