Files
GANE-AreaManager/src/app/models/RawLayer.ts
T

114 lines
3.5 KiB
TypeScript

import VectorLayer from 'ol/layer/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import {Vector as VectorSource} from 'ol/source';
import {EPSG} from "../enums/EPSG";
import Style from 'ol/style/Style';
import Stroke from 'ol/style/Stroke';
import Fill from 'ol/style/Fill';
import {Config} from "../misc/Config";
import {Feature} from 'ol/Feature';
/**
* Represents a raw (not processed) map layer
*/
export class RawLayer {
constructor(
/**
* Name of layer
*/
public name: string,
/**
* Data of Layer (normally in geojson format)
*/
public data: string
) {
}
/**
* Creates a RawLayer instance from a VectorLayer and saves the style properties in geojson. Consider that the
* layer color information are applied to each feature!
* @param layer
*/
public static fromVectorLayer(layer: VectorLayer): RawLayer {
const source = layer.getSource();
const features = source.getFeatures();
const geoJSONString = RawLayer.featuresToGeoJSON(features);
return new RawLayer(layer.get('name'), geoJSONString);
}
/**
* Creates a VectorLayer instance from a RawLayer and applies the layer color to all features
* @param rawLayer
*/
public static toVectorLayer(rawLayer: RawLayer): VectorLayer {
const vectorSource = new VectorSource();
const features = RawLayer.geoJSONToFeatures(rawLayer.data);
// apply style to all features
features.forEach(feature => {
// TODO
// determine if this feature has a own color! If so, do not apply default color and use own color.
const featureProperties = feature.getProperties();
const strokeColor = featureProperties[Config.GEO_JSON_PREFIX]['default-style-stroke'];
const fillColor = featureProperties[Config.GEO_JSON_PREFIX]['default-style-fill'];
feature.setStyle(new Style({
stroke: new Stroke({
color: strokeColor,
width: 1
}),
fill: new Fill({
color: fillColor
})
}));
})
// add features to source
vectorSource.addFeatures(features);
return new VectorLayer({
name: rawLayer.name,
renderMode: 'image',
source: vectorSource
});
}
/**
* Saves a default color property to geoJSON
*/
public saveDefaultColor(color: number[]) {
const features = RawLayer.geoJSONToFeatures(this.data);
features.forEach(feature => {
// save default color in feature properties
const stylesToSave = {};
stylesToSave[Config.GEO_JSON_PREFIX] = {};
stylesToSave[Config.GEO_JSON_PREFIX]['default-style-stroke'] = color;
stylesToSave[Config.GEO_JSON_PREFIX]['default-style-fill'] = [...color.slice(0, 3), 0.1];
feature.setProperties(stylesToSave);
})
this.data = RawLayer.featuresToGeoJSON(features);
}
/**
* Reads the default color property from geoJSON
*/
public getDefaultColor(): number[]{
const features = RawLayer.geoJSONToFeatures(this.data);
const featureProperties = features[0].getProperties();
return featureProperties[Config.GEO_JSON_PREFIX]['default-style-stroke'];
}
/**
* Creates the features from geoJSON
*/
private static geoJSONToFeatures(geoJSON: string): Feature[] {
return new GeoJSON().readFeatures(geoJSON, {featureProjection: EPSG.openLayersDefault});
}
/**
* Creates geoJSON string from features
* @param features
*/
private static featuresToGeoJSON(features: Feature[]): string {
return new GeoJSON().writeFeatures(features, {featureProjection: EPSG.openLayersDefault});
}
}