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中最常用的函数之一,可以帮助你在处理地理数据前了解数据的属性和特征。


相关推荐
tkevinjd6 小时前
MiniCode 项目详解6:原项目控制系统的10个缺陷(已修复)
python·llm·agent
星栈独行7 小时前
翻完 Pi 源码:它和 Codex、Claude Code 有何不同
开发语言·javascript·人工智能·程序人生
dNGUZ7UGj7 小时前
10分钟完成第一个Python小游戏
python·django
没有梦想的咸鱼185-1037-16637 小时前
AI-Python机器学习与深度学习技术:CNN/Transformer/扩散模型、SHAP可解释及Hermes智能体自动化
人工智能·python·深度学习·机器学习·chatgpt·cnn·transformer
qq_448011167 小时前
C语言中的变量和函数的定义与声明
android·c语言·开发语言
霸道流氓气质8 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
孫治AllenSun9 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm
Draina10 小时前
CBC填充预言攻击-CBC Padding Oracle Crypto Attack
python·安全·web安全·网络安全·密码学·安全性测试
c2385610 小时前
把 C++ 内存分配拆透:new 与 malloc 的三层血缘
开发语言·c++·算法
Angel Q.10 小时前
特征提取 | DINO 到 DINOv3
图像处理·python