# Editing rectangular polygons

**URL:** <https://community.thinkgeo.com/t/editing-rectangular-polygons/455>\
**Category:** WinForms\
**Created:** [June 3, 2009, 8:25am UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455 "2009-06-03T08:25:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Edwardson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@David\_Edwardson](https://community.thinkgeo.com/u/David_Edwardson)\
**Post date:** [June 3, 2009, 8:25am UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/1 "2009-06-03T08:25:03Z")

</div>

I&nbsp;have&nbsp;polygon&nbsp;shapes&nbsp;on&nbsp;my&nbsp;map&nbsp;that&nbsp;I&nbsp;want&nbsp;to&nbsp;have&nbsp;the&nbsp;user&nbsp;size&nbsp;(rectangles&nbsp;but&nbsp;not&nbsp;necessarily&nbsp;flat&nbsp;on&nbsp;the&nbsp;X/Y&nbsp;axis).&nbsp;&nbsp;Is&nbsp;there&nbsp;any&nbsp;way&nbsp;to&nbsp;set&nbsp;the&nbsp;edit&nbsp;overlay&nbsp;so&nbsp;that&nbsp;when&nbsp;I&nbsp;drag&nbsp;a&nbsp;control&nbsp;point&nbsp;on&nbsp;the&nbsp;corner&nbsp;the&nbsp;adjacent&nbsp;control&nbsp;points&nbsp;move&nbsp;so&nbsp;that&nbsp;the&nbsp;rectangular&nbsp;shape&nbsp;remains&nbsp;rather&nbsp;than&nbsp;just&nbsp;having&nbsp;the&nbsp;single&nbsp;control&nbsp;point&nbsp;move?

Thanks

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [June 4, 2009, 12:19am UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/2 "2009-06-04T00:19:31Z")

</div>

David,

This&nbsp;is&nbsp;an&nbsp;advanced&nbsp;requirement.&nbsp;We&nbsp;can&nbsp;achieve&nbsp;this&nbsp;by&nbsp;write&nbsp;our&nbsp;won&nbsp;EditInterativeOverlay&nbsp;logic.

&nbsp;Following&nbsp;is&nbsp;the&nbsp;code&nbsp;basically&nbsp;we&nbsp;write&nbsp;according&nbsp;to&nbsp;your&nbsp;requirement,&nbsp;and&nbsp;you&nbsp;can&nbsp;modify&nbsp;it&nbsp;if&nbsp;you&nbsp;want.  
&nbsp;  
`public class CustomerEditInteractiveOverlay : EditInteractiveOverlay    {        private ResizeMode resizeMode;        public CustomerEditInteractiveOverlay()        {        }        public ResizeMode ResizeMode        {            get { return resizeMode; }            set { resizeMode = value; }        }        protected override Feature AddVertexCore(Feature targetFeature, PointShape targetPointShape, double searchingTolerance)        {            return new Feature();        }        protected override IEnumerable<Feature> CalculateResizeControlPointsCore(Feature feature)        {            Collection<Feature> resizeControlPoints = new Collection<Feature>();                        PolygonShape polygonShape = feature.GetShape() as PolygonShape;            if (polygonShape != null)            {                foreach (Vertex vertex in polygonShape.OuterRing.Vertices)                {                    resizeControlPoints.Add(new Feature(vertex, feature.Id));                }            }            return resizeControlPoints;        }        protected override Feature ResizeFeatureCore(Feature sourceFeature, PointShape sourceControlPoint, PointShape targetControlPoint)        {            Feature editedFeature = new Feature();            PolygonShape polygonShape = sourceFeature.GetShape() as PolygonShape;                       int fixedPointIndex = GetFixedPointIndex(polygonShape, sourceControlPoint);            PointShape fixedPointShape = new PointShape(polygonShape.OuterRing.Vertices[fixedPointIndex]);            if (resizeMode == ResizeMode.OnePointFixed)            {                RectangleShape newRectangleShape = new LineShape(new Vertex[] { new Vertex(fixedPointShape), new Vertex(targetControlPoint) }).GetBoundingBox();                editedFeature = new Feature(newRectangleShape.GetWellKnownBinary(), sourceFeature.Id, sourceFeature.ColumnValues);                if (!string.Equals(polygonShape.GetBoundingBox().GetWellKnownText(), polygonShape.GetWellKnownText()))                {                    editedFeature = base.ResizeFeatureCore(sourceFeature, sourceControlPoint, targetControlPoint);                }            }            else            {                editedFeature = base.ResizeFeatureCore(sourceFeature, sourceControlPoint, targetControlPoint);            }            return editedFeature;        }        private int GetFixedPointIndex(PolygonShape sourcePolygonShape, PointShape sourceControlPointShape)        {            int index = 0;            for (int i = 0; i < sourcePolygonShape.OuterRing.Vertices.Count; i++)            {                Vertex vertex = sourcePolygonShape.OuterRing.Vertices[i];                if (Math.Abs(vertex.X - sourceControlPointShape.X) <= 10E-6 && Math.Abs(vertex.Y - sourceControlPointShape.Y) <= 10E-6)                {                    index = i;                    break;                }            }            int fixedPointIndex = 0;            if (index <= 2)            {                fixedPointIndex = index + 2;            }            else            {                fixedPointIndex = index - 2;            }            return fixedPointIndex;        }    }    public enum ResizeMode    {        Default = 0,        Standard = 1,        OnePointFixed = 2    }`

Based&nbsp;on&nbsp;this&nbsp;CustomerEditInteractiveOverlay,&nbsp;We&nbsp;can&nbsp;use&nbsp;it&nbsp;in&nbsp;the&nbsp;following&nbsp;way.

`  winformsMap1.MapUnit = GeographyUnit.DecimalDegree;            winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);            //ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"..\..\..\..\..\PostsData\Countries02.shp", ShapeFileReadWriteMode.ReadOnly);            //worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;            //worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;            //LayerOverlay worldOverlay = new LayerOverlay();            //worldOverlay.Layers.Add("WorldLayer", worldLayer);            //winformsMap1.Overlays.Add("WorldOverlay", worldOverlay);            CustomerEditInteractiveOverlay customerEditOverlay = new CustomerEditInteractiveOverlay();            customerEditOverlay.ResizeMode = ResizeMode.OnePointFixed;            winformsMap1.EditOverlay = customerEditOverlay;            winformsMap1.ZoomLevelSnapping = ZoomLevelSnappingMode.None;            winformsMap1.CurrentExtent = new RectangleShape(-180, 90, 180, -90);            winformsMap1.Refresh();`

Any&nbsp;queries&nbsp;just&nbsp;let&nbsp;me&nbsp;know.

&nbsp;  
Thanks.  
&nbsp;  
Yale.

&nbsp;

---

<div class="post-metadata">

**Author:** ![David\_Edwardson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@David\_Edwardson](https://community.thinkgeo.com/u/David_Edwardson)\
**Post date:** [June 5, 2009, 3:32pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/3 "2009-06-05T15:32:13Z")

</div>

Thanks.&nbsp;&nbsp;That&nbsp;put&nbsp;me&nbsp;on&nbsp;the&nbsp;trail.&nbsp;

---

<div class="post-metadata">

**Author:** ![James](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@James](https://community.thinkgeo.com/u/James)\
**Post date:** [June 5, 2009, 10:59pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/4 "2009-06-05T22:59:54Z")

</div>

Cool,&nbsp;I&nbsp;also&nbsp;want&nbsp;to&nbsp;extend&nbsp;EditInteractiveOverlay,&nbsp;this&nbsp;is&nbsp;really&nbsp;good&nbsp;example.&nbsp;  
&nbsp;Thank&nbsp;Yale.

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [June 7, 2009, 8:33pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/5 "2009-06-07T20:33:28Z")

</div>

David&nbsp;&&nbsp;James,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;for&nbsp;your&nbsp;words.&nbsp;Any&nbsp;more&nbsp;questions&nbsp;just&nbsp;let&nbsp;me&nbsp;know.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Yale&nbsp;

---

<div class="post-metadata">

**Author:** ![jmccrillis](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@jmccrillis](https://community.thinkgeo.com/u/jmccrillis)\
**Post date:** [October 2, 2009, 2:08pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/6 "2009-10-02T14:08:21Z")

</div>

What&nbsp;a&nbsp;great&nbsp;example,&nbsp;thanks&nbsp;from&nbsp;me,&nbsp;Yale.&nbsp;I&nbsp;have&nbsp;a&nbsp;couple&nbsp;of&nbsp;questions&nbsp;on&nbsp;it,&nbsp;please.&nbsp;  
&nbsp;&nbsp;  
&nbsp;1)&nbsp;In&nbsp;my&nbsp;application,&nbsp;the&nbsp;user&nbsp;has&nbsp;already&nbsp;selected&nbsp;the&nbsp;feature&nbsp;she&nbsp;wants&nbsp;to&nbsp;edit&nbsp;when&nbsp;I&nbsp;copy&nbsp;it&nbsp;into&nbsp;the&nbsp;EditInteractiveOverlay,&nbsp;so&nbsp;how&nbsp;can&nbsp;I&nbsp;put&nbsp;the&nbsp;new&nbsp;feature&nbsp;into&nbsp;an&nbsp;edit&nbsp;mode&nbsp;right&nbsp;away,&nbsp;rather&nbsp;than&nbsp;making&nbsp;the&nbsp;user&nbsp;select&nbsp;it&nbsp;again?&nbsp;  
&nbsp;&nbsp;  
&nbsp;2)&nbsp;Is&nbsp;there&nbsp;already&nbsp;a&nbsp;mechanism&nbsp;for&nbsp;the&nbsp;user&nbsp;to&nbsp;signal&nbsp;she&nbsp;is&nbsp;done&nbsp;editing,&nbsp;or&nbsp;must&nbsp;I&nbsp;provide&nbsp;one&nbsp;(and&nbsp;if&nbsp;it’s&nbsp;the&nbsp;latter,&nbsp;do&nbsp;you&nbsp;have&nbsp;an&nbsp;example&nbsp;I&nbsp;can&nbsp;re-use)?&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thank&nbsp;you,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Janene&nbsp;McCrillis&nbsp;  
&nbsp;Wireless&nbsp;Seismic

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [October 4, 2009, 9:43am UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/7 "2009-10-04T09:43:55Z")

</div>

Janene,&nbsp;  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.&nbsp;there&nbsp;are&nbsp;two&nbsp;ways&nbsp;to&nbsp;to&nbsp;edit&nbsp;a&nbsp;feature.&nbsp;One&nbsp;is&nbsp;using&nbsp;mouse&nbsp;to&nbsp;select&nbsp;it&nbsp;as&nbsp;you&nbsp;already&nbsp;know;&nbsp;the&nbsp;other&nbsp;one&nbsp;is&nbsp;to&nbsp;add&nbsp;this&nbsp;feature&nbsp;to&nbsp;EditInteractiveOverlay&nbsp;directly.&nbsp;There&nbsp;is&nbsp;an&nbsp;InMemoryFeatureLayer&nbsp;called&nbsp;EditShapeLayer&nbsp;in&nbsp;EditInteractiveOverlay,&nbsp;you&nbsp;can&nbsp;add&nbsp;the&nbsp;features&nbsp;to&nbsp;the&nbsp;InternalFeatures&nbsp;property&nbsp;of&nbsp;it.&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp;It&nbsp;will&nbsp;raise&nbsp;an&nbsp;event&nbsp;when&nbsp;each&nbsp;operation&nbsp;(such&nbsp;rotate,&nbsp;drag,&nbsp;add&nbsp;vertex&nbsp;etc)&nbsp;start&nbsp;or&nbsp;end.&nbsp;&nbsp;I&nbsp;think&nbsp;you&nbsp;can&nbsp;use&nbsp;the&nbsp;end&nbsp;event&nbsp;as&nbsp;the&nbsp;signal&nbsp;of&nbsp;done&nbsp;editing.&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben

---

<div class="post-metadata">

**Author:** ![jmccrillis](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@jmccrillis](https://community.thinkgeo.com/u/jmccrillis)\
**Post date:** [October 5, 2009, 11:06am UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/8 "2009-10-05T11:06:33Z")

</div>

Hi&nbsp;Ben,&nbsp;thanks&nbsp;for&nbsp;the&nbsp;reply.

My&nbsp;first&nbsp;question&nbsp;was&nbsp;more&nbsp;aimed&nbsp;at&nbsp;the&nbsp;question&nbsp;of&nbsp;how&nbsp;to&nbsp;put&nbsp;a&nbsp;feature&nbsp;into&nbsp;editing&nbsp;mode&nbsp;automatically,&nbsp;so&nbsp;the&nbsp;control&nbsp;points&nbsp;for&nbsp;editing&nbsp;are&nbsp;available&nbsp;to&nbsp;the&nbsp;user&nbsp;without&nbsp;making&nbsp;her&nbsp;select&nbsp;the&nbsp;feature&nbsp;a&nbsp;2nd&nbsp;time.&nbsp;I'm&nbsp;adding&nbsp;my&nbsp;current&nbsp;code&nbsp;below,&nbsp;please&nbsp;let&nbsp;me&nbsp;know&nbsp;where&nbsp;I&nbsp;need&nbsp;to&nbsp;add&nbsp;or&nbsp;change&nbsp;something&nbsp;to&nbsp;show&nbsp;the&nbsp;feature&nbsp;ready&nbsp;to&nbsp;be&nbsp;edited:

&nbsp;

`            RectangleShape newRectangleShape = currentlySelectedFeature.GetBoundingBox() as RectangleShape;            lineSegmentEditOverlay.Lock.EnterWriteLock();            try {                lineSegmentEditOverlay.EditShapesLayer.InternalFeatures.Add(new Feature(newRectangleShape));                lineSegmentEditOverlay.CalculateAllControlPoints();            } finally {                lineSegmentEditOverlay.Lock.ExitWriteLock();            }            wpfMap1.Refresh();`

&nbsp;

&nbsp;

I'm&nbsp;still&nbsp;working&nbsp;on&nbsp;this,&nbsp;so&nbsp;any&nbsp;help&nbsp;would&nbsp;be&nbsp;appreciated.&nbsp;Thanks!

Janene&nbsp;McCrillis

Wireless&nbsp;Seismic

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [October 6, 2009, 10:17pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/9 "2009-10-06T22:17:37Z")

</div>

Hi&nbsp;Janene,

I&nbsp;think&nbsp;this&nbsp;block&nbsp;of&nbsp;code&nbsp;is&nbsp;enough,&nbsp;the&nbsp;control&nbsp;points&nbsp;will&nbsp;show&nbsp;on&nbsp;the&nbsp;map&nbsp;after&nbsp;executing&nbsp;these&nbsp;codes&nbsp;and&nbsp;you&nbsp;can&nbsp;edit&nbsp;the&nbsp;feature&nbsp;directly&nbsp;by&nbsp;the&nbsp;control&nbsp;points&nbsp;without&nbsp;nothing&nbsp;more&nbsp;operation.  
There&nbsp;is&nbsp;a&nbsp;simple&nbsp;sample&nbsp;in&nbsp;the&nbsp;attachment&nbsp;to&nbsp;show&nbsp;you&nbsp;how&nbsp;it&nbsp;works&nbsp;base&nbsp;on&nbsp;the&nbsp;CustomerEditInteractiveOverlay&nbsp;in&nbsp;this&nbsp;post.  
Any&nbsp;more&nbsp;questions&nbsp;please&nbsp;let&nbsp;us&nbsp;know.  
Thanks,  
sun  
  
[1283-Post5822Demo.zip](https://community.thinkgeo.com/uploads/default/original/2X/1/1a65a123eaf50ad56ffc8e5e7ab77ded71592d3a.zip) (11.5 KB)

---

<div class="post-metadata">

**Author:** ![jmccrillis](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@jmccrillis](https://community.thinkgeo.com/u/jmccrillis)\
**Post date:** [October 7, 2009, 6:03pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/10 "2009-10-07T18:03:16Z")

</div>

Hi&nbsp;Sun,&nbsp;  
&nbsp;&nbsp;  
&nbsp;The&nbsp;map&nbsp;is&nbsp;still&nbsp;not&nbsp;entering&nbsp;edit&nbsp;mode&nbsp;automatically&nbsp;-&nbsp;the&nbsp;user&nbsp;still&nbsp;has&nbsp;to&nbsp;select&nbsp;the&nbsp;feature&nbsp;again&nbsp;to&nbsp;edit&nbsp;it.&nbsp;It&nbsp;is&nbsp;also&nbsp;not&nbsp;refreshing&nbsp;automatically&nbsp;when&nbsp;the&nbsp;feature&nbsp;is&nbsp;removed,&nbsp;in&nbsp;spite&nbsp;of&nbsp;many&nbsp;calls&nbsp;to&nbsp;wpfMap.refresh()&nbsp;  
&nbsp;&nbsp;  
&nbsp;JAnene&nbsp;  
&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [October 9, 2009, 3:50am UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/11 "2009-10-09T03:50:36Z")

</div>

Janene,

I&nbsp;think&nbsp;you&nbsp;could&nbsp;use&nbsp;the&nbsp;event&nbsp;TrackEnded&nbsp;to&nbsp;implement&nbsp;the&nbsp;functionality,&nbsp;try&nbsp;the&nbsp;attachment&nbsp;sample,&nbsp;and&nbsp;hope&nbsp;it&nbsp;helps.  
&nbsp;  
Any&nbsp;more&nbsp;questions&nbsp;just&nbsp;feel&nbsp;free&nbsp;to&nbsp;let&nbsp;me&nbsp;know.  
&nbsp;  
Thanks.  
&nbsp;  
Yale  
  
[1296-Post5822\_Wpf\_sample.zip](https://community.thinkgeo.com/uploads/default/original/2X/4/4ac1f64da740422fc68aa9f1c7a40f75175c1b53.zip) (14.1 KB)

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [February 3, 2010, 7:50pm UTC](https://community.thinkgeo.com/t/editing-rectangular-polygons/455/12 "2010-02-03T19:50:15Z")

</div>

&nbsp;

Guys,  
&nbsp;  
We&nbsp;create&nbsp;a&nbsp;sample&nbsp;in&nbsp;our&nbsp;Code&nbsp;Community&nbsp;shows&nbsp;how&nbsp;to&nbsp;edit&nbsp;rectangles.  
[code.thinkgeo.com/projects/s...rectangles](http://code.thinkgeo.com/projects/show/editingrectangles)  
&nbsp;  
Thanks  
James
