s = f.readlines()

ais_state = aisparser.ais_state()

for p in s:
    result = aisparser.assemble_vdm(ais_state, p)
    #    print "%s : %s" % (result, p)
    if (result == 0):
        ais_state.msgid = aisparser.get_6bit(ais_state.six_state, 6)
        #        print "msgid = %d" % (ais_state.msgid)

        if ais_state.msgid == 1:
            msg = aisparser.aismsg_1()
            aisparser.parse_ais_1(ais_state, msg)
            (status, lat_dd,
             long_ddd) = aisparser.pos2ddd(msg.latitude, msg.longitude)

            print "%s,%d,%s,%s" % (p[0:19], msg.userid, lat_dd, long_ddd)
#            print "mmsi     : %d" % (msg.userid)
#            print "latitude : %d" % (msg.latitude)
#            print "longitude: %d" % (msg.longitude)
#            print "lat_dd   : %s" % (lat_dd)
#            print "long_ddd : %s" % (long_ddd)
#            print "pos_acc  : %d" % (ord(msg.pos_acc))

        elif ais_state.msgid == 2:
            msg = aisparser.aismsg_2()
            aisparser.parse_ais_2(ais_state, msg)
            (status, lat_dd,
             long_ddd) = aisparser.pos2ddd(msg.latitude, msg.longitude)
Exemple #2
0
def write_xml(ships):
    """ Update the XML file with the most recent ship positions 
    """
    #    pp = pprint.PrettyPrinter(indent=4)
    #    pp.pprint(ships)

    f = open(xml_file, 'w')
    f.write("<markers>\n")

    for mmsi in ships:
        info, pos, last_update = ships[mmsi]
        if (pos != None) and (last_update > time.time() - MAX_AGE):
            # Skip unknown positions
            if (pos.latitude == 54600000) or (pos.longitude == 108600000):
                continue

            (status, lat_dd,
             long_ddd) = aisparser.pos2ddd(pos.latitude, pos.longitude)
            if debug:
                print "%d %s : %0.4f %0.4f" % (ord(
                    pos.msgid), pos.userid, lat_dd, long_ddd)

            if info == None:
                name = mmsi
                callsign = ""
                dest = ""
                ship_type = 0
                imo = ""
                eta = ""
                length = 0
                width = 0
                draught = 0
            else:
                imo = str(info.imo)
                name = info.name
                callsign = info.callsign
                dest = info.dest
                ship_type = info.ship_type
                (month, day, hour, minute) = calc_eta(info.eta)
                if (month, day, hour, minute) == (0, 0, 24, 60):
                    eta = "NA (%d)" % (info.eta)
                else:
                    try:
                        month = months[month]
                    except:
                        month = ""
                    eta = "%s %d %d:%d (%d)" % (month, day, hour, minute,
                                                info.eta)
                length = info.dim_bow + info.dim_stern
                width = ord(info.dim_port) + ord(info.dim_starboard)
                draught = info.draught

            # Base Station, no speed, course or status
            if ord(pos.msgid) == 4:
                pos.sog = 0
                pos.cog = 0
                pos.nav_status = "\0"

#            pp = pprint.PrettyPrinter(indent=4)
#            print ">>>>>>>>"
#            pp.pprint(pos)
#            print ">>>>>>>>"

# mmsi!imo!callsign!destination!eta!speed!course!status!length!width!draft!mtime
            ais_info = ""

            try:
                ais_info = "%s!%s!%s!%s!%s!%0.1f!" % (
                    mmsi, imo, callsign, dest, eta, pos.sog / 10.0)
                ais_info += "%0.1f!%d!" % (pos.cog / 10.0, ord(pos.nav_status))
                ais_info += "%d!%d!%0.1f!%s" % (length, width, draught / 10.0,
                                                time.asctime(
                                                    time.gmtime(last_update)))

            except:
                #                pp = pprint.PrettyPrinter(indent=4)
                #                pp.pprint(info)
                raise

            f.write(
                "<marker lat='%0.4f' lon='%0.4f' name='%s' dx='%0.4f' dy='%0.4f' type='%d' ais='%s'/>\n"
                %
                (lat_dd, long_ddd, name, pos.dx, pos.dy, ship_type, ais_info))
        else:
            print "Skipped %s - %s" % (mmsi,
                                       time.asctime(time.gmtime(last_update)))

    f.write("</markers>\n")
    f.close()

    # Copy to live website
    os.system(
        "scp /tmp/data.xml [email protected]:/home/aisparser/live_html/")
#with open('log_ais_web2day.log', 'r') as f:
#	s=f.readlines()

ais_state = aisparser.ais_state()

for p in s:
    result = aisparser.assemble_vdm( ais_state, p )
#    print "%s : %s" % (result, p)
    if( result == 0):
        ais_state.msgid = aisparser.get_6bit( ais_state.six_state, 6 )
#        print "msgid = %d" % (ais_state.msgid)

        if ais_state.msgid == 1:
            msg = aisparser.aismsg_1()
            aisparser.parse_ais_1( ais_state, msg )
            (status,lat_dd,long_ddd) = aisparser.pos2ddd(msg.latitude, msg.longitude)

#            print "%s,%d,%s,%s" % (p[0:19],msg.userid,lat_dd,long_ddd)
#            print "mmsi     : %d" % (msg.userid)
#            print "latitude : %d" % (msg.latitude)
#            print "longitude: %d" % (msg.longitude)
#            print "lat_dd   : %s" % (lat_dd)
#            print "long_ddd : %s" % (long_ddd)
#            print "pos_acc  : %d" % (ord(msg.pos_acc))

        elif ais_state.msgid == 2:
            msg = aisparser.aismsg_2()
            aisparser.parse_ais_2( ais_state, msg )
            (status,lat_dd,long_ddd) = aisparser.pos2ddd(msg.latitude, msg.longitude)

#            print "%s,%d,%s,%s" % (p[0:19],msg.userid,lat_dd,long_ddd)
Exemple #4
0
def write_xml( ships ):
    """ Update the XML file with the most recent ship positions
    """
#    pp = pprint.PrettyPrinter(indent=4)
#    pp.pprint(ships)

    f = open( xml_file, 'w' )
    f.write( "<markers>\n" )

    for mmsi in ships:
        info, pos, last_update = ships[mmsi]
        if (pos != None) and (last_update > time.time() - MAX_AGE):
            # Skip unknown positions
            if (pos.latitude == 54600000) or (pos.longitude == 108600000):
                continue

            (status,lat_dd,long_ddd) = aisparser.pos2ddd( pos.latitude, pos.longitude );
            if debug: print "%d %s : %0.4f %0.4f" % (ord(pos.msgid),pos.userid,lat_dd,long_ddd)

            if info == None:
                name = mmsi
                callsign = ""
                dest = ""
                ship_type = 0
                imo = ""
                eta = ""
                length = 0
                width = 0
                draught = 0
            else:
                imo = str(info.imo)
                name = info.name
                callsign = info.callsign
                dest = info.dest
                ship_type = info.ship_type
                (month, day, hour, minute) = calc_eta( info.eta )
                if (month, day, hour, minute) == (0, 0, 24, 60):
                    eta = "NA (%d)" % (info.eta)
                else:
                    try:
                        month = months[month]
                    except:
                        month = ""
                    eta = "%s %d %d:%d (%d)" % (month, day, hour, minute, info.eta)
                length = info.dim_bow + info.dim_stern
                width = ord(info.dim_port) + ord(info.dim_starboard)
                draught = info.draught

            # Base Station, no speed, course or status
            if ord(pos.msgid) == 4:
                pos.sog = 0
                pos.cog = 0
                pos.nav_status = "\0"

#            pp = pprint.PrettyPrinter(indent=4)
#            print ">>>>>>>>"
#            pp.pprint(pos)
#            print ">>>>>>>>"

            # mmsi!imo!callsign!destination!eta!speed!course!status!length!width!draft!mtime
            ais_info = ""

            try:
                ais_info = "%s!%s!%s!%s!%s!%0.1f!" % (mmsi,imo,callsign,dest,eta,pos.sog/10.0)
                ais_info += "%0.1f!%d!" % (pos.cog/10.0,ord(pos.nav_status))
                ais_info += "%d!%d!%0.1f!%s" % (length, width, draught/10.0,time.asctime(time.gmtime(last_update)))

            except:
#                pp = pprint.PrettyPrinter(indent=4)
#                pp.pprint(info)
                raise

            f.write( "<marker lat='%0.4f' lon='%0.4f' name='%s' dx='%0.4f' dy='%0.4f' type='%d' ais='%s'/>\n" % (lat_dd,long_ddd,name,pos.dx,pos.dy,ship_type,ais_info))
        else:
            print "Skipped %s - %s" % (mmsi,time.asctime(time.gmtime(last_update)))

    f.write( "</markers>\n" )
    f.close()

    # Copy to live website
    os.system("scp /tmp/data.xml [email protected]:/home/aisparser/live_html/")