gabzim / circle-to-polygon

Receives a Coordinate, a Radius and a Number of edges and aproximates a circle by creating a polygon that fills its area
ISC License
113 stars 29 forks source link

Incorrect area of polygons #27

Closed Biaggio74 closed 4 years ago

Biaggio74 commented 4 years ago

I am getting incorrect areas of the polygons, thus probably wrong coordinates as well. I can demonstrate the result and reproduce the error by using the Turfjs library. Beside this, I have got same result while working with Google Earth engine. (...the number of pixels showed similar deviation and magnitude of error in an image classification within this computed polygon)

const circleToPolygon = require('circle-to-polygon');
const turf = require('@turf/turf');

const coordinates = [10.278653,59.8947275]; //[lon, lat]
const radius = 1610;                           // in meters
const numberOfEdges =32;                     //optional that defaults to 32

let polygon = circleToPolygon(coordinates, radius, numberOfEdges);

let areaOfPolygon = turf.area(polygon)
let areaMath = radius * radius * Math.PI / 2

console.log(JSON.stringify(polygon))
console.log('Area by Turf: ', areaOfPolygon)
console.log('Area by math: ', areaMath)

Result:

~/projects/circle_to_polygon (master) $ node to_polygon.js
{"type":"Polygon","coordinates":[[[10.278653,59.90919037607431],[10.284280645205657,59.908912356083924],[10.28969174946274,59.90808899392172],[10.294678135098811,59.90675196982125],[10.299048023224346,59.904952723057185],[10.302633427274063,59.90276046629221],[10.30529661686024,59.90025951531567],[10.306935402293933,59.897546038387084],[10.30748703798763,59.89472435169742],[10.306930598361534,59.89190290465795],[10.305287740350176,59.889190110195436],[10.302621829553926,59.8866901806016],[10.299035469941463,59.884499128639774],[10.294666537376706,59.88270108667902],[10.2896828729499,59.881365083961555],[10.284275841271294,59.88054240426573],[10.278653,59.88026462392567],[10.273030158728705,59.88054240426573],[10.2676231270501,59.881365083961555],[10.262639462623294,59.88270108667902],[10.258270530058537,59.884499128639774],[10.254684170446074,59.8866901806016],[10.252018259649825,59.889190110195436],[10.250375401638466,59.89190290465795],[10.249818962012368,59.89472435169742],[10.250370597706066,59.897546038387084],[10.25200938313976,59.90025951531567],[10.254672572725935,59.90276046629221],[10.258257976775651,59.904952723057185],[10.26262786490119,59.90675196982125],[10.267614250537259,59.90808899392172],[10.273025354794344,59.908912356083924],[10.278653,59.90919037607431]]]}
Area by Turf:  8091097.936206874
Area by math:  4071661.1586850514

I understand that the polygon shape has a different area than a perfect circle, but the error shows approx. double area than a mathematically computed one.

johachi commented 4 years ago

Hmmm... that does sound like a lot more. I'm not surprised that it could be more since it's not a circle but a convex hexagon, but twice does seem like a lot. let me look into this.

johachi commented 4 years ago

Wait a moment, why are you dividing the area by 2? The area of a circle is radius * radius * Math.PI not radius * radius * Math.PI / 2.

You say that turf returns 8091097.936206874 while the radius * raduis * Math.PI is 8143322.317370103.

8143322.317370103/8091097.936206874 = 1.0%. This does not seem like a big difference to me.

Biaggio74 commented 4 years ago

@johachi you are right, my mistake, I confused myself with diameters and division by 4, etc. ...I'm sorry. ...Sure, that 1% is very reasonable for the polygon and circle area differences.

johachi commented 4 years ago

Perfect! I'm now closing this issue :)