def genKML(self): """ Generate KML file from all the collected data (or input from CSV) """ if self.dataList == []: print("No data added") return False # Setup kml kml = simplekml.Kml(name="ENGG4810", open=1) now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") doc = kml.newdocument(name='ENGG4810', snippet=simplekml.Snippet("Created on : " + now)) doc.lookat.latitude = self.dataList[0][3] doc.lookat.longitude = self.dataList[0][4] doc.lookat.range = 1300.000000 # Create folders pathFolder = doc.newfolder(name='Paths') pointFolder = doc.newfolder(name='Points') # Create linestrings pathLine = pathFolder.newlinestring(name="Path") pathLine.altitudemode = "absolute" humidLine = pathFolder.newlinestring(name="Humidity") humidLine.altitudemode = "absolute" humidLine.style.linestyle.color = simplekml.Color.blue humidLine.extrude = 1 tempLine = pathFolder.newlinestring(name="Temperature") tempLine.altitudemode = "absolute" tempLine.style.linestyle.color = simplekml.Color.red tempLine.extrude = 1 uvLine = pathFolder.newlinestring(name="Outside/Inside") uvLine.altitudemode = "absolute" uvLine.style.linestyle.color = simplekml.Color.yellow uvLine.extrude = 1 skippedLine = pathFolder.newlinestring(name="Skipped measurements") skippedLine.altitudemode = "absolute" skippedLine.style.linestyle.color = simplekml.Color.purple skippedLine.extrude = 1 voltageLine = pathFolder.newlinestring(name="Voltage") voltageLine.altitudemode = "absolute" voltageLine.style.linestyle.color = simplekml.Color.orange voltageLine.extrude = 1 # Add all SSID to dictionary and calculate skipped meassurements at 1 # location SSID_dict = {} for data in self.dataList: if data[0] == "SKIP": index = self.dataList.index(data) found = False while index > 0: index -= 1 if self.dataList[index][0] == "OK": found = True break if found == True: self.dataList[index][-1] += 1 else: print("No packets before skipped packet") else: SSID_dict[data[11][0]] = [] # Add data to kml for data in self.dataList: if data[0] == "SKIP": continue status, packet_number, state, latitude, longtitude, time, temperature, humidity, outside, tone, voltage, network, skip = data # Create points / placemarks point = pointFolder.newpoint() point.coords = [(longtitude, latitude)] # Create string for description tone_string = "" if tone == 0: tone_string = "No tone detected" elif tone == 1: tone_string = "1kHz detected" elif tone == 2: tone_string = "4kHz detected" elif tone == 3: tone_string = "1kHz and 4kHz detected" string = "State: " + str(state) + ";\n Latitude: " + str(latitude) + ";\n Longtitude: " \ + str(longtitude) + ";\n Temp: " + str(temperature) + ";\n Humidity: " + str(humidity) \ + ";\n Tone: " + tone_string + ";\n Outside: " if outside == 1: string = string + "Yes;\n" else: string = string + "No;\n" string = string + "Voltage: " + str(voltage) + "V;\n" string = string + "No. of skipped measurements: " + \ str(skip) + ";\nReceived By: " + \ time.strftime("%Y-%m-%d %H:%M:%S") string = string + ";\n Received from SSID: " + \ str(network[0]) + " RSSI: " + str(network[2]) point.description = string # Add coords to paths try: pathLine.coords.addcoordinates([(longtitude, latitude)]) except Exception as e: print(e) try: tempLine.coords.addcoordinates([(longtitude, latitude, abs(temperature))]) except Exception as e: print(e) try: humidLine.coords.addcoordinates([(longtitude, latitude, humidity)]) except Exception as e: print(e) try: voltageLine.coords.addcoordinates([(longtitude, latitude, abs(voltage))]) except Exception as e: print(e) try: uvLine.coords.addcoordinates([(longtitude, latitude, 25 if outside == 1 else 0)]) except Exception as e: print(e) try: skippedLine.coords.addcoordinates([(longtitude, latitude, skip * 10)]) except Exception as e: print(e) # Add data to SSID dictionary SSID_dict[network[0]].append([latitude, longtitude, network]) # Create paths for each unique SSID for SSID, dataList in SSID_dict.items(): RSSILine = pathFolder.newlinestring(name="SSID: " + str(SSID)) RSSILine.altitudemode = "absolute" RSSILine.extrude = 1 altitude = 0 for data in dataList: latitude, longtitude, network = data RSSILine.coords.addcoordinates([(longtitude, latitude, abs(network[2]))]) # Save KML and generate human-readable CSV kml.save("Path.kml") print("Success generate KML file") self.genCSV()
def create_recent_kml(self): kml_file = "GPS Most Recent.kml" list_of_gps_obs = [] with open(self.root_path + self.date_path + "\GPS\GPS.csv", 'r') as f: for line in f: splitline = line.split(',') lat = splitline[2].split(' ') latdec = str(lat[0]) + str((float(lat[1]) / 60) + (float(lat[2]) / (60 * 60)))[1:] lon = splitline[3].split(' ') londec = str(lon[0]) + str((float(lon[1]) / 60) + (float(lon[2]) / (60 * 60)))[1:] list_of_gps_obs.append( GPS_Point(int(splitline[1]), splitline[0], float(latdec), float(londec))) # filter list to most recent 2 hours filtered_list_gps_obs = [] for i in list_of_gps_obs: now_stamp = int(arrow.now().timestamp) if (now_stamp - 2 * 60 * 60) < i.timestamp < now_stamp: filtered_list_gps_obs.append(i) # Sort list filtered_list_gps_obs.sort(key=lambda x: x.timestamp) # create a kml when = [] coordinates = [] for i in filtered_list_gps_obs: when.append(i.time_formatted) coordinates.append([i.lon, i.lat]) kml = simplekml.Kml(name="Live Chase GPS", open=1) doc = kml.newdocument( name='Live Chase GPS', snippet=simplekml.Snippet( 'Created ' + arrow.now().format('YYYY-MM-DD HH:mm:ss'))) fol = doc.newfolder(name='Live GPS Track') schema = kml.newschema() trk = fol.newgxtrack(name='Track from past two hours') trk.extendeddata.schemadata.schemaurl = schema.id trk.newwhen(when) trk.newgxcoord(coordinates) trk.stylemap.normalstyle.iconstyle.icon.href =\ 'http://earth.google.com/images/kml-icons/track-directional/track-0.png' trk.stylemap.normalstyle.linestyle.color = '99ffac59' trk.stylemap.normalstyle.linestyle.width = 6 kml.save(self.root_path + self.date_path + "\GPS\\" + kml_file) print "KML Updated"
def get_kml_track(miss,outdir): mn = os.path.basename(miss)[:-3] print(time.ctime() + ': Processing ' + mn) print(time.ctime() + ': Getting environment' ) env = xr.open_dataset(miss, group='Environment').to_dataframe() env = env.reset_index() tmelt = pd.melt(env, id_vars =['Depth','Dive'], value_vars =['time']) tmelt['temperature'] = pd.melt(env, id_vars =['Depth','Dive'], value_vars =['temperature'])['value'] tmelt['salinity'] = pd.melt(env, id_vars =['Depth','Dive'], value_vars =['salinity'])['value'] tmelt['fluorescence'] = pd.melt(env, id_vars =['Depth','Dive'], value_vars =['fluorescence'])['value'] tmelt = tmelt[tmelt.value > 0] print(time.ctime() + ': Getting GPS ') gps = get_gps(miss) gmelt = pd.melt(gps.reset_index(),id_vars = ['Dive'], value_vars = ['Time_start','Time_end']) gmelt['Lon'] = pd.melt(gps.reset_index(),id_vars = ['Dive'], value_vars = ['Lon_start','Lon_end'])['value'] gmelt['Lat'] = pd.melt(gps.reset_index(),id_vars = ['Dive'], value_vars = ['Lat_start','Lat_end'])['value'] gmelt = gmelt.sort_values('value') gmelt = gmelt.rename(columns={"value": "Time"}) print(time.ctime() + ': Converting for kml') tmelt['Lon'] = np.interp(tmelt.value.astype('int64'),gmelt['Time'].astype('int64'), gmelt.Lon) tmelt['Lat'] = np.interp(tmelt.value.astype('int64'),gmelt['Time'].astype('int64'), gmelt.Lat) tmelt['Pos'] = (tmelt.Dive.astype(str)) gm = tmelt.sort_values(by='Dive') g = gm.iloc[np.arange(0,gm.shape[0],2)] when = pd.to_datetime(g.value).values.astype(str).tolist() coord = g.apply(lambda X: (X["Lon"],X["Lat"],-X["Depth"]) ,axis=1).values.tolist() temperature = g.temperature.values.tolist() salinity = g.salinity.values.tolist() fluo = g.fluorescence.values.tolist() dive = g.Dive.values.tolist() depth = g.Depth.values.tolist() # Create the KML document kml = simplekml.Kml(name="Zooglider - " + mn, open=1) doc = kml.newdocument(name='Zooglider Mission', snippet= simplekml.Snippet(mn)) doc.lookat.gxtimespan.begin = str(pd.to_datetime(g.value.min())) doc.lookat.gxtimespan.end = str(pd.to_datetime(g.value.max())) doc.lookat.longitude = g.Lon.mean() doc.lookat.latitude = g.Lat.mean() #doc.lookat.range = 2300.000000 # Create a folder fol = doc.newfolder(name='Dives') # Create a schema for extended data: heart rate, cadence and power schema = kml.newschema() schema.newgxsimplearrayfield(name='dive', type=simplekml.Types.int, displayname='Dive #') schema.newgxsimplearrayfield(name='depth', type=simplekml.Types.int, displayname='Depth') schema.newgxsimplearrayfield(name='temp', type=simplekml.Types.int, displayname='Temperature') schema.newgxsimplearrayfield(name='sal', type=simplekml.Types.int, displayname='Salinity') schema.newgxsimplearrayfield(name='fluo', type=simplekml.Types.float, displayname='Fluorescence') # Create a new track in the folder trk = fol.newgxtrack(name=os.path.basename(miss)[:-3]) # Apply the above schema to this track trk.extendeddata.schemadata.schemaurl = schema.id # Add all the information to the track trk.newwhen(when) # Each item in the give nlist will become a new <when> tag trk.newgxcoord(coord) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('dive', dive) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('depth', depth) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('temp', temperature) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('sal', salinity) # Ditto trk.extendeddata.schemadata.newgxsimplearraydata('fluo', fluo) # Ditto # Styling trk.stylemap.normalstyle.iconstyle.icon.href = 'zg.ico'#'http://earth.google.com/images/kml-icons/track-directional/track-0.png' #trk.stylemap.normalstyle.linestyle.color = 'ff6600' trk.stylemap.normalstyle.linestyle.width = 6 trk.stylemap.highlightstyle.iconstyle.icon.href = 'zg.ico'#'http://earth.google.com/images/kml-icons/track-directional/track-0.png' #trk.stylemap.highlightstyle.iconstyle.scale = 1.2 #trk.stylemap.highlightstyle.linestyle.color = 'ff6600' trk.stylemap.highlightstyle.linestyle.width = 8 trk.altitudemode = simplekml.AltitudeMode.relativetoground # Save the kml to file print(time.ctime() + ': Saving - ' + outdir + mn + ".kml") kml.save(outdir + mn + ".kml") print(time.ctime() + ': Done...')