相关参考:QGIS中提取面的中心坐标到属性表-CSDN博客
polygon_layer = QgsProject.instance().mapLayersByName("Polygon")[0]polygon_layer.startEditing()
polygon_layer.dataProvider().addAttributes([QgsField("coors", QVariant.String, len=255)])
polygon_layer.updateFields()for polygon_feature in polygon_layer.getFeatures():polygon_geom = polygon_feature.geometry()vertices_list = []for vertex in polygon_geom.vertices():x = round(vertex.x(), 8)y = round(vertex.y(), 8)vertices_list.append(f"[{x:.8f}, {y:.8f}]")# 将顶点坐标列表转换为字符串,并在首尾加上方括号vertices_value = "[" + ", ".join(vertices_list) + "]"polygon_layer.changeAttributeValue(polygon_feature.id(),polygon_layer.fields().indexFromName("coors"), vertices_value)polygon_layer.commitChanges()
执行之后,Polygon图层中多了一个字段coors,值为[[90.90873336,26.43078741],[91.90873336,26.43078741],[92.90873336,26.43078741],[90.90873336,26.43078741]]这种形式