【聽如子說】-python模塊系列-AIS編解碼Pyais

Pyais Module Introduce

pyais一個簡單實用的ais編解碼模塊

工作中需要和ais打交道,在摸魚的過程中發現了一個牛逼的模塊,對ais編解碼感興趣的可以拿項目學習一下,或者運用到你的項目中!

v1 doc 棄用,看2就好

[v2] 倉庫(folked)](//github.com/Iamruzi/pyais)

現在github拉取的代碼是v2,所以例子參考直接查看github的readme就好了,貼的第一個鏈接是v1與v2倉庫的文檔

模塊特點

AIS消息的編解碼。

100% 純Python。

支持AIVDM / AIVDO消息。

支持單消息,文件和TCP/UDP套接字。

首先直接pip安裝模塊

!pip install pyais

編碼例子

from pyais import encode_dict

# Every message needs at least a MMSI and a message-type (1-27)
data = {'mmsi': 12345, 'type': 1}

# Because larger payloads may need to split over several fragment sentences
# `encode_dict` always returns a list of parts (even if the message has a single part)
encoded = encode_dict(data)

看看編碼效果:

encoded
['!AIVDO,1,1,,A,E000h>@00000000000000000000000000000000000000000000000000000,4*73']
from pyais.encode import encode_dict

data = {
    'course': 219.3,
    'lat': 37.802,
    'lon': -122.341,
    'mmsi': '366053209',
    'type': 1,
}
# This will create a type 1 message for the MMSI 366053209 with lat, lon and course values specified above
encoded = encode_dict(data, radio_channel="B", talker_id="AIVDM")[0]

解碼例子

from pyais import decode

decoded = decode(b"!AIVDM,1,1,,B,15NG6V0P01G?cFhE`R2IU?wn28R>,0*05")
print(decoded)
MessageType1(msg_type=1, repeat=0, mmsi=367380120, status=<NavigationStatus.UnderWayUsingEngine: 0>, turn=None, speed=0.1, accuracy=False, lon=-122.404333, lat=37.806948, course=245.2, heading=511, second=59, maneuver=0, spare_1=b'\x00', raim=True, radio=34958)
from pyais import decode

parts = [
    b"!AIVDM,2,1,4,A,55O0W7`00001L@gCWGA2uItLth@DqtL5@F22220j1h742t0Ht0000000,0*08",
    b"!AIVDM,2,2,4,A,000000000000000,2*20",
]

# Decode a multipart message using decode
decoded = decode(*parts)
print(decoded)
MessageType5(msg_type=5, repeat=0, mmsi=368060190, ais_version=2, imo=0, callsign='WDK4954', shipname='P/V_GOLDEN_GATE', ship_type=50, to_bow=14, to_stern=7, to_port=4, to_starboard=2, epfd=0, month=0, day=0, hour=24, minute=60, draught=0.0, destination='', dte=False, spare_1=b'\x00')

其他例子

參看倉庫的readme,本博客就是一個介紹,很強大,可認真學習源碼,使用時候請注意開源協議,注意引用來源,尊重作者。

gui使用 (todo,有時間在二次開發)

基於gooey快速開發GUI application

首先,咱們安裝一下gooey,這是一個快速生成GUI程序的一個庫,好用簡單。

!pip install Gooey

未完,待續…

可執行程序打包

未完,待續…