美文网首页
爬取高德地铁数据并转为geojson在QGIS中可视化

爬取高德地铁数据并转为geojson在QGIS中可视化

作者: 牛老师讲GIS | 来源:发表于2023-06-11 23:41 被阅读0次

概述

高德的地铁数据是可以直接爬取下来的,本文以深圳市为例,说说如何爬取地铁数据并处理为geojson数据,并用QGIS进行可视化。

实现

Snipaste_2023-06-12_23-22-31.png

实现

1. 获取数据

在浏览器中打开高德地铁图的地址(http://map.amap.com/subway/index.html),按下F12刷新可以看到数据的请求,如下图。

image.png
上图中红框标出来的就是我们要爬取的数据,其中drw为图形绘制数据。
image.png
info为信息数据。
image.png
本文只做了drw接口的解析。

2. 解析数据

class Geojson {
    constructor(features = [], metaData = {}) {
        this.type = 'FeatureCollection'
        this.metadata = metaData
        this.features = features
    }
}
class Geometry {
    constructor(type, coordinates) {
        this.type = type
        this.coordinates = coordinates
    }
}
class Feature {
    constructor(geomType, properties, coordinates) {
        this.type = 'Feature'
        this.properties = properties
        this.geometry = new Geometry(geomType, coordinates)
    }
}
const url = 'http://localhost:8089/subway?_1686580355871&srhdata=4403_drw_shenzhen.json'
fetch(url).then(res => res.json()).then(({l}) => {
    let lines = [], stations = []
    l.forEach(({kn, st})  => {
        let lineCoords = []
        st.forEach(d => {
            const sl = d.sl.split(',').map(Number)
            lineCoords.push(sl)
            stations.push(new Feature('Point', d, sl))
        })
        lines.push(new Feature('LineString', {kn}, lineCoords))
    })
    const linesGeojson = new Geojson(lines)
    const stationsGeojson = new Geojson(stations)
    console.log(JSON.stringify(linesGeojson))
    console.log(JSON.stringify(stationsGeojson))
})

说明:上面代码中引用地址为http://localhost:8089,原因是直接调用http://map.amap.com会跨域,因此在nginx中做了转发,配置如下:

http {
    
    server {
        listen       8089;
        server_name  localhost;     
        location /subway {
            proxy_pass  http://map.amap.com/service/subway;
            # 设置是否允许 cookie 传输
            add_header Access-Control-Allow-Credentials true;
            # 允许请求地址跨域 * 做为通配符
            add_header Access-Control-Allow-Origin * always;
            # 允许跨域的请求方法
            add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            proxy_set_header Host $proxy_host; 
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

2. QGIS中可视化

将控制台输出的数据复制,保存为json文件,用QGIS打开,配置图层样式即可。

相关文章

网友评论

      本文标题:爬取高德地铁数据并转为geojson在QGIS中可视化

      本文链接:https://www.haomeiwen.com/subject/almfydtx.html