Search code examples
pythonplotgeopandasshapely

Use of a single LineString to display two data fields


I have a recurring problem for which I cannot find an easy solution, whereas I guess there is one.

I have a dataset representing road traffic data at a street level. This dataset is associated with a road network graph, where each edge represents a road and each node represents an intersection. For each edge, I have a shapely LineString object containing the geopgraphic properties of the road.

My goal is to draw a map representing this traffic dataset. I can do that quite easily using geopandas for example, but my problem comes from the fact that most roads, even when bi-directionnal, are represented with a single LineString object, so one of the direction won't be seen as both will be plotted on top of each other.

Would there be a simple way to solve this problem? Ideally, I would like to end up with a plot like this: traffic data representation example


Solution

  • The following snippet use offset_curve to double up a line string, which is I think what you are looking for.

    data = np.array([np.linspace(0,10), np.sin(np.linspace(0,10))])
    road = shapely.LineString(data.T)
    constant = 0.1
    big_road = shapely.MultiLineString([road.offset_curve(constant), road.offset_curve(-constant)])
    

    which gives:

    from shapely import plotting
    plotting.plot_line(big_road)
    plt.savefig('example.jpg')
    

    enter image description here