Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
feat(sources): Adds method to update data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ibesora committed Dec 21, 2021
1 parent 95e9b6d commit 0b94943
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 75 deletions.
42 changes: 23 additions & 19 deletions dist/frontends/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,26 +264,30 @@ const leafletLayer = (options) => {
removeInspector(map) {
return map.off("click", this.inspector);
}
updateSource(name, options) {
if (!options.source) {
// Remove the view
this.views.delete(name);
// Remove the label rules
const prevLabelRules = this.label_rules.length;
this.label_rules = this.label_rules.filter((r) => r.dataSource !== name);
if (prevLabelRules !== this.label_rules.length)
this.clearLayout();
}
else {
this.views.set(name, sourceToView(options.source));
if (options.paint_rules) {
this.paint_rules = this.paint_rules.filter((r) => r.dataSource !== name);
this.paint_rules = this.paint_rules.concat(options.paint_rules);
}
if (options.label_rules) {
this.label_rules = this.label_rules.filter((r) => r.dataSource !== name);
this.label_rules = this.label_rules.concat(options.label_rules);
updateDataSources(dataSources, dataLabelsOnTop = false) {
const basemapLayerSourceName = "";
const dataLabelRules = [];
const dataSourcesMap = dataSources.reduce((agg, d) => {
agg[d.name] = d;
return agg;
}, {});
this.paint_rules = this.paint_rules.filter((r) => !r.dataSource || r.dataSource === basemapLayerSourceName);
this.label_rules = this.paint_rules.filter((r) => !r.dataSource || r.dataSource === basemapLayerSourceName);
this.views.keys().forEach((k) => {
if (k === basemapLayerSourceName)
return;
if (!dataSourcesMap[k])
this.views.delete(k);
else {
this.views.set(k, sourceToView(dataSourcesMap[k].options));
this.paint_rules = this.paint_rules.concat(dataSourcesMap[k].paintRules);
dataLabelRules.push(...dataSourcesMap[k].labelRules);
}
});
if (dataLabelRules.length !== 0) {
this.label_rules = dataLabelsOnTop
? dataLabelRules.concat(this.label_rules)
: this.label_rules.concat(dataLabelRules);
}
}
subscribeChildEvents() {
Expand Down
37 changes: 21 additions & 16 deletions dist/protomaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5045,23 +5045,28 @@ var protomaps = (() => {
removeInspector(map) {
return map.off("click", this.inspector);
}
updateSource(name, options2) {
if (!options2.source) {
this.views.delete(name);
const prevLabelRules = this.label_rules.length;
this.label_rules = this.label_rules.filter((r2) => r2.dataSource !== name);
if (prevLabelRules !== this.label_rules.length)
this.clearLayout();
} else {
this.views.set(name, sourceToView(options2.source));
if (options2.paint_rules) {
this.paint_rules = this.paint_rules.filter((r2) => r2.dataSource !== name);
this.paint_rules = this.paint_rules.concat(options2.paint_rules);
}
if (options2.label_rules) {
this.label_rules = this.label_rules.filter((r2) => r2.dataSource !== name);
this.label_rules = this.label_rules.concat(options2.label_rules);
updateDataSources(dataSources, dataLabelsOnTop = false) {
const basemapLayerSourceName = "";
const dataLabelRules = [];
const dataSourcesMap = dataSources.reduce((agg, d) => {
agg[d.name] = d;
return agg;
}, {});
this.paint_rules = this.paint_rules.filter((r2) => !r2.dataSource || r2.dataSource === basemapLayerSourceName);
this.label_rules = this.paint_rules.filter((r2) => !r2.dataSource || r2.dataSource === basemapLayerSourceName);
this.views.keys().forEach((k) => {
if (k === basemapLayerSourceName)
return;
if (!dataSourcesMap[k])
this.views.delete(k);
else {
this.views.set(k, sourceToView(dataSourcesMap[k].options));
this.paint_rules = this.paint_rules.concat(dataSourcesMap[k].paintRules);
dataLabelRules.push(...dataSourcesMap[k].labelRules);
}
});
if (dataLabelRules.length !== 0) {
this.label_rules = dataLabelsOnTop ? dataLabelRules.concat(this.label_rules) : this.label_rules.concat(dataLabelRules);
}
}
subscribeChildEvents() {
Expand Down
4 changes: 2 additions & 2 deletions dist/protomaps.min.js

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions dist/protomaps.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4971,23 +4971,28 @@ var leafletLayer = (options) => {
removeInspector(map) {
return map.off("click", this.inspector);
}
updateSource(name, options2) {
if (!options2.source) {
this.views.delete(name);
const prevLabelRules = this.label_rules.length;
this.label_rules = this.label_rules.filter((r2) => r2.dataSource !== name);
if (prevLabelRules !== this.label_rules.length)
this.clearLayout();
} else {
this.views.set(name, sourceToView(options2.source));
if (options2.paint_rules) {
this.paint_rules = this.paint_rules.filter((r2) => r2.dataSource !== name);
this.paint_rules = this.paint_rules.concat(options2.paint_rules);
}
if (options2.label_rules) {
this.label_rules = this.label_rules.filter((r2) => r2.dataSource !== name);
this.label_rules = this.label_rules.concat(options2.label_rules);
updateDataSources(dataSources, dataLabelsOnTop = false) {
const basemapLayerSourceName = "";
const dataLabelRules = [];
const dataSourcesMap = dataSources.reduce((agg, d) => {
agg[d.name] = d;
return agg;
}, {});
this.paint_rules = this.paint_rules.filter((r2) => !r2.dataSource || r2.dataSource === basemapLayerSourceName);
this.label_rules = this.paint_rules.filter((r2) => !r2.dataSource || r2.dataSource === basemapLayerSourceName);
this.views.keys().forEach((k) => {
if (k === basemapLayerSourceName)
return;
if (!dataSourcesMap[k])
this.views.delete(k);
else {
this.views.set(k, sourceToView(dataSourcesMap[k].options));
this.paint_rules = this.paint_rules.concat(dataSourcesMap[k].paintRules);
dataLabelRules.push(...dataSourcesMap[k].labelRules);
}
});
if (dataLabelRules.length !== 0) {
this.label_rules = dataLabelsOnTop ? dataLabelRules.concat(this.label_rules) : this.label_rules.concat(dataLabelRules);
}
}
subscribeChildEvents() {
Expand Down
65 changes: 43 additions & 22 deletions src/frontends/leaflet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const reflect = (promise: Promise<Status>) => {
);
};

type DataSourceOptions = {
url: string;
maxDataZoom?: number;
levelDiff?: number;
};
type DataSource = {
name: string;
options: DataSourceOptions;
paintRules: Rule[];
labelRules: LabelRule[];
};

const leafletLayer = (options: any): any => {
class LeafletLayer extends L.GridLayer {
constructor(options: any) {
Expand Down Expand Up @@ -360,31 +372,40 @@ const leafletLayer = (options: any): any => {
return map.off("click", this.inspector);
}

public updateSource(name: string, options: any) {
if (!options.source) {
// Remove the view
this.views.delete(name);
// Remove the label rules
const prevLabelRules = this.label_rules.length;
this.label_rules = this.label_rules.filter(
(r: LabelRule) => r.dataSource !== name
);
if (prevLabelRules !== this.label_rules.length) this.clearLayout();
} else {
this.views.set(name, sourceToView(options.source));
if (options.paint_rules) {
this.paint_rules = this.paint_rules.filter(
(r: Rule) => r.dataSource !== name
public updateDataSources(
dataSources: DataSource[],
dataLabelsOnTop = false
) {
const basemapLayerSourceName = "";
const dataLabelRules: LabelRule[] = [];
const dataSourcesMap = dataSources.reduce((agg, d) => {
agg[d.name] = d;
return agg;
}, {} as { [key: string]: DataSource });

this.paint_rules = this.paint_rules.filter(
(r: Rule) => !r.dataSource || r.dataSource === basemapLayerSourceName
);
this.label_rules = this.paint_rules.filter(
(r: LabelRule) =>
!r.dataSource || r.dataSource === basemapLayerSourceName
);
this.views.keys().forEach((k: string) => {
if (k === basemapLayerSourceName) return;
if (!dataSourcesMap[k]) this.views.delete(k);
else {
this.views.set(k, sourceToView(dataSourcesMap[k].options));
this.paint_rules = this.paint_rules.concat(
dataSourcesMap[k].paintRules
);
this.paint_rules = this.paint_rules.concat(options.paint_rules);
dataLabelRules.push(...dataSourcesMap[k].labelRules);
}
});

if (options.label_rules) {
this.label_rules = this.label_rules.filter(
(r: LabelRule) => r.dataSource !== name
);
this.label_rules = this.label_rules.concat(options.label_rules);
}
if (dataLabelRules.length !== 0) {
this.label_rules = dataLabelsOnTop
? dataLabelRules.concat(this.label_rules)
: this.label_rules.concat(dataLabelRules);
}
}

Expand Down

0 comments on commit 0b94943

Please sign in to comment.