views:

94

answers:

1

Hi,

I'm trying to find a way to calculate the area of a polygon using lat long coordinates in a Flex 3 site. Hong007 on Google Maps for Flash group was cool enough to post the following function:

private function GetPolygonArea (polygon : Polygon):Number 
        { 
            var nVer : int = polygon.getOuterVertexCount(); 
            var sz : Number =0; 
            var s : Number =0; 
            var x : Number =0; 
            var y0 : Number =0; 
            var y1 : Number =0; 
            var Maplatlng:LatLng; 
            if (nVer>=3){ 
               for (var i:int=0; i<nVer; i++){ 
                   Maplatlng = polygon.getOuterVertex(i); 
                   x = Maplatlng.lng(); 
                   if (i>0){ 
                      Maplatlng = polygon.getOuterVertex(i-1); 
                      y0 = Maplatlng.lat(); 
                   } 
                   else{ 
                      Maplatlng = polygon.getOuterVertex(nVer-1); 
                      y0 = Maplatlng.lat(); 
                   }; 
                   if (i<(nVer-1)){ 
                      Maplatlng = polygon.getOuterVertex(i+1); 
                      y1 = Maplatlng.lat(); 
                   } 
                   else{ 
                      Maplatlng = polygon.getOuterVertex(0); 
                      y1 = Maplatlng.lat(); 
                   }; 
                   s = x * (y0-y1); 
                   sz+=s; 
               }; 
               //경위도시 1도의 m값을 곱한다(대략 면적 환산) 
               Maplatlng = polygon.getOuterVertex(0); 
               var Maplatlng1:LatLng = new 
com.google.maps.LatLng(Maplatlng.lat()+1, Maplatlng.lng()+1); 
               var TempDISTANCE:Number = 
Maplatlng.distanceFrom(Maplatlng1) / Math.sqrt(2); 
               return Math.abs((sz/2.0) * Math.pow(TempDISTANCE, 2)); 
            }; 
            return 0.0; 
        }

I was also playing around with the area calculator at http://www.freemaptools.com/area-calculator.htm .

These functions produce slightly different results. I'm trying to figure out which one is more accurate. It seems that hong007's function produces results that are on average slightly larger than freemaptools' function. However, I don't know which one is more accurate. Any advice?

Thanks you.

-Laxmidi

A: 

The method implemented here is pretty quick and dirty. It makes a couple of assumptions that can lead to incorrect results.

The first thing to know is that Lat/Long space is non-uniformly scaled with respect to measured distance on the ground. This means that a vector of length one meter has a different length in lat/long space depending on if the vector is pointing roughly east-west or north-south. Also, the magnitude of the difference between how the lat/long axes map to ground units changes depending on where you are on the globe (it's much more different at the poles than at the equator.)

The algorithm above does a very quick and dirty workaround for this, which is to generate a scale value based on the distance calculated for the hypotenuse of a unit right triangle. This tries to basically average the scales of the two lat/long axes for that point on the globe.

There are a couple of problems with this. If the polygon is very large (multiple geocells), then this average scale value will be off because it's only calculated for the local geocell around the 0 vertex. Secondly, the average scale approximation is really very coarse and will break down significantly if you have polygons that vary greatly in one dimension but not the other (a long skinny polygon oriented along one of the axes). This is because the scale will be computed as an average of the scale of the two axes but one of the axes should have very little influence because of the distribution of the vertices.

I didn't look at the other area calculator but I would guess if you're seeing discrepancies that this code is the less accurate version.

bshields
Hi bshields,Thank you for the very detailed reply. Now, I have a better understanding of how this function works and what it's weaknesses are. My polygons are neighborhoods in one U.S. East Coast city. On average, the polygons are from 1-3 sq. kms. So, hopefully the issues that you outlined won't cause big inaccuracies. Unfortunately, the area calculator doesn't provide the function employed So, I would have to input each polygon separately and this would be a pain. Do you think that the hong007's function is reasonably accurate at the city level? Thank you!
Laxmidi
@Laxmidi Only the first thing I mentioned is related to polygon size. The more important problem is the method of averaging the scale between the two axes. This will give correct results for a square, but irregular shaped polygons will have incorrect area, with the worst cases being those that are very oblong and oriented along one of the axes. If you want to do this right, I think you have to project these points to a projected coordinate system like UTM and calculate the area in that space.
bshields