Java 獲取詳細的天氣數據

中國氣象數據網API接口:http://data.cma.cn/Market/Detail/code/A.0012.0001/type/1.html

注意:需註冊,註冊後需一至兩天審覈通過。可免費試用7天。

China_SURF_Element.xls是可以採集的各種詳細數據。適合研究和效果開發。

分類 要素編碼 要素名稱 單位
基本信息 Station_Id_C 區站號/觀測平臺標識 (字符)  
Year
Mon
Day
Hour 時次
觀測要素 PRS 氣壓 百帕
PRS_Sea 海平面氣壓 百帕
PRS_Max 最高氣壓 百帕
PRS_Min 最低氣壓 百帕
TEM 溫度/氣溫 攝氏度(℃)
TEM_Max 最高氣溫 攝氏度(℃)
TEM_Min 最低氣溫 攝氏度(℃)
RHU 相對溼度 百分率
RHU_Min 最小相對溼度 百分率
VAP 水汽壓 百帕
PRE_1h 降水量 毫米
WIN_D_INST_Max 極大風速的風向(角度) 字符
WIN_S_Max 最大風速 米/秒
WIN_D_S_Max 最大風速的風向(角度)
WIN_S_Avg_2mi 2分鐘平均風速 米/秒
WIN_D_Avg_2mi 2分鐘平均風向(角度)
WEP_Now 現在天氣  
WIN_S_Inst_Max 極大風速 米/秒
tigan 體感溫度 攝氏度(℃)
windpower 風力  
VIS 水平能見度(人工)
CLO_Cov 總雲量 百分率
CLO_Cov_Low 低雲量 百分率
CLO_COV_LM 雲量(低雲或中雲) 百分率

China_SURF_Station.xls中是全國2000多個測象站的名稱、經緯度等信息。

特徵值說明.docx是511中天氣陰晴情況(對應接口參數WEP_Now)的說明。

可以通過接口獲取數據後打印成\t格式,可以將結果直接粘貼到Excel中,自動按照表格排列。

package com.weather.select.data;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weather.select.util.HttpUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZhangHao
 * @Description: 地點天氣
 * @Date: 2020/6/16 15:22
 * @Version: 1.0
 */
public class PoliceWeatherDataReader {
    private static PoliceWeatherDataReader ourInstance = new PoliceWeatherDataReader();

    private List<PoliceWeather> policeWeatherList;

    public List<PoliceWeather> getPoliceWeatherList() {
        return policeWeatherList;
    }

    public static PoliceWeatherDataReader getInstance() {
        return ourInstance;
    }

    private PoliceWeatherDataReader() {
        initPoliceWeatherDataReader();
    }

