Analytics

class pytrack.analytics.visualization.Map(location=None, width='100%', height='100%', left='0%', top='0%', position='relative', tiles='CartoDB positron', attr=None, min_zoom=0, max_zoom=18, zoom_start=15, min_lat=- 90, max_lat=90, min_lon=- 180, max_lon=180, max_bounds=False, crs='EPSG3857', control_scale=False, prefer_canvas=False, no_touch=False, disable_3d=False, png_enabled=False, zoom_control=True, **kwargs)[source]

Bases: Map

This class extends the folium.Map to add functionality useful to represent graphs and road paths.

Parameters
  • location (tuple or list, optional, default: None) – Latitude and Longitude of Map (Northing, Easting).

  • width (int or percentage string, optional, default: '100%')) – Width of the map.

  • height (int or percentage string, optional, default: '100%') – Height of the map.

  • tiles (str, optional, default: 'OpenStreetMap') – Map tileset to use. Can choose from a list of built-in tiles, pass a custom URL or pass None to create a map without tiles. For more advanced tile layer options, use the TileLayer class.

  • min_zoom (int, optional, default: 0) – Minimum allowed zoom level for the tile layer that is created.

  • max_zoom (int, optional, default: 18) – Maximum allowed zoom level for the tile layer that is created.

  • zoom_start (int, optional, default 10) – Initial zoom level for the map.

  • attr (string, optional, default: None) – Map tile attribution; only required if passing custom tile URL.

  • crs (str, optional, default: 'EPSG3857') – Defines coordinate reference systems for projecting geographical points into pixel (screen) coordinates and back. You can use Leaflet’s values : * EPSG3857 : The most common CRS for online maps, used by almost all free and commercial tile providers. Uses Spherical Mercator projection. Set in by default in Map’s crs option. * EPSG4326 : A common CRS among GIS enthusiasts. Uses simple Equirectangular projection. * EPSG3395 : Rarely used by some commercial tile providers. Uses Elliptical Mercator projection. * Simple : A simple CRS that maps longitude and latitude into x and y directly. May be used for maps of flat surfaces (e.g. game maps). Note that the y axis should still be inverted (going from bottom to top).

  • control_scale (bool, optional, default: False) – Whether to add a control scale on the map.

  • prefer_canvas (bool, optional, default: False) – Forces Leaflet to use the Canvas back-end (if available) for vector layers instead of SVG. This can increase performance considerably in some cases (e.g. many thousands of circle markers on the map).

  • no_touch (bool, optional, default: False) – Forces Leaflet to not use touch events even if it detects them.

  • disable_3d (bool, optional, default: False) – Forces Leaflet to not use hardware-accelerated CSS 3D transforms for positioning (which may cause glitches in some rare environments) even if they’re supported.

  • zoom_control (bool, optional, default: True) – Display zoom controls on the map.

  • **kwargs (keyword arguments, optional, default: no attributes) – Additional keyword arguments are passed to Leaflets Map class: https://leafletjs.com/reference-1.6.0.html#map

Return type

Folium Map Object

Notes

See https://github.com/python-visualization/folium/blob/551b2420150ab56b71dcf14c62e5f4b118caae32/folium/folium.py#L69 for a more detailed description

add_child(child, name=None, index=None)

Add a child.

add_children(child, name=None, index=None)

Add a child.

add_graph(G, plot_nodes=False, edge_color='#3388ff', edge_width=3, edge_opacity=1, radius=1.7, node_color='red', fill=True, fill_color=None, fill_opacity=1)[source]

Add the road network graph created with pytrack.graph.graph.graph_from_bbox method

Parameters
  • G (networkx.MultiDiGraph) – Road network graph.

  • plot_nodes (bool, optional, default: False) – If true, it will show the vertices of the graph.

  • edge_color (str, optional, default: "#3388ff") – Colour of graph edges

  • edge_width (float, optional, default: 3) – Width of graph edges

  • edge_opacity (float, optional, default: 1) – Opacity of graph edges

  • radius (float, optional, default: 1.7) – Radius of graph vertices

  • node_color (str, optional, default: "red") – Colour of graph vertices

  • fill (bool, optional, default: True) – Whether to fill the nodes with color. Set it to false to disable filling on the nodes.

  • fill_color (str or NoneType, default: None) – Fill color. Defaults to the value of the color option.

  • fill_opacity (float, optional, default: 1) – Fill opacity

add_to(parent, name=None, index=None)

