因此,我尝试使用 arcpy 编写阿拉巴马州的人口密度图,并且我有一个显示人口密度的输出 .tif 文件,唯一的问题是它是灰度的,我想为其分配自定义颜色价值。这是最后几行。
import arcpy
from pathlib import Path
from arcpy.sa import *
#Data from: https://hub.arcgis.com/datasets/RPCGB::alabama-census-tracts-2020/about
# Set workspace
work_path = Path(r'C:\Users\eebbert\Documents\ArcGIS\Projects')
data_path = Path(r'C:\Users\eebbert\Downloads\Alabama_Census_Tracts2C_2020')
save_path = Path(r'C:\Users\eebbert\Documents\Final_Outputs')
gdb_path = work_path / 'ess408_gdb.gdb'
arcpy.env.overwriteOutput = True #overwrites existing files automatically
#CREATING COUNTY BOUNDARIES
# Input census tract shapefile for Alabama
census_data = data_path / 'Alabama_Census_Tracts2C_2020.shp'
# Output county boundaries shapefile
county_boundaries = save_path / 'Alabama_County_Boundaries.shp' # Specify the save path here
# Dissolve census tracts by county
arcpy.Dissolve_management(str(census_data), str(county_boundaries), 'COUNTYFP20')
print('County boundaries for Alabama have been created successfully!')
# CALCULATING POPULATION DENSITY
population_field = "POP20" #Name for population field
area_field = "Shape__Are" #Name for the area field
# Convert area from square feet to square miles
conversion_factor = 1 / 27878400.0 # 1 square mile = 27,878,400 square feet
# Calculate population density as a new field within the census tract shapefile
density_field = "pop_den"
# Check if "pop_den" field already exists and delete it if it does
field_names = [field.name for field in arcpy.ListFields(str(census_data))]
if density_field in field_names:
arcpy.DeleteField_management(str(census_data), density_field)
arcpy.AddField_management(str(census_data), density_field, "DOUBLE")
# Calculate population density, converting area to square miles
expression = "!{}! / ({} * !{}!)".format(population_field, conversion_factor, area_field)
arcpy.CalculateField_management(str(census_data), density_field, expression, "PYTHON3")
print('Population density calculated and added as a new field to the census tract shapefile!')
# Output raster file
output_raster = save_path / 'pop_den_ras.tif'
# Field containing values to convert to raster
density_field = "pop_den"
# Cell size (optional)
cell_size = 0.001 # Change this to your desired cell size
# Spatial reference (optional)
spatial_ref = arcpy.Describe(str(census_data)).spatialReference # Use the same spatial reference as the input shapefile
# Convert polygon to raster
arcpy.PolygonToRaster_conversion(in_features=str(census_data),
value_field=density_field,
out_rasterdataset=str(output_raster),
cellsize=cell_size,
priority_field="NONE",
cell_assignment="CELL_CENTER",
build_rat="BUILD")
print("Raster file has been created successfully!")
#REMAPPING THE RASTER
input_raster = save_path / 'pop_den_ras.tif'
output_raster = save_path / 'pop_den_remap.tif'
# Define the markers/boundaries for the grouping.
# Here, we'll define all the starts of the boundaries,
# and the last value will be the stop of the last boundary.
input_range_markers = [0, 1, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
# Zip up so we have a list of (start, stop)
input_range = list(zip(input_range_markers, input_range_markers[1:]))
# Make it a flat list for ArcPy
input_range = [x for sub in input_range for x in sub]
# Make a "classifier" output value, one for each range
output_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arcpy.CheckOutExtension("Spatial")
remap_raster = arcpy.sa.Remap(str(input_raster), input_range, output_values)
remap_raster.save(str(output_raster))
print('Population Density Remapped Successfully!')
# APPLYING CUSTOM COLOR MAP
input_raster = save_path / 'pop_den_remap.tif'
output_raster = save_path / 'pop_den_recolor.tif'
colormap = {
1: (0, 0, 0), # Black
2: (0, 0, 255), # Blue
3: (0, 255, 255), # Cyan
4: (0, 255, 0), # Green
5: (255, 255, 0), # Yellow
6: (255, 128, 0), # Orange
7: (255, 0, 0), # Red
8: (128, 0, 128), # Purple
9: (128, 128, 128), # Gray
10: (255, 255, 255), # White
11: (50, 200, 50)
}
# Apply the colormap to the input raster
arcpy.AddColormap_management(input_raster, colormap)
# Save the output raster with the colormap applied
arcpy.CopyRaster_management(input_raster, output_raster)
print('Population Density Remapped Successfully!')
我尝试使用arcpy.AddColormap_management函数,但没有成功。我确保输入栅格存在,所以我不知道问题所在。
arcpy.management.AddColormap()
函数需要模板栅格作为其第二个位置参数,或 .clr 或 .act 文件作为其第三个位置参数。有关示例用法,请参阅 Esri 的文档:
##Assign colormap using template image
arcpy.AddColormap_management("nocolormap.img", "colormap.tif", "#")
##Assign colormap using clr file
arcpy.AddColormap_management("nocolormap.img", "#", "colormap_file.clr")
上面的代码片段是直接从文档中提取的。文档的使用部分还指出了可能导致问题的其他限制。