alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[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})

ref: arcpy.management.SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})


# 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"))

 

posted on   McDelfino  阅读(8)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 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#
点击右上角即可分享
微信分享提示