Quantcast
Channel: Forums - ArcGIS for Desktop - General
Viewing all articles
Browse latest Browse all 2565

measuring shared borders

$
0
0
I am completely out of my depth with trying to right any sort of script. I have a need to measure the length of a shared border between two polygons. I then need this proces to populate a new field that give me the length of shared border for each of the polygons in my map. I have tracked down a peice of VBA script that should do this for me but I have no idea how toconvet that to Python. Can anyone help me

Public Sub MeasureSharedBoundary()
' ----- You'll need to change this value ----
Const UPDATE_FIELD = "SharedLen"
' -------------------------------------------
Dim pMxDoc As IMxDocument
Dim pFtrLyr As IFeatureLayer
Dim pFtrCls As IFeatureClass
Dim pFtrCsr As IFeatureCursor
Dim pFtr As IFeature
Dim sSharedLen As String
Dim lFldIdx As Long

' Get a ref to the featureclass of the first alyer
Set pMxDoc = ThisDocument
Set pFtrLyr = pMxDoc.FocusMap.Layer(0)
Set pFtrCls = pFtrLyr.FeatureClass
' Get the index of the field we want to update
lFldIdx = pFtrCls.FindField(UPDATE_FIELD)

' Create an update cursor on all the features
Set pFtrCsr = pFtrCls.Update(Nothing, False)

' Loop thru all the features and update the field
Set pFtr = pFtrCsr.NextFeature
While Not pFtr Is Nothing
pFtr.Value(lFldIdx) = GetSharedBorderLength(pFtr, pFtrCls)
pFtrCsr.UpdateFeature pFtr
Set pFtr = pFtrCsr.NextFeature
Wend

MsgBox "Updated All Features", vbInformation, ""

End Sub


Private Function GetSharedBorderLength(pInFtr As IFeature, pInFtrCls As IFeatureClass) As String
' Returns a string containing all the shared boundary lengths (separated by " : ")
' of the specified feature
Dim pInBndry As IPolyline
Dim pBndry As IPolyline
Dim pSpFltr As ISpatialFilter
Dim pFtrCsr As IFeatureCursor
Dim pFtr As IFeature
Dim pTopOp As ITopologicalOperator
Dim pIntersectGeom As IPolyline
Dim sSharedBorderLen As String

sSharedBorderLen = ""

' Get the boundary of the feature
Set pTopOp = pInFtr.Shape
Set pInBndry = pTopOp.Boundary

' Create a spatial filter for intersecting other geometries
' but exclude this feature from the result (where clause)
Set pSpFltr = New SpatialFilter
pSpFltr.SpatialRel = esriSpatialRelIntersects
Set pSpFltr.Geometry = pInFtr.Shape
pSpFltr.WhereClause = pInFtrCls.OIDFieldName + "<>" + CStr(pInFtr.OID)

' Create a feature cursor of all the features that intersect this one
Set pFtrCsr = pInFtrCls.Search(pSpFltr, False)
' Loop thru the intersected features and store the intersection length
Set pFtr = pFtrCsr.NextFeature
While Not pFtr Is Nothing
Set pTopOp = pFtr.ShapeCopy
Set pBndry = pTopOp.Boundary
Set pTopOp = pInBndry
Set pIntersectGeom = pTopOp.Intersect(pBndry, esriGeometry1Dimension)
If Not pIntersectGeom.IsEmpty Then
sSharedBorderLen = sSharedBorderLen + CStr(pIntersectGeom.Length) + " : "
End If
Set pFtr = pFtrCsr.NextFeature
Wend

If sSharedBorderLen = "" Then sSharedBorderLen = "0"

GetSharedBorderLength = sSharedBorderLen
End Function

Viewing all articles
Browse latest Browse all 2565

Trending Articles