>I noticed that since version 5.5 of MapInfo it is possible to display
>a special file format called MapInfo Grid, very useful to display
>elevation or other interpolated data. The only way to query the grid
>is the Info Tool.
>
>Is this feature (grid querying) accessible via MapBasic? Also, is
>there any way to get all the grid values intersected by a line object
>and to store them in a table?

Here is the part of some sample code I posted earlier that shows how
to query a grid file with MapBasic. Getting all the grid values that
intersect a line object would be a natural extension of this. As I used
to hear 'it is left as an exercise for the student.'

'
********************************************************************************
Include "MapBasic.def"
Include "Icons.def"

'
********************************************************************************
'Declare external functions (all in MIGrid.DLL)
'Note: All MapBasic funtion variables are passed by reference unless
'      explicitly defined to be passed directly with the 'ByVal' key word.
'
********************************************************************************
Declare Function GE_OpenGrid Lib "Migrid.dll" (
  lpszFilename As String,
  ByVal lCacheSize As Integer,
  hGrid As Integer) As Logical

Declare Function GE_GetCoordSysInfo Lib "Migrid.dll" (
  ByVal hGrid As Integer,
  ptchCoordSys As String,
  pdMinXVal As Float,
  pdMinYVal As Float,
  pdMaxXVal As Float,
  pdMaxYVal As Float) As Logical

Declare Function GE_GetContinuousMinMax Lib "Migrid.dll" (
  ByVal hGrid As Integer,
  pdMinZVal As Float,
  pdMaxZVal As Float) As Logical

Declare Function GE_GetDimensions Lib "Migrid.dll" (
  ByVal hGrid As Integer,
  plWidth As Integer,
  plHeight As Integer) As Logical

Declare Function GE_StartRead Lib "Migrid.dll" (
  ByVal hGrid As Integer) As Logical

Declare Function GE_GetContinuousValue Lib "Migrid.dll" (
  ByVal hGrid As Integer,
  ByVal lCol As Integer,
  ByVal lRow As Integer,
  pdValue As Float,
  puchIsNull As SmallInt) As Logical

Declare Function GE_EndRead Lib "Migrid.dll" (
  ByVal hGrid As Integer) As Logical

Declare Function GE_CloseGrid Lib "Migrid.dll" (
  hGrid As Integer) As Logical

'
********************************************************************************
'Declare Local Functions
'
********************************************************************************
Declare Sub Main
Declare Sub GridInfoToolHandler

'
********************************************************************************
'Global Variables
'
********************************************************************************
Global gsPath As String

'
********************************************************************************
'
********************************************************************************
'Sub Main
'
********************************************************************************
'
********************************************************************************
Sub Main

  OnError Goto HandleError

  Alter ButtonPad "Tools"
    Add
      Separator
      ToolButton
        Calling GridInfoToolHandler
        Icon MI_ICON_INFO
        Cursor MI_CURSOR_CROSSHAIR
        DrawMode  DM_CUSTOM_POINT
        HelpMsg "Retrieve value from grid cell.\nRetrieve grid value"

    Show

  Exit Sub

HandleError:
  Note "Main: " + Error$()
  Resume Next

End Sub

'
********************************************************************************
'
********************************************************************************
' Sub GridInfoToolHandler
'
********************************************************************************
'
********************************************************************************
Sub GridInfoToolHandler

  OnError Goto HandleError

  Dim sCmd As String
  Dim i As SmallInt
  Dim lVerbose As Logical
  Dim x, y As Float
  Dim MapWindowID As Integer
  Dim lReturn As Logical
  Dim hGrid As Integer
  Dim sPath As String
  Dim ptchCoordSys As String
  Dim pdMinXVal, pdMinYVal, pdMaxXVal, pdMaxYVal As Float
  Dim pdMinZVal, pdMaxZVal As Float
  Dim plWidth, plHeight As Integer
  Dim lCol, lRow As Integer
  Dim pdValue As Float
  Dim puchIsNull As SmallInt

'
********************************************************************************
'Get map window and layer
'
********************************************************************************
  MapWindowID = FrontWindow()
  If WindowInfo( MapWindowID, WIN_INFO_TYPE) <> WIN_MAPPER Then
    Note "Click in a map window."
    Exit Sub
  End If

  For i = 1 To MapperInfo(MapWindowID, MAPPER_INFO_LAYERS)
    If LayerInfo(MapWindowID, i, LAYER_INFO_TYPE) =
       LAYER_INFO_TYPE_GRID Then
      sPath = LayerInfo(MapWindowID, i, LAYER_INFO_PATH)
      Exit For
    End If
  Next

'
********************************************************************************
'Get Grid file name
'
********************************************************************************
  sPath = Left$(sPath, Len(sPath)-3) + "MIG"
  If Not FileExists(sPath) Then
    If FileExists(ProgramDirectory$() + "Nwgrd30.ghl") Then
      sPath = Left$(sPath, Len(sPath)-3) + "GRD"
    End If
  End If
  If Not FileExists(sPath) Then
    Note "Cannot find grid file " + sPath
    Exit Sub
  End If

  If sPath <> gsPath Then
    gsPath = sPath
    lVerbose = TRUE
  Else
    lVerbose = FALSE
  End If

'
********************************************************************************
'set MapBasic coordinate system to match map window coordinate system
'
********************************************************************************
  sCmd = "Set " + MapperInfo(MapWindowID,
                             MAPPER_INFO_COORDSYS_CLAUSE_WITH_BOUNDS)
  Run Command sCmd

'
********************************************************************************
'Get coordinates of cursor location
'
********************************************************************************
  x = CommandInfo(CMD_INFO_X)
  y = CommandInfo(CMD_INFO_Y)
  Print "X = " + x + ", Y = " + y

'
********************************************************************************
'Open grid file
'
********************************************************************************
  lReturn = GE_OpenGrid(sPath, 1024, hGrid)
  If Not lReturn Then
    Note "Open " + sPath + " failed"
    Exit Sub
  End If
  If hGrid = 0 Then
    Note "Open " + sPath + " failed: grid handle = 0"
    Exit Sub
  End If

  If lVerbose Then
    Print "  Opened " + sPath + " with handle " + hGrid
  End If

'
********************************************************************************
'Get grid coordinate system information (especially min and max
coordinates)
'
********************************************************************************
  ptchCoordSys = Space$(255) 'Initialize to allocate actually memory.
  lReturn = GE_GetCoordSysInfo(hGrid, ptchCoordSys, pdMinXVal,
                               pdMinYVal, pdMaxXVal, pdMaxYVal)
  If lVerbose Then
    Print "  " + ptchCoordSys
    Print "  MinXVal = " + pdMinXVal + ", MinYVal = " + pdMinYVal +
          ", MaxXVal = " + pdMaxXVal + ", MaxYVal = " + pdMaxYVal
  End If

'
********************************************************************************
'Get minimum and maximum grid values
'
********************************************************************************
  lReturn = GE_GetContinuousMinMax(hGrid, pdMinZVal, pdMaxZVal)
  If lVerbose Then
    Print "  MinZVal = " + pdMinZVal + ", MaxZVal = " + pdMaxZVal
  End If

'
********************************************************************************
'Get grid dimensions (rows and columns)
'
********************************************************************************
  lReturn = GE_GetDimensions(hGrid, plWidth, plHeight)
  If lVerbose Then
    Print "  Width = " + plWidth + ", Height = " + plHeight
  End If

'
********************************************************************************
'Prepare to read grid
'
********************************************************************************
  lReturn = GE_StartRead(hGrid)
  If lReturn Then

'
********************************************************************************
'Calculate row and column of cursor location
'
********************************************************************************
    lCol = (plWidth * (x - pdMinXVal) / (pdMaxXVal - pdMinXVal)) - .5
    lRow = (plHeight -
            plHeight * (y - pdMinYVal) / (pdMaxYVal - pdMinYVal)) - .5

'
********************************************************************************
'Retrieve and display grid value
'
********************************************************************************
    lReturn = GE_GetContinuousValue(hGrid, lCol, lRow, pdValue, puchIsNull)
    If lCol < 0 Or lRow < 0 Or lCol >= plWidth Or lRow >= plHeight Then
      If pdValue = 0 Then
        Print "  Value at col: " + (lCol+1) +
                       ", row: " + (lRow+1) + " is undefined."
      Else
        Print "  Value at col: " + (lCol+1) +
                       ", row: " + (lRow+1) + " = " + pdValue +
                       ", but should be undefined."
      End If
    Else
      If puchIsNull Then
        Print "  Value at col: " + (lCol+1) +
                       ", row: " + (lRow+1) + " is NULL."
      Else
        Print "  Value at col: " + (lCol+1) +
                       ", row: " + (lRow+1) + " = " + pdValue
      End If
    End If
    lReturn = GE_EndRead(hGrid)
  Else
    Print "  StartRead(" + hGrid + ") failed"
  End If
  lReturn = GE_CloseGrid(hGrid)

  Exit Sub

HandleError:
  Note "GridInfoToolHandler: " + Error$()
  Resume Next

End Sub




----------------------------------------------------------------------
To unsubscribe from this list, send e-mail to [EMAIL PROTECTED] and put
"unsubscribe MAPINFO-L" in the message body, or contact [EMAIL PROTECTED]

Reply via email to