-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsubset_via_shp_update.pro
148 lines (60 loc) · 2.7 KB
/
subset_via_shp_update.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;idl 批量裁剪代码
PRO Subset_via_shp_update
COMPILE_OPT idl2
ENVI,/restore_base_save_files
envi_batch_init,LOG_FILE='batch.log'
;打开要裁剪的图像
image_dir='C:\Users\lhy\Desktop\气象数据\' ;根据文件存放的目录进行相应修改
image_files=file_search(image_dir,'*.tif',count=numfiles) ;根据相应的文件格式修改过滤条件
for i=0,numfiles-1 do begin
image_file=image_files[i]
print,image_file
if strlen(image_file) eq 0 then return
ENVI_OPEN_FILE, image_file, r_fid=fid, /no_interactive_query, /no_realize
IF fid EQ -1 THEN RETURN
ENVI_FILE_QUERY, fid, file_type=file_type, nl=nl, ns=ns,dims=dims,nb=nb
;打开shape文件
;shapefile = DIALOG_PICKFILE(title='choose the SHP file:',filter='*.shp')
shapefile=file_search(image_dir,'*.shp')
if strlen(shapefile) eq 0 then return
oshp = OBJ_NEW('IDLffshape',shapefile)
oshp->Getproperty,n_entities=n_ent,Attribute_info=attr_info,$
n_attributes=n_attr,Entity_type=ent_type
roi_shp = LONARR(n_ent)
FOR ishp = 0,n_ent-1 DO BEGIN
entitie = oshp->Getentity(ishp)
IF entitie.shape_type EQ 5 THEN BEGIN
record = *(entitie.vertices)
;转换文件坐标
ENVI_CONVERT_FILE_COORDINATES,fid,xmap,ymap,record[0,*],record[1,*]
;创建ROI
roi_shp[ishp] = ENVI_CREATE_ROI(ns=ns,nl=nl)
ENVI_DEFINE_ROI,roi_shp[ishp],/polygon,xpts=REFORM(xmap),ypts=REFORM(ymap)
;记录X,Y的区间,裁剪用
IF ishp EQ 0 THEN BEGIN
xMin = ROUND(MIN(xMap,max = xMax))
yMin = ROUND(MIN(yMap,max = yMax))
ENDIF ELSE BEGIN
xMin = xMin < ROUND(MIN(xMap))
xMax = xMax > ROUND(MAX(xMap))
yMin = yMin < ROUND(MIN(yMap))
yMax = yMax > ROUND(MAX(yMap))
ENDELSE
ENDIF
oshp->Destroyentity,entitie
ENDFOR;ishp
xMin = xMin >0
xMax = xMax < ns-1
yMin = yMin >0
yMax = yMax < nl-1
;判断输出文件路径,在原文件名基础上输出
outfiledir=file_dirname(image_file,/MARK_DIRECTORY)
out_name = outfiledir +'\' +file_baseName(image_file,'.tif')+'_roi.img
out_dims = [-1,xMin,xMax,yMin,yMax]
pos = INDGEN(nb)
ENVI_DOIT,'ENVI_SUBSET_VIA_ROI_DOIT',background=0,fid=fid,dims=out_dims,out_name=out_name,$
ns = ns, nl = nl,pos=pos,roi_ids=roi_shp
endfor
tmp = DIALOG_MESSAGE('裁切结束!',/info)
envi_batch_exit
END