plotting.py 2.46 KB
Newer Older
1 2
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
3 4 5
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
6
from typing import Tuple
7

8

9
def plot_locations_osm(
10 11 12 13
    gps_series: gpd.GeoSeries,
    c_series: np.ndarray = None,
    zoom_level: int = 10,
    scatter_size: int = 10,
14
    fig_size: Tuple[int, int] = (12, 10),
15 16
    save_path=None,
    show=True,
17
    **kwargs
18 19
):
    """
20 21 22 23
    Plot a series of gps locations onto a OSM map

    :param gps_series: geopandas series of the gps value in EPSG:4326 to plot
    :param c_series: series of values to be interpreted as colors in the scatterplot
24
    :param scatter_size: scalar or series to be interpreted as size in the scatterplot
25 26
    :param zoom_level: zoom level for resolution of OSM tiles
    :param fig_size: the figure size of the generated plot
27
    :param **kwargs: additional keword arguements which will directly be passed on to scatterplot
28
    :param save_path: if not none then the plot will be saved under this path
29
    """
30

31 32 33 34 35 36 37 38 39 40 41 42
    if gps_series.crs != "EPSG:4326":
        raise ValueError("Make sure to pass data with EPSG:4326 projection")

    request = cimgt.OSM()
    extent = gps_series.total_bounds[[0, 2, 1, 3]]
    plt.figure(figsize=fig_size)
    ax = plt.axes(projection=request.crs)
    ax.set_xlabel("Longitude")
    ax.set_ylabel("Latitude")
    ax.set_extent(extent)
    ax.add_image(request, zoom_level)
    if c_series is not None:
43 44 45 46 47 48
        ax.scatter(
            gps_series.x,
            gps_series.y,
            c=c_series,
            transform=ccrs.PlateCarree(),
            s=scatter_size,
49
            **kwargs
50
        )
51
    else:
52
        ax.scatter(
53 54 55 56 57
            gps_series.x,
            gps_series.y,
            transform=ccrs.PlateCarree(),
            s=scatter_size,
            **kwargs
58
        )
59 60 61 62
    if save_path is not None:
        plt.savefig(save_path, bbox_inches="tight")
    if show:
        plt.show()
63

64 65 66 67 68

def plot_street_nodes(
    street_series: gpd.GeoSeries, axs, marker: str = "o", color: str = "blue"
):
    """
69 70 71 72 73 74
    Plots the street_series with nodes along each way.

    :param street_series: geopandas series of LineStrings
    :param axs: denotes the axis on which to plot
    :param marker: marker for the nodes
    :param color: color of the plotted lines and nodes
75
    """
76
    for linestring in street_series.geometry:
77
        axs.plot(
Sonja Tripkovic's avatar
Sonja Tripkovic committed
78 79
            np.array(linestring.xy[0]),
            np.array(linestring.xy[1]),
80 81
            marker=marker,
            color=color,
82
        )