Problem with writing a batch of routes to a GeoJSON file


#1

I am trying to return routes from a batch of coordinates to a single location. This is a volunteer project for a local foodbank and I am quite inexperienced in writing code.

Here is what I wrote in Python:

    import csv
import openrouteservice
from openrouteservice import convert

client = openrouteservice.Client(key='5b3ce3597851110001cf624863085b996464465cbf0974fe690fc4ef')
with open ('ClientCoordinates10test.csv' , 'r') as csv_coords:
    csv_reader = csv.reader(csv_coords) 
    EXTRA_COORD = (2.23179, 53.464774)
    new_csv_coords = []
    routelist = []
    for line in csv_reader:
        new_csv_coords.append(((float(line[0]), float(line[1])), EXTRA_COORD))
            
        coords = (new_csv_coords)
        print(coords[0]) 
        routes = client.directions(coords[0])
        
        decoded = convert.decode_polyline(routes['routes'][0]['geometry'])
        
        print(decoded) 
        
        routelist.append(decoded)

# create a file to write to
f = open('route.geojson', 'w+')

# write our path to the file
f.write(str(routelist))

However when I try to load the GeoJSON into QGIS I get an “invalid GeoJSON” error. I’m wondering whether I need to write more properties to the GeoJSON? and if so how do I do this?

Any help is greatly appreciated.


#2

Well, as far as I can see, you’re not generating a GeoJSON, just a list of coordinates. As there’s multiple things “wrong” here, it’ll be a whole lot easier for you to install ORS Tools for QGIS, which makes the above code redundant.