Can't calculate Latitude. using Formula - c#

I need to calculate Longitude and Latitude based on an old Longitude and Latitude, with distance and direction from that point.
From this link: http://www.movable-type.co.uk/scripts/latlong.html
I got these Formulae:
newLatitude = Math.Asin(Math.Sin(oldLatitude) * Math.Cos(distanceTravelled / earthRadius) + Math.Cos(oldLatitude) * Math.Sin(distanceTravelled / earthRadius) * Math.Cos(currentDirection));
newLongitude = oldLongitude + Math.Atan2(Math.Sin(currentDirection) * Math.Sin(distanceTravelled / earthRadius) * Math.Cos(oldLatitude), Math.Cos(distanceTravelled / earthRadius) - Math.Sin(oldLatitude) * Math.Sin(oldLatitude));
I have Acceleration e.g. 0.1 m/sec2
time travelled: Calculated from current time - start time.
Then I calculate the Distance travelled:
distanceTravelled = distanceTravelled/1000;
I also have movement of direction in degrees: e.g. 90 degree. (East)
But I am getting Error in new Latitude see-image:
Do I have to enter direction in Radian?
Distance in KM instead of meter?
Please help me get the right Latitude?

I found at the solution.
I was using degree and decimal of latitude and longitude in above formulae.
We have to use Radians
so convert Latitude, Longitude and Direction to Radians:
oldLatitude = Math.PI * oldLatitude / 180;
oldLongitude = Math.PI * oldLongitude / 180;
currentDirection = Math.PI * currentDirection / 180.0;
then convert new Longitude and latitude from Radians to Degree again
newLatitude = 180 * newLatitude / Math.PI;
newLongitude = 180 * newLongitude / Math.PI;

Related

C# - Converting xyz tile to lat/long and vice-verse giving different results

I tried converting lat/long to XYZ tile and again using the XYZ values trying to get back the lat/long. The values of lat/long are not the same (the original one and the converted one). I referred to https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#C.23 and got the formula for conversion.
Below is the code for your reference:
Converting from lat/long to XYZ:
var x = (int)Math.Floor((longitude + 180.0) / 360.0 * Math.Pow(2, zoom));
var y = (int)Math.Floor((1 - Math.Log(Math.Tan((Math.PI / 180) * latitude) + 1 / Math.Cos((Math.PI / 180) * latitude)) / Math.PI) / 2 * Math.Pow(2, zoom));
Converting from XYZ to lat/long:
var longitude = x / Math.Pow(2, z) * 360 - 180;
var n = Math.PI - 2 * Math.PI * y / Math.Pow(2, z);
var latitude = 180 / Math.PI * Math.Atan(0.5 * (Math.Exp(n) - Math.Exp(-n)));
When I pass latitude = -477.42187500000006, longitude = 37.3002752813443, zoom = 15, then I get x = 19779 and y = -2147483648.
But when I pass x = 19779, y = -2147483648, z = 15, then I get latitude = 90.0 and longitude = 37.298583984375 which is wrong.
I am not sure why the second conversion is giving the wrong results. Any help is highly appreciated.
Latitudes are by definition belong to range -90 (south pole) to 90 (north pole).
So you start with invalid one -477.421875 and the result can be arbitrary.
The longitude is correct. It is slightly different from original, which is expected since you truncate x to nearest integer. If you want the same value, don't round to integer.

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.

How would i get a point on a sphere?

So im trying to get a point on a sphere via an angle.
double X = Math.Cos(pitch * (Math.PI / 180));
double Y = Math.Sin(pitch * (Math.PI / 180)) * Math.Sin(yaw * (Math.PI / 180));
double Z = Math.Sin(pitch * (Math.PI / 180)) * Math.Cos(yaw * (Math.PI / 180));
I've tried this, but the point it outputs seems to be clamped to the XZ axis, and increasing/decreasing the yaw doesn't do anything to the point it spits out.
I cant figure out whats wrong with this code? Could someone help me identify the problem?

Calculate SW & NE Lat/Longs of Square X miles from a Centre Lat/Long

