因为在使用的时候没找到可参考资料,在师傅指导下才完成的数据筛选,记录一下过程。因为是新新新手,请大家谨慎食用。
一、在函数表达式界面,点加号按钮新添py脚本,起个名字
二、qgis他给出的默认代码,保留import部分,去掉函数的部分自己写,比如我的是这样。
from qgis.core import *
from qgis.gui import *
@qgsfunction(group='Custom', referenced_columns=[])
def my_filter(value1):
"""把字符串变成数字"""
if "X" in value1:
# print(value1, "/n============")
value3 = [str.replace(value1, "X", ",")]
# print(value3, "/n============")
if "+" in value3[0]:
value2 = [str.replace(value3[0], "+", ",")]
# print("+++++", value2)
else:
value2 = value3
# print("$$$$$$", value2)
elif "+" in value1:
value2 = [str.replace(value1, "+", ",")]
elif "Y2" in value1:
value2 = [str.replace(value1, "Y2", "")]
elif "Z2" in value1:
value2 = [str.replace(value1, "Z2", "")]
elif "Y3" in value1:
value2 = [str.replace(value1, "Y3", "")]
else:
value2 = value1
if type(value2) == list:
list1 = value2[0].split(",")
for i in range(len(list1)):
list1[i] = int(list1[i])
list1.sort(reverse=1)
value2 = list1[0]
return value2
在写的过程中最难的是调试这些数据类型处理过程中的报错。。。
还有就是要理解qgis的处理对象,因为我要按照字段处理每个单元格的值,我处理的是单元格值对象,他是该字段定义的数据类型如STR,而不是列表或者array等其他数据类型。
三、在vscode调试时,需要输入单个数据进行调试,在vscode中添加这样一段进行调试使用。
if __name__ == "__main__":
for i in range(10):
num = input("输入你的数据")
val = my_filter(num)
print(val)
10.20更新
把上述问题用gdal解决了一下!遇到的问题有数据创建和保存时各个层级的数据的关系,中文编码如中文字段的创建存在乱码问题
from osgeo import ogr, gdal
def pip_filter(inshp):
# 1.打开原数据读取信息
datasource = ogr.Open(inshp)
print("数据信息", datasource)
inlayer = datasource.GetLayer(0)
print(inlayer)
print("图层中要素数:", inlayer.GetFeatureCount())
# 获取文件空间参考
spatial = inlayer.GetSpatialRef()
# 2.创建新文件,要素另存
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
driver = ogr.GetDriverByName("ESRI Shapefile")
new_file = driver.CreateDataSource("filtered_pipe.shp")
# 设置图层对象
new_layer = new_file.CreateLayer("pipe", spatial, ogr.wkbLineString)
inlayer_defn = inlayer.GetLayerDefn()
# 复制全部的字段
field_names = []
for i in range(0, inlayer_defn.GetFieldCount()):
field_defn = inlayer_defn.GetFieldDefn(i)
field_name = field_defn.name
field_names.append(field_name)
# 输出layer创建字段
new_layer.CreateField(field_defn)
# 2.遍历要素,将数据清洗为有效值
inlayer.ResetReading()
for i in range(inlayer.GetFeatureCount()):
feature1 = inlayer.GetNextFeature()
# while feature1:
# print("-------------------开始判断---------------")
# feature1=layer.GetNextFeature()
# print('要素:',feature1.keys())
value1 = feature1.GetField("管径")
# print('初始值:',value1,)
# print(type(value1))
if "X" in value1:
# print(value1, "/n============")
value3 = [str.replace(value1, "X", ",")]
# print(value3, "/n============")
if "+" in value3[0]:
value2 = [str.replace(value3[0], "+", ",")]
# print("+++++", value2)
else:
value2 = value3
# print("$$$$$$", value2)
elif "+" in value1:
value2 = [str.replace(value1, "+", ",")]
elif "Y2" in value1:
value2 = [str.replace(value1, "Y2", "")]
elif "Z2" in value1:
value2 = [str.replace(value1, "Z2", "")]
elif "Y3" in value1:
value2 = [str.replace(value1, "Y3", "")]
else:
value2 = value1
if type(value2) == list:
list1 = value2[0].split(",")
for i in range(len(list1)):
list1[i] = int(list1[i])
list1.sort(reverse=1)
value2 = list1[0]
feature1.SetField("管径", value2)
if int(feature1.GetField("管径")) < 600:
pass
elif int(feature1.GetField("管径")) >= 600:
# print(feature1.keys())
# print(feature1.GetField('管径'))
new_layer.CreateFeature(feature1)
new_file.Destroy()
# new_file.destroy()
# inlayer.SetAttributeFilter("管径 < 600")
# for feature in new_layer:
# print (feature.GetField("管径"))
# 3.筛选数据
# for feature in new_layer:
# print( '看看结果',feature.GetField("管径"))
if __name__ == "__main__":
filepath = r"D:\BaiduSyncdisk\code\python\mygdal\pipe_data\rain_pipe.shp"
pip_filter(filepath)
3.qgis打开数据,乱码问题的解决: