IDL编程:利用shp文件批量裁剪

IDL编程:利用shp文件批量裁剪

  • 源码简介:
    • 源码下载
    • 源码
    • 参考文献链接及运行效果展示

源码简介:

主要是在“https://www.cnblogs.com/gisoracle/p/3663707.html”源码的基础上增加了“ ENVI_FILE_MNG, id =fid,/remove” 释放内存的部分。

源码下载

源码下载: CSDN下载搜:bach_subset_viashp.pro.

源码

;;;;image_dir为影像路径
;;;;vector_dir矢量路径
;;;;outfile_dir裁剪后结果路径
pro Bach_subset_viashp,Image_dir,vector_dir,outfile_dir
  COMPILE_OPT IDL2
  ;compole_opt是对idl编译规则的修改,关键字有defint32,strictarr,
  ;idl2等
  ;defint32把默认的IDL整型数据16位改为32位
  ;strictarr强迫数组元素用中括号,不用小括号
  ;idl2则为上述二者的并。
  e=ENVI(/Headless)
  ;启动ENVI
  
  image_dir='F:\E\data\grass_yield\2000_2018GOSIF\8day'
  ;根据文件存放的目录进行相应修改
  
  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
   ENVI_FILE_QUERY, fid, file_type=file_type, BNAMES=BNAMES,nl=nl, ns=ns,dims=dims,nb=nb
    
  ;打开shape文件
 vector_dir='F:\E\data\grass_yield\shp\WGS84\minqin'
 ;根据文件存放的目录进行相应修改
 shapefile=file_search(vector_dir,'*.shp',count=numshps)
 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

 ;判断输出文件路径,在原文件名基础上输出
  outfile_dir=file_dirname(image_file,/MARK_DIRECTORY)
  out_name = outfile_dir +'\' +file_baseName(image_file,'.tif')$
  +'_'+file_baseName(shapefile,'.shp')+'_roi.tif'
    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    
    ENVI_FILE_MNG, id =fid,/remove
ENDFOR;i=numfiles
  tmp = DIALOG_MESSAGE('cut is end!',/info)
  envi_batch_exit
END

参考文献链接及运行效果展示

参考1: https://www.cnblogs.com/gisoracle/p/3663707.html.
参考2: https://blog.csdn.net/qq_37970770/article/details/99195735.

原始图片: IDL编程:利用shp文件批量裁剪_第1张图片裁剪到县级的图片

你可能感兴趣的:(遥感IDL编程,其他)