UNPKG

terriajs

Version:

Geospatial data visualization platform.

85 lines 3.52 kB
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 { action, computed, makeObservable } from "mobx"; import { parseZip } from "shpjs"; import { isJsonObject } from "../../../Core/Json"; import loadBlob, { isZip } from "../../../Core/loadBlob"; import TerriaError from "../../../Core/TerriaError"; import GeoJsonMixin from "../../../ModelMixins/GeojsonMixin"; import ShapefileCatalogItemTraits from "../../../Traits/TraitsClasses/ShapefileCatalogItemTraits"; import CommonStrata from "../../Definition/CommonStrata"; import CreateModel from "../../Definition/CreateModel"; import proxyCatalogItemUrl from "../proxyCatalogItemUrl"; import { fileApiNotSupportedError } from "./GeoJsonCatalogItem"; export function isJsonArrayOrDeepArrayOfObjects(value) { return (Array.isArray(value) && value.every((child) => isJsonObject(child) || isJsonArrayOrDeepArrayOfObjects(child))); } class ShapefileCatalogItem extends GeoJsonMixin(CreateModel(ShapefileCatalogItemTraits)) { static type = "shp"; constructor(...args) { super(...args); makeObservable(this); } get type() { return ShapefileCatalogItem.type; } get typeName() { return i18next.t("models.shapefile.name"); } setFileInput(file) { this.setTrait(CommonStrata.user, "url", URL.createObjectURL(file) + "#" + file.name); } get hasLocalData() { return this.url?.startsWith("blob:") ?? false; } async forceLoadGeojsonData() { if (this.url) { if (this.hasLocalData || isZip(this.url)) { if (typeof FileReader === "undefined") { throw fileApiNotSupportedError(this.terria); } const blob = await loadBlob(proxyCatalogItemUrl(this, this.url)); return await parseShapefile(blob); } else { throw TerriaError.from("Invalid URL: Only zipped shapefiles are supported (the extension must be `.zip`)"); } } throw TerriaError.from("Failed to load shapefile - no URL of file has been defined"); } } __decorate([ action ], ShapefileCatalogItem.prototype, "setFileInput", null); __decorate([ computed ], ShapefileCatalogItem.prototype, "hasLocalData", null); const mergeFeatureCollections = (items) => { const output = { type: "FeatureCollection", features: [] }; for (let i = 0; i < items.length; i++) { const item = items[i]; for (let j = 0; j < item.features.length; j++) { output.features.push(item.features[j]); } } return output; }; async function parseShapefile(blob) { const asAb = await blob.arrayBuffer(); const json = await parseZip(asAb); if (isJsonArrayOrDeepArrayOfObjects(json)) { return mergeFeatureCollections(json); } return json; } export default ShapefileCatalogItem; //# sourceMappingURL=ShapefileCatalogItem.js.map