简体   繁体   English

在 jacascript 中对来自 Overpass API 的数据使用 osmtogeojson

[英]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.好的,osmtogeojson 的另一个(我猜是小问题)问题......所以基本上我需要通过 Overpass API 从 openstreetmap 下载数据,但采用 geojson 格式。 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 :我不太习惯 HTTPS 请求和响应,所以我想我错过了这和使用 osmtogeojson 之间的一些东西,因为我得到了这个:

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).使用以下测试代码(xml 数据似乎正确,当我使用 osmtogeojson 测试页面时,它在 geojson 中正确翻译)。

    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.希望这次只是我忘记了一些东西......提前致谢。

I had the same Error with my call using axios when I used the response.data You have to use the xml of the response.当我使用 response.data 时,我使用 axios 调用时遇到了相同的错误。您必须使用响应的 xml。 Than it worked fine for me.对我来说效果很好。

axios.get(overpassUrl).then(response => {
                    var resultAsGeojson = osmtogeojson(response.request.responseXML);

If XMLHttpRequest has not xml response you have to parse your response to xml.如果 XMLHttpRequest 没有 xml 响应,则必须将响应解析为 xml。

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");

最后我找到了另一个解决方案(实际上完全相同):我只是要求一个 json 结果,然后我解析响应并且它确实有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM