使用PyQGIS / Python与另一个图层的要素相交时是否拆分要素?


12

我有一个缓冲层(绿色多边形),当它越过障碍(蓝线)时,我想将其分成两个多边形。我一直在尝试使用“ splitGeometry”方法,但是我无法使其正常工作。到目前为止,我的代码是这样的:

while ldbuffprovider.nextFeature(feat):
  while barprovider.nextFeature(feat2):
    if feat.geometry().intersects(feat2.geometry()):
        intersection = feat.geometry().intersection(feat2.geometry())
        result, newGeometries, topoTestPoints=feat.geometry().splitGeometry(intersection.asPolyline(),True) 

结果(错误)返回1,newGeometries返回空列表。任何帮助是极大的赞赏。

在此处输入图片说明


Answers:


7

为此,您可以使用对象的reshapeGeometry功能,该功能QgsGeometry沿与线的交点切割多边形。

以下将使缓冲区多边形与直线相交,并将分割的多边形要素添加到存储层(QGIS 2.0语法):

# Get the dataProvider objects for the layers called 'line' and 'buffer'
linepr = QgsMapLayerRegistry.instance().mapLayersByName('line')[0].dataProvider()
bufferpr = QgsMapLayerRegistry.instance().mapLayersByName('buffer')[0].dataProvider()

# Create a memory layer to store the result
resultl = QgsVectorLayer("Polygon", "result", "memory")
resultpr = resultl.dataProvider()
QgsMapLayerRegistry.instance().addMapLayer(resultl)


for feature in bufferpr.getFeatures():
  # Save the original geometry
  geometry = QgsGeometry.fromPolygon(feature.geometry().asPolygon())
  for line in linepr.getFeatures():
    # Intersect the polygon with the line. If they intersect, the feature will contain one half of the split
    t = feature.geometry().reshapeGeometry(line.geometry().asPolyline())
    if (t==0):
      # Create a new feature to hold the other half of the split
      diff = QgsFeature()
      # Calculate the difference between the original geometry and the first half of the split
      diff.setGeometry( geometry.difference(feature.geometry()))
      # Add the two halves of the split to the memory layer
      resultpr.addFeatures([feature])
      resultpr.addFeatures([diff])


1
这非常出色。我先尝试了另一种解决方案,它奏效了,因此即使在我读完本书之前,也为此提供了帮助。这个解决方案绝对是完美的,并且更适合我的脚本。对此表示抱歉:/
Alex

呵呵,没问题!很高兴有帮助!
杰克

我支持您的回答,因为它可以完美地发挥作用,而我的只是一个近似值。@PeyMan感谢您的悬赏,但是当我的悬赏结束时,除了我以外没有其他答案。始终欢迎更好的解决方案。
Antonio Falciano

有什么办法可以拆分特定图层的所有多边形?
穆罕默德·法赞汗

我只有一个图层,并且有多个多边形,我想通过槽编码将它们分开
Muhammad Faizan Khan

2

使用SQLite和SpatiaLite编译的GDAL> = 1.10.0可以很好地近似于将图层(例如poligon.shpline.shp)包装在OGR VRT文件(例如,layers.vrt)中:

<OGRVRTDataSource>
    <OGRVRTlayer name="buffer_line">
        <SrcDataSource>line.shp</SrcDataSource>
        <SrcSQL dialect="sqlite">SELECT ST_Buffer(geometry,0.000001) from line</SrcSQL>
    </OGRVRTlayer>
    <OGRVRTlayer name="polygon">
        <SrcDataSource>polygon.shp</SrcDataSource>
    </OGRVRTlayer>
</OGRVRTDataSource>

为了在line.shp周围有一个非常小的缓冲区(例如1微米),获得* buffer_line *层。然后,我们可以使用SpatiaLite在这些几何上应用对称差和差:

ogr2ogr splitted_polygons.shp layers.vrt -dialect sqlite -sql "SELECT ST_Difference(ST_SymDifference(g1.geometry,g2.geometry),g2.geometry) FROM polygon AS g1, buffer_line AS g2" -explodecollections

显然,所有这些东西都可以从Python脚本完美执行:

os.system("some_command with args")

希望这可以帮助!


@Jake reshapeGeometry抛出异常未知错误,那么还有其他方法可以检查多边形和折线之间的交集吗?
user99

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.