ET GeoWizards Scripting
Split a Polygon dataset with Polylines - How to do it?
A common question posted to the ArcGIS forums and to our support line. There is a tool available in ArcMap that allows the user to draw a polyline which will divide the selected polygon. ET GeoTools has a tool that will divide the polygons using selected polylines from a polyline layer. The problem with these tools is that only the polylines that have both their ends outside of a polygon will be used for splitting.
How should we split a polygon representing a catchment area with the rivers inside this catchment? One possible solution is described below and can be achieved using ET GeoWizards scripting with few lines of code.
Step 1: Convert your polygons to polylines
Step 2: Merge the polylines derived in Step 1 with the polylines to be used for
splitting
Step 3: Clean the merged polylines
Step 4: Build polygons from the cleaned polylines
Step 5: Transfer the attributes from the original polygons to the newly created
ones
This is achievable with the functions available in ET GeoWizards together with some standard ArcGIS functions, but each of the steps needs to be performed via the interface.
The process:
The scheme below gives a graphic description of the entire process
With the Scripting introduced in ET GeoWizards 9.0 this process can be performed with just few lines of code.
Note - If we remove the comments, the error checking (which repeats after each procedure), the declarations etc. - the entire procedure is accomplished in 6 lines of code!
Sub SplitPolygonLayer() Dim pMxDoc As IMxDocument Dim pMap As IMap Dim pFeatureLayer As IFeatureLayer Dim pInFeatureClass As IFeatureClass Dim pOutFeatureClass As IFeatureClass Dim pTempFeatureClass As IFeatureClass Dim bDone As Boolean Set pMxDoc = ThisDocument Set pMap = pMxDoc.FocusMap Set pFeatureLayer = pMxDoc.SelectedLayer Set pInFeatureClass = pFeatureLayer.FeatureClass '========================================= Dim ET As New ETGW_Core Dim sOutFileName As String Dim dFuzzyTol As Double Dim pSplitLayer As IFeatureLayer Dim pSplitFeatureClass As IFeatureClass 'an example on how to get a feature layer by name ' Get the polyline layer to be used for splitting Set pSplitLayer = ET.GetLayer("split_layer") If pSplitLayer Is Nothing Then Exit Sub End If Set pSplitFeatureClass = pSplitLayer.FeatureClass sOutFileName = "c:\00\split2.shp" 'Convert the polygons to polylines Set pTempFeatureClass = ET.PolygonToPolyline(pInFeatureClass, "c:\00\pg2pl.shp") If pTempFeatureClass Is Nothing Then MsgBox "Error in step 0" Exit Sub End If 'Merge the polylines with the Split polyline layer Set pTempFeatureClass = ET.merge(pTempFeatureClass, pSplitFeatureClass, "c:\00\merged.shp") If pTempFeatureClass Is Nothing Then MsgBox "Error in step 1" Exit Sub End If 'Build polygons from merged dataset 'Note that the bClean must be set to TRUE ==> the merged polylines should be cleaned before using them to build polygons dFuzzyTol = 0.002 Set pOutFeatureClass = ET.BuildPolygons(pTempFeatureClass, "c:\00\pg_temp.shp", True, dFuzzyTol) If pOutFeatureClass Is Nothing Then MsgBox "Error in step 2" Exit Sub End If 'In order to get the attributes from the original polygons we need to '1. Get the label points of the newly created polygons Set pTempFeatureClass = ET.PolygonToPoint(pOutFeatureClass, "c:\00\lab1.shp", _ "Label", False) If pTempFeatureClass Is Nothing Then MsgBox "Error in step 3" Exit Sub End If '2. Attach the attributes from the original polygons to the label points Set pTempFeatureClass = ET.Spatial_Join(pTempFeatureClass, pInFeatureClass, _ "c:\00\lab2.shp", "Within", False, 0) If pTempFeatureClass Is Nothing Then MsgBox "Error in step 4" Exit Sub End If '3. Attach the attributes from the label points to the split polygons Set pOutFeatureClass = ET.Spatial_Join(pOutFeatureClass, pTempFeatureClass, _ sOutFileName, "Nearest", False, 0) If pOutFeatureClass Is Nothing Then MsgBox "Error in step 5" Exit Sub End If ' Delete the intermediate datasets bDone = ET.Deletefeature class("c:\00\pg2pl.shp") bDone = ET.Deletefeature class("c:\00\merged.shp") bDone = ET.Deletefeature class("c:\00\pg_temp.shp") bDone = ET.Deletefeature class("c:\00\lab1.shp") bDone = ET.Deletefeature class("c:\00\lab2.shp") '========================================= 'Add the split polygons as a layer in the Map Set pFeatureLayer = New FeatureLayer Set pFeatureLayer.FeatureClass = pOutFeatureClass pFeatureLayer.Name = pOutFeatureClass.AliasName pMap.AddLayer pFeatureLayer End Sub |