0

After executing a certain amount of routing calculations, I have a list of vehicle routes as shown in the image below. Now I have added an opacity to these lines to visualize the frequency of certain roads being used but ideally these would be given a color scale for that purpose.

routes

These routes are calculated on a backend (valhalla) and returned to the client where they are visualize using leaflet. The user will be able to filter these routes so everything that is render related is preferably done on the client side.

Has anyone ever done something like this, or has an idea about how this could be achieved? There does not seem to be a leaflet plugin for something like this. An idea could be to split the lines into parts, aggregate them and calculate the occurrence of each part. Then I could color it based on the occurrence value. I have also checked Turf.js to do this, but couldn't figure out how. Each route is a single line and not divided per street (that would make this much easier).

Any ideas are more than welcome!

Edit: Here is a code snippet of the current map.

7
  • 1
    One possible idea would be via TopoJSON, see for example stackoverflow.com/questions/57543715/….
    – TomazicM
    Jun 28, 2021 at 9:27
  • 1
    Would you mind posting a sandbox, or at least a sample of the data structure returned by valhalla that is creating the image you have there? Jun 28, 2021 at 16:10
  • @TomazicM, TopoJSON looks interesting indeed! Something I hadn't heard of before but seems to be quite capable. But would it be possible to filter n lines and color the arcs based on the filtered number of occurrences of that arc?
    – Sam
    Jun 29, 2021 at 8:12
  • @SethLutske Thanks for the reminder, I added a code sandbox link to the React Leaflet example.
    – Sam
    Jun 29, 2021 at 8:12
  • Since GIS SE site is not a discussion forum, I'll give just a general idea hot to use TopoJSON in this case. First use topojson.topology([geoJSON]) to break GeoJSON of your combined routes to segments, called arcs in TopoJSON. From here on you have to analyze TopoJSON yourselves to see how many times each arc accurs in your routes. Then again you have to construct GeoJSON/vector layer from those arcs and color them according to number of calculated occurrences.
    – TomazicM
    Jun 29, 2021 at 8:48

1 Answer 1

0

I managed to get what I wanted by using the TopoJSON format as suggested by TomazicM in the comments. By converting the data to this format before sending it to the client I also used a mere 12% of the original file size (4Mb to 500Kb). Then on the client side I made a feature for each arc in the TopoJSON format. Finally, by counting how often the arc occurred in the original geometries, I could dynamically style the lines given certain filters and conditions.

Here is a code snippet of the solution I came up with.

enter image description here

It is still not perfect, some of the source lines are ever so slightly different which makes TopoJSON interpret them as different arcs, while they should be the same. But that is an issue with the source data and not the aggregation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.