3

I am using overpass API and querying the api for ways,as a result it gives points, whereas I need only lines, multiline strings and polygons. But while using the following code, I get a GeoJSON file and when I load it into QGIS it gives me points and lines, when I switch off the points layer the lines are not loaded onto QGIS. I require only the roads(for which lines, multilines and multipolygons is required) and not Points.

How do I modify the query to get only multilinestrings,lines and multipolygons and not Points?

import overpass
import geojson

api = overpass.API()

overpass_url="""

(
  way["highway"="motorway"](20.8,77.7,21.2,78);
  way["highway"="trunk"](20.8,77.7,21.2,78);
  way["highway"="primary"](20.8,77.7,21.2,78);
);
out body;
>;
out skel qt;

"""

res=api.get(overpass_url)


with open("test.geojson",mode="w") as f:
  geojson.dump(res,f)

1 Answer 1

2

I solved the issue after going through little bit of documentation.To fetch only the lines, the following modification to the code can be made.

import overpass
import geojson

api = overpass.API()

overpass_url="""

(
  way["highway"="motorway"](20.8,77.7,21.2,78);
  way["highway"="trunk"](20.8,77.7,21.2,78);
  way["highway"="primary"](20.8,77.7,21.2,78);
);
out geom;    # change here for only geometry output
"""

res=api.get(overpass_url)


with open("test.geojson",mode="w") as f:
  geojson.dump(res,f)
1
  • I got an error when using it like it. I first needed to get the list of features from the feature collection with: with open('test.geojson', mode = 'w') as f: geojson.dump(res['features'], f)
    – i.i.k.
    Mar 2 at 11:49

Your Answer

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

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