简体   繁体   English

用于验证用户在 pyqgis 中所做的条目的脚本

[英]Script for validating the entries made by the user in pyqgis

I am writing a script in pyqgis which should perform the following task:我正在 pyqgis 中编写一个脚本,它应该执行以下任务:

  1. First it should read a csv file which we consider it as a input csv,csv file will be something like this:首先它应该读取一个 csv 文件,我们将其视为输入 csv,csv 文件将是这样的:

     layer,columnname,condition,values layer1,id,not blank, layer1,type,matching,type1,type2,type3,NA layer2,gisid,not blank,integer, layer2,layer_condition,matching,a,b,c,d
  2. It should read layer name unique and check whether those layer is present or not它应该读取唯一的图层名称并检查这些图层是否存在

  3. layer will be having the columns mentioned in the csv it should check based on the condition column for example in layer1 for column id condition is not blank so all the features in the layer should not have any null values or blank spaces and for column like type condition is matching so all the feautre values should have one of the values mentioned in the values column.层将具有 csv 中提到的列,它应该根据条件列进行检查,例如在 layer1 中,列 id 条件不为空,因此该层中的所有特征不应有任何 null 值或空格以及类似列的类型条件匹配,因此所有特征值都应具有值列中提到的值之一。

So far i have tried the following code:到目前为止,我已经尝试了以下代码:

import csv

layer=QgsProject.instance().mapLayersByName('layer1')[0]

with open(r'layer1_Validation.csv') as file_obj:

    reader_obj = csv.DictReader(file_obj)

    for i,row in enumerate(reader_obj):
        
        if row['Condition'] == "Matching":

            a=row['values'].split(',')

            column=row['ColumnName']

            print(a)

            print(column)

            for f in layer.getFeatures():

                for s in a:

                    if f[column] == a:

                        print(f.id())

            break

For the above question i tried my self and came up with the following code:对于上述问题,我尝试了自己并想出了以下代码:

import csv
import pandas as pd
csv_path = r"E:\sample.csv"
df=pd.read_csv(csv_path)
df1=df.loc[df['Condition'].eq('Matching')]['ColumnName'].values


for i in df['LayerName'].unique():
    layer=QgsProject.instance().mapLayersByName(i)[0]
    for field in layer.fields():
        current_field=field.name()
            if (current_field in  df1):
                with open(csv_path) as file_obj:
                    reader_obj = csv.DictReader(file_obj)
                    for row in reader_obj:
                        if row['Condition'] == "Matching" and row['ColumnName'] == current_field:
                            a=(word.strip() for word in row['values'].split(','))
                            query = "Select PID" + " from " + layer.name()+" where "+current_field+" not in "+str(tuple(a))+" or "+current_field+" is NULL group by PID"
                            vlayer = QgsVectorLayer( "?query={}".format(query), layer.name()+"_"+current_field+"_errors", "virtual" )
                             QgsProject.instance().addMapLayer(vlayer)
                             index = vlayer.featureCount()
                             vlayer.setName(vlayer.name()+"[{}]".format(str(index)))
                             if index == 0:
                                 QgsProject.instance().removeMapLayer(vlayer)

Above script will take the csv file as input and for the matching condition it checks and if their are any errors it creates the virtual layer上面的脚本将 csv 文件作为输入,它检查匹配条件,如果它们有任何错误,它会创建虚拟层

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM