├── pyAISm.py ├── LICENSE ├── .gitignore ├── README.md └── examples.py /pyAISm.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pirpyn/pyAISm/HEAD/pyAISm.py -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Pierre Payen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 2 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 3 | 4 | # User-specific stuff 5 | .idea/**/workspace.xml 6 | .idea/**/tasks.xml 7 | .idea/**/usage.statistics.xml 8 | .idea/**/dictionaries 9 | .idea/**/shelf 10 | 11 | .idea/** 12 | 13 | # Generated files 14 | .idea/**/contentModel.xml 15 | __pycache__/ 16 | *.pyc 17 | 18 | # Sensitive or high-churn files 19 | .idea/**/dataSources/ 20 | .idea/**/dataSources.ids 21 | .idea/**/dataSources.local.xml 22 | .idea/**/sqlDataSources.xml 23 | .idea/**/dynamic.xml 24 | .idea/**/uiDesigner.xml 25 | .idea/**/dbnavigator.xml 26 | 27 | # Gradle 28 | .idea/**/gradle.xml 29 | .idea/**/libraries 30 | 31 | # Gradle and Maven with auto-import 32 | # When using Gradle or Maven with auto-import, you should exclude module files, 33 | # since they will be recreated, and may cause churn. Uncomment if using 34 | # auto-import. 35 | # .idea/modules.xml 36 | # .idea/*.iml 37 | # .idea/modules 38 | 39 | # CMake 40 | cmake-build-*/ 41 | 42 | # Mongo Explorer plugin 43 | .idea/**/mongoSettings.xml 44 | 45 | # File-based project format 46 | *.iws 47 | 48 | # IntelliJ 49 | out/ 50 | 51 | # mpeltonen/sbt-idea plugin 52 | .idea_modules/ 53 | 54 | # JIRA plugin 55 | atlassian-ide-plugin.xml 56 | 57 | # Cursive Clojure plugin 58 | .idea/replstate.xml 59 | 60 | # Crashlytics plugin (for Android Studio and IntelliJ) 61 | com_crashlytics_export_strings.xml 62 | crashlytics.properties 63 | crashlytics-build.properties 64 | fabric.properties 65 | 66 | # Editor-based Rest Client 67 | .idea/httpRequests 68 | 69 | # Android studio 3.1+ serialized cache file 70 | .idea/caches/build_file_checksums.ser 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pyAISm 2 | 3 | A small and incomplete python decoder for AIS messaging. 4 | 5 | A bunch of naive pythons functions to decode somes ais_data/AIVDM messages: 6 | 7 | Based on the doc here: (http://catb.org/gpsd/AIVDM.html). 8 | 9 | ## How to use it 10 | In python 2 console: 11 | ```python 12 | > msg = '!AIVDO,1,1,,,B00000000868rA6 ais_data = decod_ais(msg) # Return a dictionnary 14 | > ais_format = format_ais(ais_data) # A more human readable dictionnary 15 | > print ais_data, ais_data['lon'], ais_format['lon'] # Accessing the value of the key 16 | ``` 17 | 18 | Decode a file: 19 | ```python 20 | def decode_file_example(): 21 | """ 22 | Example for decoding a file 23 | :return: 24 | """ 25 | with open("ais.exploratorium.edu", "r") as file: 26 | for aline in file: 27 | try: 28 | msg = aline.rstrip("\n") 29 | ais_data = pyAISm.decod_ais(msg) # Return a dictionnary 30 | ais_format = pyAISm.format_ais(ais_data) # A more human readable dictionnary 31 | print(ais_format) # Accessing the value of the key 32 | except pyAISm.UnrecognizedNMEAMessageError as e: 33 | print e 34 | except pyAISm.BadChecksumError as e: 35 | print e 36 | except Exception as e: 37 | print e 38 | print('End of file') 39 | ``` 40 | 41 | Decode a stream: 42 | ```python 43 | def decode_stream_example(): 44 | """ 45 | Example for decoding an online data stream 46 | :return: 47 | """ 48 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 49 | s.connect(("ais.exploratorium.edu", 80)) 50 | s.sendall("GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n".encode()) 51 | while (True): 52 | msg = (s.recv(4096).decode('utf-8')).splitlines() 53 | for m in msg: 54 | try: 55 | msg = m.rstrip("\n") 56 | ais_data = pyAISm.decod_ais(msg) # Return a dictionnary 57 | ais_format = pyAISm.format_ais(ais_data) # A more human readable dictionnary 58 | print(ais_format) # Accessing the value of the key 59 | except pyAISm.UnrecognizedNMEAMessageError as e: 60 | print e 61 | except pyAISm.BadChecksumError as e: 62 | print e 63 | except Exception as e: 64 | print e 65 | ``` -------------------------------------------------------------------------------- /examples.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | import pyAISm 4 | 5 | 6 | def decode_file_example(): 7 | """ 8 | Example for decoding a file 9 | :return: 10 | """ 11 | with open("ais.exploratorium.edu", "r") as file: 12 | for aline in file: 13 | try: 14 | msg = aline.rstrip("\n") 15 | ais_data = pyAISm.decod_ais(msg) # Return a dictionnary 16 | ais_format = pyAISm.format_ais(ais_data) # A more human readable dictionnary 17 | print(ais_format) # Accessing the value of the key 18 | except pyAISm.UnrecognizedNMEAMessageError as e: 19 | print e 20 | except pyAISm.BadChecksumError as e: 21 | print e 22 | except Exception as e: 23 | print e 24 | print('End of file') 25 | 26 | 27 | ###################################################################################### 28 | 29 | def count_message_types_example(): 30 | """ 31 | Example for decoding a file and counting the number of message per type 32 | :return: 33 | """ 34 | with open("ais.exploratorium.edu", "r") as file: 35 | message_types = dict() 36 | for aline in file: 37 | try: 38 | msg = aline.rstrip("\n") 39 | ais_data = pyAISm.decod_ais(msg) # Return a dictionnary 40 | if 'type' in ais_data: 41 | if ais_data['type']: 42 | msg_type = ais_data['type'] 43 | if msg_type in message_types: 44 | message_types[msg_type] += 1 45 | else: 46 | message_types[msg_type] = 1 47 | except pyAISm.UnrecognizedNMEAMessageError as e: 48 | pass 49 | except pyAISm.BadChecksumError as e: 50 | pass 51 | except Exception as e: 52 | print e 53 | print('End of file') 54 | print(message_types) 55 | 56 | 57 | ###################################################################################### 58 | 59 | def decode_stream_example(): 60 | """ 61 | Example for decoding an online data stream 62 | :return: 63 | """ 64 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 65 | s.connect(("ais.exploratorium.edu", 80)) 66 | s.sendall("GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n".encode()) 67 | while (True): 68 | msg = (s.recv(4096).decode('utf-8')).splitlines() 69 | for m in msg: 70 | try: 71 | msg = m.rstrip("\n") 72 | ais_data = pyAISm.decod_ais(msg) # Return a dictionnary 73 | ais_format = pyAISm.format_ais(ais_data) # A more human readable dictionnary 74 | print(ais_format) # Accessing the value of the key 75 | except pyAISm.UnrecognizedNMEAMessageError as e: 76 | print e 77 | except pyAISm.BadChecksumError as e: 78 | print e 79 | except Exception as e: 80 | print e 81 | 82 | --------------------------------------------------------------------------------