二、ENVI/IDL批处理入门
ENVI/IDL集成了ENVI软件的高级功能,比如打开文件直接使用envi_open_file, File ,r_fid=fid即可,不用考虑什么格式等,再比如做文件投影转换只需给定目标投影,然后使用envi_convert_file_map_projection即可,因此可以使用简单的代码,结合循环进行ENVI的批量操作。本篇分两部分展开,第一部分假定你了解ENVI/IDL的知识,结合批量转投影的例子讲解批处理;第二部分假定你不了解ENVI/IDL,可以先看第二部分的基础知识和教程,再看第一部分。有问题请Google。
(一)ENVI/IDL批处理
这里讲的批处理是标题党,其实不过是利用IDL的循环调用ENVI的API函数而已,但是这个叫法比较形象而且批处理是刚需。以文件投影转换为例先描述下ENVI/IDL批处理的思路:
1)获取(ENVI_GET_PROJECTION)或给定(ENVI_PROJ_CREATE)目标投影信息
2)给定待处理数据的文件夹,按特定后缀循环遍历文件(envi_open_file)
3)读取文件信息(envi_file_query)
4)进行投影转换(envi_convert_file_map_projection)
下面以一个例子说明其中的关键点:
1)获取(ENVI_GET_PROJECTION)或给定(ENVI_PROJ_CREATE)目标投影信息
在进行投影转换时,如果我们不清楚目标投影的信息,一种简单的获取方式就是打开具有该投影的文件(或者手动在ENVI里转换得到一个),读取其投影信息。例如:
File = 'C:\data.dat'
envi_open_file, File ,r_fid=fid
o_proj = ENVI_GET_PROJECTION(FID = fid)
这里的o_proj 便读取到了投影信息。
如果没有可参考的投影文件,或者投影是内置的,或是想自行输入,可以给定投影信息,例如:
o_proj = ENVI_PROJ_CREATE(/geographic)
这里的投影geographic是内置的。
如果了解参数可以自定义投影,这里的参数不分先后,只是按照字母顺序排序的,点击下面的参数可以链接到具体说明:
Result = ENVI_PROJ_CREATE([, /ARBITRARY] [, DATUM=value] [, /GEOGRAPHIC] [, /MAP_BASED] [, NAME=string] [, PARAMS=array] [, PE_COORD_SYS_CODE=integer] [, PE_COORD_SYS_STR=string] [, /SOUTH] [, /STATE_PLANE] [, TYPE=integer] [, UNITS=integer] [, /UTM] [, ZONE=integer])
4)进行投影转换(envi_convert_file_map_projection),这里的参数不分先后,只是按照字母顺序排序的,点击下面的参数可以链接到具体说明:
ENVI_CONVERT_FILE_MAP_PROJECTION [, BACKGROUND=integer] [, DEGREE=value], DIMS=array, FID=file ID[, GCP_NAME=string] [, GRID=array] [, O_PIXEL_SIZE=array], O_PROJ=structure [, OUT_BNAME=string array], OUT_NAME=string, POS=array [, R_FID=variable] [, RESAMPLING={0 | 1 | 2}] [, WARP_METHOD={0 | 1 | 2 | 3}] [, /ZERO_EDGE]
简单说几个常用参数:BACKGROUND是背景值,DIMS和FID是envi_file_query读取的,O_PIXEL_SIZE是像元大小,RESAMPLING是重采样方法0: Nearest neighbor, 1: Bilinear, 2: Cubic convolution,个人习惯0, WARP_METHOD的0: Rotation, scaling, and translation (RST), 1: Polynomial, 2: Triangulation, 3: Rigorous (pixel-by-pixel),个人习惯2。
下面附一个批量投影转换的例子供参考:
pro enviprojection1
; Firstrestore all the base save files.
COMPILE_OPT IDL2
e = envi()
;Initialize ENVI and send all errors
; andwarnings to the file batch.txt
envi_batch_init, log_file='batch.txt'
; Open theinput file
;定义投影
File = 'C:\data.dat'
envi_open_file, File ,r_fid=fid
o_proj = ENVI_GET_PROJECTION(FID = fid)
; o_proj = ENVI_PROJ_CREATE(/geographic)
cd,'C:\data\input\'
BINFiles = FILE_Search("*.dat")
FileCount = N_ELEMENTS(BINFiles)
IF FileCount EQ 0 THEN RETURN
FOR NX = 0,FileCount -1 DO BEGIN
FileName = BINFiles[NX]
envi_open_file, FileName ,r_fid=fid
if (fid eq -1)then begin
envi_batch_exit
return
endif
; Setupthe values for the keywords
envi_file_query, fid[0], dims=dims, nb=nb
pos = lindgen(nb)
indexstr = strpos(FileName,'.dat')
out_name = strmid(FileName,0,indexstr) +'_Geo.raw'
;设置输出像元大小
o_pixel_size = [5000,5000];
envi_convert_file_map_projection, fid=fid, $
pos=pos, dims=dims, o_proj=o_proj, $
o_pixel_size=o_pixel_size, $
out_name=out_name, warp_method=2, $
resampling=0, background=0
ENVI_FILE_MNG,id = fid,/remove
ENDFOR
; ExitENVI
envi_batch_exit
end
(二)ENVI/IDL基础
这里先分别给出ENVI/IDL的图形界面介绍、编程指导手册(下载和在线版)和一个批量转投影的代码。
了解ENVI/IDL的图形用户界面和简单编程
https://webpages.uidaho.edu/for570/Labs/IDL_Labs/IDL%20Lab%201%20-%20The%20IDL%20Interface.pdf
ENVI/IDL编程指导(ENVI Programmer’sGuide)下载版:
https://lost-contact.mit.edu/afs/enea.it/software/rsi/envi/docs/progguid.pdf
或 www.harrisgeospatial.com/portals/0/pdfs/idl/refguide.pdf
全面的ENVI/IDL在线教程:
https://www.harrisgeospatial.com/docs/ProgrammingGuideIntroduction.html
· Frequently AskedQuestions
· Getting Started with Writing ENVI API Scripts
o Objects
o Writing Your First Script
o Where to Find Help
· Display Control
o Views and Layers
o Portals
· Working with Rasters
o Access Data Files
o Open Rasters in ENVI
o View Raster Properties
o Define Spatial Subsets
o Export Rasters to Disk
o Virtual Rasters
o Mask Rasters
o Raster Pixel State
· Raster Metadata
o Standard Metadata
o User-Defined Metadata
· Map Information
o Georeferencing Options
o Georeference Imagery using RPCs
o Create a Georeferenced Layer Stack
o Convert Rasters from Once Coordinate System toAnother
o Converting Between Different Coordinate Types
· Data Processing using ENVITasks
o Discover What Tasks are Available
o Define Input Properties
o Validate Tasks
o Run Tasks
o Create Output
· ENVI Classification
o Prepare Data for Classification
o Define and Train the Classifier
o Evaluate the Classifier
o Run the Classifier
o Code Example: Support Vector Machine Classificationusing API Objects
o Code Example: Softmax Regression Classification usingAPI Objects
o Classification Framework Glossary
· Custom Tasks
o Custom Task Base Classes
o Style Sheets for User Interface Elements
· Processing Large Rasters Using Tile Iterators
o Spatial and Spectral Modes
· Advanced Concepts
o Error Handling
o Event Handling
o Messaging
o Custom File Readers
o Toolbox Extensions
· List of Routines by Function