简单的问题,我想编辑一个图层来添加一个正方形:
x,y
10,10
10,20
20,20
20,10
但是我不想把它放入csv/txt文件中,我只想一次输入一个坐标并构造多边形,有什么想法吗?
不完全确定你的意思,但你可以创建一个带有坐标的众所周知的文本文件:
"id","st_astext"
3,"POLYGON((380021.900198576 277004.918072763,380001.683890889 276942.789907678,380057.895087871
然后导入csv?
感谢您的回答,但很明显QGIS没有用于添加单个坐标的插件(就像ArcGIS使用F6快捷键所做的那样)。
这是一个简单的例子,但我相信可以理解这个问题。
我有一个简单的插件,它加载多边形层(注意:只有一个)并且这是空的,并使用此模式输入新多边形“x0,y0;x1,y1....;x0,y0” :
并输入以下值:
-4.6142578125,39.40224434029275;-2.6806640625,39.40224434029275;-2.6806640625,40.97989806962013;-4.6142578125,40 .97989806962013;-4.6142578125,39.40224434029275
单击“Aceptar”按钮时添加新的多边形。
这是 run() 方法:
def run(self):
self.dlg.cmb_TOC.clear()
self.dlg.input_ln.clear()
layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
layerType = layer.type()
if layerType == QgsMapLayer.VectorLayer and layer.wkbType()==QGis.WKBPolygon:
layer_list.append(layer.name())
self.dlg.cmb_TOC.addItems(layer_list)
"""Run method that performs all the real work"""
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result == 1:
if unicode(self.dlg.cmb_TOC.currentText())=="" or self.dlg.input_ln.text()=="":
self.iface.messageBar().pushMessage("Info", "Empty parameters", level=QgsMessageBar.WARNING, duration=3)
return 1
t_e="-4.6142578125,39.40224434029275;-2.6806640625,39.40224434029275;-2.6806640625,40.97989806962013;-4.6142578125,40.97989806962013;-4.6142578125,39.40224434029275"
t_e=self.dlg.input_ln.text()
v=t_e.rstrip('\n').split(';')
#Create New Polygon
points =[]
for p in v:
point=QgsPoint( float(p.split(',')[0]), float(p.split(',')[1]))
points.append(point)
polygon = QgsGeometry.fromPolygon([points])
selectedLayerIndex = self.dlg.cmb_TOC.currentIndex()
selectedLayer = layers[selectedLayerIndex]
pr = selectedLayer.dataProvider()
poly = QgsFeature()
poly.setGeometry(polygon)
pr.addFeatures([poly])
selectedLayer.updateExtents()
selectedLayer.triggerRepaint()
希望对你有帮助
注意:我重复使用了另一个示例
是的,如果你使用Python,可以通过Python创建一个多边形