Add element to a parent.

choropleth(*args, **kwargs)[source]

Call the Choropleth class with the same arguments.

This method may be deleted after a year from now (Nov 2018).

default_css = [('leaflet_css', 'https://cdn.jsdelivr.net/npm/leaflet@1.6.0/dist/leaflet.css'), ('bootstrap_css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'), ('bootstrap_theme_css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css'), ('awesome_markers_font_css', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'), ('awesome_markers_css', 'https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css'), ('awesome_rotate_css', 'https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css')]
default_js = [('leaflet', 'https://cdn.jsdelivr.net/npm/leaflet@1.6.0/dist/leaflet.js'), ('jquery', 'https://code.jquery.com/jquery-1.12.4.min.js'), ('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'), ('awesome_markers', 'https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js')]
draw_candidates(candidates, radius)[source]

Draw the candidate nodes of the HMM matcher

Parameters
  • candidates (dict) – Candidates’ dictionary computed via pytrack.matching.candidate.get_candidates method

  • radius (float) – Candidate search radius

draw_path(G, trellis, predecessor, path_name='Matched path')[source]

Draw the map-matched path

Parameters
  • G (networkx.MultiDiGraph) – Road network graph.

  • trellis (nx.DiGraph) – Trellis DAG graph created with pytrack.matching.mpmatching_utils.create_trellis method

  • predecessor (dict) – Predecessors’ dictionary computed with pytrack.matching.mpmatching.viterbi_search method

  • path_name (str) – Name of the path to be drawn

fit_bounds(bounds, padding_top_left=None, padding_bottom_right=None, padding=None, max_zoom=None)[source]

Fit the map to contain a bounding box with the maximum zoom level possible.

Parameters
  • bounds (list of (latitude, longitude) points) – Bounding box specified as two points [southwest, northeast]

  • padding_top_left ((x, y) point, default None) – Padding in the top left corner. Useful if some elements in the corner, such as controls, might obscure objects you’re zooming to.

  • padding_bottom_right ((x, y) point, default None) – Padding in the bottom right corner.

  • padding ((x, y) point, default None) – Equivalent to setting both top left and bottom right padding to the same value.

  • max_zoom (int, default None) – Maximum zoom to be used.

Examples

>>> m.fit_bounds([[52.193636, -2.221575], [52.636878, -1.139759]])
get_bounds()

Computes the bounds of the object and all it’s children in the form [[lat_min, lon_min], [lat_max, lon_max]].

get_name()

Returns a string representation of the object. This string has to be unique and to be a python and javascript-compatible variable name.

get_root()

Returns the root of the elements tree.

keep_in_front(*args)[source]

Pass one or multiple layers that must stay in front.

The ordering matters, the last one is put on top.

Parameters

*args – Variable length argument list. Any folium object that counts as an overlay. For example FeatureGroup or TileLayer. Does not work with markers, for those use z_index_offset.

render(**kwargs)[source]

Renders the HTML representation of the element.

save(outfile, close_file=True, **kwargs)

Saves an Element into a file.

Parameters
  • outfile (str or file object) – The file (or filename) where you want to output the html.

  • close_file (bool, default True) – Whether the file has to be closed after write.

to_dict(depth=- 1, ordered=True, **kwargs)

Returns a dict representation of the object.

to_json(depth=- 1, **kwargs)

Returns a JSON representation of the object.

pytrack.analytics.visualization.draw_trellis(T, figsize=None, dpi=None, node_size=500, font_size=8, **kwargs)[source]

Draw a trellis graph

Parameters
  • T (networkx.DiGraph) – A directed acyclic graph

  • figsize ((float, float), optional, default: [15.0, 12.0]) – Width, height figure size tuple in inches, optional

  • dpi (float, optional, default: 300.0) – The resolution of the figure in dots-per-inch

  • node_size (scalar or array, optional, default: 500) – Size of nodes. If an array is specified it must be the same length as nodelist.

  • font_size (int, optional, default: 8) – Font size for text labels

  • kwargs (keyword arguments, optional, default: no attributes) – See networkx.draw_networkx_nodes(), networkx.draw_networkx_edges(), networkx.draw_networkx_labels() and matplotlib.pyplot.figure() for a description of optional keywords.

Returns

trellis_diag – Graphical illustration of the Trellis diagram used in the Hidden Markov Model process to find the path that best matches the actual GPS data

Return type

matplotlib.pyplot.Figure