Hello,
I have zip codes in my SalesArea and I need to merge(union) the zips codes of a SalesArea at the time displaying so that it will have only one boundry line.
I create SalesArea in "InMemoryFeatureLayer" and display them.
Union of features works as in the sample code. I also have developed using provided sampe code in my project and it works. But developing almost exactly the same to merge(union) for my own zip codes does not work.
e.g. this is from sample code that i tried in my project and it works.
AreaBaseShape targetShape = (AreaBaseShape)saLayer.InternalFeatures["AreaShape1"].GetShape();
saLayer.Open();
saLayer.EditTools.BeginTransaction();
saLayer.EditTools.Union("AreaShape2", targetShape);
saLayer.EditTools.Delete("AreaShape1");
saLayer.EditTools.CommitTransaction();
saLayer.Close();
And this is the same development for merging (union) of my zip codes ( features). It does not work.
AreaBaseShape targetSA = (AreaBaseShape)saLayer.InternalFeatures["08812"].GetShape();
saLayer.Open();
saLayer.EditTools.BeginTransaction();
saLayer.EditTools.Union("08846", targetSA);
saLayer.EditTools.Delete("08812");
saLayer.EditTools.CommitTransaction();
saLayer.Close();
Here is my full source code. Would you please let me know what is that I need to modify to make it work?
Please let me know to (shwe.ko@cegedim.com) if you need shape file.
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
namespace NJTerritory2
{
public partial class WebForm17 : System.Web.UI.Page
{
Dictionary<string, ArrayList> g_SalesAreaZips = new Dictionary<string, ArrayList>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Map1.MapUnit = GeographyUnit.DecimalDegree;
Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
Map1.MapTools.MouseCoordinate.Enabled = true;
ShapeFileFeatureSource.BuildIndexFile(@"C:\temp\WebSites\ThinkGeo\Data\NJ\tl_2010_34_zcta510.shp", @"C:\temp\WebSites\ThinkGeo\Data\NJ\tl_2010_34_zcta510.idx", "ZCTA5CE10", "\\w", BuildIndexMode.DoNotRebuild);
FillSalesAreas();
//===============================
ShapeFileFeatureLayer stateLayer = new ShapeFileFeatureLayer(@"C:\temp\WebSites\ThinkGeo\Data\NJ\tl_2010_34_zcta510.shp", @"C:\temp\WebSites\ThinkGeo\Data\NJ\tl_2010_34_zcta510.idx");
stateLayer.Name = "StateLayer";
stateLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Park1;
stateLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;//20 is zoomest
//stateLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("ZCTA5CE10", "Arial", 8, DrawingFontStyles.Italic, GeoColor.StandardColors.Black, 3, 3);
Map1.StaticOverlay.Layers.Add(stateLayer);
//=================================
LayerOverlay saOverlay = new LayerOverlay();
saOverlay.Name = "SAOverlay";
foreach (string saName in g_SalesAreaZips.Keys)
CreateSalesAreaLayer(saOverlay, saName);
saOverlay.IsBaseOverlay = false;
Map1.CustomOverlays.Add(saOverlay);
foreach (string saName in g_SalesAreaZips.Keys)
MergeThem(saName);
//================================
Map1.CurrentExtent = GetFullExtent(stateLayer);
}
}
private void CreateSalesAreaLayer(LayerOverlay saOverlay, string saName)
{
if (g_SalesAreaZips.ContainsKey(saName))
{
InMemoryFeatureLayer saZipsLayer = new InMemoryFeatureLayer();
saZipsLayer.Name = saName;
//saZipsLayer.Open();
//saZipsLayer.Columns.Add(new FeatureSourceColumn("ZCTA5CE10"));
//saZipsLayer.Close();
saZipsLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Brown), GeoColor.FromArgb(150, GeoColor.StandardColors.Aqua));
saZipsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
//saZipsLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("ZCTA5CE10", "Arial", 8, DrawingFontStyles.Italic, GeoColor.StandardColors.Black, 3, 3);
//==================
BaseShape areaShape1 = new RectangleShape(-75, 40, -74.75, 39.75);
areaShape1.Id = "AreaShape1";
BaseShape areaShape2 = new RectangleShape(-74.85, 39.85, -74.5, 39.6);
areaShape2.Id = "AreaShape2";
saZipsLayer.InternalFeatures.Add("AreaShape1", new Feature(areaShape1));
saZipsLayer.InternalFeatures.Add("AreaShape2", new Feature(areaShape2));
//==================
Collection<string> columnNames = new Collection<string>();
columnNames.Add("ZCTA5CE10");
LayerOverlay stateOverlay = (LayerOverlay)Map1.StaticOverlay;//stateOverlay
ShapeFileFeatureLayer stateLayer = (ShapeFileFeatureLayer)stateOverlay.Layers[0];
stateLayer.Open();
Collection<Feature> features = stateLayer.FeatureSource.GetAllFeatures(columnNames);
stateLayer.Close();
ArrayList zips = g_SalesAreaZips[saName];
foreach (string zip in zips)
{
foreach (Feature f in features)
{
if (f.ColumnValues["ZCTA5CE10"] != zip) continue;
if (!saZipsLayer.InternalFeatures.Contains(f))
saZipsLayer.InternalFeatures.Add(zip, f);
}
}
saOverlay.Layers.Add(saName, saZipsLayer);
}
}
private void MergeThem(string saName)
{
InMemoryFeatureLayer saLayer = null;
LayerOverlay saOverlay = (LayerOverlay)Map1.CustomOverlays[0];//SAOverlay
try
{
saLayer = (InMemoryFeatureLayer)saOverlay.Layers[saName];
}
catch (Exception ex)
{
//does nothing
}
AreaBaseShape targetShape = (AreaBaseShape)saLayer.InternalFeatures["AreaShape1"].GetShape();
saLayer.Open();
saLayer.EditTools.BeginTransaction();
saLayer.EditTools.Union("AreaShape2", targetShape);
saLayer.EditTools.Delete("AreaShape1");
saLayer.EditTools.CommitTransaction();
saLayer.Close();
//---------------
AreaBaseShape targetSA = (AreaBaseShape)saLayer.InternalFeatures["08812"].GetShape();
saLayer.Open();
saLayer.EditTools.BeginTransaction();
saLayer.EditTools.Union("08846", targetSA);
saLayer.EditTools.Delete("08812");
saLayer.EditTools.CommitTransaction();
saLayer.Close();
saOverlay.Redraw();
}
private void FillSalesAreas()
{
ArrayList zips = new ArrayList();
zips.Add("08812");
zips.Add("08846");
g_SalesAreaZips.Add("SALESAREA1", zips);
}
private RectangleShape GetFullExtent(Layer layer)
{
Collection<BaseShape> rectangleShapes = new Collection<BaseShape>();
layer.Open();
if (layer.HasBoundingBox == true)
rectangleShapes.Add(layer.GetBoundingBox());
return ExtentHelper.GetBoundingBoxOfItems(rectangleShapes);
}
}
}