From 443ff278d94b7b250153bc980bc9b068efdcf5df Mon Sep 17 00:00:00 2001 From: Christoffer Weckstrom Date: Tue, 27 Jun 2017 12:18:42 +0300 Subject: [PATCH] gtfspy/mapviz.py: added functionality for plotting xy values with attribute, added colormaps.py for easier colormapping --- gtfspy/colormaps.py | 22 ++++++++++++++++++++++ gtfspy/mapviz.py | 27 ++++++++++++++++++++++++++- gtfspy/routing/journey_data.py | 3 ++- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 gtfspy/colormaps.py diff --git a/gtfspy/colormaps.py b/gtfspy/colormaps.py new file mode 100644 index 0000000..1777546 --- /dev/null +++ b/gtfspy/colormaps.py @@ -0,0 +1,22 @@ +import matplotlib.colors +import matplotlib.cm +import numpy + +# colormaps: "viridis", "plasma_r","seismic" + + +def get_colormap(observable_name): + if "diff_minutes" in observable_name: + norm = matplotlib.colors.Normalize(vmin=-30, vmax=30) + cmap = matplotlib.cm.get_cmap(name="seismic", lut=None) + elif "diff_number" in observable_name: + norm = matplotlib.colors.Normalize(vmin=-4, vmax=4) + cmap = matplotlib.cm.get_cmap(name="seismic", lut=None) + elif "diff_percentage" in observable_name: + norm = matplotlib.colors.Normalize(vmin=-0.5, vmax=0.5) + cmap = matplotlib.cm.get_cmap(name="seismic", lut=None) + else: + norm = matplotlib.colors.Normalize(vmin=-30, vmax=30) + cmap = matplotlib.cm.get_cmap(name="seismic", lut=None) + return cmap, norm + diff --git a/gtfspy/mapviz.py b/gtfspy/mapviz.py index f80bda2..26bcdeb 100644 --- a/gtfspy/mapviz.py +++ b/gtfspy/mapviz.py @@ -17,6 +17,7 @@ smopy.TILE_SERVER = "https://cartodb-basemaps-1.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png" + def _get_median_centered_plot_bounds(g): lon_min, lon_max, lat_min, lat_max = get_spatial_bounds(g) lat_median, lon_median = get_median_lat_lon_of_stops(g) @@ -28,6 +29,7 @@ def _get_median_centered_plot_bounds(g): plot_lat_max = lat_median + lat_diff return plot_lon_min, plot_lon_max, plot_lat_min, plot_lat_max + def plot_route_network(g, ax=None, spatial_bounds=None, map_alpha=0.8, scalebar=True, legend=True, return_smopy_map=False): """ @@ -93,11 +95,13 @@ def plot_route_network(g, ax=None, spatial_bounds=None, map_alpha=0.8, scalebar= else: return ax + def _add_scale_bar(ax, lat, lon_min, lon_max, width_pixels): distance_m = util.wgs84_distance(lat, lon_min, lat, lon_max) scalebar = ScaleBar(distance_m / width_pixels) # 1 pixel = 0.2 meter ax.add_artist(scalebar) + def plot_route_network_thumbnail(g): width = 512 # pixels height = 300 # pixels @@ -121,6 +125,28 @@ def plot_route_network_thumbnail(g): return plot_route_network(g, ax, spatial_bounds, map_alpha=1.0, scalebar=False, legend=False) +def plot_stops_with_attributes(lats, lons, attribute, colorbar=True, ax=None, cmap=None, norm=None): + + lon_min = min(lons) + lon_max = max(lons) + lat_min = min(lats) + lat_max = max(lats) + smopy_map = get_smopy_map(lon_min, lon_max, lat_min, lat_max) + if ax is None: + fig = plt.figure() + ax = fig.add_subplot(111) + ax = smopy_map.show_mpl(figsize=None, ax=ax, alpha=0.8) + + xs, ys = smopy_map.to_pixels(lats, lons) + cax = ax.scatter(xs, ys, c=attribute, s=0.5, cmap=cmap, norm=norm) + + ax.set_xlim(min(xs), max(xs)) + ax.set_ylim(max(ys), min(ys)) + if colorbar: + return ax, cax + return ax + + def plot_all_stops(g, ax=None, scalebar=False): """ Parameters @@ -137,7 +163,6 @@ def plot_all_stops(g, ax=None, scalebar=False): """ assert(isinstance(g, GTFS)) - stats = g.get_stats() lon_min, lon_max, lat_min, lat_max = get_spatial_bounds(g) smopy_map = get_smopy_map(lon_min, lon_max, lat_min, lat_max) if ax is None: diff --git a/gtfspy/routing/journey_data.py b/gtfspy/routing/journey_data.py index 7cde3ff..9c023f0 100644 --- a/gtfspy/routing/journey_data.py +++ b/gtfspy/routing/journey_data.py @@ -315,7 +315,6 @@ def get_origins(self): self.origins = cur.fetchall() return self.origins - @timeit def add_coordinates(self, df, join_column='from_stop_I'): stops_df = self.gtfs.stops() return pd.merge(stops_df, df, left_on='stop_I', right_on=join_column) @@ -439,6 +438,8 @@ def od_pair_data(self, analysis_start_time, analysis_end_time): data_dict["temporal_distance"].append(profile_block.measures_as_dict()) for key, value in self.journey_parameters.items(): + if value == (None, None): + next() value = [walking_duration if x == "t_walk" else x for x in value] profile_block = fpa.get_prop_analyzer_flat(key, value[0], value[1]) data_dict[key].append(profile_block.measures_as_dict())