def createKmlV1(self, kmlFileName="output"): """ Creates a detailed KML output (one placemark per node) placed in 3 folders ("created","modified","deleted") Suitable only for reasonably small osc files (not days) Args: -output name """ print "Creating KML file..." myKml = KML.KML(kmlFileName) statsDescription = self.globalStats() myKml.placemarkDescriptive(description=statsDescription, name=myKml.kmlTitle) for aType in ["create", "modify", "delete"]: myKml.folderHead(aType) for node in self.osmNodes: if node["type"] == aType: if self.verbose == True: print node["latitude"], node["longitude"],node["idNode"],\ node["user"], node["timestamp"],node["type"] if node["user"] == None: node["user"] = "******" myKml.placemark(node["latitude"], node["longitude"], node["idNode"], node["user"], node["timestamp"], node["type"]) myKml.folderTail() myKml.close()
def createKmlV0(self, kmlFileName="output"): """ The lightest kml version possible with only one "summarized" placemark per user representing the last known position. Args: kmlFileName: the name of the resulting kml (the osc filename per default) Output: Creates a kml file """ print "Creating KML file version 0 ..." myKml = KML.KML(kmlFileName) statsDescription = self.globalStats() myKml.placemarkDescriptive(description=statsDescription, name=myKml.kmlTitle) for userName, userStat in sorted(self.statsUsers.iteritems()): myKml.folderHead("<![CDATA["+unicode(userName)\ +"("+str(self.statsUsers[userName][0])+")]]>") thisLat = 0 thisLong = 0 for pathType in [0, 1, 2]: if len(self.statsUsers[userName][4][pathType]) != 0: thisLat = self.statsUsers[userName][4][pathType][-1][0] thisLong = self.statsUsers[userName][4][pathType][-1][1] if pathType == 0: lineStyle = "lineStyleCreated" type = "create" if pathType == 1: lineStyle = "lineStyleModified" type = "modify" if pathType == 2: lineStyle = "lineStyleDeleted" type = "delete" userNodesStat = [ self.statsUsers[userName][0], self.statsUsers[userName][1], self.statsUsers[userName][2], self.statsUsers[userName][3] ] myKml.placemarkSummary(thisLat, thisLong, userName, type, userNodesStat) myKml.folderTail() myKml.close()
def outputStops(stopDF, fp): KML.writeKMLTurnPairHeader(fp) for index, row in stopDF.iterrows(): writeKMLPlaceStop(objectify(row), fp) KML.writeKMLTurnPairTrailer(fp)
"\t" + str(snr2).replace(".", ",") + "\t" + str(snr_lin2).replace(".", ",") + \ "\t" + str(Xd1).replace(".", ",") + "\t" + str(Yd1).replace(".", ",") + "\t" + str(Zd1).replace(".",",") + \ "\t" + str(Bd).replace(".", ",") + "\t" + str(Ld).replace(".", ",") + "\t" + str(Hd).replace(".",",") + \ "\t" + str(azimut2).replace(".",",") + "\t" + str(psi1).replace(".",",") + "\n" xls_obs.write(zapis) zoznam_xyz = [cas, Xd1, Yd1, Zd1] zoznam_blh = [cas, Bd, Ld, Hd, cd_obs, elev_uhol_rad, azimut2] zoznam_suradnic_xyz.append(zoznam_xyz) zoznam_suradnic_blh.append(zoznam_blh) break xls_obs.close() kruh = kruh.fkruznica(B, L, nazov) #StanicaDruzica = SHP.fLineShp(zoznam_suradnic_blh, nazov) orezanie = priesecnik.fIntersect(kruh, zoznam_suradnic_blh, nazov) kml_point = KML.fCreatePointKML(kruh, nazov) interval_a = [[80, 100], [160,215]] az_zoznam = [] for i in range(1,len(orezanie)): az = orezanie[i][4] if (az > interval_a[0][0] and az < interval_a[0][1]) or (az > interval_a[1][0] and az < interval_a[1][1]): elev_uhol_rad = orezanie[i][5] az = math.radians(orezanie[i][6]) # vypocet podla very snow depth..... #q = (lambda2*h)/math.sin(elev_uhol_rad) #w = math.pow(lambda2/(2*math.sin(elev_uhol_rad)), 2) #b = math.sqrt(q+w) # vedlajsia os elipsy #a = b/math.sin(elev_uhol_rad) # hlavna os elipsy n=1 d =n*lambda1/2
def Plot(overpass, filename, min_xco2=KML.default_cmin, max_xco2=KML.default_cmax, secondary_sources=[], arrow_scale=0.02, stability="new", wind_source="Average", wind_adjustment=0., f_plume=0.10, f_background=0.01, offset=3.e3, y_max_positive=50.e3, y_max_negative=50.e3, y_min_positive=0., y_min_negative=0., direction='y', x_max=75.e3, snr_strong_co2_min=None, chi_squared_max=None, albedo_min=None, albedo_max=None, outcome_flags={1,2}, surface_pressure_min=None, surface_pressure_max=None, lon_thresh=0.5, lat_thresh=0.5, bias_correction='corrected', npoints=500, width=0.5, height=None, labelsize=Colours.default_fontsize, x=0.05, wind_arrow_sources=['MERRA', 'ECMWF', 'GEM', 'Average']): """Creates a KML file for an overpass with parameters as specified. If xco2 limits are default values, no new colour bar is made. Otherwise, will make a new colour bar and save it alongside the kml on the server. Parameters for defining the plume and background are included because the extended data for each polygon contains a flag for whether each point is in the plume, in the background, or in neither. Polygons are plotted for 'npoints' points on either side of the box defined by the lat and lon thresholds. """ print "Making KML for",overpass.info if overpass.FullFile=="": raise ValueError("Corrupt overpass file") # Bundle together arguments that need to be passed to plume model functions # For determining background: bg_kwargs = {'background_factor':f_background, 'ymax_positive':y_max_positive, 'ymax_negative':y_max_negative, 'ymin_negative':y_min_negative, 'ymin_positive':y_min_positive, 'offset':offset, 'sign':direction } # For determining in plume points: plume_kwargs = {'plume_factor':f_plume, 'xmax':x_max } # Args to pass to full_file.quality(i,**kwargs) method quality_args = {'chi_squared_max':chi_squared_max, 'snr_strong_co2_min':snr_strong_co2_min, 'albedo_min':albedo_min, 'albedo_max':albedo_max, 'outcome_flags':outcome_flags, 'surface_pressure_min':surface_pressure_min, 'surface_pressure_max':surface_pressure_max } # parse bias_correction argument if bias_correction not in File.allowed_bias_correction: type_err = "'bias_correction' argument must be one of %s" raise TypeError(type_err % ', '.join(File.allowed_bias_correction)) # The co2 variable to read; ex, 'corrected_xco2', 'S31_xco2' co2 = bias_correction + "_xco2" try: wind = getattr(overpass,wind_source) except AttributeError: raise AttributeError("'wind_source' must be one of %s" % ', '.join(valid_winds)) # make arrow_lat, arrow_lon the average lat, lon weighted by emissions if secondary_sources: secondary_lats = [second.lat for second in secondary_sources] secondary_lons = [second.lon for second in secondary_sources] all_lats = [overpass.lat] + secondary_lats all_lons = [overpass.lon] + secondary_lons all_emissions = [overpass.get_emissions()(Units._model_units)] + \ [second.get_emissions(overpass)(Units._model_units) for second in secondary_sources] arrow_lat = numpy.average(all_lats, weights=all_emissions) arrow_lon = numpy.average(all_lons, weights=all_emissions) print "Using coordinate ({0}, {1}) for wind arrows".format(arrow_lat, arrow_lon) else: arrow_lat, arrow_lon = overpass.lat, overpass.lon # Use KML module to make the KML kml_description = overpass.strftime(KML.modis_description_fmt) Map = KML.KML(description=kml_description) oco2_name = "OCO-2 Data" oco2_description = "Data from OCO-2 v7 Full Files with '%s' "\ "bias correction" % bias_correction OCO2_folder = KML.Folder(oco2_name, oco2_description) Map.add_folder(OCO2_folder) warn_level_folders = {} # dictionary to be able to update the folders for wl in range(21): folder_name = "Warn Level %d" % wl folder_description = "Lite File Warn Level %d" % wl folder = KML.Folder(folder_name, folder_description) OCO2_folder.add_object(folder) warn_level_folders[wl]=folder wind_arrows = KML.Overlay.wind_arrow(overpass,lat=arrow_lat, lon=arrow_lon,size=arrow_scale, sources=wind_arrow_sources) height = height if height else (1.1/5.)*width cbar = Colours.colour_bar('', cmin=min_xco2, cmax=max_xco2, fontsize=labelsize) colour_scale = KML.Overlay.colour_scale(cbar, width=width, height=height, x=x) Map.add_object(wind_arrows) Map.add_object(colour_scale) print('Opening Full File; Performing Bias Correction') full_file = File.full(overpass) lite_file = File.lite(overpass) # warn_dict maps a sounding id to a warn level warn_dict = {lite_file.sounding_id[n]:lite_file.warn_level[n] for n in range(len(lite_file))} lon = overpass.lon lat = overpass.lat close_indices = [] for i in range(len(full_file)): lon_i = full_file.retrieval_longitude[i] lat_i = full_file.retrieval_latitude[i] dlon = abs(lon_i - overpass.lon)%360 dlat = abs(lat_i - overpass.lat)%360 if dlon<=lon_thresh and dlat<=lat_thresh: close_indices.append(i) i+=1 # (k_min, k_max) are limits on what points to plot k_min = max(0,close_indices[0]-npoints) k_max = min(len(full_file),close_indices[-1]+npoints) # plume-model parameters u = wind.speed F = 1.0 # actual emissions are not important for background a = overpass.a if stability=="new" else overpass.a_old x_offset, y_offset = full_file.get_offset(overpass, wind) secondary_offsets = full_file.get_secondary_offset(overpass, wind, secondary_sources=secondary_sources) print('Adding polygons to KML File') count_plume_points = 0 count_bg_points = 0 for k in range(k_min,k_max): lats = full_file.retrieval_vertex_latitude[k,0,:] lons = full_file.retrieval_vertex_longitude[k,0,:] sounding_xco2 = full_file.corrected_xco2[k] sounding_id = full_file.id[k] sounding_lat = full_file.retrieval_latitude[k] sounding_lon = full_file.retrieval_longitude[k] sounding_colour = Colours.Colour(sounding_xco2, vmin=min_xco2, vmax=max_xco2) if sounding_id in warn_dict: warn_level = warn_dict[sounding_id] else: warn_level=20 point = Geometry.CoordGeom(wind) sounding_x, sounding_y = point.coord_to_wind_basis(lat, lon, sounding_lat, sounding_lon) data = KML.Data() data.get_data(k, full_file) wl_data = data.add_new_data('Warn Level',warn_level) posn_data = data.add_new_data('Wind Basis Coord',(sounding_x,sounding_y)) data.add_new_data('Full File', os.path.split(overpass.FullFile)[1]) data.add_new_data('Lite File', os.path.split(overpass.LiteFile)[1]) data.add_new_data('Observation Mode', overpass.observation_mode) data.add_new_data('Latitude', sounding_lat) data.add_new_data('Longitude', sounding_lon) dist = ((sounding_x-x_offset)*(sounding_x-x_offset) + (sounding_y-y_offset)*(sounding_y-y_offset))**0.5 in_plume = PlumeModel.InPlume(sounding_x, sounding_y, u, F, a, **plume_kwargs) in_background = PlumeModel.InBackground(sounding_x, sounding_y, dist, u, F, a, **bg_kwargs) for (ind, second) in enumerate(secondary_sources): x0,y0 = secondary_offsets[ind] xs,ys = point.coord_to_wind_basis(second.lat, second.lon, sounding_lat, sounding_lon) second_dist = point.cartesian_distance((xs,ys),(x0,y0)) in_secondary_plume = PlumeModel.InPlume(xs, ys, u, 1.0, a, **plume_kwargs) in_secondary_bg = PlumeModel.InBackground(xs, ys, second_dist, u, 1.0, a, **bg_kwargs) in_plume = in_plume or in_secondary_plume in_background = in_background and in_secondary_bg if in_plume: model_status = KML.DataPoint('Notes','In Plume Point') if full_file.quality(k, **quality_args): count_plume_points+=1 elif in_background: model_status = KML.DataPoint('Notes','Background Point') if full_file.quality(k, **quality_args): count_bg_points+=1 else: model_status = KML.DataPoint('Notes','None') data.add_data_field(model_status) quality = full_file.quality(k, **quality_args) data.add_new_data("Quality Check", str(quality)) poly_col = sounding_colour.cformat(Colours.Colour.GE) sounding_polygon = KML.Polygon(lats, lons, sounding_id, poly_col) sounding_polygon.add_data(data) warn_level_folders[warn_level].add_object(sounding_polygon) Map.write(filename) print "KML saved as", filename print "Done Overpass"
def createKmlV2(self, kmlFileName="output", heightFactor=0, threshold=0.005): """ A version based on lines and polygons instead of placemarks Args: kmlFileName: the name of the resulting kml (the osc filename per default) threshold: lat or long detla to link together the nodes (they aer not ways) to better visualize that this nodes belong to the same user. Output: Creates a kml file """ print "Creating KML file..." myKml = KML.KML(kmlFileName) statsDescription = self.globalStats() myKml.placemarkDescriptive(description=statsDescription, name=myKml.kmlTitle) for userName, userStat in sorted(self.statsUsers.iteritems()): myKml.folderHead("<![CDATA["+unicode(userName)\ +"("+str(self.statsUsers[userName][0])+")]]>") for pathType in [0, 1, 2]: ## Extract created nodes-"path" for this user # cut subpaths if next node is above the threshold lonThreshold = threshold latThreshold = threshold # paths = [] # list of cut paths firstNode = True thisPath = "" thisNode = "" for coordinate in self.statsUsers[userName][4][pathType]: thisLat = coordinate[0] thisLong = coordinate[1] thisNode=thisLong+","+thisLat+","\ +str(heightFactor)+" " if firstNode == True: thisPath += thisNode prevLat = thisLat prevLong = thisLong firstNode = False else: #distanceThreshold=sqrt((thisLat-prevLat)**2 + (thisLong-prevLong)**2) dLon = abs(float(thisLong) - float(prevLong)) dLat = abs(float(thisLat) - float(prevLat)) if (dLon > lonThreshold) and (dLat > latThreshold): #print dLat, paths.append(thisPath) thisPath = "" elif (dLon < lonThreshold) and (dLat < latThreshold): thisPath += thisNode prevLat = thisLat prevLong = thisLong paths.append(thisPath + thisNode) #print paths #if len(self.statsUsers[userName][4][0])!=0: pathCreated=pathCreated+thisNode if pathType == 0: lineStyle = "lineStyleCreated" genre = "Created" if pathType == 1: lineStyle = "lineStyleModified" genre = "Modified" if pathType == 2: lineStyle = "lineStyleDeleted" genre = "Deleted" trackCut = 1 for path in paths: if path != "": myKml.placemarkPath(pathName=genre + "P" + str(trackCut), coordinates=path, style=lineStyle) trackCut += 1 #if userName ==None: print "Anonymous users detected" myKml.folderTail() myKml.close()
def plot(file, output_file, time, lat_range): """Makes KML files for mapping OCO-2 L1 data. time must be formatted as YYYY-mm-dd """ print "Opening file", file L1_data = h5py.File(file, 'r') datasets = {} for (name, path) in file_paths.iteritems(): datasets[name] = L1_data[path] dim = len(datasets['vertex_latitude']) kml_description = KMLFormats.oco2_header_description.format(time) print "Making KML" kml = KML.KML(description=kml_description) root_folder = KML.Folder('Footprint Outlines') kml.add_folder(root_folder) for i in range(dim): for j in range(8): sounding_lat = datasets['sounding_latitude'][i, j] if lat_range[0] <= sounding_lat <= lat_range[1]: lats = datasets['vertex_latitude'][i, j][0] lons = datasets['vertex_longitude'][i, j][0] sounding_lon = datasets['sounding_longitude'][i, j] solar_zenith = datasets['sounding_solar_zenith'][i, j] solar_azimuth = datasets['sounding_solar_azimuth'][i, j] sounding_zenith = datasets['sounding_zenith'][i, j] sounding_azimuth = datasets['sounding_azimuth'][i, j] op_mode = datasets['operation_mode'][0] poly = KML.Polygon(lats, lons, i + j, poly_fill_colour, outline=True) poly.add_data(KML.DataPoint('Sounding Latitude', sounding_lat)) poly.add_data(KML.DataPoint('Sounding Longitude', sounding_lon)) poly.add_data(KML.DataPoint('Solar Zenith Angle', solar_zenith)) poly.add_data( KML.DataPoint('Solar Azimuth Angle', solar_azimuth)) poly.add_data( KML.DataPoint('Sounding Zenith Angle', sounding_zenith)) poly.add_data( KML.DataPoint('Sounding Azimuth Angle', sounding_azimuth)) poly.add_data(KML.DataPoint('File', os.path.split(file)[1])) poly.add_data(KML.DataPoint('Operation Mode', op_mode)) root_folder.add_object(poly) kml.write(output_file) L1_data.close() print "Saved file as", output_file
locationUpdate = LocationData.translateGPRMC(line) if locationUpdate: if len(locationUpdates) > 0: lastLocation = locationUpdates[len(locationUpdates) - 1] if lastLocation.lon == locationUpdate.lon and lastLocation.lat == locationUpdate.lat: continue else: locationUpdates.append(locationUpdate) else: locationUpdates.append(locationUpdate) else: continue return locationUpdates locationUpdates = processFile(inputFile) data = pd.DataFrame.from_records([loc.toDict() for loc in locationUpdates]) data.to_csv(dataOutputFile, index_label='index') turnPairs = DetectLeftTurns.findLeftTurns(locationUpdates) print("LEFT TURNS:") for turnPair in turnPairs: print( f"-----\n{turnPair[0].lat}, {turnPair[0].lon}\n{turnPair[1].lat}, {turnPair[1].lon}\n)" ) # Write out path of car KML.writeKMLPathFile(locationUpdates, outputPathFile) # write out points for left turns KML.writeKMLTurnPointsFile(turnPairs, outputPointsFile)
import KML map = KML.KML() # style = KML.LineStyle(id = 'MyStyleId') # ls = KML.LineString('-122.364383,37.824664,0 -122.364152,37.824322,0', style=style) # map.add_object(ls) # map.write('test.kml') lats = [30, 31, 31, 30, 30] lons = [-110, -110, -111, -111, -110] alts = [100, 110, 200, 310, 100] vs = zip(lons, lats) vouter = [(-77.05788457660967, 38.87253259892824, 100), (-77.05465973756702, 38.87291016281703, 100), (-77.05315536854791, 38.87053267794386, 100), (-77.05552622493516, 38.868757801256, 100), (-77.05844056290393, 38.86996206506943, 100), (-77.05788457660967, 38.87253259892824, 100)][::-1] vinner = [(-77.05668055019126, 38.87154239798456, 100), (-77.05542625960818, 38.87167890344077, 100), (-77.05485125901024, 38.87076535397792, 100), (-77.05577677433152, 38.87008686581446, 100), (-77.05691162017543, 38.87054446963351, 100), (-77.05668055019126, 38.87154239798456, 100)][::-1] couter = KML.Coord(vouter)
while (i < max): currentDate = str(year) + "-" + str(month) + "-" + str(day) day = day - 1 # Out of calendar range, so the last day of the previous month must be found. if (day < 1): month = month - 1 # Month can just be set to December. We already know the calendar month range. # Year is also just decremented by 1. if (month < 1): month = 12 year = year - 1 # monthrange returns a tuple, so we want the upper bound returned. day = monthrange(year, month)[1] previousDate = str(year) + "-" + str(month) + "-" + str(day) weather = weatherbit.get_previous_forecast(lat, long, previousDate, currentDate) #print weather total_precip = total_precip + weather[0] total_avg_temp = total_avg_temp + weather[1] i = i + 1 # Find the average for precipitation and temperature for all days in the data. total_precip = total_precip / max total_avg_temp = total_avg_temp / max # Create the City object using the gathered data and store it in a list. City = city.City(geocoding[0], geocoding[1], geocoding[2], total_precip, total_avg_temp) city_objects_list.append(City) kml.generate_kml_file(city_objects_list)