[939] Generate a new shapefile based on a list of records and query polygons from a large shapefile
ref: arcpy.management.MakeFeatureLayer(in_features, out_layer, {where_clause}, {workspace}, {field_info})
# 1. Records are saved at ".csv" files # 2. To match each record with the polygon in the file "QLD" # 3. Remove duplicate records # 4. Generate a new shapefile import os, arcpy import geopandas as gpd root_dir = r"D:\Bingnan_Li\01_Tasks\11_20231109_PDF_reading\Planning_LGA" for i in range(len(os.listdir(root_dir))): folder = os.listdir(root_dir)[i] for file in os.listdir(os.path.join(root_dir, folder)): if file.find("csv") > -1: file_path = os.path.join(root_dir, folder, file) print(" --" + file_path) print() df = pd.read_csv(file_path) arcpy.env.workspace = os.path.join(root_dir, folder) # Create a feature layer for the input shapefile arcpy.MakeFeatureLayer_management("U:\\ADMINISTRATIVE\\Cadastral\\AUS_Cadastre.gdb\\QLD", "Templayer") # Iterate through the list of lot records and select features for i in range(len(df)): lotplan = df.loc[i, "lotplan"] query = f"lotplan = '{lotplan}'" # ADD_TO_SELECTION—The resulting selection will be added to the current selection if one exists. arcpy.SelectLayerByAttribute_management("Templayer", "ADD_TO_SELECTION", query) # Write the selected features to a new feature class arcpy.CopyFeatures_management("Templayer", "feature_result_new.shp") # Use DeleteIdentical to remove duplicates based on specified fields arcpy.management.DeleteIdentical("feature_result_new.shp", ["lotplan"]) # Delete the temporary feature layer arcpy.Delete_management("Templayer") os.chdir(os.path.join(root_dir, folder)) gdf = gpd.read_file("feature_result_new.shp") arcpy.Delete_management("feature_result_new.shp") gdf_new = gdf[['geometry', 'LOT', 'PLAN', 'LOTPLAN']] gdf_new_copy = gdf_new.copy() gdf_new_copy['LGA_NAME'] = [folder] * len(gdf_new_copy) gdf_new_copy.to_file("NEW" + file.replace("csv", "shp"))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-11-16 【769】Python时间戳转换为北京时间
2019-11-16 【452】pandas筛选出表中满足另一个表所有条件的数据
2017-11-16 【269】蓝牙键盘连接
2015-11-16 【177】IDL常见问题解答
2011-11-16 【002】◀▶ C#学习(一) - C#编程基础
2011-11-16 【001】学习计划 - 开始学习C#,用C#