Solved! Go to Solution.
private function unionGeoms(featureSet:FeatureSet):Geometry
{
var graphic:Graphic;
var retGeom:Geometry;
var mPoint:Multipoint = new Multipoint;
var poly:Polygon = new Polygon;
var mPoly:Polygon = new Polygon;
var mPolyL:Polyline = new Polyline;
var rType:String;
//Leonardo
//For each polygon, tests if exists another that contains it.
//If YES, the polygon will not included in union operation and it will added at polygonsToDiscard array.
var aGraphic:Graphic;
var polygonsToDiscard:Array = new Array;
for each (graphic in featureSet.features){
poly = graphic.geometry as Polygon;
if(graphic.geometry.type == "esriGeometryPolygon"){
for each (aGraphic in featureSet.features){
var aPoly:Polygon = aGraphic.geometry as Polygon;
if(aPoly.extent.contains(graphic.geometry) && (aPoly.extent.center.x != poly.extent.center.x || aPoly.extent.center.y != poly.extent.center.y)){
// The polygon will not included in union operation.
polygonsToDiscard.push(poly);
}
}
}
}
//Leonardo: End of tests for discard polygons.
for each (graphic in featureSet.features){
if(graphic.geometry.type == "esriGeometryPoint"){
mPoint.addPoint(graphic.geometry as MapPoint);
rType = "point";
}
if(graphic.geometry.type == "esriGeometryMultipoint"){
var mp:Multipoint = graphic.geometry as Multipoint
var pnts:MapPoint;
for (var p:int=0;p < mp.points.length; p++){
mPoint.addPoint(mp.points
);
}
rType = "point";
}
if(graphic.geometry.type == "esriGeometryPolygon"){
poly = graphic.geometry as Polygon;
//Leonardo
//Consider only the rings that not coincide with any polygon ring on polygonsToDiscard array.
var targetRings:Array = new Array;
for (var m:int = 0; m < poly.rings.length; m++){
var polygonToDiscard:Polygon;
var targetRing:Array = new Array;
var targetPolygon:Polygon = new Polygon(new Array(poly.rings), poly.spatialReference);
if (polygonsToDiscard.length > 0){
for each (polygonToDiscard in polygonsToDiscard){
var add:Boolean = true;
if (targetPolygon.extent.center.x == polygonToDiscard.extent.center.x && targetPolygon.extent.center.y == polygonToDiscard.extent.center.y) {
add = false;
break;
}
}
if(add){
targetRing[0] = m;
targetRing[1] = poly.rings;
targetRings.push(targetRing);
}
} else {
targetRing[0] = m;
targetRing[1] = poly.rings;
targetRings.push(targetRing);
}
}
//Leonardo: End of target rings selection.
//Leonardo: Loop code below was changed to use targetRings array.
for (var i2:int = targetRings.length - 1; i2 >=0; i2--){
var ringArray:Array = [];
for (var j1:int = 0; j1 < targetRings[i2][1].length; j1++){
var mp2:MapPoint = poly.getPoint(targetRings[i2][0],j1) as MapPoint;
mp2.spatialReference = poly.spatialReference;
ringArray.push(mp2);
}
mPoly.addRing(ringArray);
}
rType = "poly";
mPoly.spatialReference = poly.spatialReference;
}
if(graphic.geometry.type == "esriGeometryPolyline"){
var polyl:Polyline = graphic.geometry as Polyline;
for(var l:int=polyl.paths.length-1; l >= 0; l--){
var pathArray:Array = [];
for (var j2:int = 0; j2 < polyl.paths.length; j2++){
var mp3:MapPoint = polyl.getPoint(l,j2) as MapPoint;
mp3.spatialReference = polyl.spatialReference;
pathArray.push(mp3);
}
mPolyL.addPath(pathArray);
}
rType = "line";
}
if(graphic.geometry.type == "esriGeometryEnvelope"){
var ext:Extent = graphic.geometry as Extent;
var pA:Array = [];
pA.push(new MapPoint(ext.xmin,ext.ymin,ext.spatialReference));
pA.push(new MapPoint(ext.xmin,ext.ymax,ext.spatialReference));
pA.push(new MapPoint(ext.xmax,ext.ymax,ext.spatialReference));
pA.push(new MapPoint(ext.xmax,ext.ymin,ext.spatialReference));
pA.push(new MapPoint(ext.xmin,ext.ymin,ext.spatialReference));
mPoly.addRing(pA);
rType = "poly";
}
}
var graphic2:Graphic = new Graphic();
var sfs:SimpleFillSymbol = new SimpleFillSymbol();
sfs.color = 0x0000FF;
sfs.alpha = 0.2;
var sms:SimpleMarkerSymbol = new SimpleMarkerSymbol();
sms.color = 0x0000FF;
sms.style = SimpleMarkerSymbol.STYLE_CIRCLE;
sms.size = 8;
sfs.alpha = 0.4;
var sls:SimpleLineSymbol = new SimpleLineSymbol();
sls.color = 0x0000FF;
sls.style = SimpleLineSymbol.STYLE_SOLID;
sls.width = 1;
sls.alpha = 0.4;
switch(rType){
case "point":{
graphic2.geometry = mPoint;
graphic2.symbol = sms;
graphicsLayerBuffer.clear();
graphicsLayerBuffer.add(graphic2);
retGeom = mPoint;
break;
}
case "poly":{
graphic2.geometry = mPoly;
graphic2.symbol = sfs;
graphicsLayerBuffer.clear();
graphicsLayerBuffer.add(graphic2);
retGeom = mPoly;
break;
}
case "line":{
graphic2.geometry = mPolyL;
graphic2.symbol = sls;
graphicsLayerBuffer.clear();
graphicsLayerBuffer.add(graphic2);
retGeom = mPolyL;
break;
}
}
return retGeom;
}