File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments