Search code examples
javascriptopenstreetmapgeojson

Using osmtogeojson in jacascript on data from Overpass API


Ok, another (small, I guess) issue with osmtogeojson... So basically I need to download data from openstreetmap through Overpass API, but in geojson format. I'm not very used to HTTPS requests and responses, so I guess I missed something between this and the use of osmtogeojson, as I get this :

osmtogeojson.js:1 Uncaught TypeError: Cannot read property 'length' of undefined
    at osmtogeojson.js:1
    at i (osmtogeojson.js:1)
    at XMLHttpRequest.xhr.onload (map_common.js:184)

with the following test code (The xml data seems correct, it is correctly translated in geojson when I use the osmtogeojson test page).

    function updateData(sourcename){
        var url = "https://overpass-api.de/api/interpreter?data=node[name=\"Châtenay-Malabry\"];out;"
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.timeout = 6000;
        try{
            xhr.send(null);
        }
        catch (error){
            alert(error);
        }
        xhr.ontimeout = function(){
            console.log("time out !");
        };
        xhr.onload = function(){
            if (xhr.readyState === 4 && xhr.status===200){
                var out = xhr.response;
                console.log(out);
                var out_geojson = osmtogeojson(out);
            }
            else{
                console.log(xhr.status);
            }
        };
    }

Hope it's just I forgot something this time... Thanks in advance.


Solution

  • Finally I found another solution (quite the same actually) : I just ask for a json result, then I parse the response and it actually works!