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 ...
JEDEC JESD85A:2021 METHODS FOR CALCULATING FAILURE RATES IN UNIT
Calculating and Displaying Fatigue Results
9.3.2 Trellis diagram, free distance and asymptotic coding gain 289 9.3.3 The Viterbi decoding algorithm 292 9.3.4 Applications 296 9.4 Turbo codes 296 9.4.1 Turbo encoders 296 9.4.2 Iterative ...
该程序由主脚本文件和函数文件组成。 主脚本文件被命名为 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人工...
4 Calculating Probabilities: Taking Chances 127 5 Discrete Probability Distributions: Manage Your Expectations 197 6 Permutations and Combinations: Making Arrangements 241 7 Geometric, Binomial, and ...
Calculating The Sum of Its Parts 数据结构,树的一道题。
Matlab Tutorial - 33 - Calculating Mean, Median, and Standard Deviation of Data in a Vector
ASTM E2586 - 19 Standard Practice for Calculating and Using Basi
ASTM E2586-2019e1 Standard Practice for Calculating and Using Ba
VMworld 2009 - PA4688:Capital Investments Appraisal for VMware Virtualization Solutions - Calculating TCO-ROI-NVP and IRR for Your Customers
An improved method for calculating the no-fit polygon Hamish T. Dean∗,Yiliu Tu, John F. Raffensperger 200 Armagh Street, P.O. Box 13-761, Christchurch, New Zealand
FOR CALCULATING EULER FOR FINDING MINIMUM DISTANCE
JEDEC JESD85A:2021 METHODS FOR CALCULATING FAILURE RATES IN UNITS OF FITS - 完整英文电子版(30页).pdf
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 ...
Chapter 6 Bodies of information: data, distance and decision-making at the limits of the war prison Chapter 7 Data anxieties: objectivity and difference in early Vietnam War computing Chapter 8 ...
The book starts by showing you how to install several AWS SDKs such as iOS, Java, Node.js, PHP, Python, and Ruby and shows you how to manage objects. Then, you'll be taught how to use the installed ...
Lab3 Parallel implementation of calculating the value of pi using OpenMP (Required) 华中科技大学 并行实验 多线程计算π OpenMP
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 ....