ArcGIS Python零基础脚本开发教程---1.1 Describe 函数


文章目录


前言

arcpy.Describe函数用于获取地理数据(要素类、栅格、图层等)的属性信息,返回一个包含数据属性(如数据类型、空间参考、字段等)的对象。

基本语法

python 复制代码
python
import arcpy
# 基本用法
desc = arcpy.Describe(input_data)

参数说明

input_data 输入的地理数据(路径字符串或图层对象)


一、 基础属性示例

python 复制代码
python
# -*- coding: utf-8 -*-
import arcpy
# 描述一个要素类
fc = r" E:\data\cities.shp"
desc = arcpy.Describe(fc)
# 基本信息
print("数据类型: {}".format(desc.dataType))       # 数据类型: ShapeFile
print("名称: {}".format(desc.name))              # 名称: cities.shp
print("基础名: {}".format(desc.baseName))         # 基础名: cities
print("目录: {}".format(desc.path))              # 目录:  E:\data
print("完整路径: {}".format(desc.catalogPath))    # 完整路径:  E:\data\cities.shp
print("扩展名: {}".format(desc.extension))        # 扩展名: shp

二、要素类相关属性

python 复制代码
python
# -*- coding: utf-8 -*-
import arcpy
# 仅适用于要素类
fc = r" E:\data\temp.mdb\DLTB"
desc = arcpy.Describe(fc)
if desc.dataType == "FeatureClass":
    print("要素类型: {}".format(desc.shapeType))  # 要素类型: Polygon
    # 空间参考信息
    spatial_ref = desc.spatialReference
    print("坐标系名称: {}".format(spatial_ref.name))   #坐标系名称: CGCS2000_3_Degree_GK_CM_126E
    print("投影类型: {}".format(spatial_ref.type))     #投影类型: Projected
    # 范围信息
    extent = desc.extent
    print("X最小值: {}".format(extent.XMin))   #X最小值: 379606.02316
    print("X最大值: {}".format(extent.XMin))   #X最大值: 379606.02316
    print("Y最小值: {}".format(extent.YMin))   #Y最小值: 4870197.481
    print("Y最大值: {}".format(extent.YMax))   #Y最大值: 4874916.48099

三、字段信息

python 复制代码
python
# -*- coding: utf-8 -*-
import arcpy
fc = r" E:\data\temp.mdb\DLTB"
desc = arcpy.Describe(fc)
# 获取字段信息
fields = desc.fields
for field in fields:
    print("字段名: {}".format(field.name))
    print("字段类型: {}".format(field.type))
    print("字段长度: {}".format(field.length))
    print("是否可为空: {}".format(field.isNullable))

四、 栅格数据属性

python 复制代码
python
# -*- coding: utf-8 -*-
import arcpy
# 描述栅格数据
raster = r"E:\data\a2.img"
desc = arcpy.Describe(raster)
if desc.dataType == "RasterDataset":
    print("栅格格式: {}".format(desc.format)) #栅格格式: IMAGINE Image
    print("波段数: {}".format(desc.bandCount)) #波段数: 1
    print("压缩类型: {}".format(desc.compressionType)) #压缩类型: RLE
    # 栅格范围
    extent = desc.extent
    print("栅格范围: {}".format(extent))  #栅格范围: 128.980865478516 42.7794821090849 129.728913685288 43.4220062741493 NaN NaN NaN NaN

五、工作空间和数据集

python 复制代码
python
# -*- coding: utf-8 -*-
import arcpy
# 描述工作空间
gdb = r"E:\data\geodatabase.gdb"
desc = arcpy.Describe(gdb)
if desc.dataType == "Workspace":
    print("工作空间类型: {}".format(desc.workspaceType))  # 工作空间类型: LocalDatabase
# 描述数据集
dataset = r"E:\data\geodatabase.gdb\Transportation"
desc = arcpy.Describe(dataset)
if desc.dataType == "FeatureDataset":
    print("数据集名称: {}".format(desc.name))   #工作空间类型: LocalDatabase

六、注意事项

不是所有属性都适用于所有数据类型,使用前用hasattr()检查

python 复制代码
 python
   if hasattr(desc, 'shapeType'):
       print(desc.shapeType)

Describe函数是ArcPy中最常用的函数之一,可以帮助你在处理地理数据前了解数据的属性和特征。


相关推荐
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊3 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1233 天前
2026年第38周GitHub趋势周报
python·科技·github