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_2mi2分钟平均风速米/秒
WIN_D_Avg_2mi2分钟平均风向(角度)
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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值