MapInfo Pro

  • 1.  Mapbasic - check if layer is visible in map window

    Posted 09-20-2021 22:07
    Hi All
    Is there mapbasic code that will let me check if a layer is visible in the map window?

    I have code that zooms to a view - I want to know if a particular layer is visible in that view.

    Thanks

    Simon Hull

    GIS / Asset Officer | Burnie City Council



    ------------------------------
    Simon Hull
    City of Burnie
    Burnie TAS
    ------------------------------


  • 2.  RE: Mapbasic - check if layer is visible in map window

    Posted 09-21-2021 02:13
    Hi Simon

    A function like this should help you out:

    Function LayerIsVisible(ByVal nMID As Integer, ByVal nLayerID As Integer) As Logical

    Dim fZoomMin, fZooMax As Float

    LayerIsVisible = FALSE


    If LayerInfo(nMID, nLayerID, LAYER_INFO_DISPLAY) = LAYER_INFO_DISPLAY_OFF Then
       Exit Function
    End If

    If LayerInfo(nMID, nLayerID, LAYER_INFO_ZOOM_LAYERED) = TRUE Then
       fZoomMin = LayerInfo(nMID, nLayerID, LAYER_INFO_ZOOM_MIN)

       fZoomMax = LayerInfo(nMID, nLayerID, LAYER_INFO_ZOOM_MAX)
       If MapperInfo(nMID, MAPPER_INFO_ZOOM) Between fZoomMin And fZoomMax Then

          Exit Function
       End If
    End If

    LayerIsVisible = TRUE

    End Function

    I haven't tested it

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 3.  RE: Mapbasic - check if layer is visible in map window

    Posted 10-05-2021 19:30
    Hi Peter
    Thanks for that - just had a try with this.
    My understanding of this is that it checks if a layer is visible (i.e the whole layer) based on zoom levels of the current map.
    Which is not quite what I was after? And looking back I can see I didn't ask correctly.
    What I want to determine is if there are any objects in the layer visible in the map window?
    Not sure if this is possible?
    Thanks
    Simon

    ------------------------------
    Simon Hull
    City of Burnie
    Burnie TAS
    ------------------------------



  • 4.  RE: Mapbasic - check if layer is visible in map window

    Posted 10-07-2021 02:08
      |   view attached
    Hi Simon

    Right, that needs an extension to my previous example.
    This new function will query the layer with the extent of the map window to see how many records the layer has within the extent.

    Function LayerHasObjectsVisible(ByVal nMID As Integer, ByVal nLayerID As Integer) As Logical

    Dim sTab As String,
    oMap As Object,
    nRows As Integer

    LayerHasObjectsVisible = FALSE

    If not LayerIsVisible(nMID, nLayerID) Then
      Exit Sub
    End If

    oMap = MAPGetExtent(nMID)
    sTab = LayerInfo(nMID, nLayerID, LAYER_INFO_NAME)

    Select Count(*) "NumRecs"
      From sTab
      Where OBJ Within oMap
      Into _COUNTED_RECORDS NoSelect Hide

    If TableInfo(_COUNTED_RECORDS, TAB_INFO_NROWS) = 0 Then
      Print Time(24) & ": Empty query result for " & sTab
      Close Table _COUNTED_RECORDS
      Exit Function
    End If

    Fetch First From _COUNTED_RECORDS
    nRows = _COUNTED_RECORDS.NumRecs
    Close Table _COUNTED_RECORDS

    Print Time(24) & ": " & nRows & " record(s) from " & sTab & " are within the map extents"
    If nRows = 0 Then
      Exit Function
    End If

    LayerHasObjectsVisible = TRUE

    End Function

    I have also included a small application with source code that you can run. It reports the visibility of layers when you zoom and pan in your map window. Gives you an idea of how this will work.

    You'll notice I use calls to other functions in the function above. They are included in the attached sample application

    Remove the many print statements when you start using the function in production.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------

    Attachment(s)

    zip
    LayerVisibility.zip   2 KB 1 version


  • 5.  RE: Mapbasic - check if layer is visible in map window

    Posted 10-07-2021 19:05
    Thanks Peter - much appreciated!
    Look forward to giving it a try
    I wasn't sure could be done so I am impressed.

    ------------------------------
    Simon Hull
    City of Burnie
    Burnie TAS
    ------------------------------