polyline轉polygon不同情況buffer的實現方法

 做一個運輸署的project時需要實現一個線轉面(polyline to polygon)的函數,有點像buffer,有個buffer distance, 但是在頭尾兩邊不能是圓弧,而是直線.(確實夠搞得...)參考了一下網上的資料,將幾種類似的buffer做了一下,效果如下圖。

(左上角為polyline,用它生成其他3種buffer.注意三個polygon紅色框框位置的差別 )

先說最簡單的,直接利用ITopologicalOperator.Buffer,設定一個buffer distance就
能得到左下角的polygon, 生成的polygon在結點位置都是圓弧.
  1. Private Function ConvertPolylineToPolygon(pPolyline As IPolyline, pDist As Double) As IPolygon

  2. Dim pPolygon As IPolygon
  3. Set pPolygon = New Polygon

  4. Dim pTopo As ITopologicalOperator
  5. Set pTopo = pPolyline
  6. Set pPolygon = pTopo.Buffer(pDist)

  7. pPolygon.Densify pPolygon.Length, 360

  8. Set ConvertPolylineToPolygon = pPolygon
  9. End Function   
复制代码



如果希望首尾兩點位置上是squre buffer,則可以用下面的函數操作
  1. Public Function TrySquareBufferByDifference(pPolyline As IPolyline, bufDist As Double) As IPolygon
  2.   Dim pResult As IPolygon
  3.   Dim pTopoIn As ITopologicalOperator
  4.   Set pTopoIn = pPolyline
  5.   Set pResult = pTopoIn.Buffer(bufDist)
  6.   Dim pTopoOut As ITopologicalOperator
  7.   Set pTopoOut = pResult


  8.   Dim pNorm1 As ILine, pNorm2 As ILine, pNorm3 As ILine, pNorm4 As ILine
  9.   Dim pCloser As ILine
  10.   Dim pDiffSegs As ISegmentCollection
  11.   Dim pDiffer As IPolygon
  12.   Dim pDiffTopo As ITopologicalOperator


  13.   Set pNorm1 = New Line
  14.   Set pNorm2 = New Line
  15.   Set pNorm3 = New Line
  16.   Set pNorm4 = New Line
  17.   Set pCloser = New Line
  18.   pPolyline.QueryNormal esriNoExtension, 0, True, bufDist, pNorm1
  19.   pPolyline.QueryNormal esriNoExtension, 0, True, -bufDist, pNorm2
  20.   'on the next two lines, you may need to reverse the signs of the "length" arguments
  21.   pNorm1.QueryNormal esriNoExtension, 1, True, bufDist, pNorm3
  22.   pNorm2.QueryNormal esriNoExtension, 1, True, -bufDist, pNorm4
  23.   pCloser.PutCoords pNorm3.ToPoint, pNorm4.ToPoint


  24.   Set pDiffer = New Polygon
  25.   Set pDiffSegs = pDiffer
  26.   Set pDiffTopo = pDiffer
  27.   Set pDiffer.SpatialReference = pPolyline.SpatialReference
  28.   pDiffSegs.AddSegment pNorm1
  29.   pDiffSegs.AddSegment pNorm2
  30.   pDiffSegs.AddSegment pNorm3
  31.   pDiffSegs.AddSegment pNorm4
  32.   pDiffSegs.AddSegment pCloser
  33.   pDiffTopo.Simplify


  34.   Dim pTemp As IGeometry
  35.   Set pTemp = pTopoOut.Difference(pDiffer)
  36.   Set pTopoOut = pTemp


  37.   Set pNorm1 = New Line
  38.   Set pNorm2 = New Line
  39.   Set pNorm3 = New Line
  40.   Set pNorm4 = New Line
  41.   Set pCloser = New Line
  42.   pPolyline.QueryNormal esriNoExtension, 1, True, bufDist, pNorm1
  43.   pPolyline.QueryNormal esriNoExtension, 1, True, -bufDist, pNorm2
  44.   'on the next two lines, you may need to reverse the signs of the "length" arguments
  45.   pNorm1.QueryNormal esriNoExtension, 1, True, -bufDist, pNorm3
  46.   pNorm2.QueryNormal esriNoExtension, 1, True, bufDist, pNorm4
  47.   pCloser.PutCoords pNorm3.ToPoint, pNorm4.ToPoint

  48.   Set pDiffer = New Polygon
  49.   Set pDiffSegs = pDiffer
  50.   Set pDiffTopo = pDiffer
  51.   Set pDiffer.SpatialReference = pPolyline.SpatialReference
  52.   pDiffSegs.AddSegment pNorm1
  53.   pDiffSegs.AddSegment pNorm2
  54.   pDiffSegs.AddSegment pNorm3
  55.   pDiffSegs.AddSegment pNorm4
  56.   pDiffSegs.AddSegment pCloser
  57.   pDiffTopo.Simplify

  58.   Set TrySquareBufferByDifference = pTopoOut.Difference(pDiffer)
  59. End Function
复制代码


如果希望節點位置不出現圓弧,可以用以下的方法
  1. Public Function TrySquareBufferByOffset(pPolyline As IPolyline, bufDist As Double) As IPolygon


  2.     Dim pOffset1 As IConstructCurve, pOffset2 As IConstructCurve
  3.     Set pOffset1 = New Polyline
  4.     Set pOffset2 = New Polyline


  5.     Dim pOffCurve1 As ICurve, pOffCurve2 As ICurve
  6.     Set pOffCurve1 = pOffset1
  7.     Set pOffCurve2 = pOffset2


  8.     pOffset1.ConstructOffset pPolyline, bufDist, esriConstructOffsetSimple 'modify offsethow and bevelratio if desired
  9.     pOffset2.ConstructOffset pPolyline, -bufDist, esriConstructOffsetSimple


  10.     Dim pLine1 As ILine, pLine2 As ILine
  11.     Set pLine1 = New Line
  12.     Set pLine2 = New Line
  13.     pLine1.PutCoords pOffCurve1.FromPoint, pOffCurve2.FromPoint
  14.     pLine2.PutCoords pOffCurve1.ToPoint, pOffCurve2.ToPoint


  15.     Dim pResult As IPolygon
  16.     Set pResult = New Polygon
  17.     Set pResult.SpatialReference = pPolyline.SpatialReference


  18.     Dim pSegCol As ISegmentCollection
  19.     Set pSegCol = pResult
  20.     pSegCol.AddSegment pLine1
  21.     pSegCol.AddSegment pLine2
  22.     pSegCol.AddSegmentCollection pOffset1
  23.     pSegCol.AddSegmentCollection pOffset2
  24.     pResult.SimplifyPreserveFromTo


  25.     Set TrySquareBufferByOffset = pResult


  26. End Function
复制代码


希望對大家有幫助.....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值