X
3724

How to extract pixels within a vector boundary using POLYFILLV

This Help Article shows an example of how to use POLYFILLV to extract pixel locations within a polygon shapefile boundary. It also creates a polygon ROI from the shapefile vertices for comparison to the original shapefile.
===================
pro fill_polygon
compile_opt idl2

  ;open and query a file
  envi_open_file, 'bhtmref.img', r_fid=fid, /no_realize
  envi_file_query, fid, ns=ns, nl=nl

  ;clean up existing rois that are open, if any
  roi_cleanup = envi_get_roi_ids()
  envi_delete_rois, roi_cleanup

  ;open a shapefile - this was a multiple polygon shapefile 
  ;created from ENVI's sample dataset bhtmref.img
  myshape=OBJ_NEW('IDLffShape', 'bh_rois.shp')
  
  ; Get the number of entities so we can parse through them.
  myshape->GetProperty, N_ENTITIES=num_ent
  ; Read all the entities.

    FOR i=0, (num_ent-1) DO BEGIN
      ;Read the entity i
      ent = myshape->GetEntity(i)
      xmap=(*ent.vertices)[0, *]
      ymap =(*ent.vertices)[1, *]
      
      ;get the file coordinates
      envi_convert_file_coordinates, fid, xf, yf, xmap, ymap
      
      ;fill the polygon
      filled_poly = polyfillv(xf, yf, ns, nl)
      
      ind = array_indices([ns, nl], filled_poly, /dimensions)
      
      ;create a new ROI - point-based
      roi_id = envi_create_roi(color=i+2, $
          name = 'test roi_'+ strtrim(string(i+1), 2), $
          ns=ns, nl=nl)
      envi_define_roi, roi_id, /point, xpts=ind[0,*], ypts=ind[1,*]
    
      ;Clean-up of pointers 
      myshape->DestroyEntity, ent
    ENDFOR

  ; Close the Shapefile.
  OBJ_DESTROY, myshape
  
  ;save the ROIs
  roi_ids = envi_get_roi_ids()
  envi_save_rois, 'test.roi', roi_ids
  
  ;clean up open rois and reopen new file
  roi_cleanup = envi_get_roi_ids()
  envi_delete_rois, roi_cleanup
  envi_restore_rois, 'test.roi'
end
Review on 12/31/2013 MM