aspectumapp / osm2geojson

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

Single Quote error #15

Closed aw-west closed 3 years ago

aw-west commented 3 years ago

Due to the use of single quotes over double quotes other json readers cannot parse your output.

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Kinda-minimal working example

import os.path
import osm2geojson
import json  # OR: geopandas

fname = 'out.geojson'
if not os.path.isfile(fname):
    query = '[out:json];rel(id:2202162);out skel geom;'
    data = osm2geojson.overpass_call(query)
    gjson = osm2geojson.json2geojson(data, log_level='CRITICAL')

    gjson = gjson.replace("'",'"')  # <--- needed to avoid error.

    print(gjson, file=open(fname,'w'))
g = json.loads(open(fname))  # OR: geopandas.read_file(open(fname))

p.s. This is awesome, thank you.

rapkin commented 3 years ago

I think problem here not with lib. json2geojson and xml2geojson return dict structure (not json string). You can read tests to better understand how it works https://github.com/aspectumapp/osm2geojson/blob/master/tests/main.py

So your example should looks like

import os.path
import osm2geojson
import json  # OR: geopandas

fname = 'out.geojson'
if not os.path.isfile(fname):
    query = '[out:json];rel(id:2202162);out skel geom;'
    data = osm2geojson.overpass_call(query)
    gjson_structure = osm2geojson.json2geojson(data, log_level='CRITICAL')

    gjson = json.dumps(gjson_structure, indent=2)

    print(gjson, file=open(fname,'w'))
g = json.loads(open(fname))  # OR: geopandas.read_file(open(fname))