从我的发现中,似乎没有针对这种确切情况的现有解决方案,但是我仍然希望能够在QGIS中做到这一点,因此我投入了python脚本。
可以在这里找到编写处理算法的指南https://docs.qgis.org/2.18/zh/docs/user_manual/processing/scripts.html
要使用此代码,请打开“处理”工具箱,然后依次展开“脚本”和“工具”。选择“创建新脚本”,然后将以下代码复制并粘贴到脚本窗口中(复制和粘贴python代码时要谨慎,因为空格在语法上很重要。如果遇到问题,请将代码放入显示空格的文本编辑器中,并确保正确复制)。将其保存在任意位置,窗口顶部有一个执行脚本按钮。保存后,可以“从文件添加脚本”,并将脚本永久保存在“用户脚本”下。
当处理窗口出现时,选择包含矢量几何的图层,然后选择运行。该脚本的行为与“提取节点”相同,不同之处在于该脚本添加了一个称为MValues
和或ZValues
取决于输入几何中可用内容的列。
##input_layer=vector
##output_layer=output vector
from qgis.core import QgsWKBTypes, QgsField, QgsVectorFileWriter, QgsFeature, QgsGeometry
from PyQt4.QtCore import QVariant
def addVertices( geometry, writer, inFeature ):
coordinateSequence = geometry.coordinateSequence()
for rings in coordinateSequence:
for points in rings:
for point in points:
feature = QgsFeature( fields )
feature.setGeometry( QgsGeometry( point ) )
type = point.wkbType()
attributes = inFeature.attributes()
if QgsWKBTypes.hasM( type ):
attributes.append( point.m() )
if QgsWKBTypes.hasZ( type ):
attributes.append(point.z())
feature.setAttributes( attributes )
writer.addFeature( feature )
return
inlayer = processing.getObject( input_layer )
provider = inlayer.dataProvider()
fields = provider.fields()
geomType = QgsWKBTypes.Type(inlayer.wkbType())
outputGeomType = QgsWKBTypes.Point
if QgsWKBTypes.hasM( geomType ):
outputGeomType = QgsWKBTypes.addM( outputGeomType )
fields.append( QgsField( "MValue", QVariant.Double ) )
if QgsWKBTypes.hasZ( geomType ):
outputGeomType = QgsWKBTypes.addZ( outputGeomType )
fields.append( QgsField( "ZValue", QVariant.Double ) )
layer_options = 'SHPT=' + QgsWKBTypes.displayString(outputGeomType)
writer = QgsVectorFileWriter( output_layer, 'UTF-8', fields, outputGeomType , inlayer.crs(), layerOptions=[layer_options] )
features = inlayer.getFeatures()
featureCount = inlayer.featureCount()
featureIndex = 0
for f in features:
percent = ( featureIndex/float( featureCount ) ) * 100
progress.setPercentage( percent )
g = f.geometry().geometry()
addVertices( g, writer, f )
featureIndex +=1
del writer