This example and java script code is from link text
Look at the section on rhumb lines.
Given a start point and a distance d along constant bearing θ, this will calculate the destination point. If you maintain a constant bearing along a rhumb line, you will gradually spiral in towards one of the poles.
Formula:
α = d/R (angular distance)
lat2 = lat1 + α.cos(θ)
Δφ = ln(tan(lat2/2+π/4)/tan(lat1/2+π/4)) [= the ‘stretched’ latitude difference]
if E:W line q = cos(lat1)
otherwise q = Δlat/Δφ
Δlon = α.sin(θ)/q
lon2 = (lon1+Δlon+π) % 2.π − π
where ln is natural log and % is modulo, Δlon is taking shortest route (<180°), and R is the earth’s radius
JavaScript:
lat2 = lat1 + d*Math.cos(brng);
var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1); // E-W line gives dPhi=0
var dLon = d*Math.sin(brng)/q;
// check for some daft bugger going past the pole, normalise latitude if so
if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -(Math.PI-lat2);
lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI;
I am trying to convert it into php syntax but I am not getting the desired result. I have the latitude part working fine. I also included my test data.
MY PHP CODE
// test data
$R = 6371;
$tlatitude = 50.7;
$tlongitude = -105.214;
$theading = 124;
$d = 50;
$projlat = $tlatitude + rad2deg(($d/$R)*COS(deg2rad($theading)));
//Δφ = ln(tan(lat2/2+π/4)/tan(lat1/2+π/4))
$delta_phi = log(tan(deg2rad($projlat/2) + pi()/4)/(tan(deg2rad($tlatitude/2) + pi()/4)));
//q = Δlat/Δφ
$delta_lat = deg2rad($projlat - $tlatitude);
$q = $delta_lat/$delta_phi;
//Δlon = α.sin(θ)/q
$delta_long = rad2deg($d/$R*sin(deg2rad($theading))/$q);
$projlong = $tlongitude + $delta_long;
I get $projlong = -104.84
according to the referenced page the answer should be -104.63.
Now I am trying to get this to work disregarding the east-west and over the pole possibilities.
I had some problems when making distance calculations where my errors would grow quite a bit after a while. I discovered that if I made a cast to (double) in my code the precision increased. I have not looked in to the C-code in PHP to see what caused this though. I could after this scrap my BC-version of the code.
If you need additional precision please check out the BC-functions in PHP.
http://php.net/manual/en/book.bc.php
Also, please remember that the order that you make calculations in a computer will affect your precision. That is, the calculation bellow
$d/$R*sin(deg2rad($theading))/$q
will not render the same result as
$d*sin(deg2rad($theading))/$q/$R
and this can also give a third result
$d*sin(deg2rad($theading))/($q*$R)
This has to do with the limited precision for numbers close to zero (0) in computers.
javascript has more precision than php
look at this joke http://ru2.php.net/manual/en/function.doubleval.php
I once needed to check some IBAN with the Luhn's algorithm.
My javascript code worked nicely.
But my php failed, so after some research, i found the joke and had to recode basic operations (add, sub, compute, divide, modulo) based on string and not number.
Maybe should you recode it too, to get your expected precision.
We should not use php for high precision calculations.
Related
I want to list the latitude and longitude of itinerary. It could be all points or it could be all points in 1-2 kilometers.
What I'm trying to do is: user selected A as the starting point and B as the ending point. I want to show some places near the road between A and B on the map. But I need a positions for this.
As an example, a JavaScript code is shared here and It is said that this can be done with DirectionsResult Object.
var request = {
origin: start_point,
destination: end_point,
travelMode: google.maps.TravelMode.DRIVING
};
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var path = (response.routes[0].overview_path);
}
});
But I'm trying to do this with php and I have to do this with php.
I read google map api. I've also read the yandex map api, but this seems to be done only with javascript.
Does anyone know a way to do this with php?
From comments I understand the question is to find (using PHP) the intermediate lat,lng pairs that can be extracted from the polyline points in a google directions query.
This is a bit unusual because people normally use the polyline points for map drawing in the browser, and so the JavaScript libraries are well equipped for this task. However, not so in PHP.
The points data appears in the JSON result object as string of ascii characters, sometimes quite long and always 'unreadable'. Into this string is encoded a list of intermediate lat lng pairs between the start and end of each leg. The coding method is presented at the google site https://developers.google.com/maps/documentation/utilities/polylinealgorithm and the algorithm below is just a reversal of that and is commented accordingly.
The example shows a directions find between 2 points, on crescent shaped streets, in Perth, Australia. The start-end points were chosen to encourage multiple intermediate points as would be needed to draw the route. Substitute your own search as needed.
Note that the JSON also provides these fields also at the end of each results object.
"overview_polyline" : {
"points" : "~n{aEmbwaU_B#cCBk#Lo#d#UVOb#Mh#Ab####BBF#DGNABD`#Fh#Pb#VZn#b#d#J"
},
This is much less detailed and less accurate (if you draw will probably depart from actual road lines on map), but can also be decoded in the same way.
The best intermediate points are however, by iterating through the steps using:
"polyline" : {
"points" : "~n{aEmbwaUg##w#?{A?g#BUBUHSJ[XUVOb#Mh#Ab#"
},
Finally, the original source for the algorithm can be found here http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/. So thanks to Peter Chng for this work back in 2008! Peter also acknowledges Mark MClure who did the original coding in JavaScript. I hacked about with and added more comments - to make more aligned with the google recipe, but no more.
I have also just realised there is this link https://github.com/emcconville/google-map-polyline-encoding-tool which (I think but have not tested) provides a class and a CLI tool to do the conversions both ways.
$json = file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=20%20%20Kintyre%20Crescent,%20Churchlands&destination=%2018Kinross%20Crescent,%20Churchlands&key=");
$details = json_decode($json,true);
print_r($details); // show the full result
$points = $details['routes'][0]['legs'][0]['steps'][0]['polyline']['points'];
echo($points); // show the points string for one leg
// show the start and end locations for that leg
print_r($details['routes'][0]['legs'][0]['steps'][0]['start_location']);
print_r($details['routes'][0]['legs'][0]['steps'][0]['end_location']);
// work out the intermdiate points (normally used for drawing)
$decodedPoints= decodePolylinePoints($points);
print_r($decodedPoints); // print out the intermediate points
// This function decodes the polylone points in PHP
function decodePolylinePoints($pointsString)
{
$len = strlen($pointsString);
$latLons = array(); // the output array
$lat = 0; // temp storage for lat and lng
$lng = 0;
$index = 0; // index to curent character
while ($index < $len) // process each lat,lng pair
{
// first build the lat
// NOTE: first lat is an absolute value
// NOTE: subsequent lats are offsets from previous values for coding efficiency
$char = 0; // char as read from points string
$shift = 0; // cumulative shift amount
$value = 0; // temp value during computation
do // Read, convert and shift 5 bit chunks until terminator is reached to get lat
{
$char = ord(substr($pointsString, $index++)) - 63; // return ascii value less 63
$value |= ($char & 0x1f) << $shift; // convert to 5 bit and shift left
$shift += 5; // next shift is 5 extra
}
while ($char >= 0x20); // value of 20 indicates end of lat
$lat += (($value & 1) ? ~($value >> 1) : ($value >> 1)); // convert negative values and save
// now build the lng
// NOTE: first lng is an absolute value
// NOTE: subsequent lngs are offsets from previous values for coding efficiency
$shift = 0;
$value = 0;
do // build up lng from 5 bit chunks
{
$char= ord(substr($pointsString, $index++)) - 63; // return ascii value less 63
$value |= ($char & 0x1f) << $shift; // convert to 5 bit and shift left
$shift += 5; // next shift is 5 extra
}
while ($char >= 0x20); // value of 20 indicates end of lng
$lng += (($value & 1) ? ~($value >> 1) : ($value >> 1)); // convert negative values and save
$latLons[] = array($lat * 1e-5, $lng * 1e-5); // original values were * 1e5
}
return $latLons; // points array converted to lat,lngs
}
I'm working in php with 3D geometries(not the best choice,I know...).
I have K coplanar 3D points, also with x,y,z value. Together they form a polygon. I need to triangulate this polygon. I have already a working delaunay traingulation function which works for 2D Polygons.
So I want to rotate the given points, so that they lay on a plane parallel to the x,y plane. After that I can triangulated it using the x,y values. The following pseudocode shall describe how I want to get to this goal.
I build up the following code with reference on this (I'm usign the answer accepted from the OP): https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d, but it doesn't work as I expected. In order to know if it worked, every mapped point shall then have the same 'z' value.
Here is the question, how do I get the correct rotation matrix? Or did I made a conceptual mistake?
function matrixRotationMapping(Point $p, Point $q, Point $r)
{
$normalPolygon =calculatePlaneNormal($p, $q, $r);
$v = crossProduct($normalPolygon, new Point(0, 0, 1));
$c = dotProduct($normalPolygon, new Point(0, 0, 1));
$matrix = buildRotationMatrix($v, $c);
return $matrix;
}
function buildRotationMatrix($v, $c)
{
$R2 = new Matrix(array(array(1, -$v->z, $v->y), array($v->z, 1, -$v->x), array(-$v->y, $v->x, 1)));
$costant = 1/(1+$c);
$R3 = multiplyMatrices($R2, $R2);
$R3 = multiplyMatricesWithFactor($R3, $costant);
$finalMatrix = sumMatrices($R2, $R3);
return $finalMatrix;
}
function calc2DMapping($points)
{
$rotationMatrix = matrixRotationMapping($points[0], $points[1], $points[2]);
foreach($points as $point)
{
$mappedPoint = $rotationMatrix->multiplyWithPoint($point);
$mappedPoints[] = new MappedPoint($mappedPoint);
}
}
I found another helpful description of the problem, but I wasn't able to implement it: Mapping coordinates from plane given by normal vector to XY plane
Thanks in advance for your attention.
You need basis vectors X,Y,Z first. So let take the mid point A and two distant points to it B,C (not on single line) from your data set first. The X,Y should lie in the plane and Z should be normal to it so:
X = B-A // any non zero vector inside plane
X = X / |X| // unit in size
Y = C-A // any non zero vector inside plane
(X.Y) != 0 // but not parallel to X !!!
Y = Y / |Y| // unit in size
Compute normal to the plane your points lie in and correct Y axis.
Z = X x Y // cross product gives you perpendicular vector
Y = Z x X // now all vectors are perpendicular and unit
So feed these 3 vectors to rotation part of your transform matrix and set origin to A. But as you need to go from your data set to the plane local coordinate you need inverse matrix (or use pseudo inverse based on transposing)
Anyway now with the basis vectors you can map your plane parametrically like this:
P(u,v) = A + u*X + v*Y
Where u,v = <-inf,+inf> are surface distances form A in X,Y directions. That can get handy sometimes. If you need to compute u,v from P then exploit dot product:
u = ((P-A).X) = dot(P-A,X)
v = ((P-A).Y) = dot(P-A,Y)
Which can be also used to transform to 2D instead of using matrix ...
I have a question regarding lattitude and longitude encoding, the answer to which my brain refuses to produce.
I need to write a php function that takes the value '1446041F' and '447D1100' (Lat & Lng) does some processing (the bit I cannot fathom) and outputs '52.062297' and '0.191030'.
I am told the Lat & Lng are encoded to 4 bytes from a signed degrees, minutes and decimal minutes with a format as follows;
Latitude: SDDMM.MMMMM where 0≤DD≤90, S = [+|-], 0≤M≤9
Longitude: SDDDMM.MMMMM where 0≤DDD≤180, S = [+|-], 0≤M≤9
See that last bit, I've searched many sites but I still have no idea what that all means.
I'm aware this is a massive shot in the dark and it may be so simple that I am rightfully told to sit in the corner wearing the dunce hat but I am running low on hair to pull out!
Any advice is much appreciated.
Thanks,
Matthew
The examples you gave, 1446041F and 447D1100 are probably 32-bit signed integers in little-endian byte order.
They are to be read as follows:
1446041F -> 0x1F044614 -> 520373780
447D1100 -> 0x00117D44 -> 001146180
They can be interpreted in degrees and minutes like this:
520373780 -> 52 degrees, 03.73780 minutes
1146480 -> 0 degrees, 11.46480 minutes
The following function will convert the hex values you specified to degrees. I assume the values are integers like 0x447D1100 and the like. If I assume wrong and the input values are actually strings, let me know. I put this function into the public domain.
function hextolatlon($hex){
// Assume hex is a value like 0x1446041F or 0x447D1100
// Convert to a signed integer
$h=$hex&0xFF;
$h=($h<<8)|(($hex>>8)&0xFF);
$h=($h<<8)|(($hex>>16)&0xFF);
$h=($h<<8)|(($hex>>24)&0xFF);
$negative=($h>>31)!=0; // Get the sign
if($negative){
$h=~$h;
$h=$h&0x7FFFFFFF;
$h++;
}
// Convert to degrees and minutes
$degrees=floor($h/10000000);
$minutes=$h%10000000;
// Convert to full degrees
$degrees+=($minutes/100000.0) / 60.0;
if($negative)$degrees=-$degrees;
return $degrees;
}
Here's the PHP (the verbosity is for clarity):
function llconv($hex) {
// Pack hex string:
$bin = pack('H*', $hex);
// Unpack into integer (returns array):
$unpacked = unpack('V', $bin);
// Get first (and only) element:
$int = array_shift($unpacked);
// Decimalize minutes:
$degmin = $int / 100000;
// Get degrees:
$deg = (int)($degmin/100);
// Get minutes:
$min = $degmin - $deg*100;
// Return degress:
return round($deg + ($min/60), 6);
}
$long = '1446041F';
$lat = '447D1100';
$iLong = llconv($long);
$iLat = llconv($lat);
print "Out: $iLong x $iLat\n";
Wow, this one is though!
I'm trying to find in PHP the % of a value relative to the Y axis. If we refer to this graph : http://en.wikipedia.org/wiki/Semi-log_graph (2009 outbreak of influenza A), let's say that I want to find what % is a value "256" on the chart.
Visually, it's easy : it's a bit more than a 1/3 or 33%. If we look at the 1024 value, it's around 50% of the height of the Y axis. 131072 would be 100%.
So how do I calculate this with PHP?
Let's take this graph and take X = day 0 and Y = 256. What is 256 as a % of Y ?
Thanks a lot to anyone can compute this baby :)
percent = 100 * ( log(y) - log(y1) ) / ( log(y2) - log(y1) )
where
y = value
y1 = smallest value in y-axis
y2 = largest value in y-axis.
when y1 = 1.0 then you can simplify the other answers given here (since log(1)=0 by definition)
percent = 100 * log(y)/log(y2)
Note that not all log charts have 1.0 as the lowest value.
ln(131072) = 11.783 is 100%
ln(1024) = 6.931 is 58.824%
in PHP, that function is called log()
no need to set the base, as you are dividing them to find the relative value.
Alex got it, but to generalize for you and PHPize
logPercent = log(x) / log(top) * 100;
If you take the log of your max y value (131072 in your case) and the log of your y value (256), you end up with a height and a y value which is linear in relation to your drawn axis. you can divide them to get a decimal of the height and times by 100 for %:
using log base 2 seen as it gives integers (though any base should be fine).
log(256) / log(131072) = 8/17 = 0.47 = 47%
in php:
(log(256, 2) / log(131072, 2))*100;
I have 2 coordinates. Coordinate 1 is a 'person'. Coordinate 2 is a destination.
How do I move coordinate 1 100 meters closer to coordinate 2?
This would be used in a cron job, so only php and mysql included.
For example:
Person is at: 51.26667, 3.45417
Destination is: 51.575001, 4.83889
How would i calculate the new coordinates for Person to be 100 meters closer?
Use Haversine to calculate the difference between the two points in metres; then adjust the value of the person coordinates proportionally.
$radius = 6378100; // radius of earth in meters
$latDist = $lat - $lat2;
$lngDist = $lng - $lng2;
$latDistRad = deg2rad($latDist);
$lngDistRad = deg2rad($lngDist);
$sinLatD = sin($latDistRad);
$sinLngD = sin($lngDistRad);
$cosLat1 = cos(deg2rad($lat));
$cosLat2 = cos(deg2rad($lat2));
$a = ($sinLatD/2)*($sinLatD/2) + $cosLat1*$cosLat2*($sinLngD/2)*($sinLngD/2);
if($a<0) $a = -1*$a;
$c = 2*atan2(sqrt($a), sqrt(1-$a));
$distance = $radius*$c;
Feeding your values of:
$lat = 51.26667; // Just South of Aardenburg in Belgium
$lng = 3.45417;
$lat2 = 51.575001; // To the East of Breda in Holland
$lng2 = 4.83889;
gives a result of 102059.82251083 metres, 102.06 kilometers
The ratio to adjust by is 100 / 102059.82251083 = 0.0009798174985988102859004569070625
$newLat = $lat + (($lat2 - $lat) * $ratio);
$newLng = $lng + (($lng2 - $lng) * $ratio);
Gives a new latitude of 51.266972108109 and longitude of 3.4555267728867
Find the angle theta between the x-axis and the vector from person to destination.
theta = Atan2(dest.y-person.y, dest.x-person.x).
Now use theta and the amount you want to advance the point to calculate the new point.
newPoint.x = advanceDistance * cos(theta) + person.x
newPoint.y = advanceDistance * sin(theta) + person.y
If you understand JavaScript, you may want to check out the moveTowards() method in the following Stack Overflow post:
How to add markers on Google Maps polylines based on distance along the line?
This method returns the destination point when given a start point, an end point, and the distance to travel along that line. You can use point 1 as the starting point, point 2 as the end point, and a distance of 100 meters. It's written in JavaScript, but I'm sure it can be easily ported to PHP or MySQL.
You may also want to check out this other Stack Overflow post which implements a part of the above JavaScript implementation, as a user defined function for SQL Server 2008, called func_MoveTowardsPoint:
Moving a Point along a Path in SQL Server 2008
The above uses SQL Server 2008's in-built geography data type. However you can easily use two decimal data types for latitude and longitude in place of the single geography data type.
Both the SQL Server and the JavaScript examples were based on implementations from Chris Veness's article Calculate distance, bearing and more between Latitude/Longitude points.