how to separate coordinates to groups of nearby coordinates - coordinates

I have coordinates (lattitude and longitude) of some cities and I want to create groups of nearby cities. You can see illustration:
Do you know some tips, how can I do it?

There is an example script at the bottom of this page to find the distance between the coordinates.
http://www.movable-type.co.uk/scripts/latlong.html
If you worked out the distance for each pair of cities you could then use something like k-means clustering to create groups.

narrow down on closely cities. I.e. Lat < pos_lat + d && lat > pos_lat - d && lon < pos_lon + d && pos_lon > pos -d
Narrow down on closest to farthest by calc distance where
d = sqrt( (pos_lat - lat)^2 + (pos_lon - lon)^2)

Related

Compute coordinates position with projection

Given 2 coordinates (point 1 and 2 in red) in WGS84 I need to find the coordinates of the point perpendicular (point 3) to the line at a given distance.
I could manage to make the math to compute this perpendicular point, but when displayed on the map, the point seems to be at a wrong place, probably because of the projection.
What I want on a map:
And what I have instead on the map:
How can I take into account the projection so that the point on the map appears perpendicular to the line? The algorithm below to compute the point comes from here: https://math.stackexchange.com/questions/93424/calculate-rectangle-coordinates-from-line-and-height
public static Coords ComputePerpendicularPoint(Coords first, Coords last, double distance)
{
double slope = -(last.Lon.Value - first.Lon.Value) / (last.Lat.Value - first.Lat.Value);
// number of km per degree = ~111km (111.32 in google maps, but range varies between 110.567km at the equator and 111.699km at the poles)
// 1km in degree = 1 / 111.32km = 0.0089
// 1m in degree = 0.0089 / 1000 = 0.0000089
distance = distance * 0.0000089 / 100; //0.0000089 => represents around 1m in wgs84. /100 because distance is in cm
double t = distance / Math.Sqrt(1 + (slope * slope));
Coords perp_coord = new Coords();
perp_coord.Lon = first.Lon + t;
perp_coord.Lat = first.Lat + (t * slope);
return perp_coord;
}
Thank you in advance!

Reading Data from Firestore

I am creating an app where a user can create an event and see other events created by other users only if there are in a 10Km Radius. I am storing all the data in firestore.
This is how the app works, from the user side all the events are fetched and only those events are displayed whose distance is less than 10km.
The problem is if there are 10,000 events in the database and the user is in the 10km radius of only 10 events then obviously it will count as 10,000 reads which is too expensive.
Any suggestions for this problem?
One solution that I have in mind is to store data according to the geographical area but how to implement it is another problem.
You won't be charge for 10 000 reads but only the documents retrieved by your query, ten in your example.
Here's a good video from Firebase explaining their billing system : https://www.youtube.com/watch?time_continue=224&v=6NegFl9p_sE
Also keep in mind that for queries other than document reads, such as a request for a list of collection IDs, you are billed for one document read.
I suggest computing min and max longitude corresponding to 10 km distance from user longitude. You do the same with latitude and use those limits in Firestore query. Doing so You will have less events/reads and you can compute the exact distance to suppress events between 10 and 14 km radius... if necessary.
To compute your limit you can use the following formula from https://www.movable-type.co.uk/scripts/latlong.html (Destination point given distance and bearing from start point) and there's an online javascript calculator you can study.
φ2 = asin( sin φ1 ⋅ cos δ + cos φ1 ⋅ sin δ ⋅ cos θ )
λ2 = λ1 + atan2( sin θ ⋅ sin δ ⋅ cos φ1, cos δ − sin φ1 ⋅ sin φ2 )
where φ is latitude, λ is longitude, θ is the bearing (clockwise from north), δ is the angular distance d/R; d being the distance traveled in meters, R the earth’s radius (6371e3).
With bearing 0° you obtain LatitudeMax
With bearing 90° you obtain longitudeEast
With bearing 180° you obtain LatitudeMin
With bearing 270° you obtain longitudewest
Since earth is a sphere with longitude between -180° and 180°
longitudeMin = Min(longitudeEast, longitudeWest)
longitudeMax = Max(longitudeEast, longitudeWest)
And The Firestore part is like :
CollectionReference col = Firestore.instance.collection("mycollection");
Query latitudeMinQuery = col.where('latitude', isGreaterThan: LatitudeMin);
Query latitudeMaxQuery = latitudeMinQuery.where('latitude', isLessThan: LatitudeMin);
Query longitudeMinQuery = latitudeMaxQuery.where('longitude', isGreaterThan: LongitudeMin);
Query longitudeMaxQuery = longitudeMinQuery.where('latitude', isLessThan: LongitudeMax);
https://stackoverflow.com/a/43804487/9139407 (answers by #alex-mamo)
Hopes it helps!

