Here is an update to the MapBasic sample code I posted earlier that
contains
fixes to a couple of items pointed out by careful readers.
'
*****************************************************************************
' Copyright (c) 2000, MAPINFO CORPORATION
' All rights reserved.
'
' $Workfile: GridInfo.mb $
' $Revision: 1.1 $
' $Author: DRESSEL $
' $Date: May 22 2000 16:14:56 $
'
' Module Description:
'
' MapBasic program to retrieve grid info.
'
' Revision History:
'
' Rev 1.0 May 22 2000 16:14:56 DRESSEL
' Rev 1.1 June 6 2000 13:30:00 DRESSEL
' Check for minimum version of MapInfo Professional (5.5)
' Add support for Northwood's NWGRD30.GHL grid handler
' Fix bug with button help message
' Fix typo refering to 'rotatesymbols'
'
*****************************************************************************
Include "MapBasic.def"
Include "Menu.def"
Include "Icons.def"
Declare Sub Main
Declare Sub GridInfoToolHandler
Declare Sub About
Declare Sub GoodBye
Define AppVersion 1.1
'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
Global gsPath As String
Sub Main
OnError Goto HandleError
If SystemInfo(SYS_INFO_MIVERSION) < 550 Then
Note "This utility depends on the Grid Engine API of " +
"MapInfo Professional version 5.5 or higher to run."
Exit Sub
End If
Create Menu "&Grid Info" As
"&About Grid Info..." Calling About,
"E&xit Grid Info" Calling Goodbye
Alter Menu "Tools" Add "&Grid Info" As "&Grid Info"
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
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
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
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
sCmd = "Set " + MapperInfo(MapWindowID,
MAPPER_INFO_COORDSYS_CLAUSE_WITH_BOUNDS)
Run Command sCmd
x = CommandInfo(CMD_INFO_X)
y = CommandInfo(CMD_INFO_Y)
Print "X = " + x + ", Y = " + y
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
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
lReturn = GE_GetContinuousMinMax(hGrid, pdMinZVal, pdMaxZVal)
If lVerbose Then
Print " MinZVal = " + pdMinZVal + ", MaxZVal = " + pdMaxZVal
End If
lReturn = GE_GetDimensions(hGrid, plWidth, plHeight)
If lVerbose Then
Print " Width = " + plWidth + ", Height = " + plHeight
End If
lReturn = GE_StartRead(hGrid)
If lReturn Then
lCol = (plWidth * (x - pdMinXVal) / (pdMaxXVal - pdMinXVal)) - .5
lRow = (plHeight -
plHeight * (y - pdMinYVal) / (pdMaxYVal - pdMinYVal)) - .5
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
Sub About
OnError Goto HandleError
Dialog
Title "About Grid Info (Version " + Str$(AppVersion) + ")"
Width 160
Control StaticText
Title "Open grid and retrieve value at mouse click."
Position 10, 10
Control StaticText
Title "Select 'i' tool from 'Tools' button pad and"
Position 10, 18
Control StaticText
Title "click on grid in a map window."
Position 10, 26
Control OKButton
Title "&OK"
Exit Sub
HandleError:
Note "About: " + Error$()
Resume Next
End Sub
Sub GoodBye
OnError Goto HandleError
End Program
Exit Sub
HandleError:
Note "GoodBye: " + Error$()
Resume Next
End Sub
' End of File
----------------------------------------------------------------------
To unsubscribe from this list, send e-mail to [EMAIL PROTECTED] and put
"unsubscribe MAPINFO-L" in the message body, or contact [EMAIL PROTECTED]