subset_image_by_roi.pro
I previous posted some code to clip the boundaries of an image to a shapefile.
For some images this works no problem but for other image I get a weird Object Reference error.
The error I'm getting is "Error: Invalid object reference:
My images are only about 1GB. I have 192GB of ram and 12 cores.
Is the heap var the same as a pointer? The shapefile and image are in the same projection NAD83 UTM 19N
Below is the code that i'm using. Any ideas?
;***************************************************************
; 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
;
; joseph.h.harwood@usace.army.mil
; 228-252-1109 Office
; 228-731-5465 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_extensions_init
; Set compile options
compile_opt IDL2
; Get ENVI session
e = ENVI(/CURRENT)
; Add the extension to a subfolder
e.AddExtension, 'subset_image_by_roi', 'subset_image_by_roi', PATH='C:\Program Files\Exelis\ENVI52\extensions\subset_image_by_roi'
end
; ENVI Extension code.
pro subset_image_by_roi
; Set compile options
compile_opt IDL2
; General error handler
CATCH, err
if (err ne 0) then begin
CATCH, /CANCEL
if OBJ_VALID(e) then $
e.ReportError, 'ERROR: ' + !error_state.msg
MESSAGE, /RESET
return
endif
;Get ENVI session
e = ENVI(/CURRENT)
; Create the widgets for file selection
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)
w5 = widget_text(base5, ysize=3, xsize=33, value=['Please fill out state and project name',$
'by removing brackets'])
; Open widget
result = auto_wid_mng(base)
if (result.accept eq 0) then return
; Assign variables using the resulting input files
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
; Get image for processing
inRaster = e.OpenRaster(imgFile)
print, inRaster
; Create layers to view rasters and rois
view1 = e.GetView()
layer1 = view1.CreateLayer(inRaster)
; 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
FOREACH Roi, Task1.OUTPUT_ROI DO BEGIN
; Initialize input rois variable
rois = Roi
N_Roi = N_ELEMENTS(Roi)
print, rois
; Parse the ENVIRoi Name string (Ex. 'PSB_Block03_utm10n_DOQQ [BOX=48122b3c]')
boxName = Strmid(Roi.Name,(variable=Strpos(Roi.Name,'='))+1,Strlen(Roi.Name)-2-variable)
; 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
;start the status window and state the increments
ENVI_REPORT_INIT,['Output File:'+filePrefix+'_'+boxName,'Please wait...'], $
TITLE='Processing Box '+STRING(boxName)+' ...', BASE=REPORT_BASE
ENVI_REPORT_INC, REPORT_BASE, N_Roi
FOR i=0, N_Roi-1 DO BEGIN
ENVI_REPORT_STAT, REPORT_BASE, i, N_Roi
; 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'
ENDFOR
; Display Rois in ENVI Viewer
visrois = [visrois, layer1.AddRoi(Roi)]
;close the status window
ENVI_REPORT_INIT, BASE=REPORT_BASE,/FINISH
ENDFOREACH
END
|