Skip to content

Commit

Permalink
gtfspy/mapviz.py: added functionality for plotting xy values with att…
Browse files Browse the repository at this point in the history
…ribute, added colormaps.py for easier colormapping
  • Loading branch information
jweckstr committed Jun 27, 2017
1 parent edf7c0c commit 443ff27
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
22 changes: 22 additions & 0 deletions gtfspy/colormaps.py
Original file line number Diff line number Diff line change
@@ -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

27 changes: 26 additions & 1 deletion gtfspy/mapviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion gtfspy/routing/journey_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 443ff27

Please sign in to comment.