X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 03 Feb 2015 10:58 AM by  anon
ENVITask VectorAttributeToROIs Routine Task
 4 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
03 Feb 2015 10:58 AM
    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,

    Deleted User



    New Member


    Posts:
    New Member


    --
    03 Feb 2015 04:30 PM
    So I figured out that I need to be able to sort the "STRUCT = -> Array[#]" that is "ent = shpObj->GetAttributes(index)" or better yet "ent = shpObj->GetAttributes(/ALL)". Not sure exactly how to do this. Anyone have any ideas?

    Deleted User



    New Member


    Posts:59
    New Member


    --
    04 Feb 2015 06:42 AM
    Hello, The VectorAttributeToROI task will locate all the records of a vector that uniquely match a particular attribute column and value. The attributes values are initially sorted, and unique groups are then located. Unless the attributes strings happen to already be sorted by record index, the record index will not relate to the output ROI. The reason that box "48122b3b" occurs in an ROI before box "48122b3c" (even though the latter is an earlier record) is because the string"48122b3b" comes before the string "48122b3c". If all of the attribute names are unique, you could use the name of the output ROI and extract the attribute name from it (since it's included in the name between brackets) and then do a Where() on the input attributes to determine what the input record index was. You mentioned sorting "STRUCT = -> Array[#]" that is "ent = shpObj->GetAttributes(/ALL)". I forwarded this question to our software engineers, so hopefully we'll have some suggestions on how do this. To include the name of the attribute in the output name of your ROI .xml, you could use the following code: boxName = Strmid(roi.Name,(variable=Strpos(roi.Name,'='))+1, Strlen(roi.Name)-2-variable) roi_out = FILE_BASENAME(shpFile, '.shp')+'_'+boxName+'.xml' Let us know if this helps you. I will pass along any other information I find out.

    Deleted User



    New Member


    Posts:
    New Member


    --
    04 Feb 2015 07:23 AM
    Hey Jwolfe, Thanks for you info. I was thinking that the VectorAttributeToRoi task already be sorted. So that's why I was thinking "well, all I need to do is just sort the shapefile object. I mainly went this route because I really didn't know how else to do this. I'm very new to IDL so this seemed the easiest way at the moment that I could find from the documents. I didn't find any documentation on what you suggested below (roi.Name for instance). Either that or I completely overlooked how to use ENVIRoi module/object. I'm still working out the IDL terminology. I would like to hear from the software engineers as to whether is possible to sort this array. But I'm going to give you suggestion a try because it does seem to be what I was looking for initially. Thanks for your help. I'll let you know how this turns out. Thanks,

    Deleted User



    New Member


    Posts:
    New Member


    --
    04 Feb 2015 03:39 PM
    Hey Jwolfe, I added in the suggestion you made. Fixed the issue with now problems. I also added in a Envi classic status window report but it doesn't work as I thought it would. Does the job though. Thanks for the help! Here is the corrected code: subset_image_by_roi.pro 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
    You are not authorized to post a reply.