aspectumapp / osm2geojson

Convert OSM and Overpass XML/JSON to GeoJSON
MIT License
100 stars 14 forks source link

Error when converting OSM relation type to GeoJson using osm2geojson #12

Closed AlbertoMCS closed 4 years ago

AlbertoMCS commented 4 years ago

Hi, I am using osm2geojson (thanks BTW) in a function to retrieve OSM data with Overpass API (Json) and convert the Json output to Geojson. It works perfectly with ways and nodes but it doesn't work with relation type. Here my code:

def lines_OSM(query, lat, lon, radious, output, name):
overpass_query = """
[out:json];
(node"""+query+"""(around:"""+radious+""","""+lat+""","""+lon+""");
way"""+query+"""(around:"""+radious+""","""+lat+""","""+lon+""");
relation"""+query+"""(around:"""+radious+""","""+lat+""","""+lon+""");
);
out geom;
>;
out skel qt;
"""
response = requests.get(overpass_url,params={'data': overpass_query})
data = response.json()
data = osm2geojson.json2geojson(data)
json.dump(data,open(output,'w' ))

Below and example of the error that I get when I run the query "route" = "subway"

Failed to convert relation to shape {'ref': 8095058, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114942, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104232, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130152, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107493, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133604, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8081588, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130679, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8095058, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114942, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8081588, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130679, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8103305, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8151301, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104470, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8119312, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8115035, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114910, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108226, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108183, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133603, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107492, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130151, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133514, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8192515, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8118406, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130678, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8081588, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114942, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107491, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114942, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107491, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130678, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8081588, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104232, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133603, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107492, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130151, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130152, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107493, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133604, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8081588, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130679, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130678, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8081588, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104232, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133603, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107492, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130151, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104231, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104403, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8118897, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8085265, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 9582182, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8085265, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8118897, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104403, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104231, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104231, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104231, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8118406, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8192515, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133514, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130152, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107493, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133604, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108183, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108226, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114910, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8115035, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8095398, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8119312, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104470, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8151301, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8103305, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8103305, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8151301, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104470, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8119312, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8115035, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114910, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108226, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108183, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133603, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107492, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130151, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133514, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8192515, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8135319, 'role': 'platform_entry_only', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130152, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107493, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133604, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108183, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108226, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114910, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8115035, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8095398, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8119312, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104470, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8151301, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8118406, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8192515, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133514, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130152, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107493, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133604, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108183, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108226, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114910, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8115035, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8095398, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8119312, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104470, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8151301, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8103305, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8151301, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8104470, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8119312, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8115035, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8114910, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108226, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8108183, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8133603, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8107492, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8130151, 'role': 'platform', 'type': 'relation'}
Failed to convert relation to shape {'ref': 8135318, 'role': 'platform_exit_only', 'type': 'relation'}

Attached the Geojson created subway.zip

Many thanks.

AlbertoMCS commented 4 years ago

Sorry attached is not the geojson, but the json response from OSM (the file I want to convert into Geojson)

rapkin commented 4 years ago

Can you provide repeatable example with your problem? For example with platform https://repl.it/ . Also I see geojson inside your subway.zip (to find issue I need source json-data from Overpass).

AlbertoMCS commented 4 years ago

Hi rapkin, Thanks for your response. I tried many times osm2geojson with different queries and I get lot of issues with relations. I managed to code a very rudimentary but valid code to deal with my needs. Many thanks.

rapkin commented 4 years ago

Hi, issues with relations are possible if you have missed data from overpass (some queries can generate partial response). Also you can try this tool https://tyrasd.github.io/osmtogeojson/ to check your results

ghost commented 3 years ago

https://github.com/aspectumapp/osm2geojson/issues/12

Hi @rapkin,

I am using/trying to use your package - it works fine in a Python interpreter - virtual env (so the .geojson file generated well); until I try to run in my test app (localhost, uvicorn - FastAPI), then appears the issue mentioned earlier. 'Failed to convert relation to shape ..'

Do you have any idea what can be the reason of this?

I have tried a lot of combinations as you see:

test_overpass.py:

import overpass
import json
import osm2geojson
import requests

def collect():
    api = overpass.API()
    response = api.get('rel(1501373);out body;way(r);(._;>;);out skel qt;', responseformat="xml", verbosity="geom")
    #print(result)
    #url = 'https://overpass-api.de/api/interpreter?data=[out:xml][timeout:25];rel(1501373);way(r);out geom;'
    #url = 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:25];rel(1501373);out geom;'
    #print("URL: ", url)
    #response = requests.get(url)
    #string = response.text.replace("\n","")
    #geojson = osm2geojson.xml2geojson(string)
    geojson = osm2geojson.xml2geojson(response)

    #result = result.json()
    #geojson = osm2geojson.json2geojson(result)
    print(geojson)

main.py:

...
import library.test_overpass

 @app.get("/")
def home(request: Request):
    stat = library.test_overpass.collect()
    print(stat)
ghost commented 3 years ago

Hmm, seems the problem solved someway :s (maybe the package functions were loaded twice - donno). This code solved the problem:

import overpass
import json
import osm2geojson
import requests
import time

def collect():
    t = time.localtime()
    current_time = time.strftime("%H:%M:%S", t)
    print(current_time)
    url = 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:25];rel(1501373);out geom;'
    response = requests.get(url)
    data = response.json()
    #print("Original: ", data)
    print('-----')
    print('-----')
    print('-----')
    print('-----')
    data['elements'][0]['members'].pop(-1)
    #print("Popped:", data)
    print('-----')
    print('-----')
    print('-----')
    print('-----')    

    converted = osm2geojson.json2geojson(data)
    print(converted)
    with open("data/overpass-test.geojson", "w", encoding="utf-8") as json_file:
        json.dump(converted, json_file, ensure_ascii=False)
rapkin commented 3 years ago

Hi @poetyi

I see that you changed your query in second code snippet. Different queries load different data (sometimes partial) and it's ok to have errors like 'Failed to convert relation to shape ..' on partial data.