Hello,
I'm working on an task to subset images via roi. And I can't seem to figure out why my Task.OUTPUT_ROI usesage doesn't begin/retrieve the first attribute in the roi. As you can see below when I enter the FOREACH loop I'm setting up the index to begin with index 0, which should be the first record in this case the first BOX. But it does not do this. I'm using shpObj to get the first attribute so I can specify a suffix to attach to the name. Obviously I want these to match but that is not the case here.
Here is the code I'm using:
subset_image_by_roi_o.pro
;***************************************************************
; NAME:
; subset_image_by_roi
;
; PURPOSE:
; Read in an hyperspectral image file and shapefile to subset data via the shapefile's
; region of interest.
;
;
; AUTHOR:
; J. Heath Harwood, Joint Airborne Lidar Bathymetry Technical Center of Expertise
;
; 7225 Stennis Airport Rd.
; Suite 100
; Kiln, MS 39556
;
; 228-252-1121 Office
; 251-459-5920 Cell
;
; CATEGORY:
; ENVI, User Funtion
;
; CALLING SEQUENCE:
; subset_image, envi_event
;
; REQUIRED INPUTS:
; - Envi Event.
;
; RESTRICTIONS: Public Domain
; /*********************************************************************************************
;
; This is public domain software that was developed by or for the U.S. Naval Oceanographic
; Office and/or the U.S. Army Corps of Engineers.
;
; This is a work of the U.S. Government. In accordance with 17 USC 105, copyright protection
; is not available for any work of the U.S. Government.
;
; Neither the United States Government, nor any employees of the United States Government,
; nor the author, makes any warranty, express or implied, without even the implied warranty
; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or assumes any liability or
; responsibility for the accuracy, completeness, or usefulness of any information,
; apparatus, product, or process disclosed, or represents that its use would not infringe
; privately-owned rights. Reference herein to any specific commercial products, process,
; or service by trade name, trademark, manufacturer, or otherwise, does not necessarily
; constitute or imply its endorsement, recommendation, or favoring by the United States
; Government. The views and opinions of authors expressed herein do not necessarily state
; or reflect those of the United States Government, and shall not be used for advertising
; or product endorsement purposes.
;
;*********************************************************************************************/
;
; EXAMPLE:
;
; MODIFICATION HISTORY:
;
; version 1.0 - 7 Jan 2015 J. Heath Harwood JALBTCX
;
;***************************************************************
pro subset_image_by_roi_o
compile_opt idl2
e = ENVI(/CURRENT)
base = widget_auto_base(title='Subset Image via ROI - Batch')
base1 = widget_base(base, /frame, /col) ;input shapefile
base2 = widget_base(base, /frame, /col) ;input image file
base3 = widget_base(base, /frame, /col) ;output directory
base4 = widget_base(base, /frame, /col) ;output prefix
base5 = widget_base(base, /frame, /col) ;instructions
w1 = widget_outf(base1, uvalue='shpFile', prompt='Select the input file: ',$
xsize = 36, AUTO_MANAGE=1, default = '*.shp')
w2 = widget_outf(base2, uvalue='imgFile', prompt='Select the image file: ',$
xsize = 36, AUTO_MANAGE=1, default = '*.img')
w3 = widget_outf(base3, uvalue='outDir', prompt='Select the output directory: ',$
xsize = 36, /directory, AUTO_MANAGE=1)
w4 = widget_string(base4, prompt='Output file prefix ', xsize=36, $
uvalue='filePrefix', default = '2014_NCMP_WA_PortSusan_HSI_', AUTO_MANAGE=1)
w4 = widget_text(base5, ysize=3, xsize=33, value=['Please fill out state and project name',$
'by removing brackets'])
result = auto_wid_mng(base)
if (result.accept eq 0) then return
shpFile=result.shpFile
imgFile=result.imgFile
outDir=result.outDir
print, outDir
filePrefix=result.filePrefix
print, filePrefix
; Get shapfile for processing
inVector = e.OpenVector(shpFile)
print, inVector
; Pull shapefile into an object for processing
shpObj = OBJ_NEW('IDLffShape', shpFile)
shpObj->GetProperty, N_ENTITIES=num_ent
; Get image for processing
inRaster = e.OpenRaster(imgFile)
print, inRaster
; Create layers to view rasters and rois
view1 = e.GetView()
layer1 = view1.CreateLayer(inRaster)
;layer2 = view1.CreateLayer(outRaster)
; Get the task from the catalog of ENVITasks
Task1 = ENVITask('VectorAttributeToROIs')
; Define inputs
Task1.ATTRIBUTE_NAME = 'BOX'
Task1.INPUT_VECTOR = inVector
; Run the task
Task1.Execute
; Clip image using ROIs
visrois = !NULL
;index = !NULL
FOREACH Roi, Task1.OUTPUT_ROI, index DO BEGIN
; Initialize input rois variable
rois = Roi
print, rois
print, "The Index values is: ", index
; Read the shpFile entities for Box Name Attribute_0
;x = 0
ent = shpObj->GetAttributes(index)
boxName = ent.ATTRIBUTE_0
print, boxName
; Find Roi corners
roiPixels = rois.PixelAddresses(inRaster)
Xmin = min(roiPixels[0,*])
Xmax = max(roiPixels[0,*])
Ymin = min(roiPixels[1,*])
Ymax = max(roiPixels[1,*])
; Save the roi as an single xml
roi_out = FILE_BASENAME(shpFile, '.shp')+'_'+boxName+'.xml'
rois.Save, roi_out
; Output raster file creation
outRaster = filePrefix+'_'+boxName
print, outRaster
rasterclip = ENVIRaster(URI=outRaster, INHERITS_FROM=inRaster)
rasterclip = inRaster.subset(SUB_RECT=[Xmin,Ymin,Xmax,Ymax])
rasterclip.Export, outRaster, 'ENVI'
visrois = [visrois, layer1.AddRoi(Roi)]
;x = x + 1
;print, Roi
ENDFOREACH
END
Here is the output from the command line:
ENVI> subset_image_by_roi_o
% Compiled module: SUBSET_IMAGE_BY_ROI_O.
I:\_Programming\IDL\jalbtcx_code\subset_image_by_roi\output
2014_NCMP_WA_PortSusan_HSI_
ENVIVECTOR
AUXILIARY_URI =
COORD_SYS =
DATA_RANGE = 546396.39, 5337329.3, 555743.43, 5344359.2
RECORD_TYPE = 'PolygonZ'
URI = 'I:\_Programming\IDL\jalbtcx_code\subset_image_by_roi\PSB_Block03_utm10n_DOQQ.shp'
ENVIRASTER
AUXILIARY_SPATIALREF = !NULL
AUXILIARY_URI =
DATA_TYPE = 'int'
INTERLEAVE = 'bip'
METADATA =
NBANDS = 48
NCOLUMNS = 4933
NROWS = 1600
PYRAMID_EXISTS = 1
READ_ONLY = 1
SPATIALREF =
TIME =
URI = 'I:\_Programming\IDL\jalbtcx_code\subset_image_by_roi\2014_USGS_WA_PortSusan_HSI_REFL_48Band_400m.img'
ENVIROI
COLOR = 255, 0, 0
NAME = 'PSB_Block03_utm10n_DOQQ [BOX=48122b3b]'
N_DEFINITIONS = 1
The Index values is: 0
48122b3c
2014_NCMP_WA_PortSusan_HSI__48122b3c
ENVIROI
COLOR = 0, 128, 0
NAME = 'PSB_Block03_utm10n_DOQQ [BOX=48122b3c]'
N_DEFINITIONS = 1
The Index values is: 1
48122b3b
2014_NCMP_WA_PortSusan_HSI__48122b3b
For some reason it starts with last BOX number and not the first int the file.
Here is the sample file:
BOX CITY STATE 48122b3c Bend WA 48122b3b Kennewick WA
Any help you could provide would be great.
Thanks,
|