hello, everyone.
I have some evf files, and some of them have holes in it. I want to subset raster files in img format with these evf files. I use the ENVI open vector file , export vector file as ROI , and subset data via ROI, I can get the correct result. while I have many data to process, so I want to write IDL procedure to clip data via evf file.
the result is with some problems, the holes in the evf is not masked. why this happened? what should I do. the code is below. how to process holes in evf file??
PRO ClipViaEvf,image_file, evffile, resultfile
compile_opt idl2
CATCH, Error_status
errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "wxfei19@gmail.com"'
IF Error_status NE 0 THEN BEGIN
print, !ERROR_STATE.MSG
tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = 'error prompt')
return
ENDIF
FileName = image_file
envi_open_file, FileName , r_fid=fid
if (fid eq -1) then begin
envi_batch_exit
return
endif
PRINT, 'fid=',fid
;get information
ENVI_FILE_QUERY,fid,DIMS=dims,NS=ns,NL=nl,NB=nb, BNAMES= BNAMES
out_name = resultfile
;open vector file
evf_file =evffile
evf_id=ENVI_EVF_OPEN(evf_file)
;get information
ENVI_EVF_INFO, evf_id, num_recs=num_recs, $
data_type=data_type, projection=projection, $
layer_name=layer_name
;roi_ids = LONARR(num_recs)
;process each record
FOR i=0,num_recs-1 DO BEGIN
;record = ENVI_EVF_READ_RECORD(evf_id, i)
Result = ENVI_EVF_READ_RECORD(evf_id, i , PARTS_PTR=PARTS_PTR, TYPE=value)
;create ROI
;roi_id = ENVI_CREATE_ROI(color=4, ns = ns , nl = nl)
print,PARTS_PTR
;0 7 -17 -25 -32
;region count
regioncount = size(PARTS_PTR,/N_element)-1
;origin code
;xMap = dblarr(PARTS_PTR[regioncount-1])
;yMap = dblarr(PARTS_PTR[regioncount-1])
;new code
;point count
xMap = dblarr(abs(PARTS_PTR[regioncount]))
yMap = dblarr(abs(PARTS_PTR[regioncount]))
roi_ids = LONARR(regioncount)
;roi_id = LONARR(1)
;old code
;for index = 1L, 1 do begin
for index = 0L, regioncount-1 do begin
;origin code
;ENVI_CONVERT_FILE_COORDINATES,fid,xmaptemp,ymaptemp,Result[0,PARTS_PTR[index]:PARTS_PTR[index+1]-1],Result[1,PARTS_PTR[index]:PARTS_PTR[index+1]-1]
;ENVI_CONVERT_FILE_COORDINATES,fid,xmap,ymap,Result[0,*],Result[1,*]
if ((PARTS_PTR[index] lt 0 or PARTS_PTR[index+1] lt 0 )) then begin
;continue
;new code
ENVI_CONVERT_FILE_COORDINATES,fid,xmaptemp,ymaptemp,Result[0,abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1],Result[1,abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1]
;??ROI
;roi_id[index] = ENVI_CREATE_ROI(color=4, ns = ns , nl = nl)
roi_ids[index] = ENVI_CREATE_ROI(color=4, ns = ns , nl = nl)
;roi_shp[ishp] = ENVI_CREATE_ROI(ns=ns,nl=nl)
;ENVI_DEFINE_ROI,roi_shp[ishp],/polygon,xpts=REFORM(xmap),ypts=REFORM(ymap)
;ENVI_CONVERT_FILE_COORDINATES,fid,xmap1,ymap1,Result[0,10:46],Result[1,10:46]
ENVI_DEFINE_ROI, roi_ids[index], /polygon, xpts=REFORM(xmaptemp), ypts=REFORM(ymaptemp)
;ENVI_DELETE_ROIS, roi_ids[index];, /polygon, xpts=REFORM(xmaptemp), ypts=REFORM(ymaptemp)
;origin code
;xMap[PARTS_PTR[index]:PARTS_PTR[index+1]-1] = REFORM(xmaptemp[*])
;yMap[PARTS_PTR[index]:PARTS_PTR[index+1]-1] = REFORM(ymaptemp[*])
;new code
xMap[abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1] = REFORM(xmaptemp[*])
yMap[abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1] = REFORM(ymaptemp[*])
;print,xMap
;print,ymap
endif else begin
;print,'test'
;new code
ENVI_CONVERT_FILE_COORDINATES,fid,xmaptemp,ymaptemp,Result[0,abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1],Result[1,abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1]
;??ROI
;roi_id[index] = ENVI_CREATE_ROI(color=4, ns = ns , nl = nl)
roi_ids[index] = ENVI_CREATE_ROI(color=4, ns = ns , nl = nl)
;roi_shp[ishp] = ENVI_CREATE_ROI(ns=ns,nl=nl)
;ENVI_DEFINE_ROI,roi_shp[ishp],/polygon,xpts=REFORM(xmap),ypts=REFORM(ymap)
;ENVI_CONVERT_FILE_COORDINATES,fid,xmap1,ymap1,Result[0,10:46],Result[1,10:46]
ENVI_DEFINE_ROI, roi_ids[index], /polygon, xpts=REFORM(xmaptemp), ypts=REFORM(ymaptemp)
;origin code
;xMap[PARTS_PTR[index]:PARTS_PTR[index+1]-1] = REFORM(xmaptemp[*])
;yMap[PARTS_PTR[index]:PARTS_PTR[index+1]-1] = REFORM(ymaptemp[*])
;new code
xMap[abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1] = REFORM(xmaptemp[*])
yMap[abs(PARTS_PTR[index]):abs(PARTS_PTR[index+1])-1] = REFORM(ymaptemp[*])
endelse
;print,abs(PARTS_PTR[index])
;print,abs(PARTS_PTR[index+1])-1
endfor
; data = fltarr(5)
; data1 = fltarr(5)
; data2 = fltarr(17749-47)
; data3 = fltarr(17749-47)
;
;
; data = result[0,0:4]
; data1 = result[1,0:4]
; data2 = result[0,47:17749]
; data3 = result[1,47:17749]
;
; data4 = [[data],[data2]]
; data5 = [[data1],[data3]]
;???????
;ENVI_CONVERT_FILE_COORDINATES,fid,xmap,ymap,record[0,*],record[1,*]
; ENVI_CONVERT_FILE_COORDINATES,fid,xmap4,ymap4,Result[0,47:17749],Result[1,47:17749]
; ;ENVI_CONVERT_FILE_COORDINATES,fid,xmap,ymap,data4,data5
; ;??ROI
; ;roi_id = ENVI_CREATE_ROI(color=4, ns = ns , nl = nl)
; ENVI_DEFINE_ROI, roi_id, /polygon, xpts=REFORM(xMap4), ypts=REFORM(yMap4)
;
; ENVI_CONVERT_FILE_COORDINATES,fid,xmap1,ymap1,Result[0,10:46],Result[1,10:46]
; ENVI_DEFINE_ROI, roi_id, /polygon, xpts=REFORM(xMap1), ypts=REFORM(yMap1)
;
; ENVI_CONVERT_FILE_COORDINATES,fid,xmap2,ymap2,Result[0,5:9],Result[1,5:9]
; ENVI_DEFINE_ROI, roi_id, /polygon, xpts=REFORM(xMap2), ypts=REFORM(yMap2)
;
; ENVI_CONVERT_FILE_COORDINATES,fid,xmap3,ymap3,Result[0,0:4],Result[1,0:4]
; ENVI_DEFINE_ROI, roi_id, /polygon, xpts=REFORM(xMap3), ypts=REFORM(yMap3)
;
; xMap = dblarr(17749+1)
; xMap = dblarr(17749+1)
; xMap = [[xmap1],[xmap2],[xmap3],[xmap4]]
; yMap = [[ymap1],[ymap2],[ymap3],[ymap4]]
;roi_ids[i] = roi_id
;??XY???,???
xMapnew = where(xMap gt 0,count,COMPLEMENT=B_C, NCOMPLEMENT=count_c)
yMapnew = where(yMap gt 0,count_2,COMPLEMENT=B_C_2, NCOMPLEMENT=count_c_2)
IF i EQ 0 THEN BEGIN
;old code
;xmin = ROUND(MIN(xMap,max = xMax))
;yMin = ROUND(MIN(yMap,max = yMax))
;new code
xmin = ROUND(MIN(xMap[xMapnew],max = xMax))
yMin = ROUND(MIN(yMap[yMapnew],max = yMax))
ENDIF ELSE BEGIN
xmin = xMin ROUND(MAX(xMap[xMapnew]))
yMin = yMin ROUND(MAX(yMap[yMapnew]))
ENDELSE
ENDFOR
xMin = xMin >0
xmax = xMax 0
;yMin=yMin-3
ymax = yMax < nl-1
;ymax=ymax+5;
;????,????
roiid = where(roi_ids gt 0)
ENVI_MASK_DOIT,$
AND_OR =1, $
/IN_MEMORY, $
ROI_IDS= roi_ids[roiid], $ ;ROI?ID
;ROI_IDS= roi_id, $ ;ROI?ID
ns = ns, nl = nl, $
inside=0, $ ;?????
r_fid = m_fid
out_dims = [-1,xMin,xMax,yMin,yMax]
; print,out_dims
;
ENVI_MASK_APPLY_DOIT, FID = fid, POS = INDGEN(nb), DIMS = out_dims, $
M_FID = m_fid, M_POS = [0], VALUE = 0, $
out_name = out_name, R_FID = roi_ids[roiid]
;????ID??
ENVI_FILE_MNG, id =m_fid,/remove
;
; 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_ids
;ENDFOR
END
|