turf icon indicating copy to clipboard operation
turf copied to clipboard

Wrong Area after Difference

Open 2803media opened this issue 7 years ago • 6 comments

With the last (I guess v6) @turf/area @turf/difference

When I use difference to get the polyon between to polygons I've got the result bellow:

intersection = turf.difference(polygon, polygon2);
                          
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-0.5564351,47.4654966],[-0.5564261,47.4654668],[-0.5564026,47.4654694],[-0.5564147,47.4655096],[-0.5564351,47.4654966]],[[-0.5563936,47.465523],[-0.5563783,47.4654727],[-0.5563456,47.4654764],[-0.5563398,47.4654496],[-0.5563254,47.4654515],[-0.5563212,47.46544],[-0.5562846,47.4654448],[-0.5562867,47.4654528],[-0.5563026,47.4655119],[-0.5563602,47.4655444],[-0.5563936,47.465523]]]}}

I want to calculate the area of this new polygon (a multi polyon in this case) so I do that:

area2 = turf.area(intersection);

But the area result is wrong, it's like it only calculate the area of the first or second polyon...

Any idea to get the right area of this polygon?

Edit: It's look like the @turf/difference create a single polygon instead of multipolygon, so the error/bug maybe on Difference and not Area

It may be related to https://github.com/Turfjs/turf/issues/1475

2803media avatar Oct 18 '18 13:10 2803media

@2803media would you please add more details about your particular case? A jsfiddle with your exact code, for example, would be very useful. Are you making the difference between two polygons or multipolygons? How exactly is the resulting area wrong?

stebogit avatar Oct 18 '18 13:10 stebogit

Hi stephano,

As you see the result of Difference is not a valid Polygon because it's a Multipolygon but mark as Polygon by Turf Difference (see above). This is working with turfjs 4.7.3 but not with my build of v6

Here are the inputs:

The first one:

{"type":"Polygon","coordinates":[[[2.3494585,48.8271015],[2.3495397,48.8271599],[2.3495529,48.8271426],[2.3495594,48.8271337],[2.3495663,48.8271245],[2.3495708,48.8271186],[2.349592,48.8270905],[2.3496012,48.8270777],[2.3496958,48.8270984],[2.3497299,48.8271055],[2.3499573,48.8271552],[2.350098,48.8271853],[2.3502895,48.8272262],[2.3502814,48.8272397],[2.3502418,48.8273044],[2.3502712,48.8273118],[2.350308,48.827321],[2.350432,48.827352],[2.3503798,48.8274454],[2.350186,48.8274],[2.3501637,48.8274389],[2.3501566,48.8274505],[2.3501375,48.8274839],[2.3501228,48.8275082],[2.3500986,48.8275484],[2.3499945,48.8275243],[2.3499706,48.8275183],[2.3499094,48.8275042],[2.3499894,48.8273758],[2.3498526,48.8273464],[2.3497788,48.8274739],[2.3497638,48.8274704],[2.34974,48.8274648],[2.3496915,48.8274536],[2.3495879,48.8274297],[2.349533,48.8274169],[2.3494762,48.8274038],[2.3494363,48.8273946],[2.3494269,48.8273924],[2.3493826,48.8273821],[2.3493627,48.8273666],[2.3493306,48.8273416],[2.3493288,48.8273403],[2.3492905,48.8273099],[2.3492589,48.8272858],[2.349183,48.8272263],[2.3491487,48.8271992],[2.3493938,48.8270551],[2.3494296,48.8270807],[2.3494585,48.8271015]]]}

The second one

{"type":"MultiPolygon","coordinates":[[[[2.3500986,48.8275484],[2.3499945,48.8275243],[2.3500594,48.827391],[2.3499894,48.8273758],[2.3498526,48.8273464],[2.3495114,48.8272722],[2.3494428,48.8272208],[2.3492905,48.8273099],[2.3492589,48.8272858],[2.349183,48.8272263],[2.3494296,48.8270807],[2.3494585,48.8271015],[2.3495397,48.8271599],[2.3495384,48.8271617],[2.3495529,48.8271726],[2.3496012,48.8270777],[2.3496958,48.8270984],[2.3496526,48.827186],[2.3502336,48.8273127],[2.3501821,48.8273992],[2.350186,48.8274],[2.3501637,48.8274389],[2.3501566,48.8274505],[2.3501375,48.8274839],[2.3501228,48.8275082],[2.3500986,48.8275484]]]]}

Here is the function used:


                intersec = new L.geoJson(geojsonLayer, {
    							 filter: function(feature, layer) {
                      polygon2 = feature.geometry;
                      
                      try {
                          intersection = turf.difference(polygon, polygon2);
                          area2 = turf.area(intersection);

                          surface['surface issue du cadastre'] = area0;
                          surface['surface issue du calcul'] = area;

                          if(area > area2+1){
                            color = ['red']
                            var randomColor = color[Math.floor(Math.random()*color.length)];
                            console.log('area2',area2);
                            console.log('intersection',JSON.stringify(intersection));

                            inter.push(intersection);
                            if(area2 < 0){
                              area2 = area2*-1;
                            }
                            surface['construction ' + i] = area-area2;
                            i = i+1;

                            L.geoJson(intersection,{
                              style: {color:randomColor, fillOpacity: 0.1,fillColor:randomColor}

                            }).addTo(map);

                          }
                      } catch (error) {
                          console.log('error');
                      }

                     },

    						});

2803media avatar Oct 18 '18 14:10 2803media

So you are using a polygon and a multipolygon... maybe that's the hiccups. Anyway, the output should not be a Polygon but a Multipolygon, you're right.

As workaround, have you tried putting together (adding) the areas of the differences of polygon with each polygon inside the (multi)polygon2? The result should be the correct area... 🤔

I noticed you are inputting geometries to turf difference, have you tried with the entire feature? Although if it was an actual problem it would probably break, the specs do not mention geometries as valid inputs.

stebogit avatar Oct 18 '18 14:10 stebogit

My main source for polygon2 is a huge feature with more than 1000 polygons and multipolygons so I make a loop and get the geometry. For me it use geometry and not feature : http://turfjs.org/docs#difference

The strange thing is that it's working with previous turf difference versions.

2803media avatar Oct 18 '18 15:10 2803media

Hi @2803media

There were some changes to the underlying libs used to calculate difference calculations between v5 and v6.

Unfortunately the v6 modules should've been released as an alpha and isn't super-stable. There is now a v7 branch which has been published as an alpha which should resolve your issue I think. Otherwise v5 is the current stable version.

Hope that helps

rowanwins avatar Oct 19 '18 04:10 rowanwins

thanks @rowanwins I will try that!

2803media avatar Oct 20 '18 16:10 2803media