ThinkGeo.com    |     Documentation    |     Premium Support

Splitting and Joining Shapes

How can I split a shape into two polygons?


How can I join two adjacent polygon shapes together?


Thanks! Cyndi



 


Cyndi,
 
 I hope my quick answers helps get you pointed in the right direction. If not just let me know and we can work up some simple sample code.
1.       For splitting two polygons I need to know how you intend to split them. More specifically how will determine how it will be split? Do you have a line to split them, another polygon that overlaps the first, or maybe something special?



 
2.       For joining two polygons that touch each other it is straight forward. You will call the Union method on the first polygon and pass as its target parameter the second polygon. The return value of the Shape.Union is a new polygon that is joined. Some pseudocode would look like this.
 
Polygon polygon1 = new Polygon() ;
Polygon polygon2 = new Polygon() ;
Polygon combinedPolygon = polygon1.Union(polygon2) ;
 
Now the cominedPolygon is a union of the two. Note that this code won’t work because I never really defined polygon1 or polygon2 but I hope you get the picture.
 
An important sidebar to this is that this Union on the polygon object works well when you are joining just one polygon with another. If you plan to join a bunch of polygons together like taking all 50 states and joining them to create an outline of the US then you do not want to use this method. The reason is that the Union uses graph math and it have overhead to load the graph from the shapes and solve. If you want to union lots of things at once then you should use the Polygon.Union(IEnumerable<AreaBaseShape> targets), I mean the static method that is only the polygon class itself, actually it is static on the AreaBase shape so all classes that inherit from it have the static method such as Polygon, MultiPolygon, Ellipse, Rectangle etc.. This loads all of the areas into the graph and solves them all in one shot. It is much faster when joining more than a dozen or so polygons. Maybe this is too much but I wanted to warn you as if I didn’t you next post will be “It works but is too slow when I join lots of polygons.” :-)
David

  


 


The application I am dealing with involves fields. A field may be split into two fields. (Or joining two fields into one field.) Also I need to split a field into multiple areas if there is irrigation involved (usually a circle). 


1) Splitting a field into two areas. Draw a line through the polygon. The line may be straight or be composed of multiple line segments. The result will be two polygons.


2) Splitting a field into irrigated and non-irrigated areas. Draw a circle on the polygon. The result will be the circle will become one polygon and the regions outside of the polygon will become one or multiple polygons (depending if the circle is contained inside the polygon or intersects the sides).


 



 


Cyndi,
 
We don’t have the API to split the shape directly by a line, but with the topology methods integrated within Map Suite, you can implement the 2 functions without a problem.
 
 Picture 1 shows the original field and picture 4 shows the final result that field A is divided into 2 parts A1 and A2.  
 
Here are the steps in order:
 
Step1: Select the polygon you want to split, that is polygon A in picture 1.
 
Step 2: Allow the user to draw a polygon, this will be polygon B, around a portion of Polygon A they want to disconnect.  This is where it is different from what you originally wanted, instead f a line we need your user to draw a polygon around what area they want to break away.
 
Step 3: Call the Intersection method on Polygon A and pass as the parameter Polygon B, the user drawn shape. The result will be Polygon A2 which is the first part of our goal.
 
Step 4: Call the Difference method on Polygon A and pass in Polygon A2 as the parameter.  This will return A1 which is the second half of the problem.
 
A2 = A.Intersection(B).
A1 = A.Difference(A2)
 
Note: Both of the return value of A.Intersection(B) and A.Difference(A2) are multiPolygon as the splitter line might intersects the sides more than 2 times. If they do not it will still be a MultiPolygon though it will only have one outer ring.

Ben.



Here is the solution of how to split the field into irrigated and non-irrigated areas. Similarly, what we want is from picture 1 to picture 3. Let’s say B is the irrigation circle. First what we need is to call A.Intersection(B) and get A1, second we need to call A.Difference(A1) to get A2.


 
Picture 4 is another possible result we want. For that, we need to convert the returned shape from multiPolygonShape to polygonShape. That part should be easy as there is a Polygons property in MultiPolygonShape object.

For more information about shape topologies, please have a look at here. More queries please let us know.


 
Ben

Cyndi,


I wanted you to know that we will be adding a Split method on area based shapes such as Polygons on one of the next beta releases.  It will not support splitting by a line but will accept any other area based shape.  Even though you can accomplish it with a few lines of code the code is a bit tricky and it would be better is we wrapped it up in an easy to use API.  I just wanted to let you know and to thank you for bringing that to our attention.  It's feedback like yours that help is have the best .Net mapping control out there.


Thanks again..


David



Hi, I’m very interested in splitting, could you provide a sample with this functionality please ? 
 Thank you very much.

Hi Gautier, 
  
 You can find that sample in  HowDoI sample\Features\Find the difference of two features. 
  
 Please let me know if you have any questions. 
 Thanks, 
 Lee

I got it ! 
 Thank you !

You are welcome, 
 Feel free to let us know if you got any questions. 
  
 Thanks, 
 Lee