air-thai-api
Version:
Find PM2.5 around us
51 lines • 1.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const pm25API = "http://air4thai.pcd.go.th/forappV2/getAQI_JSON.php";
/* km unit */
const radGlobe = 6371;
const FindStation = async (input) => {
const { lat, long } = input;
const { stations } = await CollectStation();
let min = 99999;
let closest;
for (let i = 0; i < stations.length; ++i) {
const dif = EquirectangularProjection(lat, long, stations[i].lat, stations[i].long);
if (dif < min) {
closest = i;
min = dif;
}
}
return stations[closest];
};
const EquirectangularProjection = (lat1, long1, lat2, long2) => {
const radLat1 = Deg2Rad(lat1);
const radLon1 = Deg2Rad(long1);
const radLat2 = Deg2Rad(lat2);
const radLon2 = Deg2Rad(long2);
/* Equirectangular projection */
const x = (radLon2 - radLon1) * Math.cos((radLat1 + radLat2) / 2);
const y = radLat2 - radLat1;
const distance = Math.sqrt(x * x + y * y) * radGlobe;
return distance;
};
const Deg2Rad = (deg) => {
return (deg * Math.PI) / 180;
};
const CollectStation = () => {
const config = {
method: "get",
url: pm25API,
};
return axios_1.default(config).then((res) => {
if (res.data)
return res.data;
else
return;
});
};
exports.default = FindStation;
//# sourceMappingURL=findstation.js.map