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


相关推荐
m0_748554815 小时前
golang如何实现用户订阅偏好管理_golang用户订阅偏好管理实现总结
jvm·数据库·python
smj2302_796826526 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
阿正呀6 小时前
Redis怎样实现本地缓存的高效失效通知
jvm·数据库·python
九转成圣7 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
SmartRadio7 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
2501_901200537 小时前
mysql如何设置InnoDB引擎参数_优化innodb_buffer_pool
jvm·数据库·python
laowangpython7 小时前
Rust 入门:GitHub 热门内存安全编程语言
开发语言·其他·rust·github
我叫汪枫7 小时前
在后台管理系统中,如何递归和选择保留的思路来过滤菜单
开发语言·javascript·node.js·ecmascript
_.Switch7 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
软件技术NINI7 小时前
webkit简介及工作流程
开发语言·前端·javascript·udp·ecmascript·webkit·yarn