例如,我有一个包含两个列“
Id”和“
geom”的csv文件,其中geom包含您的POLYGON示例。
![enter image description here](https://istack.dev59.com/yFxwV.webp)
前往图层->添加图层->添加分隔文本图层,浏览csv文件并在几何字段下拉框中选择包含WKT数据的列,例如我的是"geom",然后在几何定义中选择(WKT)选项
![enter image description here](https://istack.dev59.com/tG3oO.webp)
结果为:
![enter image description here](https://istack.dev59.com/fr5b7.webp)
另一种方式是使用Python:
uri ='file:///C://Users//fjraga//Desktop//test.csv?delimiter=%s&crs=epsg:4326&wktField=%s' % (",", "geom")
lyr = QgsVectorLayer(uri, 'Test','delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr)
但如果您只想在QGIS Python控制台中加载此WKT几何图形,请尝试以下方法:
wkt = "POLYGON ((79.87749999947846 6.997500000409782, 79.88249999947845 6.997500000409782, 79.88249999947845 7.002500000409782, 79.87749999947846 7.002500000409782, 79.87749999947846 6.997500000409782))"
temp = QgsVectorLayer("Polygon?crs=epsg:4326", "result", "memory")
QgsMapLayerRegistry.instance().addMapLayer(temp)
temp.startEditing()
geom = QgsGeometry()
geom = QgsGeometry.fromWkt(wkt)
feat = QgsFeature()
feat.setGeometry(geom)
temp.dataProvider().addFeatures([feat])
temp.commitChanges()