跳转至

ENVI Extensions: Getting notifications from ENVI ROIs

原文链接: https://www.nv5geospatialsoftware.com/Learn/Blogs/Blog-Details/envi-extensions-getting-notifications-from-envi-rois

20321 Rate this article:

3.3

ENVI Extensions: Getting notifications from ENVI ROIs

Anonym Thursday, February 25, 2016

A recent project I worked on required an ENVI extension to respond ROI changes made in the ENVI Layer Manager.   Fortunately, ENVI provides a mechanism that allows an object to do this.  First, the object must be added to the list of observers for each ROI layer.  The following command provides access to these layers:

IDLcf$Get , /INCLUDE_VIS_CHILDREN, VIS_LAYERS=oVisLayers

The variable oVisLayers will contain all visible layers present in the ENVI display.  To get only the ROIs, loop through its contents looking for any instances of IDLcfVisROINative.  When one is encountered the object can be added to the ROI layer’s list of observers using the following:

void = oVisLayers[i]->AddObserver(self)

Now that the object has been added to the list of observers an OnNotify method must be added.  Any notifications from the ROI layers will be sent to this method.

functionmy_class ::OnNotify, oSubject, strMsg, strArg

The input parameters are:\

  • oSubject – A reference to the ENVI object sending the notification
  • strMsg – A string containing the name of the notification (e.g. SETPROPERTY)
  • strArg – A string containing additional notification information (e.g. HIDE)

Here is a stand-alone example that demonstrates setting up an object to receive and act on notifications from ROIs in the ENVI Layer Manager:

;##############################################################################

; This example demonstrates how to get notifications from ENVI when changes are

; made to ROIs in the Layer Manager.

;##############################################################################

;------------------------------------------------------------------------------

;+

; Event handler used by XMANAGER

;

; :Params:

;   sEvent: in, required, type="struct"

;     An IDL event structure

;-

pro roi_demo_event, sEvent

widget_control, sEvent.top, GET_UVALUE=oRoiDemo

oRoiDemo->Event, sEvent

end

;------------------------------------------------------------------------------

;+

; Checks ENVI for any ROIs.  DIsplays all of the ROIs found in the message

; text box.

;

; :Keywords:

;   PREFIX: in, optional, type="string"

;     A string or string array of text to be added to the beginning of the ROI

;     message.

;-

pro roi_demo::CheckROIs, $

PREFIX=prefix

compile_opt idl2, logical_predicate

IDLcf$Get, /INCLUDE_VIS_CHILDREN, VIS_LAYERS=oVisLayers

name = []

for i = 0, n_elements(oVisLayers)-1 do begin

if isa(oVisLayers[i], 'IDLcfVisROINative') then begin

void = oVisLayers[i]->AddObserver(self)

oROI = oVIsLayers[i]->GetParameter('ROI')

if obj_valid(oROI) then begin

name = [name, oROI.name]

endif

endif

endfor

if (n_elements(prefix) EQ 0) then prefix = 'ROIs found:'

str = [prefix,(n_elements(name) GT 0) ? name : 'No ROIs found']

widget_control, widget_info(self.tlb, FIND_BY_UNAME='text_message'), SET_VALUE=str

end

;------------------------------------------------------------------------------

;+

; Creates a dialog for displaying information regarding the ENVI ROIs

;-

pro roi_demo::Dialog

compile_opt idl2, logical_predicate

e = envi(/CURRENT)

self.tlb = widget_base(/COLUMN, /FLOATING, GROUP_LEADER=e.widget_id, $

TITLE='ENVI ROI Demo')

xSize = 100

wLabel = widget_label(self.tlb, /ALIGN_LEFT, VALUE='Notification Information')

wText = widget_text(self.tlb, UNAME='text_params', XSIZE=xSize, YSIZE=3)

wLabel = widget_label(self.tlb, /ALIGN_LEFT, VALUE='ROI Information')

wText = widget_text(self.tlb, UNAME='text_message', XSIZE=50, YSIZE=10)

wBase = widget_base(self.tlb, /ALIGN_RIGHT, /ROW)

wButton = widget_button(wBase, UNAME='check_roi', VALUE='Check ROIs')

widget_control, self.tlb, /REALIZE, SET_UVALUE=self

