This month I’ve been programming quite a bit in PHP and MySQL with respect to GIS. Snooping around the net, I actually had a hard time finding some of the Geographic calculations to find the distance between two locations so I wanted to share them here.
If you remember ‘the old days’ of calculating a distance between two points, it was simply the hypotenuse of a triangle (A² + B² = C²).
That’s an interesting start but it doesn’t apply with Geography since the distance between lines of latitude and longitude are not an equal distance apart. As you get closer to the equator, lines of latitude get further apart. If you use some kind of simple triangulation equation, it may measure distance accurately in one location and terribly wrong in the other, because of the curvature of the Earth.
That brings up the Haversine formula, which uses trigonometry to allow for the curvature of the earth. When you’re finding the distance between 2 places on earth (as the crow flies), a straight line is really an arc. This is applicable in air flight – have you ever looked at the actual map of flights and noticed they are arched? That’s because it’s shorter to fly in an arch between two points sometimes than directly to the location.
Anyways, here’s the PHP formula for calculating the distance between two points (along with Mile vs. Kilometer conversion) rounded to two decimal places:function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515; switch($unit) {
case 'Mi': break; case 'Km' : $distance = $distance * 1.609344;
}
return (round($distance,2));
}
It’s also possible to use MySQL to do a calculation to find all records within a specific distance. In this example, I’m going to query MyTable to find all the records that are less than or equal to variable $distance (in Miles) to my location at $latitude and $longitude:$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance
FROM `MyTable`
WHERE distance >= ".$distance."
For Kilometers:$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance
FROM `MyTable`
WHERE distance >= ".$distance."
I utilized similar computations in the Wild Birds Unlimited mapping platformthat we built.
相关推荐
由复杂应力应变实验曲线求材料常数,肖建华,,In many experimental cases, the stress is directly measured by the stress-sensor mounted on force action face and the strain is directly measured by the ...
JESD85A标准,全称为“2021 Methods for Calculating Failure Rates in Units”,是JEDEC发布的一个关键文档,旨在为电子设备的可靠性评估提供一种统一的方法,特别是针对故障率(Failure In Time, FITs)的计算。...
该程序由主脚本文件和函数文件组成。 主脚本文件被命名为 myCameraCalibration。 这也包括在我的笔记本电脑的网络摄像头以640x480分辨率拍摄的棋盘格图像中。 该程序将根据棋盘图像上的像素计算点,即图像坐标及其...
matlab导入excel代码utl_calculating_the_rolling_product_using_wps_sas_and_r 使用wps sas和r计算滚动产品。 关键字:sas sql join合并大数据分析宏oracle teradata mysql sas社区stackoverflow statistics人工...
### 计算与显示疲劳结果 #### 引言 疲劳分析是材料工程中一个非常重要的领域,它关注的是材料在循环载荷作用下发生破坏的可能性。ANSYS疲劳模块提供了广泛的功能来执行计算并呈现分析结果。本文将详细介绍该模块...
在IT领域,特别是计算机科学和算法设计中,"Calculating The Sum of Its Parts" 这一主题涉及到数据结构和树的运用。这个问题的核心是通过树的结构计算节点的总和,可能涉及到对树的遍历、节点值的累加以及查询处理...
Matlab Tutorial - 33 - Calculating Mean, Median, and Standard Deviation of Data in a Vector
git clone https://github.com/lucioerlan/calculate-distance-matrix.git$ cd calculate-distance-matrix/client -and- calculate-distance-matrix/server$ npm install 注意事项: 正确格式化 .csv 文件,在 /...
标题 "ASTM E2586 - 19 Standard Practice for Calculating and Using Basic Statistics" 涉及的是一个由美国材料与试验协会(ASTM)制定的行业标准,该标准详细阐述了如何计算和应用基本统计学方法。这个标准特别适用...
FOR CALCULATING EULER FOR FINDING MINIMUM DISTANCE
You will learn how to manipulate data in various ways by applying various filters, logic, and calculating various aggregate measures. Finally, in the third module, you learn about Tableau Public ...
This software used for calculating pcb array size and pcb panel size
Lab3 Parallel implementation of calculating the value of pi using OpenMP (Required) 华中科技大学 并行实验 多线程计算π OpenMP
However, calculating a global weather forecast and hosting a database impose different requirements on an operating system. Linux must accommodate all possible usage scenarios with optimal ...
- **Distance Metric**: The document distance metric is defined as the inner product of the vectors \( D_1 \) and \( D_2 \) containing the word frequencies for all words in the two documents....
在VMworld 2009大会上,John Churchhouse,VMware公司英国与爱尔兰高级经理,发表了关于"Capital Investments Appraisal for VMware Virtualization Solutions - Calculating TCO-ROI-NVP and IRR for Your ...
Table of Contents Preface, Notes, Licenses . . . . . . . . ....1. Licenses for Third-Party Components ....1.1. FindGTest.cmake License ....1.2. LPeg Library License ....1.3. LuaFileSystem Library License ....