How to measure areas and lengths of geometry drawn on sketchlayer?

3000
8
08-02-2011 09:13 PM
AnoopMohandas
New Contributor
Hi all,

I'm new to ArcGis SDK, lately I have been trying to find the x-y coordinate, length and area of AGSPoint, AGSPolyline and AGSPolygon drawn on a sketchlayer.

The steps I did
1. Created an instance of AGSGeometryTaskService, initialized with server and set delegate.
2. Extraced the AGSGeometry information from sketchlayer.geometry.
3. Created an instance of AGSAreasAndLengthsParameters, set the units, assigned an array of Geometry objects and passed these parameters to areasAndLengthsWithParameters
4. The response obtained was not in the units specified in the parameters.

self.gst = [[[AGSGeometryServiceTask alloc]initWithURL:[NSURL URLWithString:kGeometryServerURL]]autorelease];
    
    self.gst.delegate = self;
    
    AGSGeometry* sketchGeometry = [[_sketchLayer.geometry copy] autorelease];
    
    //NSLog(@"%@",sketchGeometry);
    
    AGSAreasAndLengthsParameters *areaAndLengthParams = [[AGSAreasAndLengthsParameters alloc]init];
    
    areaAndLengthParams.areaUnit = AGSAreaUnitsSquareMeters;
    areaAndLengthParams.lengthUnit = AGSSRUnitMeter;
    areaAndLengthParams.polygons = [NSArray arrayWithObject:sketchGeometry];
    
    [self.gst areasAndLengthsWithParameters:areaAndLengthParams];
    
    [areaAndLengthParams release];


Any information on where I'm doing wrong is much appreciated .
0 Kudos
8 Replies
RickJones
Occasional Contributor II
Take a look at SketchLayer example code.

I modified the respondToGeomChanged event for my app:

//Get the sketch geometry
AGSGeometryEngine *geo = [AGSGeometryEngine defaultGeometryEngine];
AGSGeometry* sketchGeometry = [[_sketchLayer.geometry copy] autorelease];
double length = 0;
double area = 0;

        // there are only line and poly buttons in my version
switch (_sketchTools.selectedSegmentIndex) {
  case 0: // line
   length = [geo lengthOfGeometry:sketchGeometry];
   _measureText.text = [NSString stringWithFormat:@"Length: %f", length];
   break;
  case 1: // poly
   area = [geo areaOfGeometry:sketchGeometry];
   _measureText.text = [NSString stringWithFormat:@"Area: %f", area];
   break;
  default:
   _measureText.text = @"";
   break;
}


For some reason, the poly area shows negative if the points are drawn counter-clockwise.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Anoop,

You should use AGSGeometryEngine for all your geometric operations.

Regards,
Nimesh
0 Kudos
AnoopMohandas
New Contributor
Thank you Rick Jones and Nimesh for your help :D.

So both AGSGeometryServiceTask and AGSGeometryEngine does the same function, the later does it locally without any webservice. Sorry for the trouble again, the returned geometry information is in the spatial reference of the geometry. How can we convert these returned values into a particular unit:o?

Why is the area information given as negative, if the geometry is drawn in counter clockwise direction?

And also why did the GeometryServiceTask returned the information in the spatial reference even though I had specified a particular unit in the parameters:confused:?

-Anoop
0 Kudos
RickJones
Occasional Contributor II
If the area is negative, just multiply by -1.
0 Kudos
AnoopMohandas
New Contributor
Thank you Rick for the reply, but my concern is not the area coming in negative value .

I'm getting both area and length in the spatial reference  of the geometry( I guessed its in SR of the geometry, the docs say" if no  unit is specified, the units will be in the spatial reference of the  given geometry").

I tried AGSUnitsToUnits and AGSAreaUnitsToAreaUnits. But how to specify the fromUnits:confused:? Is there a way to specify the unit of SpatialReference?
0 Kudos
GarimaVyas
New Contributor
The AGSAreasAndLengthsParameters Class does allow you to set the areaUnit and lengthUnit. Is that what you are looking for?
http://help.arcgis.com/en/arcgismobile/10.0/apis/iOS/2.0/reference/interface_a_g_s_areas_and_lengths...
0 Kudos
NimeshJarecha
Esri Regular Contributor
Anoop,

Use AGSSketchGraphicsLayer::units or AGSSpatialReference:unit property.

For your previous questions..

How can we convert these returned values into a particular unit?
- You are on a right track. You can use ASGSpatialReferece's convertValue:fromUnit: or convertValue:toUnit: methods. But keep in mind that the conversion will be in linear unit not in square for area.

Why is the area information given as negative, if the geometry is drawn in counter clockwise direction?
- Yes, because geometry drawn in counter clockwise.

And also why did the GeometryServiceTask returned the information in the spatial reference even though I had specified a particular unit in the parameters?
- In which spatial reference your sketch layer was? Need more information...

Regards,
Nimesh
0 Kudos
AnoopMohandas
New Contributor
Thank you Garima,Nimesh.

But still I couldn't get it working. I'm working on ArcGIS server version:9.31 with Spatial Reference 4326 , does that support these operations?

I tried to obtain the unit of spatial reference by using [geometry.spatialReference unit] and [sketchlayer units] but it returned only "nan" which means i tried to convert angular units to linear units. The documentation is not sufficient for someone relatively new in GIS to get stuff working :(. Can you brief about the difference between AGSUnits and AGSSRUnit, any reference is much appreciated.

Documentation asks to simplify [<AGSGeometryEngine> simplifyGeometry:<geometry>], if geometry of sketchLayer is used, but the output was same.

Then I tried using [<AGSGeometryEngine> geodesicLengthOfGeometry:<geometry> inUnit:AGSSRUnitSurveyMile], this returned the length in miles 🙂 .

-Anoop
0 Kudos