CTAmap数据处理的代码
# 数据处理代码
原理:通过指定要素名称、要素的区划码,批量修改曾用名、备注信息。
# 实现省级属性对照到英文type1属性中:
import arcpy
# 定义工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2024年初地级"
# 定义省级类型对应关系字典
type_mapping = {
"自治区": "Autonomous Region",
"直辖市": "Municipality",
"省": "Province",
"特别行政区": "Special District",
"不统计": "NULL"
}
# 更新要素属性的函数
def update_type1(feature_class):
count = 0
with arcpy.da.UpdateCursor(feature_class, ["省级类", "TYPE_1"]) as cursor:
for row in cursor:
province_type = row[0] # 省级类
if province_type in type_mapping:
row[1] = type_mapping[province_type] # 更新 TYPE_1
cursor.updateRow(row)
count += 1
return count
# 执行更新并显示结果
try:
updated_count = update_type1(feature_class)
print(f"更新完成,共更新了 {updated_count} 条记录。")
except Exception as e:
print(f"更新过程中出现错误: {str(e)}")
# 实现地级属性对照到英文type2属性中:
import arcpy
# 定义工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2014年县级"
# 定义地级类型对应关系字典
type_mapping = {
"地级市": "Prefecture City",
"地区": "Prefecture",
"自治州": "Autonomous Prefecture",
"盟": "League",
"不统计": "NULL"
}
# 更新要素属性的函数
def update_type2(feature_class):
count = 0
with arcpy.da.UpdateCursor(feature_class, ["地级类", "TYPE_2"]) as cursor:
for row in cursor:
prefecture_type = row[0] # 地级类
if prefecture_type in type_mapping:
row[1] = type_mapping[prefecture_type] # 更新 TYPE_2
cursor.updateRow(row)
count += 1
return count
# 执行更新并显示结果
try:
updated_count = update_type2(feature_class)
print(f"更新完成,共更新了 {updated_count} 条记录。")
except Exception as e:
print(f"更新过程中出现错误: {str(e)}")
# 实现县级属性对照到英文type3属性中:
import arcpy
# 定义工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2009年县级"
# 定义县级类型对应关系字典
type_mapping = {
"市辖区": "District",
"旗": "Banner",
"县": "County",
"自治旗": "Autonomous Banner",
"县级市": "County City",
"林区": "Forestry Area",
"自治县": "Autonomous County",
"特区": "Special District",
"不统计": "NULL"
}
# 更新要素属性的函数
def update_type3(feature_class):
count = 0
with arcpy.da.UpdateCursor(feature_class, ["县级类", "TYPE_3"]) as cursor:
for row in cursor:
county_type = row[0] # 县级类
if county_type in type_mapping:
row[1] = type_mapping[county_type] # 更新 TYPE_3
cursor.updateRow(row)
count += 1
return count
# 执行更新并显示结果
try:
updated_count = update_type3(feature_class)
print(f"更新完成,共更新了 {updated_count} 条记录。")
except Exception as e:
print(f"更新过程中出现错误: {str(e)}")
# 打印每个省有哪些县
# -*- coding: utf-8 -*-
import arcpy
# 设置工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2009年县级"
# 使用SearchCursor来查询浙江省的市辖区
print("浙江省的市辖区列表:")
with arcpy.da.SearchCursor(feature_class, ["地名", "县级类"],
"省级 = '浙江省' AND 县级类 = '市辖区'") as cursor:
for row in cursor:
print(row[0]) # 打印地名
# 检查每个省份的县级类型的个数
# -*- coding: utf-8 -*-
import arcpy
import collections
# 设置工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2024年初县级"
# 定义县级类类型列表
county_types = ['市辖区', '县级市', '县', '自治县', '旗', '自治旗', '林区', '特区','不统计']
# 创建一个嵌套的defaultdict来存储统计结果
stats = collections.defaultdict(lambda: collections.defaultdict(int))
# 使用SearchCursor来遍历要素类
with arcpy.da.SearchCursor(feature_class, ["省级", "县级类"]) as cursor:
for row in cursor:
province = row[0]
county_type = row[1]
stats[province][county_type] += 1
# 打印统计结果
print("各省级单位县级类统计结果:")
# 打印表头
header = "省级"
for type_name in county_types:
header += f"\t{type_name}"
header += "\t总计"
print(header)
# 打印各省数据
for province, types in stats.items():
# 计算总计时排除"不统计"项
total = sum(count for type_name, count in types.items() if type_name != '不统计')
line = province
for type_name in county_types:
line += f"\t{types[type_name]}"
line += f"\t{total}"
print(line)
# 计算总计
total_stats = collections.defaultdict(int)
for types in stats.values():
for county_type, count in types.items():
total_stats[county_type] += count
# 计算总数时排除"不统计"项
total_sum = sum(count for type_name, count in total_stats.items() if type_name != '不统计')
print("\n总计:")
for type_name in county_types:
print(f"{type_name}: {total_stats[type_name]}")
print(f"总数: {total_sum}")
# 地级类数量统计
# -*- coding: utf-8 -*-
import arcpy
import collections
# 设置工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2015年初地级"
# 创建一个嵌套的defaultdict来存储统计结果
stats = collections.defaultdict(lambda: collections.defaultdict(int))
# 使用SearchCursor来遍历要素类
with arcpy.da.SearchCursor(feature_class, ["省级", "地级类"]) as cursor:
for row in cursor:
province = row[0]
city_type = row[1]
stats[province][city_type] += 1
# 打印统计结果
print("各省级单位地级类统计结果:")
print("省级\t地级市\t地区\t自治州\t盟\t不统计\t总计")
for province, types in stats.items():
total = sum(types.values())
print(f"{province}\t{types['地级市']}\t{types['地区']}\t{types['自治州']}\t{types['盟']}\t{types['不统计']}\t{total}")
# 计算总计
total_stats = collections.defaultdict(int)
for types in stats.values():
for city_type, count in types.items():
total_stats[city_type] += count
total_sum = sum(total_stats.values())
print("\n总计:")
print(f"地级市: {total_stats['地级市']}")
print(f"地区: {total_stats['地区']}")
print(f"自治州: {total_stats['自治州']}")
print(f"盟: {total_stats['盟']}")
print(f"不统计: {total_stats['不统计']}")
print(f"总数: {total_sum}")
# 县级转区,名字不变,修改2014年-2023年,添加曾用名属性
# -*- coding: utf-8 -*-
import arcpy
#需要寻找的行政区划代码
find_code="370812"
#曾用名
old_name="兖州市"
#备注
note="2013年撤销兖州市,设立兖州区"
# 定义工作空间
workspace = r"F:\Baidusyndisk\论文\CT\amap\工程文件\2013年前行政区划数据\2013年前行政区划数据.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_classes = [f"T{year}年县级" for year in range(2014, 2024)]
def update_attributes(feature_class):
modified = False
with arcpy.da.UpdateCursor(feature_class, ["区划码", "曾用名", "备注"]) as cursor:
for row in cursor:
if row[0] == find_code:
if not row[1].strip() and not row[2].strip(): # 检查 "曾用名" 和 "备注" 是否为空或仅包含空格
row[cursor.fields.index("曾用名")] = old_name # 更新曾用名
row[cursor.fields.index("备注")] = note # 更新备注信息
cursor.updateRow(row)
modified = True
else:
print(f"{feature_class} 已存在信息 - 曾用名: {row[1]}, 备注: {row[2]}")
return modified
# 对每个要素类进行更新并记录修改信息
for fc in feature_classes:
if update_attributes(fc):
print(f"{fc} 信息已修改。")
else:
print(f"{fc} 信息未修改。")
print("更新完成。")
# 地级转区,名字不变,修改2014年-2023年,添加曾用名属性
# -*- coding: utf-8 -*-
import arcpy
#需要寻找的行政区划代码
find_code="630200"
#曾用名
old_name="海东地区"
#备注
note="2013年撤销海东地区,设立海东是"
# 定义工作空间
workspace = r"F:\Baidusyndisk\论文\CT\amap\工程文件\2013年前行政区划数据\2013年前行政区划数据.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_classes = [f"T{year}年地级" for year in range(2014, 2024)]
def update_attributes(feature_class):
modified = False
with arcpy.da.UpdateCursor(feature_class, ["区划码", "曾用名", "备注"]) as cursor:
for row in cursor:
if row[0] == find_code:
if not row[1].strip() and not row[2].strip(): # 检查 "曾用名" 和 "备注" 是否为空或仅包含空格
row[cursor.fields.index("曾用名")] = old_name # 更新曾用名
row[cursor.fields.index("备注")] = note # 更新备注信息
cursor.updateRow(row)
modified = True
else:
print(f"{feature_class} 已存在信息 - 曾用名: {row[1]}, 备注: {row[2]}")
return modified
# 对每个要素类进行更新并记录修改信息
for fc in feature_classes:
if update_attributes(fc):
print(f"{fc} 信息已修改。")
else:
print(f"{fc} 信息未修改。")
print("更新完成。")
# 县级转区,2013年属性更新,修改英文名
# -*- coding: utf-8 -*-
import arcpy
# 需要寻找的2013年行政区划代码
find_code = "320107"
# 地名
name = "下关区"
# 新的区划码
code = "320107"
# 县级类 县 县级市 市辖区 旗 自治旗
xianji_lei = "市辖区"
# ENG_NAME
ENG_NAME = "Xiaguan"
# VAR_NAME
VAR_NAME = "Xià Guān"
# TYPE_3 County District County City
TYPE_3 = "District"
# 定义工作空间
workspace = r"F:\Baidusyndisk\论文\CT\amap\工程文件\2013年前行政区划数据\2013年前行政区划数据.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2013年初县级"
# 更新要素属性的函数
def update_attributes(feature_class):
modified = False
with arcpy.da.UpdateCursor(feature_class, ["地名", "区划码", "县级", "县级码", "县级类", "ENG_NAME", "VAR_NAME", "code", "NAME_3", "VAR_NAME3", "GID_3", "TYPE_3"]) as cursor:
for row in cursor:
if row[cursor.fields.index("区划码")] == find_code:
row[cursor.fields.index("地名")] = name # 地名
row[cursor.fields.index("县级")] = name # 县级名称
row[cursor.fields.index("县级码")] = code # 县级码
row[cursor.fields.index("县级类")] = xianji_lei # 县级类
row[cursor.fields.index("ENG_NAME")] = ENG_NAME # 英文名
row[cursor.fields.index("VAR_NAME")] = VAR_NAME # VAR_NAME
row[cursor.fields.index("code")] = code
row[cursor.fields.index("NAME_3")] = ENG_NAME
row[cursor.fields.index("VAR_NAME3")] = VAR_NAME
row[cursor.fields.index("GID_3")] = code
row[cursor.fields.index("TYPE_3")] = TYPE_3
cursor.updateRow(row)
modified = True
return modified
# 更新要素类并记录修改信息
if update_attributes(feature_class):
print(f"{feature_class} 信息已修改。")
print("更新完成。")
# 县级转区,2013年属性更新,不修改英文名
# -*- coding: utf-8 -*-
import arcpy
# 需要寻找的2013年行政区划代码
find_code = "230833"
# 地名
name = "抚远市"
# 新的区划码
code = "230883"
# 县级类 县 县级市 市辖区 旗 自治旗
xianji_lei = "县级市"
# TYPE_3 County District County City
TYPE_3 = "County City"
# 定义工作空间
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# 要素类名称
feature_class = "T2024年初县级"
# 更新要素属性的函数
def update_attributes(feature_class):
modified = False
with arcpy.da.UpdateCursor(feature_class, ["区划码", "地名", "县级", "县级码", "县级类", "code", "GID_3", "TYPE_3", "区划码"]) as cursor:
for row in cursor:
if row[0] == find_code:
row[cursor.fields.index("地名")] = name # 地名
row[cursor.fields.index("县级")] = name # 县级名称
row[cursor.fields.index("县级码")] = code # 县级码
row[cursor.fields.index("县级类")] = xianji_lei # 县级类
row[cursor.fields.index("code")] = code
row[cursor.fields.index("GID_3")] = code
row[cursor.fields.index("TYPE_3")] = TYPE_3
row[cursor.fields.index("区划码")] = code
cursor.updateRow(row)
modified = True
return modified
# 更新要素类并记录修改信息
if update_attributes(feature_class):
print(f"{feature_class} 信息已修改。")
print("更新完成。")
上次更新: 2024/11/21, 14:52:08