Skip to content

Commit 6573740

Browse files
Copilotm-mohr
andauthored
Prevent invalid geometries in GeoJSON map editor (#373)
Co-authored-by: Matthias Mohr <m.mohr@moregeo.it>
1 parent ae85085 commit 6573740

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/components/maps/GeoJsonMapEditor.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,31 @@ export default {
7676
}
7777
},
7878
79+
isValidGeometry(geometry) {
80+
if (!geometry) {
81+
return false;
82+
}
83+
const type = geometry.getType();
84+
if (type === 'Polygon' || type === 'MultiPolygon') {
85+
return geometry.getArea() > 0;
86+
}
87+
if (type === 'LineString' || type === 'MultiLineString') {
88+
return geometry.getLength() > 0;
89+
}
90+
return true;
91+
},
92+
7993
geoJsonEditor(geojson) {
8094
var layer = this.addGeoJson(geojson);
8195
96+
// Remove degenerate features (e.g. polygons with zero area from clicking
97+
// the same point multiple times) as soon as they are added to the source
98+
layer.getSource().on('addfeature', (e) => {
99+
if (!this.isValidGeometry(e.feature.getGeometry())) {
100+
layer.getSource().removeFeature(e.feature);
101+
}
102+
});
103+
82104
var mainbar = new Bar();
83105
this.map.addControl(mainbar);
84106
@@ -136,6 +158,10 @@ export default {
136158
var olFeatures = this.geoJsonLayer.getSource().getFeatures();
137159
var gjFeatures = [];
138160
for(var i in olFeatures) {
161+
// Skip features with degenerate geometries (e.g. zero-area polygons)
162+
if (!this.isValidGeometry(olFeatures[i].getGeometry())) {
163+
continue;
164+
}
139165
gjFeatures.push(geojson.writeFeatureObject(
140166
olFeatures[i],
141167
{

0 commit comments

Comments
 (0)