java实现百度地铁数据下载和处理

发布于 2022-09-27 | 作者: zyy_pipi | 来源: CSDN博客 | 转载于: CSDN博客

前言

在学习webgis和一些特定行业得应用中我们或多或少对地铁数据还是有使用需求,从osm或者其他渠道下载得数据基本都是年份比较老的数据,与实际效果有一定差别,百度地铁数据一直保持着比较新的状态,在学习和使用中效果比较好,其实这里也对比过高德地铁数据发现有些城市高德没有比如洛阳,所以最终使用百度的地铁数据。


一、数据获取流程

数据获取的来源是百度地图的地铁数据页面,地址:https://map.baidu.com/subway/北京/@12959238.56,4825347.47,12z/ccode=131&cname=%E5%8C%97%E4%BA%AC

页面F12我们可以看到接口请求获取到对应城市的编码

然后根据城市编码获取对应城市的地铁数据,如下图131则是背景城市编码

二、代码实现

1、获取到城市地铁数据

String BDBaseUrl = "https://map.baidu.com/?qt=subways&c=" + cityCode + "&format=json&t=1659489179441&auth=7zf0dDWSJP6BKBwA8eNB@ZQzgAcV3NgvuxLHTERTxNTtE7DDJJYJ7MAEzxz6ZwWvvkGcuVtvvhguVtvyheuxBt2M75GdFvCQMuVtvIPcuxtw8wkvASuDenSC@Bv@vcuVtvc3CuVtvcPPuVtveGvuxzLztxgjLwzvwiKDv7uvhgMuzVVtvrMhuBTHBto20N=5CGIbFUuuouKi3egvcguxLHTERTxNT&seckey=be9yjZlzHxAhYeh4+hURnhUc+aG080+T45VtRwZ+ABg=,aCF54nA7VdJ4fnRnx7_EQtA6GyAExeXnV7gAtlA4kqxld0Dhh6sLCftyXIMd5yWt1fHvTKyCBwZJUZbIZyYoJLiKZeIQU3q-d6uVE7U6qwwlK_U1b4S0WCtjzHf8Ug890_nPs8vYYFF4bshMPMqu-z_7IBvQOi3z31KNkkA29D-dsujJhlsv7_9RynttwXpn&pcevaname=pc4.1&newfrom=zhuzhan_webmap";
String result = HttpUtil.get(BDBaseUrl);
JSONObject subwayReq = JSONObject.parseObject(result);
JSONObject resultContent = subwayReq.getJSONObject("result");
if(resultContent.get("error").toString().equalsIgnoreCase("0")){
    // 遍历数据 写成geojson
    JSONObject subways = subwayReq.getJSONObject("subways");
    return geoJSONResult(subways);
}

2、数据处理写成geojson

这里要注意返回的数据里面的坐标是百度墨卡托坐标需要处理转换

public JSONObject geoJSONResult(JSONObject subways) {
    JSONArray subwayList = subways.getJSONArray("l");
    String cityName = subways.getJSONObject("sw_xmlattr").get("cid").toString();
    JSONObject result = new JSONObject();
    result.put("type", "FeatureCollection");
    JSONArray features = new JSONArray();
    // 遍历获取线路
    for (int i = 0; i < subwayList.size(); i++) {
        JSONObject lineInfo = subwayList.getJSONObject(i);
        //线路基础描述
        JSONObject lineBaseInfo = lineInfo.getJSONObject("l_xmlattr");
        //地铁线路名称
        String lineName = lineBaseInfo.getString("lid");
        // 地铁线路颜色
        String lineColor = lineBaseInfo.getString("lc");
        //站点信息
        JSONArray stationList = lineInfo.getJSONArray("p");
        for (int j = 0; j < stationList.size(); j++) {
            JSONObject stationInfo = stationList.getJSONObject(j).getJSONObject("p_xmlattr");
            if(StringUtils.isNotBlank(stationInfo.getString("sid"))) {
                Map feature = new HashMap<>();
                // 赋值 属性字段
                Map properties = new HashMap<>();
                properties.put("cityname", cityName);
                properties.put("address", lineName);
                properties.put("name", stationInfo.getString("sid")+"(地铁站)");
                properties.put("remark", stationInfo.getString("ln"));
                double x = stationInfo.getDouble("px");
                double y = stationInfo.getDouble("py");
                // 百度墨卡托转百度09
                Map location = BDCRSTools.convertMC2LL(x, y);
                // 百度09转wgs84
                double[] wgs84Point = WGS_Encrypt.bd09ToWgs84(location.get("lng"),location.get("lat"));
                // 赋值 空间字段
                Map geometry = new HashMap<>();
                geometry.put("type", "Point");
                double[] point = new double[2];
                point[0] = wgs84Point[1];
                point[1] = wgs84Point[0];
                properties.put("lng",wgs84Point[1] );
                properties.put("lat", wgs84Point[0]);
                geometry.put("coordinates", point);
                feature.put("type", "Feature");
                feature.put("properties", properties);
                feature.put("geometry", geometry);
                features.add(feature);
            }
        }
    }
    result.put("features",features);
    return result;
}

百度墨卡托转wgs84

public JSONObject geoJSONResult(JSONObject subways) {
    JSONArray subwayList = subways.getJSONArray("l");
    String cityName = subways.getJSONObject("sw_xmlattr").get("cid").toString();
    JSONObject result = new JSONObject();
    result.put("type", "FeatureCollection");
    JSONArray features = new JSONArray();
    // 遍历获取线路
    for (int i = 0; i < subwayList.size(); i++) {
        JSONObject lineInfo = subwayList.getJSONObject(i);
        //线路基础描述
        JSONObject lineBaseInfo = lineInfo.getJSONObject("l_xmlattr");
        //地铁线路名称
        String lineName = lineBaseInfo.getString("lid");
        // 地铁线路颜色
        String lineColor = lineBaseInfo.getString("lc");
        //站点信息
        JSONArray stationList = lineInfo.getJSONArray("p");
        for (int j = 0; j < stationList.size(); j++) {
            JSONObject stationInfo = stationList.getJSONObject(j).getJSONObject("p_xmlattr");
            if(StringUtils.isNotBlank(stationInfo.getString("sid"))) {
                Map feature = new HashMap<>();
                // 赋值 属性字段
                Map properties = new HashMap<>();
                properties.put("cityname", cityName);
                properties.put("address", lineName);
                properties.put("name", stationInfo.getString("sid")+"(地铁站)");
                properties.put("remark", stationInfo.getString("ln"));
                double x = stationInfo.getDouble("px");
                double y = stationInfo.getDouble("py");
                // 百度墨卡托转百度09
                Map location = BDCRSTools.convertMC2LL(x, y);
                // 百度09转wgs84
                double[] wgs84Point = WGS_Encrypt.bd09ToWgs84(location.get("lng"),location.get("lat"));
                // 赋值 空间字段
                Map geometry = new HashMap<>();
                geometry.put("type", "Point");
                double[] point = new double[2];
                point[0] = wgs84Point[1];
                point[1] = wgs84Point[0];
                properties.put("lng",wgs84Point[1] );
                properties.put("lat", wgs84Point[0]);
                geometry.put("coordinates", point);
                feature.put("type", "Feature");
                feature.put("properties", properties);
                feature.put("geometry", geometry);
                features.add(feature);
            }
        }
    }
    result.put("features",features);
    return result;
}