    private void initPoliceWeatherDataReader() {
        policeWeatherList = new ArrayList<>();
        Map<String, Police> policeMap = PoliceDataReader.getInstance().getPoliceMap();
        Collection<Police> policeCollection = policeMap.values();
        List<Police> policeList = new ArrayList<>(policeCollection);
        int policeListSize = policeList.size();
        System.out.println("省\t城市\t經度\t緯度\t氣壓/百帕\t海平面氣壓/百帕\t最高氣壓/百帕\t最低氣壓/百帕\t溫度/氣溫/攝氏度(℃)\t最高氣溫/攝氏度(℃)\t最低氣溫/攝氏度(℃)\t相對溼度/百分率\t最小相對溼度/百分率\t水汽壓/百帕\t降水量/毫米\t極大風速的風向(角度)/度\t最大風速/米/秒\t最大風速的風向(角度)/度\t2分鐘平均風速/米/秒\t2分鐘平均風向(角度)/度\t現在天氣\t極大風速/米/秒\t體感溫度/攝氏度(℃)\t風力\t水平能見度(人工)/米\t總雲量/百分率\t低雲量/百分率\t雲量(低雲或中雲)/百分率");
        for (int policeIndex = 0; policeIndex < policeListSize; ) {
            StringBuilder policeGroupStringBuilder = new StringBuilder();

            // 每次查詢最多支持30個站點
            int policeGroupSize = 30;
            int remainPoliceNumber = policeListSize - policeIndex;
            if (remainPoliceNumber < 30) {
                policeGroupSize = remainPoliceNumber;
            }

            for (int policeInGroupIndex = 0; policeInGroupIndex < policeGroupSize; policeInGroupIndex++) {
                policeGroupStringBuilder.append(policeList.get(policeIndex).getId()).append(policeIndex == policeList.size() - 1 ? "" : ",");
                policeIndex++;
            }

            String userId = "中國地面氣象站逐小時觀測資料API接口選購後被分配的賬號";
            String pwd = "中國地面氣象站逐小時觀測資料API接口選購後被分配的賬號";
            
            // [20200615000000,20200616000000]爲時間段,注意:[20200615000000,20200615240000]是錯誤的
            // Station_Id_C,Year,Mon,Day,Hour是默認的,不能修改
            // PRE_1h表示1h降水
            String httpAPI = "http://api.data.cma.cn:8090/api?userId="+userId+"&pwd="+pwd+"&dataFormat=json&interfaceId=getSurfEleByTimeRangeAndStaID&dataCode=SURF_CHN_MUL_HOR&timeRange=[20200615000000,20200616000000]&staIDs=" + policeGroupStringBuilder.toString() + "&elements=Station_Id_C,Year,Mon,Day,Hour,PRS,PRE_1h,PRS_Sea,PRS_Max,PRS_Min,TEM,TEM_Max,TEM_Min,RHU,RHU_Min,VAP,PRE_1h,WIN_D_INST_Max,WIN_S_Max,WIN_D_S_Max,WIN_S_Avg_2mi,WIN_D_Avg_2mi,WEP_Now,WIN_S_Inst_Max,tigan,windpower,VIS,CLO_Cov,CLO_Cov_Low,CLO_COV_LM";
            String resultJSONString = HttpUtil.sendGet(httpAPI);
            JSONObject resultJSONObject = JSONObject.parseObject(resultJSONString);
            JSONArray DSJSONArray = resultJSONObject.getJSONArray("DS");
            for (JSONObject pliceWeatherJSONObject : DSJSONArray.toJavaList(JSONObject.class)) {
                // 取全天中12點的天氣
                if ("12".equals(pliceWeatherJSONObject.getString("Hour"))) {
                    PoliceWeather policeWeather = new PoliceWeather();
                    Police police = policeMap.get(pliceWeatherJSONObject.getString("Station_Id_C"));
                    policeWeather.setPolice(police);
                    Weather weather = new Weather();
                    weather.setPRS(Double.parseDouble(pliceWeatherJSONObject.getString("PRS")));
                    weather.setPRS_Sea(Double.parseDouble(pliceWeatherJSONObject.getString("PRS_Sea")));
                    weather.setPRS_Max(Double.parseDouble(pliceWeatherJSONObject.getString("PRS_Max")));
                    weather.setPRS_Min(Double.parseDouble(pliceWeatherJSONObject.getString("PRS_Min")));
                    weather.setTEM(Double.parseDouble(pliceWeatherJSONObject.getString("TEM")));
                    weather.setTEM_Max(Double.parseDouble(pliceWeatherJSONObject.getString("TEM_Max")));
                    weather.setTEM_Min(Double.parseDouble(pliceWeatherJSONObject.getString("TEM_Min")));
                    weather.setRHU(Double.parseDouble(pliceWeatherJSONObject.getString("RHU")));
                    weather.setRHU_Min(Double.parseDouble(pliceWeatherJSONObject.getString("RHU_Min")));
                    weather.setVAP(Double.parseDouble(pliceWeatherJSONObject.getString("VAP")));
                    weather.setPRE_1h(Double.parseDouble(pliceWeatherJSONObject.getString("PRE_1h")));
                    weather.setWIN_D_INST_Max(Double.parseDouble(pliceWeatherJSONObject.getString("WIN_D_INST_Max")));
                    weather.setWIN_S_Max(Double.parseDouble(pliceWeatherJSONObject.getString("WIN_S_Max")));
                    weather.setWIN_D_S_Max(Double.parseDouble(pliceWeatherJSONObject.getString("WIN_D_S_Max")));
                    weather.setWIN_S_Avg_2mi(Double.parseDouble(pliceWeatherJSONObject.getString("WIN_S_Avg_2mi")));
                    weather.setWIN_D_Avg_2mi(Double.parseDouble(pliceWeatherJSONObject.getString("WIN_D_Avg_2mi")));
                    weather.setWEP_Now(pliceWeatherJSONObject.getString("WEP_Now"));
                    weather.setWIN_S_Inst_Max(Double.parseDouble(pliceWeatherJSONObject.getString("WIN_S_Inst_Max")));
                    weather.setTigan(Double.parseDouble(pliceWeatherJSONObject.getString("tigan")));
                    weather.setWindpower(Double.parseDouble(pliceWeatherJSONObject.getString("windpower")));
                    weather.setVIS(Double.parseDouble(pliceWeatherJSONObject.getString("VIS")));
                    weather.setCLO_Cov(Double.parseDouble(pliceWeatherJSONObject.getString("CLO_Cov")));
                    weather.setCLO_Cov_Low(Double.parseDouble(pliceWeatherJSONObject.getString("CLO_Cov_Low")));
                    weather.setCLO_COV_LM(Double.parseDouble(pliceWeatherJSONObject.getString("CLO_COV_LM")));
                    policeWeather.setWeather(weather);
                    System.out.println(police.getProvince() + "\t" + police.getStation() + "\t" + police.getLongitude() + "\t" + police.getLatitude() + "\t" + weather.getPRS() + "\t" + weather.getPRS_Sea() + "\t" + weather.getPRS_Max() + "\t" + weather.getPRS_Min() + "\t" + weather.getTEM() + "\t" + weather.getTEM_Max() + "\t" + weather.getTEM_Min() + "\t" + weather.getRHU() + "\t" + weather.getRHU_Min() + "\t" + weather.getVAP() + "\t" + weather.getPRE_1h() + "\t" + weather.getWIN_D_INST_Max() + "\t" + weather.getWIN_S_Max() + "\t" + weather.getWIN_D_S_Max() + "\t" + weather.getWIN_S_Avg_2mi() + "\t" + weather.getWIN_D_Avg_2mi() + "\t" + weather.getWEP_Now() + "\t" + weather.getWIN_S_Inst_Max() + "\t" + weather.getTigan() + "\t" + weather.getWindpower() + "\t" + weather.getVIS() + "\t" + weather.getCLO_Cov() + "\t" + weather.getCLO_Cov_Low() + "\t" + weather.getCLO_COV_LM());
                    policeWeatherList.add(policeWeather);
                }
            }
        }

    }

    public static void main(String[] args) {
        PoliceWeatherDataReader.getInstance().getPoliceWeatherList();
    }
}

完整工程下載:https://download.csdn.net/download/haoranhaoshi/12548292

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章