1

Hya all, I am plotting aircraft positions contained in a DataFrame df, on a leaflet map using Python. An icon is created with an airplane symbol. So I add all markers to the map, using:

    for i in range(0, len(df)):
        ac = Marker(name = df['callsign'][i],
                    location=(df['latitude'][i], df['longitude'][i]), 
                    icon            = icon, 
                    rotation_angle  = float(df['heading'][i]), 
                    rotation_origin = 'center')
        m.add_layer(ac)

For the next update cycle I need to remove these layers (could be up to a hundred) but I cannot find a way to address specifically the layers that need to be removed, although they are defined with the 'name' field:

    # remove all the ac symbols before we go into the next loop
    for i in range(0, len(df)):
        m.remove_layer(i)

Gives this error:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18204/2482639859.py in <module>
     71     # remove all the ac symbols before we go into the next loop
     72     for i in range(0, len(df)):
---> 73         m.remove_layer(i)
     74         #print(ac.name)
     75 

~\anaconda3\lib\site-packages\ipyleaflet\leaflet.py in remove_layer(self, rm_layer)
   2152             The layer to remove.
   2153         """
-> 2154         if rm_layer.model_id not in self._layer_ids:
   2155             raise LayerException('layer not on map: %r' % rm_layer)
   2156         self.layers = tuple([layer for layer in self.layers if layer.model_id != rm_layer.model_id])

AttributeError: 'int' object has no attribute 'model_id'

So it looks like I can reference to these layers somehow, but cannot find how. Any clues much appreciated.

Job Brüggen
  • 173
  • 10

1 Answers1

4

So the answer looks like as follows:

    # remove all the ac symbols before we go into the next loop
    # except the first layer because that is the background map
    
    print('There are', len(m.layers), 'layers')
    for i in range(1, len(m.layers)):
        print('Removing layer',i, m.layers[1].name)
        m.remove_layer(m.layers[1])

Layer 1 is the map. Because I remove always layer 2 (indexed as 1), the total number of layers goes minus one. So the next cycle I need AGAIN to remove layer 2 (indexed as 1). Hope this makes sense.

However, this removing of layers is very slow... and very visible on the map. It is not an almost invisible update. Hmmm....

Job Brüggen
  • 173
  • 10