Select to view content in your preferred language

Area And Perimeter

5188
26
10-09-2010 10:31 AM
NathalieNeagle
Regular Contributor
I'm using this example and it works great  in the example with projection 102100 but I'm using projection 4326 WGS 1984 esridecimaldegrees and my numbers are way off can someone tell what I need to change
buthttp://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AreasAndLengths

I think it is here  but not sure

   private void GeometryService_AreasAndLengthsCompleted(object sender, AreasAndLengthsEventArgs args)
        {
            //TODO: Geometry service not returning correct values, area unit seems invalid, may need to use data in 
            //planar coordinate space

            // convert results from meters into miles and sq meters into sq miles for our display
            double miles = args.Results.Lengths[0] * 0.0006213700922;
            double sqmi = Math.Abs(args.Results.Areas[0]) * 0.0000003861003;
            ResponseTextBlock.Text = String.Format("Polygon area: {0} sq. miles\nPolygon perimeter: {1} miles.", Math.Round(sqmi, 3), Math.Round(miles, 3));
        }
0 Kudos
26 Replies
JenniferNery
Esri Regular Contributor
These are the changes I would make to the Area and Perimeter sample, if you were to use the Editor.

XAML-code:
(Under Grid.Resources)
<esri:Editor x:Key="MyEditor" EditCompleted="Editor_EditCompleted" Map="{Binding ElementName=MyMap}" LayerIDs="MyGraphicsLayer"/>

(Somewhere in the Grid)
<Button x:Name="AddBtn" Content="Add" DataContext="{StaticResource MyEditor}" Command="{Binding Add}" CommandParameter="{StaticResource DefaultFillSymbol}"/>


In the code-behind, make geometryService a private member. Remove code that uses Draw and code that adds to your GraphicsLayer.  You won't need them since the Editor Add will do that for you and it will correct the polygon no matter what direction you have drawn it.
Use this instead:
GeometryService geometryService;
        private void Editor_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            if (e.Action == Editor.EditAction.Add)
            {
                foreach (var edit in e.Edits)
                {
                    geometryService.AreasAndLengthsAsync(new List<Graphic>(){edit.Graphic});
                    break;
                }
            }
        }


Also, move this code to the constructor:
 geometryService =
                      new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
            geometryService.AreasAndLengthsCompleted += GeometryService_AreasAndLengthsCompleted;
            geometryService.Failed += GeometryService_Failed;
0 Kudos
dotMorten_esri
Esri Notable Contributor
To get a fairly accurate result, you are better off projection your geometry to an Equal Area Projection (for instance 54004) and use that for the area calculation. Most of the projections in use are all introducing enourmous amount of distortion, especially with respect to areas and lengths. So using a projection where relative area is not distorted is one approach often used.
0 Kudos
ChristineZeller
Occasional Contributor
Sorry to restore this one. I went away for awhile and just return to try and solve this.

First I must say: THanks to Jennifer and Morton for helping.

Jennifer is right that if the polygon is drawn in different directions it will produce a negitive or a positive number but I don't think this is my problem and by the sounds of it I don't think it is Nathalie either.

My number is extremely small like a decimal degree number and I'm thinking this has to do with the fact that my maps spatial ref is 4326. So I was trying to do what morton suggest but FIddler keeps return my area in decimal degrees. I've changed it to 102100, 54004, 2230 I've tried it all.

See my attached fiddler for my results:

You can see I'm trying to overwrite the spatial projection of the polygon drawn in my code and this is showing up in fiddler but the polygon "ring" x,y points never change they seem like they are always in Decimal Degrees.

What do I need to change to get them out of Decimal Degrees?

 #region Cauculate Area Poly
        ////Calculate Area

        private void MyDrawObject_DrawBegin(object sender, EventArgs args)
        {
            GraphicsLayer graphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();
        }

        private void MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
        {
            ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
            polygon.SpatialReference = new SpatialReference(54004);  
Graphic graphic = new Graphic()
            {
                Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
                Geometry = polygon,
            };

            GeometryService geometryService =
                new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
            geometryService.AreasAndLengthsCompleted += GeometryService_AreasAndLengthsCompleted;
            geometryService.Failed += GeometryService_Failed;

            GraphicsLayer graphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.Graphics.Add(graphic);

            List<Graphic> graphicList = new List<Graphic>();
            graphicList.Add(graphic);


            geometryService.AreasAndLengthsAsync(graphicList);

 
        }

0 Kudos
JollyJacob
Emerging Contributor
Hi All,

We are having a similar problem with measure distances in decimal degree maps. Zero values are returned no matter which direction we draw the lines / polygons in. Measurements are returned correctly in maps with spatial reference 32643 and metres as units. But with maps with SR 4326 and decimal degrees as units, zero values are returned. It would seem that not only the SR will have to be changed on the fly, the units will have to be changed. But this is not practical when a site has different map services in different SRs. Any way to fix this?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
 
polygon.SpatialReference = new SpatialReference(54004);  
 


Setting the spatial reference by code is not enough because by doing this, the coordinates of the geometry don't change.
You have to project the coordinates by using a geometry service.

As a side note, I think that the equal area projection is 54034 and not 54004.
0 Kudos
ChristineZeller
Occasional Contributor


Setting the spatial reference by code is not enough because by doing this, the coordinates of the geometry don't change.
You have to project the coordinates by using a geometry service.



Thanks dbroux for the solution/heads up!

I see that there is this example out there: to project the coordinates by using a geometry service.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Project

The example is only projection one point and obviously the drawn area (polygon) or line could/will have several (undefined) number of points.  So I'm thinking I have to some how loop through the drawn feature and  project the coordinates but I'm lost. 

Any help.

Thanks
Chris
0 Kudos
DominiqueBroux
Esri Frequent Contributor

The example is only projection one point and obviously the drawn area (polygon) or line could/will have several (undefined) number of points.

The project geometry service is able to project any geometry type (and even any list of geometries), so you don't need to project point by point.

The global process should be this one:
1) Project your geometry (call ProjectAsync)
2) On ProjectCompleted : calculate the area/lenght (call AreasAndLenghtsAsync)
3) On AreasAndLenghtsAsyncCompleted : your measure is available

Hope this help.
0 Kudos