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

Style Parsing #164

Merged
merged 13 commits into from
Aug 9, 2023
3 changes: 2 additions & 1 deletion elements/map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "module",
"dependencies": {
"lit-html": "^2.7.4",
"ol": "7.3.0"
"ol": "7.3.0",
"ol-mapbox-style": "^10.6.0"
},
"devDependencies": {
"@eox/eslint-config": "^1.0.0",
Expand Down
24 changes: 23 additions & 1 deletion elements/map/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as olLayers from "ol/layer";
import * as olSources from "ol/source";
import * as olFormats from "ol/format";
import { applyStyle } from "ol-mapbox-style";

type EoxLayer = {
type: olLayers.Layer;
Expand All @@ -25,7 +26,7 @@ export const generateLayers = (layerArray: Array<EoxLayer>) => {
if (layer.source && !newSource) {
throw new Error(`Source type ${layer.source.type} not supported!`);
}
return new newLayer({
const olLayer = new newLayer({
...layer,
...(layer.source && {
source: new newSource({
Expand All @@ -42,6 +43,27 @@ export const generateLayers = (layerArray: Array<EoxLayer>) => {
layers: layer.layers.reverse().map((l) => createLayer(l)),
}),
});
if (olLayer.get("style")) {
// existing layer source will not get overridden by "style" property
// to allow vector layers without defined sources, create a dummy-geojson-source
// if source does exist
const style = olLayer.get("style");
if (!style.sources) {
style.sources = {};
}
const sourceName = style.layers[0].source;
if (!style.sources[sourceName]) {
style.sources[sourceName] = {
type: "geojson",
data: {
type: "FeatureCollection",
features: [],
},
};
}
applyStyle(olLayer, style, sourceName);
}
return olLayer;
}

return layerArray.reverse().map((l) => createLayer(l));
Expand Down
20 changes: 20 additions & 0 deletions elements/map/test/vectorLayer.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@ describe("Vector Layer", () => {
expect(eoxMap.getLayerById("regions")).to.exist;
});
});
it("correctly applies mapbox style", () => {
cy.get("eox-map").should(($el) => {
return new Cypress.Promise((resolve) => {
const eoxMap = <EOxMap>$el[0];
eoxMap.setLayers(vectorLayerStyleJson);
const layers = eoxMap.map.getLayers().getArray();
// wait for features to load
//@ts-ignore
layers[0].getSource().on("featuresloadend", () => {
//@ts-ignore
const feature = layers[0].getSource().getFeatures()[0];
//@ts-ignore
const styles = layers[0].getStyleFunction()(feature);
// 2 styles expected, one for the stroke, one for the fill.
expect(styles).to.have.length(2);
resolve();
});
});
});
});
});
29 changes: 27 additions & 2 deletions elements/map/test/vectorLayer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
[
{
"type": "Vector",
"background": "#1a2b39",
"background": "#1366dd",
"properties": {
"id": "regions"
"id": "regions",
"style": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if here in the test we could use a minimal example to show what the minimum fields are to make this work. I understand that version is necessary, but other things are not. Also, the source name does not need to match the layer id, maybe it's better to explicitly show this in the test, for example:

{
  version: 8,
  layers: [
    {
      type: "fill",
      source: "foobar",
      paint: {
        "fill-color": ["get", "COLOR"],
        "fill-opacity": 1,
      },
    },
    {
      type: "line",
      source: "foobar",
      paint: {
        "line-color": "#fff",
        "line-width": 1.5,
      },
    },
  ],
}

Maybe, for convenience, the source property could be automatically generated with an arbitrary string, since it serves no purpose. Or am I missing a functionality here?

"version": 8,
"name": "states",
"sources": {},
"layers": [
{
"id": "regions_fill",
"type": "fill",
"source": "regions",
"paint": {
"fill-color": ["get", "COLOR"],
"fill-opacity": 1
}
},
{
"id": "regions_outline",
"type": "line",
"source": "regions",
"paint": {
"line-color": "#141414",
"line-width": 1.5
}
}
]
}
},
"source": {
"type": "Vector",
Expand Down
32 changes: 22 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading