Unable to union two AGSEnvelope with unionWithEnvelope

2415
2
12-30-2012 11:02 PM
PrasadJ
New Contributor II
Please help to do Union of Multiple polygons. Below is my code.


FeatureSet contains Polygon Feature

AGSMutableEnvelope    * env2;
        if([featureSet.features count]>0){
            for (int i=0 ; i<[featureSet.features count]; i++ ) {
                graphic=[featureSet.features objectAtIndex:i];
                if (i==0)
                   
                    env2=(AGSMutableEnvelope *)graphic.geometry.envelope;
               
                else
                {
                    AGSEnvelope *env1=graphic.geometry.envelope;
                    //[env2 unionWithEnvelope:<#(AGSEnvelope *)#>]
                    @try {
                        [env2 unionWithEnvelope:env1];
                    }
                    @catch (NSException *exception) {
                        NSLog(@"%@",[exception description]);
                    }
                                  
                }
                graphic.symbol=fillSymbol;
               
                //graphic.infoTemplateDelegate=self.projectInfoTemp;
                [self.graphicsProjectQryLayer addGraphic:graphic];
                
            }
0 Kudos
2 Replies
Nicholas-Furness
Esri Regular Contributor
You cannot just cast an AGSEnvelope to AGSMutableEnvelope (see here).

Try something like this:

AGSMutableEnvelope * env2 = nil;
if([featureSet.features count]>0)
{
  for (int i=0 ; i<[featureSet.features count]; i++ )
  {
    graphic=[featureSet.features objectAtIndex:i];
    if (env2 == nil) 
    {
      // graphic.geometry.envelope is an AGSEnvelope
      // We need to derive a new AGSMutableEnvelope from it.
      env2=[graphic.geometry.envelope mutableCopy];
    }
    else
    {
      // This is not the first graphic we've come across, so we'll
      // extend the working envelope (env2) to include it
      [env2 unionWithEnvelope:graphic.geometry.envelope];
    }
    // Add the graphic to our graphics layer
    graphic.symbol=fillSymbol;
    [self.graphicsProjectQryLayer addGraphic:graphic];
  }
  // env2 will now contain all the graphics we just looped over.

  // Refresh the graphics layer
  [self.graphicsProjectQryLayer dataChanged];
}


This is typical with Cocoa development and Mutable versions of classes. You can generally cast a Mutable object to its non-mutable form (typically it's parent class, so an NSMutableArray is a NSArray), but the other way around requires a new object of some sort.
0 Kudos
PrasadJ
New Contributor II
Thank you nfurness
0 Kudos