# Urgent!

**URL:** <https://community.thinkgeo.com/t/urgent/5998>\
**Category:** WebForms\
**Created:** [November 4, 2011, 3:11pm UTC](https://community.thinkgeo.com/t/urgent/5998 "2011-11-04T15:11:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-marie](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jean-marie](https://community.thinkgeo.com/u/Jean-marie)\
**Post date:** [November 4, 2011, 3:11pm UTC](https://community.thinkgeo.com/t/urgent/5998/1 "2011-11-04T15:11:33Z")

</div>

I&nbsp;would&nbsp;like&nbsp;to&nbsp;create&nbsp;one&nbsp;shape&nbsp;per&nbsp;sales&nbsp;rep&nbsp;that&nbsp;is&nbsp;made&nbsp;of&nbsp;multiple&nbsp;zip.&nbsp;&nbsp;I&nbsp;think&nbsp;that&nbsp;the&nbsp;code&nbsp;is&nbsp;OK&nbsp;but&nbsp;I&nbsp;am&nbsp;not&nbsp;that&nbsp;sure...

anyway&nbsp;when&nbsp;I&nbsp;execute&nbsp;it&nbsp;I&nbsp;got&nbsp;this&nbsp;exception&nbsp;on&nbsp;the&nbsp;:&nbsp;unionMultipolygonShape&nbsp;=&nbsp;MultipolygonShape.Union(areaBaseShapes);

statement.&nbsp;&nbsp;The&nbsp;selectedZipFeatures&nbsp;is&nbsp;made&nbsp;of&nbsp;643&nbsp;features.&nbsp;(number&nbsp;of&nbsp;zips&nbsp;for&nbsp;a&nbsp;&nbsp;single&nbsp;sales&nbsp;rep).

thanks&nbsp;for&nbsp;your&nbsp;help!

``

``

`

  foreach (DictionaryEntry s in _repNameList)

                    {

                        inMemoryFeatureLayer_Zips_Colorization.FeatureSource.Open();

                        selectedZipFeatures = inMemoryFeatureLayer_Zips_Colorization.FeatureSource.GetFeaturesByColumnValue(RequestedColumns_RepPerZip[0].ColumnName.ToString(), s.Value.ToString());

                        inMemoryFeatureLayer_Zips_Colorization.FeatureSource.Close();

                        areaBaseShapes = new Collection<AreaBaseShape>();

                        if (selectedZipFeatures.Count > 0)

                        {

                            foreach (Feature feature in selectedZipFeatures)

                            {

                                areaBaseShapes.Add((AreaBaseShape)feature.GetShape());

                            }

                            unionMultipolygonShape = MultipolygonShape.Union(areaBaseShapes);

                            if (unionMultipolygonShape.Polygons.Count > 0)

                                inMemoryFeatureLayer_Zips_Colorization_union.InternalFeatures.Add(new Feature(unionMultipolygonShape));

                        }

                    }

`

&nbsp;

&nbsp;

&nbsp;System.OutOfMemoryException&nbsp;was&nbsp;unhandled&nbsp;by&nbsp;user&nbsp;code

&nbsp;&nbsp;Message=Exception&nbsp;of&nbsp;type&nbsp;'System.OutOfMemoryException'&nbsp;was&nbsp;thrown.  
&nbsp;&nbsp;Source=NetTopologySuite  
&nbsp;&nbsp;StackTrace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Noding.SegmentNodeList.CreateSplitEdge(SegmentNode&nbsp;ei0,&nbsp;SegmentNode&nbsp;ei1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Noding.SegmentNodeList.AddSplitEdges(IList&nbsp;edgeList)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Noding.SegmentString.GetNodedSubstrings(IList&nbsp;segStrings,&nbsp;IList&nbsp;resultEdgelist)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Noding.SegmentString.GetNodedSubstrings(IList&nbsp;segStrings)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Noding.MCIndexNoder.GetNodedSubstrings()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferBuilder.ComputeNodedEdges(IList&nbsp;bufferSegStrList,&nbsp;IPrecisionModel&nbsp;precisionModel)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferBuilder.Buffer(IGeometry&nbsp;g,&nbsp;Double&nbsp;distance)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.BufferOriginalPrecision()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.ComputeGeometry()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.GetResultGeometry(Double&nbsp;distance)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.Buffer(IGeometry&nbsp;g,&nbsp;Double&nbsp;distance)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;GisSharpBlog.NetTopologySuite.Geometries.Geometry.Buffer(Double&nbsp;distance)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.AreaBaseShape.Union(IEnumerable`1&nbsp;areaShapes)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;MapTest.WebForm1.Page\_Load(Object&nbsp;sender,&nbsp;EventArgs&nbsp;e)&nbsp;in&nbsp;C:\Users\a09zmzz\Documents\Visual&nbsp;Studio&nbsp;2010\Projects\MapTest\MapTest\WebForm1.aspx.cs:line&nbsp;238  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr&nbsp;fp,&nbsp;Object&nbsp;o,&nbsp;Object&nbsp;t,&nbsp;EventArgs&nbsp;e)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object&nbsp;sender,&nbsp;EventArgs&nbsp;e)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;System.Web.UI.Control.OnLoad(EventArgs&nbsp;e)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;System.Web.UI.Control.LoadRecursive()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;System.Web.UI.Page.ProcessRequestMain(Boolean&nbsp;includeStagesBeforeAsyncPoint,&nbsp;Boolean&nbsp;includeStagesAfterAsyncPoint)  
&nbsp;&nbsp;InnerException:&nbsp;  
&nbsp;

---

<div class="post-metadata">

**Author:** ![Jean-marie](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jean-marie](https://community.thinkgeo.com/u/Jean-marie)\
**Post date:** [November 4, 2011, 3:12pm UTC](https://community.thinkgeo.com/t/urgent/5998/2 "2011-11-04T15:12:32Z")

</div>

Better&nbsp;code&nbsp;view&nbsp;  
&nbsp;&nbsp;  
&nbsp;`                    foreach (DictionaryEntry s in _repNameList)                    {                        inMemoryFeatureLayer_Zips_Colorization.FeatureSource.Open();                        selectedZipFeatures = inMemoryFeatureLayer_Zips_Colorization.FeatureSource.GetFeaturesByColumnValue(RequestedColumns_RepPerZip[0].ColumnName.ToString(), s.Value.ToString());                        inMemoryFeatureLayer_Zips_Colorization.FeatureSource.Close();                        areaBaseShapes = new Collection<AreaBaseShape>();                        if (selectedZipFeatures.Count > 0)                        {                            foreach (Feature feature in selectedZipFeatures)                            {                                areaBaseShapes.Add((AreaBaseShape)feature.GetShape());                            }                            unionMultipolygonShape = MultipolygonShape.Union(areaBaseShapes);                            if (unionMultipolygonShape.Polygons.Count > 0)                                inMemoryFeatureLayer_Zips_Colorization_union.InternalFeatures.Add(new Feature(unionMultipolygonShape));                        }                    } `

---

<div class="post-metadata">

**Author:** ![Ethan](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ethan/32/19964_2.png) [@Ethan](https://community.thinkgeo.com/u/Ethan)\
**Post date:** [November 4, 2011, 3:28pm UTC](https://community.thinkgeo.com/t/urgent/5998/3 "2011-11-04T15:28:32Z")

</div>

Jean-marie,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Are&nbsp;you&nbsp;necessary&nbsp;to&nbsp;union&nbsp;them?&nbsp;Could&nbsp;you&nbsp;try&nbsp;to&nbsp;find&nbsp;some&nbsp;other&nbsp;solution&nbsp;in&nbsp;your&nbsp;scenario?&nbsp;  
&nbsp;&nbsp;  
&nbsp;The&nbsp;union&nbsp;operation&nbsp;may&nbsp;have&nbsp;some&nbsp;issue&nbsp;if&nbsp;the&nbsp;shape&nbsp;data&nbsp;not&nbsp;so&nbsp;standard,&nbsp;and&nbsp;we&nbsp;cannot&nbsp;change&nbsp;it&nbsp;because&nbsp;this&nbsp;part&nbsp;we&nbsp;using&nbsp;the&nbsp;3rd&nbsp;part&nbsp;library.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Regards,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Don

---

<div class="post-metadata">

**Author:** ![Jean-marie](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jean-marie](https://community.thinkgeo.com/u/Jean-marie)\
**Post date:** [November 4, 2011, 3:37pm UTC](https://community.thinkgeo.com/t/urgent/5998/4 "2011-11-04T15:37:58Z")

</div>

Well&nbsp;I&nbsp;have&nbsp;to&nbsp;create&nbsp;a&nbsp;shape&nbsp;made&nbsp;of&nbsp;ZIP5&nbsp;codes…&nbsp;how&nbsp;should&nbsp;I&nbsp;do&nbsp;that???&nbsp;&nbsp;My&nbsp;expectation&nbsp;was&nbsp;based&nbsp;on&nbsp;the&nbsp;fact&nbsp;that&nbsp;ThinkGeo&nbsp;was&nbsp;providing&nbsp;a&nbsp;solution&nbsp;to&nbsp;handle&nbsp;this&nbsp;kind&nbsp;of&nbsp;problem!&nbsp;&nbsp;isn’t&nbsp;it?&nbsp;  
&nbsp;&nbsp;  
&nbsp;Any&nbsp;ideas&nbsp;on&nbsp;how&nbsp;to&nbsp;make&nbsp;this&nbsp;thing&nbsp;to&nbsp;work?&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;for&nbsp;your&nbsp;help.

---

<div class="post-metadata">

**Author:** ![Val](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/v/e56c9b/32.png) [@Val](https://community.thinkgeo.com/u/Val)\
**Post date:** [November 4, 2011, 3:38pm UTC](https://community.thinkgeo.com/t/urgent/5998/5 "2011-11-04T15:38:32Z")

</div>

Jean&nbsp;Marie,

&nbsp;&nbsp;One&nbsp;thing&nbsp;I&nbsp;want&nbsp;to&nbsp;make&nbsp;sure&nbsp;first.&nbsp;Are&nbsp;you&nbsp;using&nbsp;the&nbsp;same&nbsp;method&nbsp;for&nbsp;unioning&nbsp;polygons&nbsp;as&nbsp;you&nbsp;can&nbsp;see&nbsp;in&nbsp;those&nbsp;two&nbsp;Code&nbsp;Community&nbsp;samples,&nbsp;_Dissolve&nbsp;on&nbsp;Shapefile&nbsp;_and&nbsp;_Dissolve&nbsp;with&nbsp;Statistics_?

[wiki.thinkgeo.com/wiki/Map\_Suite\_We...\_Shapefile](http://wiki.thinkgeo.com/wiki/Map_Suite_Web_Edition_Spatial_Functions_Samples#Dissolve_on_Shapefile)

[wiki.thinkgeo.com/wiki/Map\_Suite\_We...Statistics](http://wiki.thinkgeo.com/wiki/Map_Suite_Web_Edition_Spatial_Functions_Samples#Dissolve_with_Statistics)

&nbsp;In&nbsp;those&nbsp;samples,&nbsp;you&nbsp;are&nbsp;going&nbsp;to&nbsp;find&nbsp;a&nbsp;fast&nbsp;and&nbsp;reliable&nbsp;way&nbsp;to&nbsp;union&nbsp;polygons&nbsp;together.&nbsp;The&nbsp;end&nbsp;result&nbsp;is&nbsp;in&nbsp;a&nbsp;shapefile&nbsp;but&nbsp;this&nbsp;is&nbsp;basically&nbsp;the&nbsp;same&nbsp;method&nbsp;in&nbsp;unioning&nbsp;as&nbsp;for&nbsp;InMemoryFeatureLayer.&nbsp;If&nbsp;you&nbsp;are&nbsp;still&nbsp;having&nbsp;problem,&nbsp;I&nbsp;would&nbsp;suggest&nbsp;you&nbsp;send&nbsp;us&nbsp;your&nbsp;zip&nbsp;code&nbsp;shape&nbsp;and&nbsp;we&nbsp;will&nbsp;examine&nbsp;it.&nbsp;Thank&nbsp;you.

&nbsp;

---

<div class="post-metadata">

**Author:** ![Jean-marie](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jean-marie](https://community.thinkgeo.com/u/Jean-marie)\
**Post date:** [November 8, 2011, 1:10pm UTC](https://community.thinkgeo.com/t/urgent/5998/6 "2011-11-08T13:10:27Z")

</div>

Hi,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;was&nbsp;able&nbsp;to&nbsp;make&nbsp;my&nbsp;function&nbsp;to&nbsp;work&nbsp;well&nbsp;but&nbsp;the&nbsp;response&nbsp;time&nbsp;is&nbsp;not&nbsp;good…&nbsp;&nbsp;for&nbsp;instance&nbsp;if&nbsp;you&nbsp;need&nbsp;to&nbsp;union&nbsp;let&nbsp;say&nbsp;10&nbsp;US&nbsp;states&nbsp;(10&nbsp;features)&nbsp;  
&nbsp;it&nbsp;takes&nbsp;about&nbsp;15&nbsp;seconds&nbsp;to&nbsp;complete!&nbsp;&nbsp;I&nbsp;ran&nbsp;for&nbsp;6&nbsp;hours&nbsp;and&nbsp;generate&nbsp;41&nbsp;shapes&nbsp;(one&nbsp;per&nbsp;sales&nbsp;rep)&nbsp;each&nbsp;shape&nbsp;is&nbsp;made&nbsp;of&nbsp;up&nbsp;to&nbsp;600&nbsp;zips&nbsp;shapes.&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;really&nbsp;need&nbsp;to&nbsp;find&nbsp;a&nbsp;better&nbsp;way&nbsp;to&nbsp;solve&nbsp;this&nbsp;problem!&nbsp;&nbsp;&nbsp;the&nbsp;positive&nbsp;side&nbsp;is&nbsp;that&nbsp;the&nbsp;colorized&nbsp;InMemoryFeatureLayer&nbsp;where&nbsp;there&nbsp;is&nbsp;one&nbsp;shape&nbsp;zip&nbsp;is&nbsp;about&nbsp;831MB…&nbsp;now&nbsp;the&nbsp;InMemoryFeatureLayer&nbsp;with&nbsp;only&nbsp;41&nbsp;shapes&nbsp;is&nbsp;95MB!&nbsp;much&nbsp;better&nbsp;to&nbsp;load.&nbsp;  
&nbsp;&nbsp;  
&nbsp;any&nbsp;advice&nbsp;on&nbsp;how&nbsp;to&nbsp;optimize&nbsp;the&nbsp;union&nbsp;operation?&nbsp;  
&nbsp;&nbsp;  
&nbsp;thanks&nbsp;a&nbsp;lot.&nbsp;  
&nbsp;&nbsp;  
&nbsp;jm&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;my&nbsp;code:&nbsp;  
&nbsp;&nbsp;  
&nbsp;`  public InMemoryFeatureLayer unionizedShapes(Collection<FeatureSourceColumn> RequestedColumns, InMemoryFeatureLayer inMemoryFeatureLayer_in, SortedList nameList, string colKey)        {            Collection<Feature> selectedFeatures;            Collection<AreaBaseShape> areaBaseShapes;            MultipolygonShape unionMultipolygonShape;            InMemoryFeatureLayer inMemoryFeatureLayer_out = new InMemoryFeatureLayer();            foreach (DictionaryEntry s in nameList)            {                inMemoryFeatureLayer_in.FeatureSource.Open();                selectedFeatures = inMemoryFeatureLayer_in.FeatureSource.GetFeaturesByColumnValue(RequestedColumns[0].ColumnName.ToString(), s.Value.ToString());                inMemoryFeatureLayer_in.FeatureSource.Close();                areaBaseShapes = new Collection<AreaBaseShape>();                if (selectedFeatures.Count > 0)                {                    foreach (Feature feature in selectedFeatures)                    {                        areaBaseShapes.Add((AreaBaseShape)feature.GetShape());                    }                    unionMultipolygonShape = PolygonShape.Union(areaBaseShapes);                    if (unionMultipolygonShape.Polygons.Count > 0)                    {                        Feature f = new Feature(unionMultipolygonShape);                        f.ColumnValues.Add(colKey, selectedFeatures[0].ColumnValues[colKey].ToString());                        inMemoryFeatureLayer_out.InternalFeatures.Add(f);                    }                }            }            return (inMemoryFeatureLayer_out);        }`

---

<div class="post-metadata">

**Author:** ![Jean-marie](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jean-marie](https://community.thinkgeo.com/u/Jean-marie)\
**Post date:** [November 9, 2011, 11:57am UTC](https://community.thinkgeo.com/t/urgent/5998/7 "2011-11-09T11:57:40Z")

</div>

Hi&nbsp;Found&nbsp;a&nbsp;way&nbsp;to&nbsp;go&nbsp;faster&nbsp;and&nbsp;it&nbsp;is&nbsp;working!&nbsp;  
&nbsp;&nbsp;  
&nbsp;`   public InMemoryFeatureLayer unionizedShapes(Collection<FeatureSourceColumn> RequestedColumns, InMemoryFeatureLayer inMemoryFeatureLayer_in, SortedList nameList, string colKey)        {            InMemoryFeatureLayer inMemoryFeatureLayer_out = new InMemoryFeatureLayer();            Parallel.ForEach(nameList.Cast<DictionaryEntry>(), s =>            {                              inMemoryFeatureLayer_in.FeatureSource.Open();                Collection<Feature> selectedFeatures = inMemoryFeatureLayer_in.FeatureSource.GetFeaturesByColumnValue(RequestedColumns[0].ColumnName.ToString(), s.Value.ToString());                inMemoryFeatureLayer_in.FeatureSource.Close();                Collection<AreaBaseShape> areaBaseShapes; areaBaseShapes = new Collection<AreaBaseShape>();                if (selectedFeatures.Count > 0)                {                    foreach (Feature feature in selectedFeatures)                    {                        areaBaseShapes.Add((AreaBaseShape)feature.GetShape());                    }                    MultipolygonShape unionMultipolygonShape = PolygonShape.Union(areaBaseShapes);                    if (unionMultipolygonShape.Polygons.Count > 0)                    {                        Feature f = new Feature(unionMultipolygonShape);                        f.ColumnValues.Add(colKey, selectedFeatures[0].ColumnValues[colKey].ToString());                        inMemoryFeatureLayer_out.InternalFeatures.Add(f);                    }                }              });            return (inMemoryFeatureLayer_out);        }`

---

<div class="post-metadata">

**Author:** ![Val](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/v/e56c9b/32.png) [@Val](https://community.thinkgeo.com/u/Val)\
**Post date:** [November 14, 2011, 8:17am UTC](https://community.thinkgeo.com/t/urgent/5998/8 "2011-11-14T08:17:30Z")

</div>

Jean&nbsp;Marie,

&nbsp;&nbsp;Yes.&nbsp;You&nbsp;are&nbsp;right.&nbsp;The&nbsp;most&nbsp;efficient&nbsp;way&nbsp;to&nbsp;union&nbsp;polygons&nbsp;together&nbsp;is&nbsp;to&nbsp;first&nbsp;add&nbsp;them&nbsp;into&nbsp;a&nbsp;collection&nbsp;and&nbsp;then&nbsp;use&nbsp;the&nbsp;static&nbsp;function&nbsp;Union&nbsp;of&nbsp;PolygonShape&nbsp;to&nbsp;union&nbsp;them&nbsp;all&nbsp;in&nbsp;one&nbsp;step.

&nbsp;

`//First, add all the polygons into a collectionCollection<AreaBaseShape> polygonShapes = new Collection<AreaBaseShape>();Collection<Feature> features = shapeFileFeatureLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);foreach (Feature feature in features){    AreaBaseShape polygonShape = (AreaBaseShape)feature.GetShape();    polygonShapes.Add(polygonShape);}//Second, uses the PolygonShape.Union static method to union all the shapes in one step.MultipolygonShape multiPolygonShape = PolygonShape.Union(polygonShapes);`
