概述
高德的地铁数据是可以直接爬取下来的,本文以深圳市为例,说说如何爬取地铁数据并处理为geojson数据,并用QGIS进行可视化。
实现
![](https://img.haomeiwen.com/i6826673/f03aa4cf4fb377cd.png)
实现
1. 获取数据
在浏览器中打开高德地铁图的地址(http://map.amap.com/subway/index.html),按下F12刷新可以看到数据的请求,如下图。
![](https://img.haomeiwen.com/i6826673/0374032832354b4a.png)
上图中红框标出来的就是我们要爬取的数据,其中
drw
为图形绘制数据。![](https://img.haomeiwen.com/i6826673/e767c87295441def.png)
info
为信息数据。![](https://img.haomeiwen.com/i6826673/84effafb2818d8b3.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打开,配置图层样式即可。
网友评论