UNPKG

pvtools

Version:

pvtools is a package created during my final degree project in Rey Juan Carlos Universidad, Madrid that contains all the tools you need to deal with Solar energy, and PV power

98 lines (78 loc) 2.3 kB
/** * * * @param {Number} degrees * @returns {Number} the value in Radians */ function degreesToRadians(degrees) { var pi = Math.PI; return degrees * (pi/180); } /** * * @description This function uses Haversine formula * @param {object} point1 * @param {object} point2 * @returns {Number} distance in m */ function getDistance(point1, point2) { //Earth radius var R = 6371; var dLat = degreesToRadians(point2.lat-point1.lat); var dLon = degreesToRadians(point2.lon-point1.lon); console.log(dLat); lat1 = degreesToRadians(point1.lat); lat2 = degreesToRadians(point2.lat); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c * 1000; } /** * * * @param {[]} area * @returns Number */ function getBounds(area) { let minLat = area[0].latitude; let minLong = area[0].longitude; let maxLat = area[0].latitude; let maxLong = area[0].longitude; for (let i = 0; i < area.length; i++) { let y = area[i].latitude, x = area[i].longitude; if (x > maxLong) { maxLong = x; } if (x < minLong) { minLong = x; } if (y > maxLat) { maxLat = y; } if (y < minLat) { minLat = y; } } return {minLat: minLat, maxLat: maxLat, minLng: minLong, maxLng: maxLong }; } function pointInArea (point, area) { var x = point.lat, y = point.lon; var inside = false; for (var i = 0, j = area.length - 1; i < area.length; j = i++) { var xi = area[i].latitude, yi = area[i].longitude; var xj = area[j].latitude, yj = area[j].longitude; var intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); if (intersect) inside = !inside; } return inside; }; module.exports.degreesToRadians = degreesToRadians; module.exports.getDistance = getDistance; module.exports.getBounds = getBounds; module.exports.pointInArea = pointInArea;