M0r13n / pyais

AIS message decoding and encoding in Python (AIVDM/AIVDO)
MIT License
176 stars 61 forks source link

Get Country Code #131

Closed mnaranjo closed 5 months ago

mnaranjo commented 5 months ago

Hello,

What would I have to do to obtain the 'country code' of a vessel?

Thank you in advance

M0r13n commented 5 months ago

Hey @mnaranjo,

which country code are you referring to? As far as I know, there are two types of "country codes" in AIS messages:

Neither is currently implemented in pyais. Support for the MID could easily be added. The latter is a rather rare type of message that is very specific and almost never occurs in real world scenarios. Therefore, I am a bit hesitant to add support for this subtype.

mnaranjo commented 5 months ago

Hi @M0r13n , I was referring to the possibility to know the flag of the ship, i.e. to know which country each ship belongs to when data is collected.

M0r13n commented 5 months ago

@mnaranjo This can only be achieved by examining the first digits of the MMSI. These two articles explain how this works:

As of right now, pyais does not provide the functionality to extract the country/flag from the MMSI. However, I believe that I will add this feature in the near future

mnaranjo commented 5 months ago

Thank you very much for your help.

M0r13n commented 5 months ago

Hey @mnaranjo,

with the newest version of pyais you can get the country code and name:

from pyais.util import get_country

# The first 3 digits of any MMSI number are indicative of the vessel's flag
country_code, country_name = get_country(249110000)
assert country_code, country_name == ('MT', 'Malta')