ARCGIS笔记:对于质心的计算

import arcpy

# Set the workspace (change this to the path of your shapefile or feature class)
arcpy.env.workspace = "D:/GEE红树林/提取红树林/区域红树林/中部区域"

# List of feature classes
feature_classes = [
    "1987", "2000", "2004", "2008", "2011", "2013",
    "2014", "2015", "2016", "2017", "2018", "2019",
    "2020", "2021", "2022", "2023"
]

# Fields to be added
new_fields = [
    ("XX", "FLOAT"),
    ("YY", "FLOAT"),
    ("CXX", "FLOAT"),
    ("CYY", "FLOAT")
]

# Loop through each feature class and perform operations
for fc in feature_classes:
    print "Processing " + fc + "..."
    fields = [field.name for field in arcpy.ListFields(fc)]
    
    # Add fields if they do not already exist
    for field_name, field_type in new_fields:
        if field_name not in fields:
            arcpy.AddField_management(fc, field_name, field_type)
        else:
            print "Field " + field_name + " already exists in " + fc
    
    # Calculate centroid coordinates and multiplied values
    with arcpy.da.UpdateCursor(fc, ["SHAPE@XY", "area", "XX", "YY", "CXX", "CYY"]) as cursor:
        for row in cursor:
            centroid = row[0]  # This is a tuple (x, y)
            area = row[1]
            row[2] = centroid[0]  # XX coordinate
            row[3] = centroid[1]  # YY coordinate
            row[4] = area * centroid[0]  # CXX
            row[5] = area * centroid[1]  # CYY
            cursor.updateRow(row)

    print "Finished processing " + fc + "."

根据论文Dynamic changes in mangroves of the largest delta in northern Beibu Gulf, China: Reasons and causes中对于质心计算的介绍,本文章针对使用python快速增加字段计算质心,
 

第一个
import arcpy
... 
... # Set the workspace (change this to the path of your shapefile or feature class)
... arcpy.env.workspace = "D:/GEE红树林/提取红树林/区域红树林/东部区域"
... 
... # Set the feature class (change this to the name of your feature class)
... feature_class = "1987"
... 2008
... # Add fields
... arcpy.AddField_management(feature_class, "X", "FLOAT")
... arcpy.AddField_management(feature_class, "Y", "FLOAT")
... arcpy.AddField_management(feature_class, "CX", "FLOAT")
... arcpy.AddField_management(feature_class, "CY", "FLOAT")
... 
... # Calculate centroid coordinates and multiplied values
... with arcpy.da.UpdateCursor(feature_class, ["SHAPE@XY", "area", "X", "Y", "CX", "CY"]) as cursor:
...     for row in cursor:
...         centroid = row[0]  # This is a tuple (x, y)
...         area = row[1]
...         row[2] = centroid[0]  # X coordinate
...         row[3] = centroid[1]  # Y coordinate
...         row[4] = area * centroid[0]  # CX
...         row[5] = area * centroid[1]  # CY
...         cursor.updateRow(row)       
第二个多个一起
import arcpy

# Set the workspace (change this to the path of your shapefile or feature class)
arcpy.env.workspace = "D:/GEE红树林/提取红树林/区域红树林/中部区域"

# List of feature classes
feature_classes = [
    "1987", "2000", "2004", "2008", "2011", "2013",
    "2014", "2015", "2016", "2017", "2018", "2019",
    "2020", "2021", "2022", "2023"
]

# Loop through each feature class and perform operations
for fc in feature_classes:
    print "Processing " + fc + "..."

    # Add fields
    arcpy.AddField_management(fc, "X", "FLOAT")
    arcpy.AddField_management(fc, "Y", "FLOAT")
    arcpy.AddField_management(fc, "CX", "FLOAT")
    arcpy.AddField_management(fc, "CY", "FLOAT")

    # Calculate centroid coordinates and multiplied values
    with arcpy.da.UpdateCursor(fc, ["SHAPE@XY", "area", "X", "Y", "CX", "CY"]) as cursor:
        for row in cursor:
            centroid = row[0]  # This is a tuple (x, y)
            area = row[1]
            row[2] = centroid[0]  # X coordinate
            row[3] = centroid[1]  # Y coordinate
            row[4] = area * centroid[0]  # CX
            row[5] = area * centroid[1]  # CY
            cursor.updateRow(row)

    print "Finished processing " + fc + "."
第三个
import arcpy

# Set the workspace (change this to the path of your shapefile or feature class)
arcpy.env.workspace = "D:/GEE红树林/提取红树林/区域红树林/东部区域"

# List of feature classes
feature_classes = [
    "1987", "2000", "2004", "2008", "2011", "2013",
    "2014", "2015", "2016", "2017", "2018", "2019",
    "2020", "2021", "2022", "2023"
]

# Fields to be added
new_fields = [
    ("XX", "FLOAT"),
    ("YY", "FLOAT"),
    ("CXX", "FLOAT"),
    ("CYY", "FLOAT")
]

# Loop through each feature class and perform operations
for fc in feature_classes:
    print "Processing " + fc + "..."
    fields = [field.name for field in arcpy.ListFields(fc)]
    
    # Add fields if they do not already exist
    for field_name, field_type in new_fields:
        if field_name not in fields:
            arcpy.AddField_management(fc, field_name, field_type)
        else:
            print "Field " + field_name + " already exists in " + fc
    
    # Calculate centroid coordinates and multiplied values
    with arcpy.da.UpdateCursor(fc, ["SHAPE@XY", "area"] + [f[0] for f in new_fields]) as cursor:
        for row in cursor:
            centroid = row[0]  # This is a tuple (x, y)
            area = row[1]
            row[2] = centroid[0]  # X coordinate
            row[3] = centroid[1]  # Y coordinate
            row[4] = area * centroid[0]  # CX
            row[5] = area * centroid[1]  # CY
            cursor.updateRow(row)

    print "Finished processing " + fc + "."
#计算总和代码
import arcpy

# Set the workspace (change this to the path of your shapefile or feature class)
arcpy.env.workspace = "D:/GEE红树林/提取红树林/区域红树林/东部区域"

# List of feature classes
feature_classes = [
    "1987", "2000", "2004", "2008", "2011", "2013",
    "2014", "2015", "2016", "2017", "2018", "2019",
    "2020", "2021", "2022", "2023"
]

# Dictionary to store sum of the fields for each feature class
sums_dict = {fc: {'area': 0, 'CXX': 0, 'CYY': 0} for fc in feature_classes}

# Loop through each feature class and sum the fields
for fc in feature_classes:
    print "Processing " + fc + "..."
    
    # Initialize sums to zero
    total_area = 0
    total_cxx = 0
    total_cyy = 0

    # Sum the values for area, CXX, and CYY
    with arcpy.da.SearchCursor(fc, ["area", "CXX", "CYY"]) as cursor:
        for row in cursor:
            total_area += row[0]
            total_cxx += row[1]
            total_cyy += row[2]

    # Store the sums in the dictionary
    sums_dict[fc]['area'] = total_area
    sums_dict[fc]['CXX'] = total_cxx
    sums_dict[fc]['CYY'] = total_cyy

    print "Finished processing " + fc + "."

# Print out the sums for each feature class
for fc, sums in sums_dict.items():
    print "Year: " + fc
    print "Total Area: " + str(sums['area'])
    print "Total CXX: " + str(sums['CXX'])
    print "Total CYY: " + str(sums['CYY'])
    print "-----"
import arcpy

# Set the workspace
arcpy.env.workspace = "D:/GEE红树林/提取红树林/区域红树林/东部区域"

# List of feature classes
feature_classes = [
    "1987", "2000", "2004", "2008", "2011", "2013",
    "2014", "2015", "2016", "2017", "2018", "2019",
    "2020", "2021", "2022", "2023"
]
# 要添加的字段
new_fields = [
    ("XXXX", "FLOAT"),
    ("YYYY", "FLOAT"),
    ("CXXXX", "FLOAT"),
    ("CYYYY", "FLOAT")
]

# 遍历每个特征类并执行操作
for fc in feature_classes:
    fc_path = arcpy.env.workspace + "/" + fc
    print("正在处理特征类: {}".format(fc_path))
    if arcpy.Exists(fc_path):
        # 继续执行剩下的代码...
        print("找到特征类: {}".format(fc_path))
    else:
        print("未找到特征类: {}".format(fc_path))
        # 如果找不到特征类,跳过此次循环的其余部分
        continue

# Loop through each feature class and perform operations
for fc in feature_classes:
    print("Processing " + fc + "...")

    # Project the feature class to WGS 1984 to get decimal degrees
    projected_fc = "{}_WGS1984".format(fc)
    arcpy.Project_management(fc, projected_fc, arcpy.SpatialReference(4326))  # WGS 1984

    # Add fields if they do not already exist
    fields = [field.name for field in arcpy.ListFields(projected_fc)]
    for field_name, field_type in new_fields:
        if field_name not in fields:
            arcpy.AddField_management(projected_fc, field_name, field_type)
        else:
            print("Field {} already exists in {}".format(field_name, projected_fc))

    # Calculate centroid coordinates in decimal degrees and multiplied values
    with arcpy.da.UpdateCursor(projected_fc, ["SHAPE@XY", "area", "XXXX", "YYYY", "CXXXX", "CYYYY"]) as cursor:
        for row in cursor:
            centroid = row[0]  # This is a tuple (x, y)
            area = row[1]
            row[2] = centroid[0]  # XXXX coordinate in decimal degrees
            row[3] = centroid[1]  # YYYY coordinate in decimal degrees
            row[4] = area * centroid[0]  # CXXXX
            row[5] = area * centroid[1]  # CYYYY
            cursor.updateRow(row)

    print("Finished processing {}".format(projected_fc))
import arcpy

# 设置工作空间
workspace_path = "D:/GEE红树林/提取红树林/区域红树林/中部区域"
arcpy.env.workspace = workspace_path
print("工作空间设置为: {}".format(workspace_path))

# 要处理的特征类列表
feature_classes = [
    # ... [你的特征类列表] ...
]

print("将要处理的特征类: {}".format(feature_classes))

# 要添加的字段
new_fields = [
    ("XXXX", "FLOAT"),
    ("YYYY", "FLOAT"),
    ("CXXXX", "FLOAT"),
    ("CYYYY", "FLOAT")
]

# 遍历每个特征类并执行操作
for fc in feature_classes:
    fc_path = arcpy.env.workspace + "/" + fc
    print("正在处理特征类: {}".format(fc_path))
    if arcpy.Exists(fc_path):
        # 继续执行剩下的代码...
        print("找到特征类: {}".format(fc_path))
    else:
        print("未找到特征类: {}".format(fc_path))
        # 如果找不到特征类,跳过此次循环的其余部分
        continue

    # 你的其他代码...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值