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

Fix ol9 regularshape #167

Merged
merged 2 commits into from
Feb 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package.json
bs-config.js
docs/*
docs/assets/sldreader.js
10 changes: 8 additions & 2 deletions docs/_includes/head_custom.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/ol.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/codemirror.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/codemirror.min.css" >

{% if page.custom_css %}
<link rel="stylesheet" href="{{ page.custom_css }}">
{% endif %}

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.5.3/core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script>

<script src="assets/sldreader.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: API
nav_order: 5
nav_order: 999
---

# Basic usage
Expand Down
25 changes: 25 additions & 0 deletions docs/assets/mark-gallery.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#mark-gallery {
display: flex;
flex-wrap: wrap;
}

.mark-card {
display: flex;
flex-direction: column;
border: 1px solid #5c5962;
max-width: 92px;
margin: 1rem;
}

.mark-card .mark-box {
width: 92px;
height: 92px;
}

.mark-card .mark-title {
display: flex;
justify-content: center;
margin-bottom: 0.5rem;
font-size: 10;
font-family: monospace;
}
132 changes: 132 additions & 0 deletions docs/assets/mark-gallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* global ol SLDReader */
const BOX_WIDTH = 92; // px
const BOX_HEIGHT = 92; // px

let styleFunction = null; // Style that maps a feature with a wellknownname property to a mark symbolizer.

// prettier-ignore
const wellknownnames = [
'circle', 'triangle', 'star', 'cross', 'hexagon', 'octagon',
'cross2', 'x', 'diamond', 'horline', 'line', 'backslash', 'slash',
];

function createFeatureTypeStyle() {
const featureTypeStyle = {
rules: wellknownnames.map(wellknownname => ({
filter: {
type: 'comparison',
operator: 'propertyisequalto',
matchcase: true,
expression1: {
type: 'propertyname',
typeHint: 'string',
value: 'wellknownname',
},
expression2: wellknownname,
},
pointsymbolizer: [
{
graphic: {
mark: {
wellknownname,
fill: {
styling: {
fill: '#F5F5F5',
},
},
stroke: {
styling: {
stroke: '#7253ed',
strokeWidth: 2.0,
},
},
},
size: 40,
},
},
],
})),
};

// Add else filter to display unknown wellknownname as a boring gray square.
featureTypeStyle.rules.push({
elsefilter: true,
pointsymbolizer: [
{
graphic: {
mark: {
wellknownname: 'square',
fill: {
styling: {
fill: '#cccccc',
},
},
stroke: {
styling: {
stroke: '#000000',
strokeWidth: 1.0,
},
},
},
size: 20,
},
},
],
});

return featureTypeStyle;
}

function getOlMarkStyle(wellknownname) {
if (typeof styleFunction !== 'function') {
styleFunction = SLDReader.createOlStyleFunction(createFeatureTypeStyle());
}
const olFeature = new ol.Feature({
wellknownname,
geometry: new ol.geom.Point([0, 0]),
});
const style = styleFunction(olFeature)[0];
return style;
}

function prepareGallery() {
const galleryContainer = document.querySelector('#mark-gallery');
wellknownnames.forEach(wellknownname => {
const markCard = document.createElement('div');
markCard.classList.add('mark-card');
galleryContainer.appendChild(markCard);

const markBox = document.createElement('div');
markBox.classList.add('mark-box');
markCard.appendChild(markBox);

// Draw point symbol using point style corresponding to the symbol wellknownname.
const canvasWidth = BOX_WIDTH * ol.has.DEVICE_PIXEL_RATIO;
const canvasHeight = BOX_HEIGHT * ol.has.DEVICE_PIXEL_RATIO;
const symbolCanvas = document.createElement('canvas');
symbolCanvas.width = canvasWidth;
symbolCanvas.height = canvasHeight;
markBox.appendChild(symbolCanvas);

const context = symbolCanvas.getContext('2d');
const olContext = ol.render.toContext(context, {
size: [BOX_WIDTH, BOX_HEIGHT],
});
const symbolStyle = getOlMarkStyle(wellknownname);
olContext.setStyle(symbolStyle);
const centerX = BOX_WIDTH / 2;
const centerY = BOX_HEIGHT / 2;
olContext.drawGeometry(new ol.geom.Point([centerX, centerY]));

const markTitle = document.createElement('div');
markTitle.classList.add('mark-title');
markTitle.textContent = wellknownname;
markCard.appendChild(markTitle);
});
}

function init() {
prepareGallery();
}

