I am completely new to GeoDjango, and I am trying to calculate distances between objects.
I am doing everything as per this post https://coderwall.com/p/k1gg1a/distance-calculation-in-geodjango , but my results differ from what Google Maps tell me.
When I try to calculate distance between London and Paris, I get this
london = GEOSGeometry('POINT(-0.056922 51.480415)', srid=4326)
paris = GEOSGeometry('POINT(2.350918 48.867744)', srid=4326)
london = london.transform(900913, clone=True)
paris = paris.transform(900913, clone=True)
print(london.distance(paris))
>>> 527450.6633622452
Which looks like it's about 527 km.
However, Google Maps tell me that the distance is 341 km:
What am I doing wrong?
You're not doing anything wrong. Google maps is (I think) returning the Great Circle Distance while GEOS (which is an acronym for Geometry Engine - Open Source) is returning the Euclidean distance (GEOS is more about geometry and topology than geography). If you want to approximate the Great Circle Distance, you could try the Haversine Formula with the following code taken from this question:
>>>from math import radians, cos, sin, asin, sqrt
...
>>>def haversine(lon1, lat1, lon2, lat2):
... # convert decimal degrees to radians
... lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
... # haversine formula
... dlon = lon2 - lon1
... dlat = lat2 - lat1
... a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
... c = 2 * asin(sqrt(a))
... r = 6371 # Radius of earth in kilometers. Use 3956 for miles
... return c * r
>>>haversine(-0.056922, 51.480415, 2.350918, 48.867744)
>>>337.303...
Related
I have a 3D space where I can move my spaceship right/left and up/down with mouse. I'm able to calculate speed of the spaceship on a plane, having the facing (alfa angle), like:
float cosf = cos(alfa * M_PI/180.0);
float sinf = sin(alfa * M_PI/180.0);
vxm = + cosf*vx - sinf*vz;
vym = vy;
vzm = + sinf*vx + cosf*vz;
However I'm having trouble when adding a Beta angle (the vertical facing of ship), how can I calculate the three velocities on each axis, having Alfa and Beta angle?
If magnitude of velocity is V and Beta is angle between V direction and OXY plane, then
v_z = V * Sin(Beta)
v_x = V * Cos(Beta) * Cos(Alpha)
v_y = V * Cos(Beta) * Sin(Alpha)
How can I find the coordinates of P ? I've seen other posts in 2D and also in 3D but they say that I need 3 angles for 3D and some say I only need two but I dont understand which ones, I suck at math.
All See this image, I have is those two angles and the distance between B and P
To define a vector in 3D, given its length, you need 2 angles. These 3 coordinates (length + 2 angles) are named "spherical coordinates". There are 3 conventions for defining such angles. The most common one is the radius-elevation-azimuth. In this convention, expression of the cartesian coordinates of the vector, given the radius, elevation angle and azimuth angle are:
x = radius * sin(elevation) * cos(azimuth)
y = radius * sin(elevation) * sin(azimuth)
z = radius * cos(elevation)
Let's say I have two latitude,longitude pairs. (for example, (10.786377, 106.700292), (10.787126, 106.725805)). I know these are 2.789 km apart.
I have a function
def within(latitude, longitude, radius) do
point2 = %Geo.Point{coordinates: {latitude, longitude}}
query = from post in Post, where: st_distance(post.location, ^point2) < ^radius, select: post
Repo.all(query)
end
That radius unit is not meters.
With our two original points, this is the smallest distance I could get to include the other point:
Post.within(10.786377, 106.700292, 0.025523999)
That's about ~109.26 km per unit here. 1 degree at 106.700292 longitude is 111601 m x 0.025523999 = 2,848 m which is close to the known 2.789 km difference.
Is there a way to query based on meters, not degrees at a given latitude?
Figured it out.
I was using :geometry as my type in migration. You need to use :geography
I have two variables which define a static longitude and latitude. I want to be able to define a multi dimensional array which holds the following: id, longitude and latitude.
What i want to achieve - from the static longitude to feed through the loop of arrays and if finds something within a radius of 50 miles (or whatever), it selects the potential ones and lists them.
I am not sure what formula or algorithm i can use to check the radius and bring back the nearest values.
Please note: not using databases in this example.
So far:
$mainLong = 57.7394571;
$mainLat = -4.386997;
$main = array(
array("loc_1",57.7394571,-4.686997),
array("loc_2",51.5286416,-0.1015987),
array("loc_3",51.2715146,-0.3953564),
array("loc_4",50.837418,-0.1061897)
);
foreach ( $main as $key => $value ) {
if($value[1] == $mainLong){
print_r($value);
}
}
Haversine formula could help.
Here is a sample algorithm.
You can try it out.
%% Begin calculation
R = 6371; % Earth's radius in km
delta_lat = locs{2}(1) - locs{1}(1); % difference in latitude
delta_lon = locs{2}(2) - locs{1}(2); % difference in longitude
a = sin(delta_lat/2)^2 + cos(locs{1}(1)) * cos(locs{2}(1)) * ...
sin(delta_lon/2)^2;
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c; % distance in km
%% Convert result to nautical miles and miles
nmi = km * 0.539956803; % nautical miles
mi = km * 0.621371192; % miles
I have began working on an autonomous rc helicopter in c. I need help finding a way to calculate the bearing acuratley. I am using two sets of coordinates (latitude and longitude), one is the current location and the other is the destination point. I have converted the latitude and longitude into decimal format so....
40°58'19.86"N = 40.972183
74°14'52.74"W = 74.247983
Can anyone show me code in c to find the bearing or a formula i can use?
i have looked at: http://www.movable-type.co.uk/scripts/latlong.html and when i transfer it to c, the results make no sense.
This is what i have tried:
double x = Sin(Longitude2 - Longitude1) * Cos(Latitude2);
double y = Cos(Latitude1) * Sin(Latitude2) - Sin(Latitude1) * Cos(Latitude2) * Cos(Longitude2 - Longitude1);
double heading = (Atan2(x, y) % 2 * 3.14159265) (180/3.14159265);
Have you converted your coordinates from degrees to radians before calculations ?
angleRad = 3.14159265 * angleDeg / 180;
And
bearing = Atan2(y, x);
in that website.
The bearing should be converted from rad to deg in turn:
bearing = 180 * bearing / 3.14159265;
and in case of negative value eventually:
bearing = bearing + 360;
I don't know how to write this using the convention above (with "%").
Latitude1 and Longitude1 are coordinates of the observer
Latitude2 and Longitude2 are coordinates of destination point.
Formulas from that website work well.
For the very small area you are considering you can assume the world is flat.
If you also only need this to work locally you can pre-calculate the value of a deg lat/lon on the ground in metres (or ft)
Then simply convert the delta lat/lon into distance and do the normal trig.
To convert distance to lat/lon you can either use google earth to measure it or
How to convert latitude or longitude to meters?