hslayers-ng
Version:
HSLayers-NG mapping library
253 lines (237 loc) • 8.54 kB
JavaScript
import { Vector } from 'ol/source';
import { SparqlJson } from 'hslayers-ng/common/layers';
import * as loadingStrategy from 'ol/loadingstrategy';
import { get } from 'ol/proj';
var EndpointErrorHandling;
(function (EndpointErrorHandling) {
EndpointErrorHandling["ignore"] = "ignore";
EndpointErrorHandling["toast"] = "toast";
})(EndpointErrorHandling || (EndpointErrorHandling = {}));
function isErrorHandlerFunction(object) {
if (typeof object == 'string' || object === undefined) {
return false;
}
return 'handle' in object;
}
const STATE_VALUES = [
'PENDING',
'STARTED',
'FAILURE',
'NOT_AVAILABLE',
];
var HsLaymanGetLayerWfsWmsStatus;
(function (HsLaymanGetLayerWfsWmsStatus) {
HsLaymanGetLayerWfsWmsStatus["AVAILABLE"] = "AVAILABLE";
HsLaymanGetLayerWfsWmsStatus["PREPARING"] = "PREPARING";
HsLaymanGetLayerWfsWmsStatus["NOT_AVAILABLE"] = "NOT_AVAILABLE";
})(HsLaymanGetLayerWfsWmsStatus || (HsLaymanGetLayerWfsWmsStatus = {}));
/**
WHAT ABOUT THESE??
useTiles: boolean;
featureId?: string;
*/
//https://stackoverflow.com/questions/40863488/how-can-i-iterate-over-a-custom-literal-type-in-typescript
const SERVICES_SUPPORTED_BY_URL = [
'wms',
'wfs',
'wmts',
'kml',
'gpx',
'geojson',
'arcgis',
'geosparql',
'xyz',
];
var OverwriteResponse;
(function (OverwriteResponse) {
OverwriteResponse["cancel"] = "cancel";
OverwriteResponse["add"] = "add";
OverwriteResponse["overwrite"] = "overwrite";
})(OverwriteResponse || (OverwriteResponse = {}));
class VectorLayerDescriptor {
constructor(type, name, title, abstract, url, options, mapProjection) {
/**
* Artificial object which is used when layer is saved to composition.
* It describes format (ol.format.KML)
*/
const definition = {};
this.mapProjection = mapProjection;
this.layerParams = {
abstract,
definition,
name,
title,
opacity: options.opacity ?? 1,
fromComposition: options.fromComposition || false,
removable: true,
path: options.path,
visible: options.visible,
workspace: options.workspace,
access_rights: options.access_rights,
queryCapabilities: options.queryCapabilities,
sld: options.sld,
qml: options.qml,
style: options.style,
};
switch (type ? type.toLowerCase() : '') {
case 'kml':
definition.format = 'KML';
definition.url = url;
break;
case 'geojson':
definition.format = 'GeoJSON';
definition.url = url;
break;
case 'gpx':
definition.format = 'GPX';
definition.url = url;
break;
case 'sparql':
definition.format = 'Sparql';
break;
case 'wfs':
Object.assign(this.layerParams, {
editor: {
editable: true,
defaultAttributes: {
name: title,
},
},
});
break;
default:
}
}
}
class VectorSourceFromUrl extends Vector {
constructor(descriptor) {
super({
format: descriptor.sourceParams.format,
url: descriptor.sourceParams.url,
strategy: loadingStrategy.all,
});
this.featureProjection = get(descriptor.sourceParams.srs);
this.mapProjection = descriptor.mapProjection;
super.set('extractStyles', descriptor.sourceParams.extractStyles);
super.setLoader(this.loaderFunction);
}
async loaderFunction(extent, resolution, projection) {
try {
super.set('loaded', false);
const response = await fetch(super.getUrl());
let data = await response.text();
if (data.type == 'GeometryCollection') {
const temp = {
type: 'Feature',
geometry: data,
};
data = temp;
}
super.addFeatures(super.getFormat().readFeatures(data, {
dataProjection: this.featureProjection,
featureProjection: this.mapProjection,
}));
super.set('loaded', true);
}
catch (err) {
this.error = true;
this.errorMessage = err.status;
super.set('loaded', true);
}
}
}
class VectorSourceDescriptor {
constructor() { }
/**
* Construction method which replaces constructor method in order to allow async.
* Should be called after common class initiation new VectorSourceDescriptor()
*/
async init(type, url, srs, options, mapProjection) {
this.mapProjection = mapProjection;
this.sourceParams = {
fromComposition: options.fromComposition || false,
srs,
};
const handlers = {
'kml': async () => {
const { default: KML } = await import('ol/format/KML');
this.sourceParams.url = url;
this.sourceParams.format = new KML({
extractStyles: options.extractStyles,
});
this.sourceClass = VectorSourceFromUrl;
},
'geojson': async () => {
const { default: GeoJSON } = await import('ol/format/GeoJSON');
this.sourceParams.url = url;
this.sourceParams.format = new GeoJSON();
this.sourceClass = VectorSourceFromUrl;
},
'gpx': async () => {
const { default: GPX } = await import('ol/format/GPX');
this.sourceParams.url = url;
this.sourceParams.format = new GPX();
this.sourceClass = VectorSourceFromUrl;
},
'sparql': async () => {
this.sourceParams = {
geomAttribute: options.geomAttribute ?? '?geom',
idAttribute: options.idAttribute,
url: url.includes('=') ? url : null,
endpointUrl: url,
query: options.query,
category_field: url.includes('foodie-cloud') || url.includes('plan4all')
? 'http://www.openvoc.eu/poi#categoryWaze'
: null,
optimization: url.includes('wikidata') ? 'wikibase' : undefined,
projection: 'EPSG:3857',
minResolution: 1,
maxResolution: 38,
};
this.sourceClass = SparqlJson;
},
'wfs': async () => {
this.sourceClass = Vector;
},
'default': async () => {
const { default: GeoJSON } = await import('ol/format/GeoJSON');
this.sourceClass = Vector;
const format = new GeoJSON();
let features = options.features || [];
if (typeof features === 'string') {
features = format.readFeatures(options.features, {
dataProjection: srs,
featureProjection: this.mapProjection,
});
}
this.sourceParams = {
srs,
options,
features,
};
},
};
const handler = handlers[type?.toLowerCase()] || handlers['default'];
await handler.call(this);
}
}
const FILES_SUPPORTED = [
'kml',
'gpx',
'geojson',
'shp',
'raster',
'raster-ts',
];
//https://stackoverflow.com/questions/40863488/how-can-i-iterate-over-a-custom-literal-type-in-typescript
const COMPOSITION_VERSION = '3.0.0';
const QUERY_POPUP_WIDGETS = [
'layer-name',
'feature-info',
'clear-layer',
];
/**
* Generated bundle index. Do not edit.
*/
export { COMPOSITION_VERSION, EndpointErrorHandling, FILES_SUPPORTED, HsLaymanGetLayerWfsWmsStatus, OverwriteResponse, QUERY_POPUP_WIDGETS, SERVICES_SUPPORTED_BY_URL, STATE_VALUES, VectorLayerDescriptor, VectorSourceDescriptor, VectorSourceFromUrl, isErrorHandlerFunction };
//# sourceMappingURL=hslayers-ng-types.mjs.map