document.addEventListener('DOMContentLoaded', init);
1 change: 1 addition & 0 deletions docs/assets/ol.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fetch('assets/sld-tasmania.xml')
.then(response => response.text())
.then(text => {
const sldObject = SLDReader.Reader(text);
window.sldObject = sldObject;
styleSelector(sldObject);

const setLayerStyle = (layer, stylename) => {
Expand Down
12 changes: 6 additions & 6 deletions docs/assets/sldreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@
color: 'red',
}),
points: 4,
radius1: 8,
radius: 8,
radius2: 0,
stroke: new style.Stroke({
color: 'red',
Expand Down Expand Up @@ -2033,7 +2033,7 @@
return new style.RegularShape({
fill: fill,
points: 5,
radius1: radius,
radius: radius,
radius2: radius / 2.5,
stroke: stroke,
rotation: rotationRadians,
Expand All @@ -2043,7 +2043,7 @@
return new style.RegularShape({
fill: fill,
points: 4,
radius1: radius,
radius: radius,
radius2: 0,
stroke:
stroke ||
Expand Down Expand Up @@ -2089,7 +2089,7 @@
angle: Math.PI / 4,
fill: fill,
points: 4,
radius1: Math.sqrt(2.0) * radius,
radius: Math.sqrt(2.0) * radius,
radius2: 0,
stroke:
stroke ||
Expand All @@ -2104,7 +2104,7 @@
return new style.RegularShape({
fill: fill,
points: 4,
radius1: radius,
radius: radius,
stroke: stroke,
rotation: rotationRadians,
});
Expand Down Expand Up @@ -2156,7 +2156,7 @@
fill: fill,
points: 4,
// For square, scale radius so the height of the square equals the given size.
radius1: radius * Math.sqrt(2.0),
radius: radius * Math.sqrt(2.0),
stroke: stroke,
rotation: rotationRadians,
});
Expand Down
22 changes: 22 additions & 0 deletions docs/mark-gallery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
layout: default
title: PointSymbolizer gallery
nav_order: 5
custom_css: assets/mark-gallery.css
---

<div class="row">
<div class="col-md-12">
<h1>PointSymbolizer gallery</h1>
<p><a href="assets/mark-gallery.js">View code</a></p>
</div>
</div>
<div class="row">
<p>
This page displays all supported PointSymbolizer marks by wellknownname.
</p>
<div class="col-md-6">
<div id="mark-gallery"></div>
</div>
</div>
<script src="assets/mark-gallery.js"></script>
2 changes: 1 addition & 1 deletion src/styles/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const imageErrorPointStyle = new Style({
color: 'red',
}),
points: 4,
radius1: 8,
radius: 8,
radius2: 0,
stroke: new Stroke({
color: 'red',
Expand Down
10 changes: 5 additions & 5 deletions src/styles/wellknown.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function getWellKnownSymbol(
return new RegularShape({
fill,
points: 5,
radius1: radius,
radius,
radius2: radius / 2.5,
stroke,
rotation: rotationRadians,
Expand All @@ -56,7 +56,7 @@ function getWellKnownSymbol(
return new RegularShape({
fill,
points: 4,
radius1: radius,
radius,
radius2: 0,
stroke:
stroke ||
Expand Down Expand Up @@ -102,7 +102,7 @@ function getWellKnownSymbol(
angle: Math.PI / 4,
fill,
points: 4,
radius1: Math.sqrt(2.0) * radius,
radius: Math.sqrt(2.0) * radius,
radius2: 0,
stroke:
stroke ||
Expand All @@ -117,7 +117,7 @@ function getWellKnownSymbol(
return new RegularShape({
fill,
points: 4,
radius1: radius,
radius,
stroke,
rotation: rotationRadians,
});
Expand Down Expand Up @@ -169,7 +169,7 @@ function getWellKnownSymbol(
fill,
points: 4,
// For square, scale radius so the height of the square equals the given size.
radius1: radius * Math.sqrt(2.0),
radius: radius * Math.sqrt(2.0),
stroke,
rotation: rotationRadians,
});
Expand Down
15 changes: 10 additions & 5 deletions test/OlStyler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('creates point style', () => {
{
graphic: {
mark: {
wellknownname: 'circle',
wellknownname: 'star',
fill: {},
stroke: {},
},
Expand All @@ -109,12 +109,17 @@ describe('creates point style', () => {
],
};
it('returns array', () => {
const style = OlStyler(styleDescription, getMockOLFeature('Point'));
expect(style).to.be.an('array');
const styles = OlStyler(styleDescription, getMockOLFeature('Point'));
expect(styles).to.be.an('array');
});
it('returns style', () => {
const style = OlStyler(styleDescription, getMockOLFeature('Point'));
expect(style['0']).to.be.an.instanceOf(Style);
const [style] = OlStyler(styleDescription, getMockOLFeature('Point'));
expect(style).to.be.an.instanceOf(Style);
});
it('uses radius and radius2 for star-like regular shape', () => {
const [style] = OlStyler(styleDescription, getMockOLFeature('Point'));
expect(style.getImage().getRadius()).to.equal(5);
expect(style.getImage().getRadius2()).to.equal(2);
});
});

Expand Down
Loading