self->CheckROIs

xmanager, 'roi_demo', self.tlb, /NO_BLOCK

end

;------------------------------------------------------------------------------

;+

; Event handler for the ROI demo's dialog

;

; :Params:

;   sEvent: in, required, type="struct"

;     An IDL event structure

;-

pro roi_demo::Event, sEvent

compile_opt idl2, logical_predicate

case widget_info(sEvent.id, /UNAME) of

'check_roi': self->CheckROIs

else: print, widget_info(sEvent.id, /UNAME)

endcase

end

;------------------------------------------------------------------------------

;+

; Lifecycle method for initializing the object

;

; :Returns:

;   1 if the object initializes successfully and 0 otherwise

;-

function roi_demo::Init, $

_REF_EXTRA=refExtra

compile_opt idl2, logical_predicate

if (~self->IDLmiObserver::Init(_EXTRA=refextra)) then begin

print, 'Failed to initialize IDLmiObserver'

return, 0

endif

return, 1

end

;------------------------------------------------------------------------------

;+

; This method is for receiving notifications from the notifiers for which this

; class is an observer.

;

; :Returns:

;   1

;

; :Params:

;   oSubject: in, required, type="objref"

;     The ENVI object that is sending the notifications

;   strMsg: in, required, type="string"

;     The notification string (e.g. SETPROPERTY)

;   strArg: in, required, type="string"

;     A string containing additional notification information (e.g. HIDE)

;-

function roi_demo::OnNotify, oSubject, strMsg, strArg

compile_opt idl2, logical_predicate

oSubject->GetProperty, NAME=name

help, oSubject, OUTPUT=strHelp

widget_control, widget_info(self.tlb, FIND_BY_UNAME='text_params'), $

SET_VALUE=[strHelp, 'STRMSG: '+strMsg, 'STRARG: '+strArg]

str = ''

case strupcase(strMsg) of

'ADDITEMS': begin

str = ['Adding a new ROI','Press Check ROIs when finished']

end

'REMOVEITEMS': begin

self->CheckROIs, PREFIX=['ROI has been removed','Remaining ROIs:']

return, 1

end

'ROI_DEFINITION_CHANGE': begin

str = name+' has been updated'

end

'SETPROPERTY': begin

case strupcase(strArg) of

'COLOR': begin

oROI = oSubject->GetParameter('ROI')

oROI->GetProperty, COLOR=color

str = name+' color changed to '+strjoin(strtrim(fix(color),2),',')

end

'HIDE': begin

oSubject->GetProperty, HIDE=hide

str = name+' has been '+(hide ? 'hidden' : 'shown')

end

else:

endcase

end

else: print, 'MESSAGE: '+strMsg

endcase

wText = widget_info(self.tlb, FIND_BY_UNAME='text_message')

widget_control, wText, SET_VALUE=str

return, 1

end

;------------------------------------------------------------------------------

;+

; Class structure definition

;

; :Inherits:

;   IDLmiObserver

;  

; :Fields:

;   tlb: This widget ID of the dialog's top level base.

;-

pro roi_demo__define

compile_opt idl2, logical_predicate

void = {roi_demo           $

, inherits IDLmiObserver $

, tlb  : 0L              $

}

end

;------------------------------------------------------------------------------

;+

;-

pro roi_demo

compile_opt idl2, logical_predicate

e = envi(/CURRENT)

if ~isa(e, 'envi') then begin

e = envi()

eView = e->GetView()

file = Filepath('qb_boulder_msi', ROOT_DIR=e.root_dir, SUBDIRECTORY=['data'])

eRaster = e->OpenRaster(file)

eLayer = eView->CreateLayer(eRaster)

file = Filepath('qb_boulder_roi.xml', ROOT_DIR=e.root_dir, SUBDIRECTORY=['data'])

eROI = e->OpenROI(file)

nROI = n_elements(eROI)

eLayerROI = objarr(nROI)

for i = 0, nROI-1 do begin

eLayerROI[i] = eLayer->AddROI(eROI[i])

endfor

endif

oROIDemo = obj_new('roi_demo')

oROIDemo->Dialog

end

Save Time and Effort by Organizing Your Data with Jagwire The Truth about Vegetation Species Identification from Imagery