improve color saving; improve property handling

This commit is contained in:
Fabian Jeglinski
2019-05-03 17:02:53 +02:00
parent 258cc9abb6
commit d0892f4c57
6 changed files with 159 additions and 84 deletions
+35 -28
View File
@@ -6,7 +6,7 @@ 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
*/
@@ -19,14 +19,8 @@ export class RawLayer {
/**
* Data of Layer (normally in geojson format)
*/
public data: string,
/**
* Layer stroke color as number array with opacity like -> [255, 255, 255, 0.5]
* The fill color is an opaque decreased derivation of this value.
*/
public color: number[]
public data: string
) {
this.saveColorToGeoJSON();
}
/**
@@ -35,16 +29,10 @@ export class RawLayer {
* @param layer
*/
public static fromVectorLayer(layer: VectorLayer): RawLayer {
const writer = new GeoJSON();
const source = layer.getSource();
// get default style values
const features = source.getFeatures();
const featureProperties = features[0].getProperties();
const rawLayerColor = featureProperties[Config.GEO_JSON_PREFIX]['default-style-stroke'];
const geoJSONString = writer.writeFeatures(features,{featureProjection: EPSG.openLayersDefault});
return new RawLayer(layer.get('name'), geoJSONString, rawLayerColor);
const geoJSONString = RawLayer.featuresToGeoJSON(features);
return new RawLayer(layer.get('name'), geoJSONString);
}
/**
@@ -53,10 +41,7 @@ export class RawLayer {
*/
public static toVectorLayer(rawLayer: RawLayer): VectorLayer {
const vectorSource = new VectorSource();
const geoJSON = new GeoJSON();
// load geoJSON features from rawLayer
const features = geoJSON.readFeatures(rawLayer.data, {featureProjection: EPSG.openLayersDefault});
const features = RawLayer.geoJSONToFeatures(rawLayer.data);
// apply style to all features
features.forEach(feature => {
// TODO
@@ -87,20 +72,42 @@ export class RawLayer {
}
/**
* Adds the RawLayer color property to all feature properties from data attribute
* Saves a default color property to geoJSON
*/
private saveColorToGeoJSON() {
const geoJSON = new GeoJSON();
// load geoJSON features from rawLayer
const features = geoJSON.readFeatures(this.data, {featureProjection: EPSG.openLayersDefault});
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'] = this.color;
stylesToSave[Config.GEO_JSON_PREFIX]['default-style-fill'] = [...this.color.slice(0, 3), 0.1];
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 = geoJSON.writeFeatures(features, {featureProjection: EPSG.openLayersDefault});
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});
}
}