def create_kml_map( self, outputfile, kmzoutput=True, linestring=True, livemap=False, livemaptimeout=480, orderby='Types', region='A'): """ create a KML map of all the vessels we have on record Args: outputfile(str): full path to output to kmzoutput(bool): whether to create a kmz with custom icons (True) or a basic kml file (False) linestring(bool): display a line showing where the vessel has been livemap(bool): is this for a constantly updating KML file livemaptimeout(int): if the last postion time of a station is greater than this, then the station will not be displayed on the map, default is 480 seconds (8 minutes) APPLIES TO LIVE MAP ONLY orderby(str): order the stations by 'Types', 'Flags' or 'Class' default is 'Types' region(str): IALA region, default is A """ types = set() headings = set() cogs = set() if kmzoutput and not livemap: docpath = os.path.join(os.path.dirname(outputfile), 'doc.kml') else: docpath = os.path.join(outputfile) kmlmap = kml.KMLOutputParser(docpath) kmlmap.create_kml_header(kmz=kmzoutput, ialaregion=region) organisedstns = self.sort_mmsi_by_catagory() for catagory in organisedstns[orderby]: kmlmap.open_folder(catagory) for mmsi in organisedstns[orderby][catagory]: stn = self.stations[mmsi] try: lastpos = stn.get_latest_position() if livemap: currenttime = datetime.datetime.utcnow() lastpostime = datetime.datetime.strptime( lastpos['Time'], '%Y/%m/%d %H:%M:%S') timediff = currenttime - lastpostime if timediff.seconds > livemaptimeout: continue desc = lastpos['Time'] else: stninfo = stn.get_station_info() desc = kmlmap.format_kml_placemark_description(stninfo) if stn.name != '': displayname = stn.mmsi + ' - ' + stn.name else: displayname = stn.mmsi kmlmap.open_folder(displayname) try: alt = str(lastpos['Altitude (m)']) except KeyError: alt = '0' try: heading = lastpos['True Heading'] if heading != HEADINGUNAVAILABLE and kmzoutput: headings.add(heading) hdesc = 'HEADING - {}'.format(heading) kmlmap.add_kml_placemark( hdesc, '', str(lastpos['Longitude']), str(lastpos['Latitude']), str(heading) + 'TH', alt, kmzoutput) except KeyError: pass try: cog = int(lastpos['CoG']) if cog != COGUNAVAILABLE and kmzoutput: cogs.add(cog) hdesc = 'CoG - {}'.format(cog) kmlmap.add_kml_placemark( hdesc, '', str(lastpos['Longitude']), str(lastpos['Latitude']), str(cog) + 'CoG', alt, kmzoutput) except KeyError: pass if linestring: posreps = stn.posrep kmlmap.add_kml_placemark_linestring( stn.mmsi, posreps) kmlmap.add_kml_placemark(displayname, desc, str(lastpos['Longitude']), str(lastpos['Latitude']), stn.stntype, alt, kmzoutput) kmlmap.close_folder() types.add(icons.ICONS[stn.stntype]) except NoSuitablePositionReport: continue kmlmap.close_folder() kmlmap.close_kml_file() kmlmap.write_kml_doc_file() if kmzoutput and not livemap: kml.make_kmz(outputfile, iconslist=types, greenarrows=headings, orangearrows=cogs)
def create_kml_map(self, outputfile, kmzoutput=True): """ create a KML map of this stations positions Args: outputfile(str): full path to output to kmzoutput(bool): whether to create a kmz with custom icons (True) or a basic kml file (False) """ if kmzoutput: docpath = os.path.join(os.path.dirname(outputfile), 'doc.kml') else: docpath = os.path.join(outputfile) kmlmap = kml.KMLOutputParser(docpath) kmlmap.create_kml_header(kmz=kmzoutput) stninfo = self.get_station_info(messagetally=False) if self.name != '': displayname = self.mmsi + ' - ' + self.name else: displayname = self.mmsi kmlmap.open_folder(displayname) posnumber = 1 for pos in self.posrep: timematch = TIMEREGEX.search(pos['Time']) if timematch: posfoldername = str(posnumber) + ' - ' + timematch.group() try: kmltimestamp = kml.convert_timestamp_to_kmltimestamp( pos['Time']) except kml.InvalidDateTimeString: kmltimestamp = '' else: posfoldername = str(posnumber) kmltimestamp = '' kmlmap.open_folder(posfoldername) stninfo['Last Known Position'] = pos desc = kmlmap.format_kml_placemark_description(stninfo) try: alt = str(pos['Altitude (m)']) except KeyError: alt = '0' try: heading = pos['True Heading'] if heading != HEADINGUNAVAILABLE and kmzoutput: hdesc = 'TRUE HEADING - {}'.format(heading) kmlmap.add_kml_placemark('TH', hdesc, str(pos['Longitude']), str(pos['Latitude']), str(heading) + 'TH', alt, kmzoutput, kmltimestamp) except KeyError: pass try: cog = int(pos['CoG']) if cog != COGUNAVAILABLE and kmzoutput: hdesc = 'COURSE OVER GROUND - {}'.format(cog) kmlmap.add_kml_placemark('CoG', hdesc, str(pos['Longitude']), str(pos['Latitude']), str(cog) + 'CoG', alt, kmzoutput, kmltimestamp) except KeyError: pass try: kmlmap.add_kml_placemark(displayname, desc, str(pos['Longitude']), str(pos['Latitude']), self.stntype, alt, kmzoutput, kmltimestamp) except KeyError: pass kmlmap.close_folder() posnumber += 1 kmlmap.add_kml_placemark_linestring(self.mmsi, self.posrep) kmlmap.close_folder() kmlmap.close_kml_file() kmlmap.write_kml_doc_file() if kmzoutput: kml.make_kmz(outputfile)
def create_kml_map(self, outputfile, kmzoutput=True, region='A'): """ create a KML map of this stations positions Args: outputfile(str): full path to output to kmzoutput(bool): whether to create a kmz with custom icons (True) or a basic kml file (False) region(str): IALA region, default is A """ staticstations = ('Base Station', 'Navigation Aid') greenarrows = set() orangearrows = set() if kmzoutput: docpath = os.path.join(os.path.dirname(outputfile), 'doc.kml') else: docpath = os.path.join(outputfile) kmlmap = kml.KMLOutputParser(docpath) kmlmap.create_kml_header( kmz=kmzoutput, iconsused=self.stntype, ialaregion=region) stninfo = self.get_station_info(messagetally=False) if self.name != '': displayname = self.mmsi + ' - ' + self.name else: displayname = self.mmsi kmlmap.open_folder(displayname) if self.stnclass in staticstations: lastpos = self.get_latest_position() stninfo = self.get_station_info(messagetally=True) desc = kmlmap.format_kml_placemark_description(stninfo) kmlmap.add_kml_placemark(displayname, desc, str(lastpos['Longitude']), str(lastpos['Latitude']), self.stntype, '0', kmzoutput) else: posnumber = 1 stninfo = self.get_station_info(messagetally=False) for pos in self.posrep: timematch = TIMEREGEX.search(pos['Time']) if timematch: posfoldername = str(posnumber) + ' - ' + timematch.group() try: kmltimestamp = kml.convert_timestamp_to_kmltimestamp( pos['Time']) except kml.InvalidDateTimeString: kmltimestamp = '' else: posfoldername = str(posnumber) kmltimestamp = '' kmlmap.open_folder(posfoldername) stninfo['Last Known Position'] = pos desc = kmlmap.format_kml_placemark_description(stninfo) try: alt = str(pos['Altitude (m)']) except KeyError: alt = '0' try: heading = pos['True Heading'] if heading != HEADINGUNAVAILABLE and kmzoutput: greenarrows.add(heading) hdesc = 'HEADING - {}'.format(heading) kmlmap.add_kml_placemark(hdesc, '', str(pos['Longitude']), str(pos['Latitude']), str(heading) + 'TH', alt, kmzoutput, kmltimestamp) except KeyError: pass try: cog = int(pos['CoG']) if cog != COGUNAVAILABLE and kmzoutput: orangearrows.add(cog) hdesc = 'CoG - {}'.format(cog) kmlmap.add_kml_placemark(hdesc, '', str(pos['Longitude']), str(pos['Latitude']), str(cog) + 'CoG', alt, kmzoutput, kmltimestamp) except KeyError: pass try: kmlmap.add_kml_placemark(displayname, desc, str(pos['Longitude']), str(pos['Latitude']), self.stntype, alt, kmzoutput, kmltimestamp) except KeyError: pass kmlmap.close_folder() posnumber += 1 kmlmap.add_kml_placemark_linestring(self.mmsi, self.posrep) kmlmap.close_folder() kmlmap.close_kml_file() kmlmap.write_kml_doc_file() if kmzoutput: stntypes = [icons.ICONS[self.stntype]] kml.make_kmz(outputfile, stntypes, greenarrows, orangearrows)
def setUp(self): self.maxDiff = None self.parser = kml.KMLOutputParser(None)