CTAmap Data-Processing Code
# Data-Processing Code
Principle: by specifying the feature name and the feature's administrative-division code, batch-modify the former-name and remarks information.
# Mapping Province-Level Attributes to the English Type1 Attribute:
import arcpy
# Define workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2024_Early_Prefecture"
# Dictionary mapping province-level types
type_mapping = {
"自治区": "Autonomous Region",
"直辖市": "Municipality",
"省": "Province",
"特别行政区": "Special Administrative Region",
"不统计": "NULL"
}
# Function to update feature attributes
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] # Province-level type
if province_type in type_mapping:
row[1] = type_mapping[province_type] # Update TYPE_1
cursor.updateRow(row)
count += 1
return count
# Execute update and display result
try:
updated_count = update_type1(feature_class)
print(f"Update complete. {updated_count} records updated in total.")
except Exception as e:
print(f"Error during update: {str(e)}")
# Mapping Prefecture-Level Attributes to the English Type2 Attribute:
import arcpy
# Define workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2014_County"
# Dictionary mapping prefecture-level types
type_mapping = {
"地级市": "Prefecture-level city",
"地区": "Prefecture",
"自治州": "Autonomous Prefecture",
"盟": "League",
"不统计": "NULL"
}
# Function to update feature attributes
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] # Prefecture-level type
if prefecture_type in type_mapping:
row[1] = type_mapping[prefecture_type] # Update TYPE_2
cursor.updateRow(row)
count += 1
return count
# Execute update and display result
try:
updated_count = update_type2(feature_class)
print(f"Update complete. {updated_count} records updated in total.")
except Exception as e:
print(f"Error during update: {str(e)}")
# Mapping County-Level Attributes to the English Type3 Attribute:
import arcpy
# Define workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2009_County"
# Dictionary mapping county-level types
type_mapping = {
"市辖区": "District",
"旗": "Banner",
"县": "County",
"自治旗": "Autonomous Banner",
"县级市": "County-level city",
"林区": "Forestry Area",
"自治县": "Autonomous County",
"特区": "Special District",
"不统计": "NULL"
}
# Function to update feature attributes
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] # County-level type
if county_type in type_mapping:
row[1] = type_mapping[county_type] # Update TYPE_3
cursor.updateRow(row)
count += 1
return count
# Execute update and display result
try:
updated_count = update_type3(feature_class)
print(f"Update complete. {updated_count} records updated in total.")
except Exception as e:
print(f"Error during update: {str(e)}")
# Print the Counties in Each Province
# -*- coding: utf-8 -*-
import arcpy
# Set workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2009_County"
# Use SearchCursor to query districts of Zhejiang Province
print("List of districts in Zhejiang Province (浙江省):")
with arcpy.da.SearchCursor(feature_class, ["地名", "县级类"],
"省级 = '浙江省' AND 县级类 = '市辖区'") as cursor:
for row in cursor:
print(row[0]) # Print place name
# Check the Count of Each County-Level Type per Province
# -*- coding: utf-8 -*-
import arcpy
import collections
# Set workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2024_Early_County"
# Define the list of county-level types
county_types = ['市辖区', '县级市', '县', '自治县', '旗', '自治旗', '林区', '特区','不统计']
# Create a nested defaultdict to store statistics
stats = collections.defaultdict(lambda: collections.defaultdict(int))
# Use SearchCursor to traverse the feature class
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 statistics
print("Statistics of county-level types for each province-level unit:")
# Print header
header = "Province"
for type_name in county_types:
header += f"\t{type_name}"
header += "\tTotal"
print(header)
# Print data for each province
for province, types in stats.items():
# Exclude "Not counted" items from the total
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)
# Compute totals
total_stats = collections.defaultdict(int)
for types in stats.values():
for county_type, count in types.items():
total_stats[county_type] += count
# Exclude "Not counted" items from the total
total_sum = sum(count for type_name, count in total_stats.items() if type_name != '不统计')
print("\nTotal:")
for type_name in county_types:
print(f"{type_name}: {total_stats[type_name]}")
print(f"Total: {total_sum}")
# Prefecture-Level Type Count Statistics
# -*- coding: utf-8 -*-
import arcpy
import collections
# Set workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2015_Early_Prefecture"
# Create a nested defaultdict to store statistics
stats = collections.defaultdict(lambda: collections.defaultdict(int))
# Use SearchCursor to traverse the feature class
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 statistics
print("Statistics of prefecture-level types for each province-level unit:")
print("Province\tPrefecture-level city\tPrefecture\tAutonomous Prefecture\tLeague\tNot Counted\tTotal")
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}")
# Compute totals
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("\nTotal:")
print(f"Prefecture-level city: {total_stats['地级市']}")
print(f"Prefecture: {total_stats['地区']}")
print(f"Autonomous Prefecture: {total_stats['自治州']}")
print(f"League: {total_stats['盟']}")
print(f"Not Counted: {total_stats['不统计']}")
print(f"Total: {total_sum}")
# County-to-District Conversion, Name Unchanged; Modify 2014–2023 and Add Former-Name Attribute
# -*- coding: utf-8 -*-
import arcpy
# Administrative-division code to find
find_code="370812"
# Former name
old_name="Yanzhou City (兖州市)"
# Remarks
note="In 2013, Yanzhou City (兖州市) was abolished and Yanzhou District (兖州区) was established"
# Define workspace
workspace = r"F:\Baidusyndisk\论文\CT\amap\工程文件\2013年前行政区划数据\2013年前行政区划数据.gdb"
arcpy.env.workspace = workspace
# Feature class names
feature_classes = [f"T{year}_County" 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(): # Check whether "Former name" and "Remarks" are empty or contain only spaces
row[cursor.fields.index("曾用名")] = old_name # Update former name
row[cursor.fields.index("备注")] = note # Update remarks
cursor.updateRow(row)
modified = True
else:
print(f"{feature_class} Information already exists - Former name: {row[1]}, Remarks: {row[2]}")
return modified
# Update each feature class and log the modifications
for fc in feature_classes:
if update_attributes(fc):
print(f"{fc} Information modified.")
else:
print(f"{fc} Information not modified.")
print("Update complete.")
# Prefecture-to-District Conversion, Name Unchanged; Modify 2014–2023 and Add Former-Name Attribute
# -*- coding: utf-8 -*-
import arcpy
# Administrative-division code to find
find_code="630200"
# Former name
old_name="Haidong Prefecture (海东地区)"
# Remarks
note="In 2013, Haidong Prefecture (海东地区) was abolished and Haidong City (海东市) was established"
# Define workspace
workspace = r"F:\Baidusyndisk\论文\CT\amap\工程文件\2013年前行政区划数据\2013年前行政区划数据.gdb"
arcpy.env.workspace = workspace
# Feature class names
feature_classes = [f"T{year}_Prefecture" 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(): # Check whether "Former name" and "Remarks" are empty or contain only spaces
row[cursor.fields.index("曾用名")] = old_name # Update former name
row[cursor.fields.index("备注")] = note # Update remarks
cursor.updateRow(row)
modified = True
else:
print(f"{feature_class} Information already exists - Former name: {row[1]}, Remarks: {row[2]}")
return modified
# Update each feature class and log the modifications
for fc in feature_classes:
if update_attributes(fc):
print(f"{fc} Information modified.")
else:
print(f"{fc} Information not modified.")
print("Update complete.")
# County-to-District Conversion, 2013 Attribute Update, Modify English Name
# -*- coding: utf-8 -*-
import arcpy
# 2013 administrative-division code to find
find_code = "320107"
# Place name
name = "Xiaguan District (下关区)"
# New administrative-division code
code = "320107"
# County-level type: County, County-level City, District, Banner, Autonomous Banner
xianji_lei = "District"
# ENG_NAME
ENG_NAME = "Xiaguan"
# VAR_NAME
VAR_NAME = "Xià Guān"
# TYPE_3 County District County-level city
TYPE_3 = "District"
# Define workspace
workspace = r"F:\Baidusyndisk\论文\CT\amap\工程文件\2013年前行政区划数据\2013年前行政区划数据.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2013_Early_County"
# Function to update feature attributes
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 # Place name
row[cursor.fields.index("县级")] = name # County name
row[cursor.fields.index("县级码")] = code # County code
row[cursor.fields.index("县级类")] = xianji_lei # County type
row[cursor.fields.index("ENG_NAME")] = ENG_NAME # English 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
# Update feature class and log the modifications
if update_attributes(feature_class):
print(f"{feature_class} Information modified.")
print("Update complete.")
# County-to-District Conversion, 2013 Attribute Update, Do Not Modify English Name
# -*- coding: utf-8 -*-
import arcpy
# 2013 administrative-division code to find
find_code = "230833"
# Place name
name = "Fuyuan City (抚远市)"
# New administrative-division code
code = "230883"
# County-level type: County, County-level City, District, Banner, Autonomous Banner
xianji_lei = "County-level City"
# TYPE_3 County District County-level city
TYPE_3 = "County-level city"
# Define workspace
workspace = r"F:\PAPER\SHENGSHIXIAN\shengshixian\shengshxiain.gdb"
arcpy.env.workspace = workspace
# Feature class name
feature_class = "T2024_Early_County"
# Function to update feature attributes
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 # Place name
row[cursor.fields.index("县级")] = name # County name
row[cursor.fields.index("县级码")] = code # County code
row[cursor.fields.index("县级类")] = xianji_lei # County type
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
# Update feature class and log the modifications
if update_attributes(feature_class):
print(f"{feature_class} Information modified.")
print("Update complete.")
Last Updated: 2026/09/01, 08:25:20