I am working on getting data out of the Noaa API.
When talking to the Noaa API you can get a list of weather stations within a square. They call them "extents" and they are a 2 sets of lat/longs. The bottom left lat/long and the top right lat/long
As detailed here:
http://www.ncdc.noaa.gov/cdo-web/webservices/v2#stations
I have been given a list of lat/longs for a number of cities in the US and I am trying to work out the best way of putting a box around.
So, my first guess would be to take the central lat/long, work out the lat/long 75 miles to the West, then work out the lat/long of the point 75 miles South of that point.
Ideally I would like to have this as a c# function.
Has anyone got any ideas on the best way to code this please?
Thanks
Yippee! - found the solution...
First a simple class:
public class LatLonAlt
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public double Altitude { get; set; }
}
Then a function to calculate a new position:
public static HelpersModel.LatLonAlt CalculateDerivedPosition(HelpersModel.LatLonAlt source, double range, double bearing)
{
double latA = Convert.ToDouble(source.Latitude) * (Math.PI / 180);
double lonA = Convert.ToDouble(source.Longitude) * (Math.PI / 180);
double angularDistance = range / 6371;
double trueCourse = bearing * (Math.PI / 180);
double lat = Math.Asin(
Math.Sin(latA) * Math.Cos(angularDistance) +
Math.Cos(latA) * Math.Sin(angularDistance) * Math.Cos(trueCourse));
double dlon = Math.Atan2(
Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latA),
Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));
double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;
HelpersModel.LatLonAlt results = new HelpersModel.LatLonAlt();
results.Latitude = lat * (180 / Math.PI);
results.Longitude = lon * (180 / Math.PI);
results.Altitude = source.Altitude;
return results;
}
Then, and I know I can do this better.. but it works for now...
2 functions that work out the bottmleft and topright extent:
public static HelpersModel.LatLonAlt FindBottomLeftExtent(HelpersModel.LatLonAlt startpoint)
{
// first move left
HelpersModel.LatLonAlt movedleft = CalculateDerivedPosition(startpoint, 72.42, 270);
// move down
HelpersModel.LatLonAlt moveddown = CalculateDerivedPosition(movedleft, 72.42, 180);
return moveddown;
}
public static HelpersModel.LatLonAlt FindTopRightExtent(HelpersModel.LatLonAlt startpoint)
{
// first move right
HelpersModel.LatLonAlt movedright = CalculateDerivedPosition(startpoint, 72.42, 90);
// move up
HelpersModel.LatLonAlt movedup = CalculateDerivedPosition(movedright, 72.42, 0);
return movedup;
}
HTH!
Trev

Calculating the distance between 2 points in c#

I am trying to sort out a method to calculate the distance between 2 points in c#.
This is the code I have been trying though I fear the answer I get is not correct.
static void Main()
{
//postcode australia 2600 -> 3000
float latA = -31.997976f;
float longA = 115.762877f;
float latB = -31.99212f;
float longB = 115.763228f;
decimal distance = (DistanceBetween(latA, latB, longA, longB));
Console.WriteLine("Distance is" + distance);
Console.ReadLine();
}
static decimal DistanceBetween(float latA, float longA, float latB, float longB)
{
var RadianLatA = Math.PI * latA / 180;
var RadianLatb = Math.PI * latB / 180;
var RadianLongA = Math.PI * longA / 180;
var RadianLongB = Math.PI * longB / 180;
double theDistance = (Math.Sin(RadianLatA)) *
Math.Sin(RadianLatb) +
Math.Cos(RadianLatA) *
Math.Cos(RadianLatb) *
Math.Cos(RadianLongA - RadianLongB);
return Convert.ToDecimal(((Math.Acos(theDistance) * (180.0 / Math.PI)))) * 69.09M * 1.6093M;
}
this was adapted from a response found on this site here
Distance between addresses
Any thoughts on what is going wrong/
Thanks
Ryan
The class I usually use is GeoCoordinate
double latA = -31.997976f;
double longA = 115.762877f;
double latB = -31.99212f;
double longB = 115.763228f;
var locA = new GeoCoordinate(latA, longA);
var locB = new GeoCoordinate(latB, longB);
double distance = locA.GetDistanceTo(locB ); // metres
double lat1 = {};
double lat2 = {};
double lon1 = {};
double lon2 = {};
var R = 6376.5000; //Km
lat1 = lat1.ToRad();
lat2 = lat2.ToRad();
lon1 = lon1.ToRad();
lon2 = lon2.ToRad();
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.Pow(Math.Sin(dLat / 2), 2) + (Math.Pow(Math.Sin(dLon / 2), 2) * Math.Cos(lat1) * Math.Cos(lat2));
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var distance = R * c;
public double ToRad(this double degs) {
return degs * (Math.PI/180.0);
}
Input expects doubles.
This is the haversine formula, it's used to calculate the distances on our globe between two points. This is the distance in a straight line, if you need the distance on a path you will have to find all points on that path and then calculate the distances between each two points and then take the sum of that.
You can use DbGeography for spatial calculation. It has DbGeography.Distance method which is used to calculate the distance between two gps points.
Otherwise, try Ref: Harversine Formula to calculate the distance between two points.
Distance Formula: Given the two points (x1, y1) and (x2, y2), the distance between these points is given by the formula:
use it accordingly

Categories

Resources