Plotting TROCAS 6 underway data from 1-min-binned merged file created by Catherine Kuhn (TROCAS6_GPS_SONDE_CO2.csv).
Demos with Holoviz/GeoViews and Folium packages. Run with customized holoviz conda environment.
7/25/2019. Emilio
In [1]:
import os
import pandas as pd
import geopandas as gpd
import shapely.geometry as shpgeom
from cartopy import crs
import folium
import branca
import geoviews as gv
import geoviews.feature as gf
gv.extension('bokeh')
In [2]:
%matplotlib inline
In [3]:
pd.__version__, gpd.__version__, folium.__version__, gv.__version__
Out[3]:
In [4]:
data_dpth = "/home/mayorga/Desktop/Richey-Amazon-Proposals/data/GD_TROCASData/fromKuhn/Data/T6"
In [5]:
t6_merged_fname = "TROCAS6_GPS_SONDE_CO2.csv"
In [6]:
t6_df = pd.read_csv(os.path.join(data_dpth, t6_merged_fname),
parse_dates=['times'], infer_datetime_format=True)
In [7]:
t6_df.shape
Out[7]:
In [8]:
t6_gdf = gpd.GeoDataFrame(t6_df,
geometry=t6_df[['longitude', 'latitude']].apply(shpgeom.Point, axis=1),
crs={'init': 'epsg:4326'})
In [9]:
t6_gdf.head()
Out[9]:
In [10]:
t6_gdf.dtypes
Out[10]:
Remove some redundant or unused columns, to generate a slimmer dataframe
In [11]:
t6_gdf.drop(columns=['CO2 mol/L', 'ODO mol/L', 'ODO ug/L', 'Site Name'], inplace=True)
In [12]:
t6_gdf['CO2 ppm'].describe()
Out[12]:
In [13]:
t6_gdf.plot(column='CO2 ppm',
vmin=t6_gdf['CO2 ppm'].min(), vmax=t6_gdf['CO2 ppm'].max(),
legend=True, figsize=(12, 6));
In [14]:
# throw out rows with invalid point geometries
validgeo_gdf = t6_gdf[t6_gdf.is_valid]
In [15]:
parameter = 'CO2 ppm'
filtered_gdf = validgeo_gdf[~validgeo_gdf[parameter].isna()]
In [16]:
uw_points = gv.Points(filtered_gdf, vdims=['CO2 ppm', 'times']) # , label='CO2 (ppm)'
In [17]:
esri_wtm_tiles = ('http://services.arcgisonline.com/arcgis/rest/services/'
'World_Topo_Map/MapServer/MapServer/tile/{Z}/{Y}/{X}')
In [18]:
geo_bg = gv.tile_sources.WMTS(esri_wtm_tiles).options(alpha=1)
In [19]:
title = "Underway Surface CO2, {} to {}".format(
filtered_gdf.times.min().date(),
filtered_gdf.times.max().date())
customhover = 'hover'
geo_bg * uw_points.opts(title=title,
width=900, height=600,
color='CO2 ppm', colorbar=True, cmap='viridis', size=4,
toolbar='above', tools=[customhover]
)
Out[19]:
In [20]:
def underway_folium_map(source_gdf, parameter, units, tooltip_prec=1, cmap_name='YlOrRd_03'):
# m = folium.Map(tiles="cartodbpositron")
esri_wtm_tiles = ('http://services.arcgisonline.com/arcgis/rest/services/'
'World_Topo_Map/MapServer/MapServer/tile/{z}/{y}/{x}')
filtered_gdf = source_gdf[~source_gdf[parameter].isna()]
m = folium.Map(tiles=esri_wtm_tiles, attr='ESRI')
cmap = branca.colormap.linear._colormaps[cmap_name].scale(
filtered_gdf[parameter].min(),
filtered_gdf[parameter].max())
cmap.caption = parameter
cmap.add_to(m)
# popup, tooltip
# fillOpacity, fillColor, weight
for idx, feature in filtered_gdf.iterrows():
folium.CircleMarker(location=[feature.geometry.y, feature.geometry.x],
radius=3,
color='black',
weight=0.1,
fill=True,
fill_color=cmap(feature[parameter]),
fill_opacity=1,
tooltip="{:.{prec}f} {} on {}".format(
feature[parameter], units, feature['times'], prec=tooltip_prec)
).add_to(m)
m.fit_bounds(m.get_bounds())
return m
In [21]:
m1 = underway_folium_map(validgeo_gdf, parameter='CO2 ppm', units='ppm',
tooltip_prec=0, cmap_name='viridis')
m1
Out[21]:
In [22]:
m2 = underway_folium_map(validgeo_gdf, parameter='Chlorophyll ug/L', units='ug/L',
tooltip_prec=1, cmap_name='YlOrRd_03')
m2
Out[22]:
In [ ]: