Calculation of proximity between of one location to many other locations - c#

I have a database with a list of latitude and longitude
-DeviceName
-Latitude
-Longitude
Given my current device's latitude and longitude, I want to get all devices within the database list in distance/proximity of X kilometres.
How do I calculate the proximity of my location vs other locations?

I think you want to have a peep at this amazing presentation. it will tell you how to use (and for bonus points explains!) the haversine formula to calcuate distances on the surface of the earth accounting for curviture and how to avoid some common mistakes in your database queries etc. His dataset is pretty much exactly what yours is - item, longitude and latitude.

If you're after the raw code, this should help you:
private static Double rad2deg(Double rad) {
return (rad / Math.PI * 180.0);
}
private static Double deg2rad(Double deg) {
return (deg * Math.PI / 180.0);
}
private const Double kEarthRadiusKms = 6376.5;
private static Double CalculateDistance(Double latitude1, Double longitude1, Double latitude2, Double longitude2) {
double theta = longitude1 - longitude2;
double dist = Math.Sin(deg2rad(latitude1)) * Math.Sin(deg2rad(latitude2)) + Math.Cos(deg2rad(latitude1)) * Math.Cos(deg2rad(latitude2)) * Math.Cos(deg2rad(theta));
dist = Math.Acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
return (dist);
}

Related

How do I change the heading point of compass? [duplicate]

I want to calculate bearing between 2 GPS positions, I foollowed this page recommandations for my algorythm:
public static double Bearing(IPointGps pt1, IPointGps pt2)
{
double x = Math.Cos(pt1.Latitude) * Math.Sin(pt2.Latitude) - Math.Sin(pt1.Latitude) * Math.Cos(pt2.Latitude) * Math.Cos(pt2.Longitude - pt1.Longitude);
double y = Math.Sin(pt2.Longitude - pt1.Longitude) * Math.Cos(pt2.Latitude);
// Math.Atan2 can return negative value, 0 <= output value < 2*PI expected
return (Math.Atan2(y, x) + Math.PI * 2)%(Math.PI * 2);
}
Then I transform my value in degrees using this method
public static double RadiansToDegrees(double angle)
{
return (angle * 180.0) / Math.PI;
}
I have the following test sample:
Point1 (lat, long) = 43.6373638888888888888888888888889, 1.35762222222222222222222222222222
Point2 (lat, long) = 43.6156444444444444444444444444444,1.380225
Expected bearing = 323°
However, I obtain a bearing of 315.5° (5.5062235835910762 rad). If i calculate the expected radian value, i get 5.637413 which leaves no doubt that my problem lies in my bearing method.
I already implemented other computation methods using .Net Math package (including Cos, Sin, Tan and ATan methods) and my unit tests pass with 1e-12 precision. What am I missing?
PS: I also tryied to reimplement the Atan2 method in case there is a lack of precision in it. I obtain the very same result
edit: My Latitude and Longitude are double as per the following interface
public interface IPointGps
{
double Latitude { get; }
double Longitude { get; }
}
Math.Sin() and all similar methods expect argument in radians, but your latitudes and longitudes are in degrees. You have to convert IPointGps to radians before you calculate bearing, or modify Bearing calculation, e.g.:
public static double Bearing(IPointGps pt1, IPointGps pt2)
{
double x = Math.Cos(DegreesToRadians(pt1.Latitude)) * Math.Sin(DegreesToRadians(pt2.Latitude)) - Math.Sin(DegreesToRadians(pt1.Latitude)) * Math.Cos(DegreesToRadians(pt2.Latitude)) * Math.Cos(DegreesToRadians(pt2.Longitude - pt1.Longitude));
double y = Math.Sin(DegreesToRadians(pt2.Longitude - pt1.Longitude)) * Math.Cos(DegreesToRadians(pt2.Latitude));
// Math.Atan2 can return negative value, 0 <= output value < 2*PI expected
return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
}
public static double DegreesToRadians(double angle)
{
return angle * Math.PI / 180.0d;
}
returns bearing 5.637716736134105.
It looks like your latitude and longitude variables are float (single precision). If that is the case, then your are facing a precision error.

Changing (gradient) color of Game Object due to distance in an AR Application

I am trying to make an AR Application that shows POI's around. These POI's are from different distances, due distance i want to make them in different color with gradient scale.
I have calculated distances of POI's from GPS coordinates with Haversine Formula and tried to change the color due to distance but it doesn't update the color even though change the distance by walking while i see the POI's. I use WorldScaleAr scene for AR. Below code is only for one POI.
using System;
using static System.Math;
double[,] coords = new double[,] { { 39.870613, 32.73341 } }; // POI location
void Start()
{
// get poi Location
lat1 = Math.PI * coords[0, 0] / 180.0;
// lat2 = Math.PI * user[0, 0] / 180.0;
lon1 = Math.PI * coords[0, 1] / 180.0;
// lon2 = Math.PI * user[0, 1] / 180.0;
}
void Update()
{
// Get user location
// Latitude
x = getLocation.x1.ToString();
user_lat = Convert.ToDouble(x);
user_lat_rad = Math.PI * user_lat / 180.0; // Radian
// Longitude
y = getLocation.y1.ToString();
user_lon = Convert.ToDouble(y);
user_lon_rad = Math.PI * user_lon / 180.0; // Radian
// Change POIs sizes
distances = Convert.ToSingle(distance(user_lat_rad, user_lon_rad));
GetComponent<Renderer>().material.color = new Color((distances*255f/1000f)/255f, (distances*255f/1000f)/255f, (distances*255f/1000f)/255f);
public double distance(double lat2, double lon2)
{
// Haversine Formula
// Lat2,Lon2 = User Location
// Lat1,Lon1 = POI Location
double dist1 = Sqrt((Pow(Sin((lat2 - lat1) / 2), 2)) + Cos(lat2) * Cos(lat2) * (Pow(Sin((lon2 - lon1) / 2), 2)));
double distance = 2 * r * Asin(dist1);
return distance;
}
What values does the distance function return? I'm asking about the order of magnitude of the numerical value. This: distances*255f/1000f)/255f is equal to distance/1000 so you might just as well get values close to zero, or if it's more than 1000 (not sure what the units are here), a >1 value.

Bearing calculation in C#

I want to calculate bearing between 2 GPS positions, I foollowed this page recommandations for my algorythm:
public static double Bearing(IPointGps pt1, IPointGps pt2)
{
double x = Math.Cos(pt1.Latitude) * Math.Sin(pt2.Latitude) - Math.Sin(pt1.Latitude) * Math.Cos(pt2.Latitude) * Math.Cos(pt2.Longitude - pt1.Longitude);
double y = Math.Sin(pt2.Longitude - pt1.Longitude) * Math.Cos(pt2.Latitude);
// Math.Atan2 can return negative value, 0 <= output value < 2*PI expected
return (Math.Atan2(y, x) + Math.PI * 2)%(Math.PI * 2);
}
Then I transform my value in degrees using this method
public static double RadiansToDegrees(double angle)
{
return (angle * 180.0) / Math.PI;
}
I have the following test sample:
Point1 (lat, long) = 43.6373638888888888888888888888889, 1.35762222222222222222222222222222
Point2 (lat, long) = 43.6156444444444444444444444444444,1.380225
Expected bearing = 323°
However, I obtain a bearing of 315.5° (5.5062235835910762 rad). If i calculate the expected radian value, i get 5.637413 which leaves no doubt that my problem lies in my bearing method.
I already implemented other computation methods using .Net Math package (including Cos, Sin, Tan and ATan methods) and my unit tests pass with 1e-12 precision. What am I missing?
PS: I also tryied to reimplement the Atan2 method in case there is a lack of precision in it. I obtain the very same result
edit: My Latitude and Longitude are double as per the following interface
public interface IPointGps
{
double Latitude { get; }
double Longitude { get; }
}
Math.Sin() and all similar methods expect argument in radians, but your latitudes and longitudes are in degrees. You have to convert IPointGps to radians before you calculate bearing, or modify Bearing calculation, e.g.:
public static double Bearing(IPointGps pt1, IPointGps pt2)
{
double x = Math.Cos(DegreesToRadians(pt1.Latitude)) * Math.Sin(DegreesToRadians(pt2.Latitude)) - Math.Sin(DegreesToRadians(pt1.Latitude)) * Math.Cos(DegreesToRadians(pt2.Latitude)) * Math.Cos(DegreesToRadians(pt2.Longitude - pt1.Longitude));
double y = Math.Sin(DegreesToRadians(pt2.Longitude - pt1.Longitude)) * Math.Cos(DegreesToRadians(pt2.Latitude));
// Math.Atan2 can return negative value, 0 <= output value < 2*PI expected
return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
}
public static double DegreesToRadians(double angle)
{
return angle * Math.PI / 180.0d;
}
returns bearing 5.637716736134105.
It looks like your latitude and longitude variables are float (single precision). If that is the case, then your are facing a precision error.

Units in [Location1].DistanceTo[Location2]

I have been trying to find out what unit the .DistanceTo gives me.
The articles related so far mention geounits, but no further explanation
Using an elapsed time, and 2 locations I am calculating the speed my device is going.
I'm not sure how to get the result from distance=Location1.DistanceTo(Location2); in Km, or any other METRIC unit. I have to use metric for my app.
Any help solving this or guiding me toward the solution would be helpful.
According to the developer documentation the method returns the distance in metres.
Returns the approximate distance in meters between this location and
the given location.
So to get it in kilometres, just divide the result by 1000:
float distanceInMetres = Location1.DistanceTo(Location2);
float distanceInKilometres = distanceInMetres / 1000;
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
int Radius=6371;//radius of earth in Km
double lat1 = StartP.getLatitudeE6()/1E6;
double lat2 = EndP.getLatitudeE6()/1E6;
double lon1 = StartP.getLongitudeE6()/1E6;
double lon2 = EndP.getLongitudeE6()/1E6;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult= Radius*c;
double km=valueResult/1;
DecimalFormat newFormat = new DecimalFormat("####");
kmInDec = Integer.valueOf(newFormat.format(km));
meter=valueResult%1000;
meterInDec= Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value",""+valueResult+" KM "+kmInDec+" Meter "+meterInDec);
return Radius * c;
}

Calculate Current Speed in .NET - With GPS

I would like to know what's the best way to calculate the current speed with GPS.
I've an external GPS receiver which is connected via USB to my car-notebook. It gives me just the following information:
- Longitude
- Latitude
- Altitude
My try is to get two location-infos with timestamps.
Then I am finding the difference in time (timestamp2 - timestamp1) and calculating the speed (distance/time).
Are there any other possibilites oder maybe any libraries available?
To calculate the distance, you will need the Haversine Formula.
You will find many implementations of it around the web, here is one I use in C#:
private static double ArcInMeters(double lat0, double lon0, double lat1, double lon1)
{
double earthRadius = 6372797.560856; // m
return earthRadius * ArcInRadians(lat0, lon0, lat1, lon1);
}
private static double ArcInRadians(double lat0, double lon0, double lat1, double lon1)
{
double latitudeArc = DegToRad(lat0 - lat1);
double longitudeArc = DegToRad(lon0 - lon1);
double latitudeH = Math.Sin(latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = Math.Sin(longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = Math.Cos(DegToRad(lat0)) * Math.Cos(DegToRad(lat1));
return 2.0 * Math.Asin(Math.Sqrt(latitudeH + tmp * lontitudeH));
}
private static double DegToRad(double x)
{
return x * Math.PI / 180;
}

Categories

Resources