Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Parse RasterSymbolizer #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions data/mapbox/raster_simpleraster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const rasterSimpleRaster: any = {
"version": 8,
"name": "Simple Raster",
"layers": [
{
"id": "Simple Raster",
"type": "raster",
"paint": {
'raster-opacity': 0.5,
'raster-brightness-max': 1,
'raster-brightness-min': 0,
'raster-saturation': 1,
'raster-contrast': 1,
'raster-resampling': 'linear',
'raster-fade-duration': 200
}
}
]
};

export default rasterSimpleRaster;
20 changes: 20 additions & 0 deletions data/styles/raster_simpleraster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Style } from 'geostyler-style';

const rasterSimpleRaster: Style = {
'name': 'Simple Raster',
'rules': [{
'name': 'Simple Raster',
'symbolizers': [{
'kind': 'Raster',
'opacity': 0.5,
'brightnessMax': 1,
'brightnessMin': 0,
'saturation': 1,
'contrast': 1,
'resampling': 'linear',
'fadeDuration': 200
}]
}]
};

export default rasterSimpleRaster;
20 changes: 20 additions & 0 deletions src/MapboxStyleParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import line_patternline from '../data/styles/line_patternline';
import mb_line_patternline from '../data/mapbox/line_patternline';
import point_placeholdertext_simple from '../data/styles/point_placeholderText_simple';
import mb_point_placeholdertext_simple from '../data/mapbox/point_placeholderText_simple';
import raster_simpleraster from '../data/styles/raster_simpleraster';
import mb_raster_simpleraster from '../data/mapbox/raster_simpleraster';

it('MapboxStyleParser is defined', () => {
expect(MapboxStyleParser).toBeDefined();
Expand Down Expand Up @@ -165,6 +167,15 @@ describe('MapboxStyleParser implements StyleParser', () => {
});
});

it('can read a mapbox Raster style', () => {
expect.assertions(2);
return styleParser.readStyle(mb_raster_simpleraster)
.then((geoStylerStyle: Style) => {
expect(geoStylerStyle).toBeDefined();
expect(geoStylerStyle).toEqual(raster_simpleraster);
});
});

it('can read a mapbox style with multiple layers', () => {
expect.assertions(2);
return styleParser.readStyle(mb_multi_simpleline_simplefill)
Expand Down Expand Up @@ -257,6 +268,15 @@ describe('MapboxStyleParser implements StyleParser', () => {
});
});

it('can write a mapbox Raster style', () => {
expect.assertions(2);
return styleParser.writeStyle(raster_simpleraster)
.then((mbStyle: any) => {
expect(mbStyle).toBeDefined();
expect(JSON.parse(mbStyle)).toEqual(mb_raster_simpleraster);
});
});

it('can write a mapbox style with multiple symbolizers', () => {
expect.assertions(2);
return styleParser.writeStyle(multi_simpleline_simplefill)
Expand Down
83 changes: 81 additions & 2 deletions src/MapboxStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
SymbolizerKind,
MarkSymbolizer,
ScaleDenominator,
UnsupportedProperties
UnsupportedProperties,
RasterSymbolizer
} from 'geostyler-style';

import MapboxStyleUtil from './Util/MapboxStyleUtil';
Expand Down Expand Up @@ -88,12 +89,14 @@ export class MapboxStyleParser implements StyleParser {
return 'Symbol';
case 'circle':
return 'Circle';
case 'raster':
return 'Raster';
default:
if (this.ignoreConversionErrors) {
return 'Circle';
}
throw new Error(`Could not parse mapbox style. Unsupported layer type.
We support types 'fill', 'line', 'circle' and 'symbol' only.`);
We support types 'fill', 'line', 'circle', 'symbol' and 'raster' only.`);
}
}

Expand Down Expand Up @@ -162,6 +165,27 @@ export class MapboxStyleParser implements StyleParser {
return url;
}

/**
* Creates a GeoStylerStyle-RasterSymbolizer from a Mapbox Style Layer
*
* @param {any} layer A Mapbox Style Layer
* @return {RasterSymbolizer} A GeoStylerStyle-RasterSymbolizer
*/
getRasterSymbolizerFromMapboxLayer(paint: any, layout: any): RasterSymbolizer {
return {
kind: 'Raster',
visibility: layout['visibility'],
brightnessMax: paint['raster-brightness-max'],
brightnessMin: paint['raster-brightness-min'],
contrast: paint['raster-contrast'],
fadeDuration: paint['raster-fade-duration'],
hueRotate: paint['raster-hue-rotate'],
opacity: paint['raster-opacity'],
resampling: paint['raster-resampling'],
saturation: paint['raster-saturation']
};
}

/**
* Creates a GeoStylerStyle-MarkSymbolizer with wellKnownName 'circle'
* from a Mapbox Style Layer. This one will be handled explicitly
Expand Down Expand Up @@ -362,6 +386,8 @@ export class MapboxStyleParser implements StyleParser {
case 'Mark':
symbolizer = this.getMarkSymbolizerFromMapboxLayer(paint, layout);
break;
case 'Raster':
return this.getRasterSymbolizerFromMapboxLayer(paint, layout);
default:
if (this.ignoreConversionErrors) {
return;
Expand Down Expand Up @@ -915,6 +941,11 @@ export class MapboxStyleParser implements StyleParser {
layerType = 'symbol';
}
break;
case 'Raster':
layerType = 'raster';
paint = this.getPaintFromRasterSymbolizer(symbolizerClone as RasterSymbolizer);
layout = this.getLayoutFromRasterSymbolizer(symbolizerClone as RasterSymbolizer);
break;
// TODO check if mapbox can generate regular shapes
default:
if (!this.ignoreConversionErrors) {
Expand Down Expand Up @@ -1352,6 +1383,54 @@ export class MapboxStyleParser implements StyleParser {
};
return layout;
}

/**
* Creates a Mapbox Layer Paint object from a GeoStylerStyle-RasterSymbolizer
*
* @param {RasterSymbolizer} symbolizer A GeoStylerStyle RasterSymbolizer
* @return {any} A Mapbox Layer Paint object
*/
getPaintFromRasterSymbolizer(symbolizer: RasterSymbolizer): any {
const {
brightnessMax,
brightnessMin,
contrast,
fadeDuration,
hueRotate,
opacity,
resampling,
saturation
} = symbolizer;

const paint = {
'raster-opacity': opacity,
'raster-hue-rotate': hueRotate,
'raster-brightness-min': brightnessMin,
'raster-brightness-max': brightnessMax,
'raster-saturation': saturation,
'raster-contrast': contrast,
'raster-resampling': resampling,
'raster-fade-duration': fadeDuration
};
return paint;
}

/**
* Creates a Mapbox Layer Layout object from a GeoStylerStyle-RasterSymbolizer
*
* @param {RasterSymbolizer} symbolizer A GeoStylerStyle RasterSymbolizer
* @return {any} A Mapbox Layer Layout object
*/
getLayoutFromRasterSymbolizer(symbolizer: RasterSymbolizer): any {
const {
visibility
} = symbolizer;

const layout = {
'visibility': visibility
};
return layout;
}
}

export default MapboxStyleParser;