Created
December 15, 2016 22:33
-
-
Save byrney/61120f70d4498268f234a4fa6c2a2c90 to your computer and use it in GitHub Desktop.
Add a route from OSRM to QGIS memory layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Paste this into the QGIS python console | |
# | |
sx=-3.51249995613522 | |
sy=50.7355583126591 | |
ex=-3.53626387621797 | |
ey=50.7058687562277 | |
host = 'http://router.project-osrm.org' | |
template = '/route/v1/driving/{},{};{},{}?steps=true' | |
response = requests.get(host + template.format(sx, sy, ex, ey)) | |
route = response.json()['routes'][0] | |
steps = route['legs'][0]['steps'] | |
maneuvers = [s['maneuver'] for s in steps] | |
locations = [m['location'] for m in maneuvers] | |
c = len(locations) | |
print(c) | |
points = [] | |
for pp in locations: | |
qp = QgsPoint(pp[0], pp[1]) | |
points.append(qp) | |
vl = QgsVectorLayer('multilinestring?crs=epsg:4326', 'locations2', 'memory') | |
vl.startEditing() | |
pr = vl.dataProvider() | |
ff = QgsFeature() | |
print(points) | |
ff.setGeometry(QgsGeometry.fromPolyline(points)) | |
ff.setAttributes(["Johny", 2, 0.3]) | |
print(ff) | |
pr.addAttributes([QgsField("name", typeName='String')]) | |
pr.addFeatures([ff]) | |
vl.commitChanges() | |
vl.updateExtents() | |
QgsMapLayerRegistry.instance().addMapLayer(vl) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment