I created the following class:
class myAIS():
def __init__(self, mmsi, Alat, Alon, Aspeed, Acourse, shipname):
self.mmsi=mmsi #integer
self.lat=Alat #float
self.lon=Alon #float
self.speed=Aspeed #float
self.Acourse=Acourse #float
self.name=shipname #string
This is used to store the current key information on every ship that I am detecting using an AIS receiver. The list variable AISList is then populated with all the ships as I receive their unique ID (MMSI).
AISList.append(myAIS(dm.mmsi, LatAIS, LonAIS, SpdAIS, CourseAIS, ShipName))
As I said, this allows me to store the current key information on each ship. I in fact, use this information to plot the ship positions on a map. However, as a ship moves, its new position is broadcast. So I need to constantly check that any new information received for existing ships are updated. If it is a new ship, then it is just appended as shown above.
I have successfully been able to identify when a ship is new to the list or if it is just an update. My issue is the method required to UPDATE the list. My approach was to simply delete the older instance of the ship in the list, and append the UPDATE to the bottom of the list. There were 2 reasons I used this approach: (1) it seemed easier [but turned out not to be] (2) I have a second list that stores the markers (symbols) for the map. That list is easily modified by this method (delete the repeating index and then use append to add the new instance).
This is the issue — how to I delete a AISList[j]
from the list, where j is the index of the older instance? For example, if j = 8, how do I delete AISList[j].mmsi, AISList[j].Alat, AISList[j].Alon,...,AISList[j].shipname
? Should I try using something other than a class list?
I have tried AISList.pop(j)
, AISList[j].remove()
, and delete()
, and every perceivable combination thereof. Desperation led to trying absurd combinations of AISList and myAIS with pop, remove, and delete. I get errors with every attempt.
I’ve searched stackoverflow (as well as many other sites) for answers, but most answers focus on removing an object, like AISList.course. But I don’t need to remove the object. I just need to remove the AISList[j]
instance.
Any help would be grateful.
Here is the code section I’m working on:
try:
Alength = len(AISList)
if Alength>0:
print("AISList length = ", Alength)
#print("AISList = ", AISList)
print("dm.mmsi = ", dm.mmsi)
for j in range(0,Alength):
readmmsi=int(dm.mmsi)
readAISList=AISList[j].mmsi
if readmmsi == readAISList:
**AISv.pop(j)
AISList.pop(j)**
except Exception as e:
print("AISList error = ", e)
pass
#Now add the latest ship information to the lists
#This the AISv list is the set of markers plotted
#on the TkMapView map.
try:
LatAIS=float(dm.lat)
except ValueError as e:
latAstr= dm.lat # can't take the float of a negative string
latAstr=latAstr[1:] #remove minus sign
latA=float(latAstr) #convert +lon1 to -lon1
latAIS=0-latA #now make it negative
try:
LonAIS=float(dm.lon)
except ValueError as e:
lonAstr= dm.lon # can't take the float of a negative string
lonAstr=lonAstr[1:] #remove minus sign
lonA=float(lonAstr) #convert +lon1 to -lon1
lonAIS=0-lonA #now make it negative
SpdAIS=float(dm.speed)
CourseAIS=float(dm.course)
ShipName="none"
#print("speed = ", SpdAIS)
AISv.append(app.map_widget.set_marker(LatAIS, LonAIS,icon=AISvessel_image)) #plot symbol
AISList.append(myAIS(dm.mmsi, LatAIS, LonAIS, SpdAIS, CourseAIS, ShipName))
print(AISList[Alength-1].mmsi)
Eddie Hughes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.