def test_unsuitable_timestamp_regex_fail_seconds(self): """ test a timestamp with an invalid seconds field Note: change this when i come up with a better regex for datetimes """ testinput = '2020/11/30 17:02:78' with self.assertRaises(kml.InvalidDateTimeString): kml.convert_timestamp_to_kmltimestamp(testinput)
def test_unsuitable_timestamp_regex_fail_hour(self): """ test a timestamp with an invalid hour Note: change this when i come up with a better regex for datetimes """ testinput = '2020/11/30 26:34:09' with self.assertRaises(kml.InvalidDateTimeString): kml.convert_timestamp_to_kmltimestamp(testinput)
def test_unsuitable_timestamp_regex_fail(self): """ test a timestamp that doesn't match the regex Note: change this when i come up with a better regex for datetimes """ testinput = '2020-06-02 19:03:17' with self.assertRaises(kml.InvalidDateTimeString): kml.convert_timestamp_to_kmltimestamp(testinput)
def test_suitable_estimated_timestamp(self): """ test a valid timestamp in the correct format """ testinput = '2020/07/03 00:34:17 (estimated)' expected = '2020-07-03T00:34:17Z' teststring = kml.convert_timestamp_to_kmltimestamp(testinput) self.assertEqual(expected, teststring)
def test_suitable_timestamp(self): """ test a valid timestamp in the correct format """ testinput = '2020/06/02 19:03:17' expected = '2020-06-02T19:03:17Z' teststring = kml.convert_timestamp_to_kmltimestamp(testinput) self.assertEqual(expected, teststring)
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 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)