Matlab GPS + distance in meters

I have to calculate for a given Latitude (lat0) and Longitude (lon0) the new latitude (lat1) and longitude (lon1) if I move a distance x[m] and y[m] away from the initial position. For example:
clear all; clc;
%initial coordinates:
lat0=56;
lon0=5;
%moving away from lat0,lon0,
xcor=200; %[m]
ycor=100; %[m]
First I use this code to calculate lat1 and lon1: (source: Adding distance to a GPS coordinate)
lat1=lat0+rad2deg((xcor/6372800))
lon1=lon0+rad2deg((ycor/6372800)/(cos(lat0)))
distance=sqrt(xcor^2+ycor^2)
Now i want to check my answer with the haversine equation:
dlat = deg2rad(lat1-lat0);
dlon = deg2rad(lon1-lon0);
lat0 = deg2rad(lat0);
lat1 = deg2rad(lat1);
a = (sin(dlat./2)).^2 + cos(lat0) .* cos(lat1) .* (sin(dlon./2)).^2;
c = 2 .* asin(sqrt(a));
distance_check=6372800*c
However, most of the time the distance from haversine is different with 100+ meters compared to the calculated distance in the first 3 lines of code.
What is going wrong in this code?
In the first code, 2nd line, cos(lat0) has to be cos(deg2rad(lat0)).

Calculate Distance in Km and Miles

I have two points whose latitude and longitude i know.
How can i calculate the distance(in Km and Miles) between them. What is the formulae?
You can use the haversine formula to calculate such distances.
Use the haversine Formula for this...
Here is the link having java script code to calculate distance
http://www.movable-type.co.uk/scripts/latlong.html
A = LAT1, B = LONG1
C = LAT2, D = LONG2 (all converted to radians: degree/57.29577951)
IF A = C AND B = D THEN DISTANCE = 0;
ELSE
IF [SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)] > 1 THEN DISTANCE = 3963.1*ARCOS[1];
ELSE
DISTANCE=3963.1*ARCOS[SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)];
For an accurate and complete (works with any pair of points) solution
use my geodesic calculator at
http://geographiclib.sf.net/cgi-bin/GeodSolve. The formulas are given in
http://arxiv.org/abs/1102.1215.

moving CGPoint a certain distance along a certain heading...iphone

this seems like such a simple problem but I have been unable to find an answer (and im no good at math). I am trying to move a UIView to a new CGPoint X distance away along a certain heading. What is the formula for determining the new coordinates?
(i do no want this to be animated, just an instantaneous move)
something like:
x = 100; (current x value)
y = 150; (current y value)
d = 25; (distance to move the point)
h = 90; (west)
\\\ insert formula to determine new x,y coords
self.car.center = (CGPointMake ([newX],[newY]);
If p is your point, D is the distance, and θ is the heading-angle from the X-axis,
pnew.x = pold.x + D * cos(θ)
pnew.y = pold.y + D * sin(θ)
Rather than storing distances and angles, though, this is usually done using vectors (which removes the need for the sin/cos)