Skip to content
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
10 changes: 9 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# @geoblocks/mapfisprint changes
# @geoblocks/mapfishprint changes

## 0.2.23

- Resolve a bug where a style with its own geometry as a Circle was transformed into a GeometryCollection resulting in an error

Breaking changes:

- VectorEncoder.featureCircleAsPolygon() was removed.

## 0.2.20

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@geoblocks/mapfishprint",
"version": "0.2.22",
"version": "0.2.23",
Copy link
Member

Choose a reason for hiding this comment

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

@gberaudo a breaking change is a major version, right ? We shoule start to follow strict semver rules.

Suggested change
"version": "0.2.23",
"version": "1.0.0",

I don't care to make a v1 on such bugfix. That's just a number.

"publishConfig": {
"access": "public"
},
Expand Down
36 changes: 16 additions & 20 deletions src/VectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ export default class VectorEncoder {
if (styleFunction) {
styleData = styleFunction(feature, resolution) as null | Style | Style[];
}
if (feature.getGeometry().getType() === 'Circle') {
feature = this.featureCircleAsPolygon(feature as Feature<Circle>);

const featureGeometry = feature.getGeometry();
if (featureGeometry.getType() === "Circle") {
feature.setGeometry(fromCircle(featureGeometry as Circle, Constants.CIRCLE_TO_POLYGON_SIDES));
}
const origGeojsonFeature = this.geojsonFormat.writeFeatureObject(feature);

Expand All @@ -186,25 +188,30 @@ export default class VectorEncoder {
// FIXME: the return of the function is very complicate and would require
// handling more cases than we actually do
let geometry: any = style.getGeometry();
let geojsonFeature;
// Fallback to the feature geometry if style doesn't give one.
if (geometry === null) {
geometry = featureGeometry;
}
// In some cases, the geometries are objects, in other cases they're functions.
// we need to ensure we're handling functions, wether they return an object or not.
if (typeof geometry === 'function') {
geometry = geometry(feature);
}
if (geometry && typeof geometry === 'object') {
// no need to encode features with no geometry
if (!geometry) return;

if (geometry.getType() === "Circle") {
geometry = fromCircle(geometry as Circle, Constants.CIRCLE_TO_POLYGON_SIDES);
}
let geojsonFeature;
if (typeof geometry === 'object') {
// note that (typeof null === 'object')
const styledFeature = feature.clone();
styledFeature.setGeometry(geometry);
geojsonFeature = this.geojsonFormat.writeFeatureObject(styledFeature);
geojsonFeatures.push(geojsonFeature);
} else {
geojsonFeature = origGeojsonFeature;
geometry = feature.getGeometry();
// no need to encode features with no geometry
if (!geometry) {
return;
}
if (!this.customizer_.geometryFilter(geometry)) {
return;
}
Expand Down Expand Up @@ -598,15 +605,4 @@ export default class VectorEncoder {
symbolizers.push(symbolizer);
}
}

/**
* Converts a circle feature to a N sides polygon feature.
* Sides are defined in Constants.CIRCLE_TO_POLYGON_SIDES.
*/
protected featureCircleAsPolygon(feature: Feature<Circle>) {
return new Feature({
...feature.getProperties(),
geometry: fromCircle(feature.getGeometry(), Constants.CIRCLE_TO_POLYGON_SIDES),
});
}
}
45 changes: 44 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'node:test';
import assert from 'node:assert';
import Map from 'ol/Map.js';
import {MFPEncoder, BaseCustomizer} from './lib/index.js';
import {MFPEncoder, MFPVectorEncoder, BaseCustomizer} from './lib/index.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';
import {View} from 'ol';
Expand Down Expand Up @@ -324,3 +324,46 @@ test('Vector features', async (t) => {
},
});
});

test('MFPVectorEncoder can encode a circle with a circle in its style.geometry', async (t) => {
const geomStyleFn = () => {
return new Style({
geometry: fCircle.getGeometry(),
fill,
stroke
});
};
fCircle.setStyle(geomStyleFn);
const vectorLayer = new VectorLayer({
source: new VectorSource({
features: [fCircle],
}),
});
const customizer = new BaseCustomizer();
const resolution = 1.0583354500042335;
const encodedSpecialLayer = new MFPVectorEncoder(vectorLayer.getLayerState(), customizer).encodeVectorLayer(resolution);

assert.deepEqual(encodedSpecialLayer.geoJson.features[0], {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[796622, 5836960],
[796619.0710678119, 5836967.071067812],
[796612, 5836970],
[796604.9289321881, 5836967.071067812],
[796602, 5836960],
[796604.9289321881, 5836952.928932188],
[796612, 5836950],
[796619.0710678119, 5836952.928932188],
[796622, 5836960],
],
],
},
properties: {
name: 'A circle',
_mfp_style: '1',
},
});
});