!******************************************************************************!
! Interactive means of testing ShapeFile.getShapesNear
!
! @issue    NXM-3622 ShapeFile.isNear finding wrong countries
! @since    4.1.1
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  call setDatDir
  invoke midas nxm.sys.lib.Shell.getSharedMidasContext()
  file open shp ^{datDir}.world.shp

  set defaultInstructions &
    "1) Zoom in on different countries, including borders between countries\n&
     2) Click and make sure the countries 'near' the click are correct\n&
     3) Try this with different radius settings&
     "


  if interAct then
    pipe init

      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot/READOUT="+OnAxis" ^{datDir}.world.shp{LAYER={LABEL=NAME,OPAQ=50}} view=latlon /MSGID=MAIN

      set shapeLayer reg.idplot.layers.world   ! Handle to world Shape file layer

      invoke coloringModes shapeLayer.coloringModes

      set coloringMode shapeLayer.coloringMode
      set tab shapeLayer.getColorTable
      set borderColor "^tab.border"

      gcontrol label mainLabel "Main"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" "^defaultInstructions" 40 8 /title
      gcontrol dval radius "Radius of search=" 1000.0 0.0 -1 100 /EDIT=true
      gcontrol text countries "Countries within range" "" 40 3 /title

    pipe off

  endif

  file close shp

endmacro

!******************************************************************************!
procedure processMessage m:msg

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name EQS "MARK" then
    !res msg.data
    res curRadius gc.radius.value
    invoke shapes shp.getShapesNear(msg.data.alt,msg.data.lat,msg.data.lon,curRadius)
    res countriesNear ""
    res noneBeforeThis true
    foreach shape IN shapes
      invoke datatable shape.getDataTable()
      table datatable getkey NAME countryName
      if noneBeforeThis then
        res countriesNear "^countryName"
        res noneBeforeThis false
      else
        res countriesNear "^{countriesNear},^{countryName}"
      endif
    endfor
    res gc.countries.value "^countriesNear"
  elseif msg.name EQS "ZOOM"
  elseif msg.name EQS "UNZOOM"
  elseif msg.name EQS "RADIUS"
  else
    ! do nothing
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif

return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
