How to computerArea of a polygon with holes in Google maps? [on hold]

Multi tool use
Multi tool use


How to computerArea of a polygon with holes in Google maps? [on hold]



Polygon with Hole is presented by multiple paths, but how to computerArea of a polygon with holes in Google maps?



My first attempt is to sum up all area of each path from google.maps.geometry.spherical.computeArea(path).




function polygonArea1(polygon) {
var paths = polygon.getPaths().getArray();
var a = 0;
for (var i = 0; i < paths.length; ++i) {
a += google.maps.geometry.spherical.computeArea(paths[i]);
}
return a;
}



It does not work because this computeArea() is unsigned!
What is your solution or suggestion?
enter image description here




#simple polygon: one path, no intersection.
42.42883995722236,-71.60123305060961
42.42874096809684,-71.60132961013414
42.42868157454647,-71.60120891072847
42.42878155365721,-71.60111637451746

#complex polygon 1: one path but has intersection.
42.42869345326113,-71.60101981499292
42.42879343235287,-71.6009259376774
42.42860733252938,-71.60083742477991
42.42871028143621,-71.60075025298693

#complex polygon 2: two or more paths without intersection.
42.42869048358269,-71.60062150695421
42.428780563765905,-71.60052092411615
42.42868751390411,-71.60037072041132
42.42859347400984,-71.60045386889078
&1th
42.42864989794301,-71.60045118668177
42.428680584633994,-71.60043174066641
42.428724139911225,-71.60050751307108
42.42870137240295,-71.6005464051018

#complex polygon 3: two or more paths with intersection.
42.42869642293929,-71.60020174124338
42.42861624157606,-71.60027281978228
42.42855882769735,-71.60014273264505
42.42863603945308,-71.60007433631517
&1th
42.428637029313705,-71.60019637682535
42.4287300792654,-71.60011322834589
42.42871028143333,-71.60007165410616
42.428616241558124,-71.60015413203337



This question appears to be off-topic. The users who voted to close gave this specific reason:





Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
– LGSon
Jul 1 at 13:55





Polygon with Hole example: developers.google.com/maps/documentation/javascript/examples/…
– Lingfa
Jul 1 at 13:56






Please provide a Minimal, Complete, and Verifiable example that demonstrates the issue in the question itself. Did you try subtracting the area of the inner polygons from the area of the outer boundary polygon?
– geocodezip
Jul 1 at 16:31





My question is not simply subtracting inner polygons from the area of the outer boundary polygon. Polygon path can be clockwise wound (positive area) or counterclockwise wound (negative area) in one contains another. Multiple polygons may overlap each other following odd rule filling (area covered by 1, or 3, or 5 ... polygons are filled (included); area covered by 2, or 4, ... polygons are holes, excluded from area calculation.
– Lingfa
Jul 2 at 2:03




1 Answer
1




class Polygon {

getPolygonArea(coords) {
let area = 0;
if (coords && coords.length > 0) {
area += Math.abs(this.getPolygonRingArea(coords[0]));
for (let i = 1; i < coords.length; i++) {
area -= Math.abs(this.getPolygonRingArea(coords[i]));
}
}
return area;
}

getRadiun(_) {
return _ * Math.PI / 180;
}

getPolygonRingArea(coords) {
let p1;
let p2;
let p3;
let lowerIndex;
let middleIndex;
let upperIndex;
let i;
let area = 0;
let coordsLength = coords.length;
const RADIUS = 6378137;

if (coordsLength > 2) {
for (i = 0; i < coordsLength; i++) {
if (i === coordsLength - 2) { // i = N-2
lowerIndex = coordsLength - 2;
middleIndex = coordsLength - 1;
upperIndex = 0;
} else if (i === coordsLength - 1) { // i = N-1
lowerIndex = coordsLength - 1;
middleIndex = 0;
upperIndex = 1;
} else { // i = 0 to N-3
lowerIndex = i;
middleIndex = i + 1;
upperIndex = i + 2;
}
p1 = coords[lowerIndex];
p2 = coords[middleIndex];
p3 = coords[upperIndex];
area += (this.getRadiun(p3[0]) - this.getRadiun(p1[0])) * Math.sin(this.getRadiun(p2[1]));
}

area = area * RADIUS * RADIUS / 2;
}

return area;
}
}

let coords = [
[
[-64.73, 32.31],
[-80.19, 25.76],
[-66.09, 18.43],
[-64.73, 32.31]
]
];

alert(new Polygon().getPolygonArea(coords));



This is the above logic to calculate the area of a Polygon and subtract the area of holes to get to the final area.



The format of coordinates -


let coords = [
[
[lng, lat],....
]
];





What is your coords format? I tried your method by passing a LatLng array, which returns 0. _getPolygonArea([{lat: 25.774, lng: -80.190},{lat: 18.466, lng: -66.118},{lat: 32.321, lng: -64.757}])
– Lingfa
Jul 1 at 14:54





It is 2d array [[lat, lng],...]
– Ashvin777
Jul 1 at 17:51





_getPolygonArea([[25.774,-80.190],[18.466,-66.118],[32.321,-64.757]]) still return 0. Could you please just give one line of code of an example call?
– Lingfa
Jul 1 at 19:07






Pass that data as string. Or else remove the first line from getPolygonArea function
– Ashvin777
Jul 2 at 1:36





I don't think your method could work. I don't think you tested your method, otherwise, I will be simply convinced by a single input-output proof,
– Lingfa
Jul 2 at 11:14

5TV1aQ zlJbDDv K2FXoQ5pMg Q8O9Yk4xl XSz QZvw2 QSKuFDlVe,mA,henn
kDZAcxTITZiLWx,iagQSj,0ltbA

Popular posts from this blog

Rothschild family

Cinema of Italy