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

Area calculation for polygons with multiple rings #254

Closed
lorenzodonini opened this issue Aug 18, 2024 · 2 comments · Fixed by #255
Closed

Area calculation for polygons with multiple rings #254

lorenzodonini opened this issue Aug 18, 2024 · 2 comments · Fixed by #255

Comments

@lorenzodonini
Copy link

Hey there,

first of all thanks of a lot for providing these libraries, I've recently started migrating an existing codebase to use these and it's been a pleasure so far.

I noticed that the area calculation for polygons assumes all rings to be oriented in the same way, otherwise the doubleArea2 function would be "adding" the area of the inner rings to the exterior ring, instead of subtracting the area of the inner rings.

Just as an example to reproduce this:

func TestSomeArea(t *testing.T) {
	w := "POLYGON((134.8119047 -22.85304622,132.7958345 -22.8419189,132.7936583 -24.4451489,134.8149233 -24.44754543,134.8119047 -22.85304622),(133.3994937 -23.33145712,134.3496305 -23.33600152,134.3551062 -24.03150094,133.387875 -24.0320138,133.3994937 -23.33145712))"
	g, err := wkt.Unmarshal(w)
	assert.NoError(t, err)
	poly, ok := g.(*geom.Polygon)
	assert.True(t, ok)
        // EPSG:4326, so all values are in degrees
	area := poly.Area() // Function returns 3.896767841301258
	assert.InDelta(t, 2.5583788122537414, area, 0.00001) // Expected area computed with PostGIS/QGIS
}

While looking at this issue I interpreted your reply as: the exterior ring needs to be oriented counter-clockwise while the inner rings need to be oriented clockwise instead, as per GeoJSON specs.
This doesn't seem to be the case for the example above. However, if I force all rings of that polygon to be oriented counter-clockwise, then the calculated area checks out:

w := "POLYGON((134.8119047 -22.85304622,132.7958345 -22.8419189,132.7936583 -24.4451489,134.8149233 -24.44754543,134.8119047 -22.85304622),(133.3994937 -23.33145712,133.387875 -24.0320138,134.3551062 -24.03150094,134.3496305 -23.33600152,133.3994937 -23.33145712))"

Did I misunderstand the intended behavior or is this a bug?

@twpayne
Copy link
Owner

twpayne commented Aug 18, 2024

Did I misunderstand the intended behavior or is this a bug?

Your understanding is correct and this is a bug. Thanks very much for spotting and reporting it! #255 fixes it.

@twpayne
Copy link
Owner

twpayne commented Aug 18, 2024

I've tagged v1.5.7 which includes the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants