Package ais :: Module ais_positions
[hide private]
[frames] | no frames]

Source Code for Module ais.ais_positions

  1  #!/usr/bin/env python 
  2   
  3  __version__ = '$Revision: 4791 $'.split()[1] 
  4  __date__ = '$Date: 2007-01-02 $'.split()[1] 
  5  __author__ = 'xmlbinmsg' 
  6   
  7  __doc__=''' 
  8   
  9  Get the name and mmsi (User ID) for each msg 5 identification message. 
 10   
 11  This program requires that the nmea strings be sorted such that the 
 12  2nd part of the message comes directly after the first.  You can do 
 13  this by grepping for the USCG N-AIS station name.  This will prevent 
 14  interleaving of msg 5 nmea strings. 
 15   
 16  @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3 
 17  @requires: U{pyproj<http:///>} 
 18   
 19  @author: '''+__author__+''' 
 20  @version: ''' + __version__ +''' 
 21  @var __date__: Date of last svn commit 
 22  @undocumented: __version__ __author__ __doc__ parser 
 23  @status: under development 
 24  @license: GPL 
 25  ''' 
 26   
 27  import sys, os 
 28   
 29  # Can decode messages 1,2,3 will any of the three codecs 
 30   
 31  import binary, ais_msg_1, aisstring 
 32  #from BitVector import BitVector 
 33   
 34  from pyproj import Proj 
 35  import math 
 36   
37 -def dist (lon1, lat1, lon2, lat2):
38 dx = (lon1-lon2) 39 dy = (lat1-lat2) 40 return math.sqrt(dx*dx + dy*dy)
41
42 -def getPosition(logfile,outfile,minDist=None):
43 44 45 # FIX: use the right utm zone. 14 is the central US so it will kind of work 46 params={'proj':'utm','zone':14} #int(options.zone)} 47 proj=None 48 if minDist!=None: 49 proj = Proj(params) 50 51 positions = {} # Last recoded ship position 52 53 #print 'mindist:',minDist 54 55 for line in logfile: 56 fields = line.split(',') 57 if '1'!=fields[2]: # Must be the start of a sequence 58 continue 59 bv = binary.ais6tobitvec(fields[5][:39]) # Hacked for speed 60 61 timestamp = fields[-1].strip() 62 mmsi = str(ais_msg_1.decodeUserID(bv)) 63 lon = ais_msg_1.decodePosition_longitude(bv) 64 lat = ais_msg_1.decodePosition_latitude(bv) 65 66 d = None 67 if mmsi not in positions: 68 positions[mmsi]=(lon,lat) 69 elif minDist != None: 70 lonOld,latOld = positions[mmsi] 71 oldUTM = proj(lonOld,latOld) 72 newUTM = proj(lon,lat) 73 d = dist(oldUTM[0],oldUTM[1],newUTM[0],newUTM[1]) 74 if str(d)=='nan': 75 continue #pass # FIX: Print but do not save nan values??? 76 elif d < minDist: 77 continue 78 else: 79 positions[mmsi]=(lon,lat) 80 81 lon = str(lon) 82 lat = str(lat) 83 84 if len(mmsi)<9: mmsi += ' '*(9-len(mmsi)) 85 86 fLen = 12 # field length ... how much space 87 88 if len(lon)>fLen: lon = lon[:fLen] 89 if len(lon)<fLen: lon += ' '*(fLen-len(lon)) 90 91 if len(lat)>fLen: lat = lat[:fLen] 92 if len(lat)<fLen: lat += ' '*(fLen-len(lat)) 93 94 #outfile.write(timestamp+' '+str(mmsi)+' '+lon+' '+lat+' -- '+str(d)+'\n') 95 outfile.write(timestamp+' '+str(mmsi)+' '+lon+' '+lat+'\n')
96 97 if __name__=='__main__': 98 from optparse import OptionParser 99 parser = OptionParser(usage="%prog [options] logfile1 [logfile2 logfile3 ...] ", version="%prog "+__version__) 100 parser.add_option('-o','--output',dest='outputFileName',default=None, 101 help='Name of the python file to write [default: stdout]') 102 parser.add_option('-m','--min-dist',dest='minDist',default=None,type='float', 103 help='minimum distance to move before output a new position in meters [default: None]') 104 105 # FIX: add option that a max time between positions to pass if the ship is not moving 106 107 (options,args) = parser.parse_args() 108 109 outfile = sys.stdout 110 if None!=options.outputFileName: 111 print 'outfilename=',options.outputFileName 112 outfile = file(options.outputFileName,'w') 113 if 0==len(args): 114 getPosition(sys.stdin,outfile,options.minDist) 115 else: 116 for filename in args: 117 getPosition(file(filename),outfile,options.minDist) 118