terriajs
Version:
Geospatial data visualization platform.
191 lines • 7.43 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import i18next from "i18next";
import { computed, makeObservable, runInAction } from "mobx";
import { GeomType, LineSymbolizer, PolygonSymbolizer } from "protomaps-leaflet";
import loadJson from "../../../Core/loadJson";
import TerriaError from "../../../Core/TerriaError";
import ProtomapsImageryProvider from "../../../Map/ImageryProvider/ProtomapsImageryProvider";
import { mapboxStyleJsonToProtomaps } from "../../../Map/Vector/Protomaps/mapboxStyleJsonToProtomaps";
import CatalogMemberMixin from "../../../ModelMixins/CatalogMemberMixin";
import MappableMixin from "../../../ModelMixins/MappableMixin";
import UrlMixin from "../../../ModelMixins/UrlMixin";
import LegendTraits, { LegendItemTraits } from "../../../Traits/TraitsClasses/LegendTraits";
import MapboxVectorTileCatalogItemTraits from "../../../Traits/TraitsClasses/MapboxVectorTileCatalogItemTraits";
import CreateModel from "../../Definition/CreateModel";
import createStratumInstance from "../../Definition/createStratumInstance";
import LoadableStratum from "../../Definition/LoadableStratum";
import StratumOrder from "../../Definition/StratumOrder";
import proxyCatalogItemUrl from "../proxyCatalogItemUrl";
class MapboxVectorTileLoadableStratum extends LoadableStratum(MapboxVectorTileCatalogItemTraits) {
item;
styleJson;
static stratumName = "MapboxVectorTileLoadable";
constructor(item, styleJson) {
super();
this.item = item;
this.styleJson = styleJson;
makeObservable(this);
}
duplicateLoadableStratum(newModel) {
return new MapboxVectorTileLoadableStratum(newModel, this.styleJson);
}
static async load(item) {
let styleJson;
if (item.styleUrl) {
try {
styleJson = await loadJson(proxyCatalogItemUrl(item, item.styleUrl));
}
catch (e) {
throw TerriaError.from(e, `Failed to load style JSON from url ${item.styleUrl}`);
}
}
return new MapboxVectorTileLoadableStratum(item, styleJson);
}
get style() {
return this.styleJson;
}
get opacity() {
return 1;
}
get legends() {
if (!this.item.fillColor && !this.item.lineColor)
return [];
return [
createStratumInstance(LegendTraits, {
items: [
createStratumInstance(LegendItemTraits, {
color: this.item.fillColor,
outlineColor: this.item.lineColor,
outlineWidth: this.item.lineColor ? 1 : undefined,
title: this.item.name
})
]
})
];
}
}
__decorate([
computed
], MapboxVectorTileLoadableStratum.prototype, "legends", null);
StratumOrder.addLoadStratum(MapboxVectorTileLoadableStratum.stratumName);
class MapboxVectorTileCatalogItem extends MappableMixin(UrlMixin(CatalogMemberMixin(CreateModel(MapboxVectorTileCatalogItemTraits)))) {
static type = "mvt";
constructor(...args) {
super(...args);
makeObservable(this);
}
get type() {
return MapboxVectorTileCatalogItem.type;
}
get typeName() {
return i18next.t("models.mapboxVectorTile.name");
}
async forceLoadMetadata() {
const stratum = await MapboxVectorTileLoadableStratum.load(this);
runInAction(() => {
this.strata.set(MapboxVectorTileLoadableStratum.stratumName, stratum);
});
}
get parsedJsonStyle() {
if (this.style) {
return mapboxStyleJsonToProtomaps(this.style, {});
}
return undefined;
}
/** Convert traits into paint rules:
* - `layer` and `fillColor`/`lineColor` into simple rules
* - `parsedJsonStyle`
*/
get paintRules() {
const rules = [];
if (this.layer) {
if (this.fillColor) {
rules.push({
dataLayer: this.layer,
symbolizer: new PolygonSymbolizer({ fill: this.fillColor }),
minzoom: this.minimumZoom,
maxzoom: this.maximumZoom,
// Only apply polygon/fill symbolizer to polygon features (otherwise it will also apply to line features)
filter: (_z, f) => f.geomType === GeomType.Polygon
});
}
if (this.lineColor) {
rules.push({
dataLayer: this.layer,
symbolizer: new LineSymbolizer({ color: this.lineColor }),
minzoom: this.minimumZoom,
maxzoom: this.maximumZoom
});
}
}
if (this.parsedJsonStyle) {
rules.push(...this.parsedJsonStyle.paintRules);
}
return rules;
}
get labelRules() {
if (this.parsedJsonStyle) {
return this.parsedJsonStyle.labelRules;
}
return [];
}
get imageryProvider() {
if (this.url === undefined) {
return;
}
return new ProtomapsImageryProvider({
terria: this.terria,
// Use the URL as the id, this is needed for backward compatibility with MapboxImageryProvider, for when picking features (as it uses the URL as the id)
id: this.url,
data: proxyCatalogItemUrl(this, this.url),
minimumZoom: this.minimumZoom,
maximumNativeZoom: this.maximumNativeZoom,
maximumZoom: this.maximumZoom,
rectangle: this.cesiumRectangle,
credit: this.attribution,
paintRules: this.paintRules,
labelRules: this.labelRules,
idProperty: this.idProperty
});
}
forceLoadMapItems() {
return Promise.resolve();
}
get mapItems() {
if (this.isLoadingMapItems || this.imageryProvider === undefined) {
return [];
}
return [
{
imageryProvider: this.imageryProvider,
show: this.show,
alpha: this.opacity,
clippingRectangle: this.clipToRectangle
? this.cesiumRectangle
: undefined
}
];
}
}
__decorate([
computed
], MapboxVectorTileCatalogItem.prototype, "parsedJsonStyle", null);
__decorate([
computed
], MapboxVectorTileCatalogItem.prototype, "paintRules", null);
__decorate([
computed
], MapboxVectorTileCatalogItem.prototype, "labelRules", null);
__decorate([
computed
], MapboxVectorTileCatalogItem.prototype, "imageryProvider", null);
__decorate([
computed
], MapboxVectorTileCatalogItem.prototype, "mapItems", null);
export default MapboxVectorTileCatalogItem;
//# sourceMappingURL=MapboxVectorTileCatalogItem.js.map