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.
I have a point with a give radius around it, say 10 miles. How to determine if another point is in the circle with the mapping functionality of Windows Phone 8.1?
From the question I assume that you have spherical coordinates (lat, lon) for both points. You can simply calculate the distance using the haversine formula, this gives you the distance 'as the crow flies'.
Example in JavaScript (taken from here):
var radians = Array.prototype.map.call(arguments, function(deg) { return deg/180.0 * Math.PI; });
var lat1 = radians[0], lon1 = radians[1], lat2 = radians[2], lon2 = radians[3];
var R = 6372.8; // km
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.sin(dLat / 2) * Math.sin(dLat /2) + Math.sin(dLon / 2) * Math.sin(dLon /2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.asin(Math.sqrt(a));
return R * c;
If you have planar coordinates, just use Pythagoras.
In my case on picture firstPoint0 - as example my first point and center of the circle, relative this point confine screenings by radius 1 km. I need to show just all points in my radius, others points thisPoint not show by linq query.
var flats = context.Flats;
var first = flats.FirstOrDefault(x => x.Lattitude && x.Longitude);
var getAllInRadius = flats.Where(? take points where distance <= 1 km)
Just use the Haversine formula that returns the great-circle distance between two points on a sphere:
// Returns the great circle distance between two flats, as meters
public static double DistanceBetweenFlats(Flat flat1, Flat flat2)
{
const int EarthRadius = 6371;
double latitude = ToRadian(flat2.Latitude - flat1.Latitude);
double longitude = ToRadian(flat2.Longitude - flat1.Longitude);
double tmp = (Math.Sin(latitude / 2) * Math.Sin(latitude / 2)) +
(Math.Cos(ToRadian(flat1.Latitude)) * Math.Cos(ToRadian(flat2.Latitude)) *
Math.Sin(longitude / 2) * Math.Sin(longitude / 2));
double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(tmp)));
double d = EarthRadius * c;
return d * 1000;
}
...
var centerFlat = ...;
var getAllInRadius = flats.Where(z => DistanceBetweenFlats(centerFlat, z) <= 1000);
Of course all of this assumes you're using LINQ in memory (not LINQ to Entities). If it's not the case, you'll have to use spatial queries.
I'm trying to merge two databases for consolidating two clients' websites. However, Client A has been using regular Lat/Lon pairs for geolocation, while Client B is using Lambert 72 (X/Y) coordinates.
I've built a script that should convert these coordinates (as I'm not sure which coordinates will be used in the final merged database, I'm trying converting them either way).
I took some snippets from here: http://zoologie.umh.ac.be/tc/algorithms.aspx
Please note that all coordinates mentioned below point to locations in Belgium.
I'm converting some coordinates to see if the calculations are correct, but the coordinates I'm getting seem to be way off. For reference, the center of Belgium is roughly (North 50.84323737103243, East 4.355735778808594), so I'd expect all coordinates to be close to these values.
I converted the Lambert 72 value (X: 151488250, Y: 170492909) to a Lat/Lon pair, but the result is: (-87.538.... , -50.724....) which is way off from the expected values.
If I convert full circle (Lambert->LatLon->Lambert and vice versa), I get the same result values as I entered, so I know my conversions are at least consistent and the conversions are perfect inversions of one another.
I tried some online converter tools as well, and they give me the same (-87.538.... , -50.724....) result.
Since multiple sources yield the same results, and my conversions are correct inversions of eachother, I'm figuring the calculations themselves are correct, but the resulting values still need to be converted/offset further?
I consider myself to be sufficient in algebra, but cartographic projections completely elude me.
Can someone please shed some light on this?
Extra Info
I hope I posted this in the correct forum. I'm not really sure where to put this as this is a mix of geography, mathematics and coding/conversion...
The mentioned Lambert coordinates (X: 151488250, Y: 170492909) point to a location in Brussels, so the Lat/Lon result should be very near to (North 50.84323737103243, East 4.355735778808594).
Please find my conversion functions below:
public static Lambert72 LatLon_To_Lambert72(LatLon latlon)
{
var lat = latlon.Lat;
var lng = latlon.Lon;
double LongRef = 0.076042943;
//=4°21'24"983
double bLamb = 6378388 * (1 - (1 / 297));
double aCarre = Math.Pow(6378388, 2);
double eCarre = (aCarre - Math.Pow(bLamb, 2)) / aCarre;
double KLamb = 11565915.812935;
double nLamb = 0.7716421928;
double eLamb = Math.Sqrt(eCarre);
double eSur2 = eLamb / 2;
//conversion to radians
lat = (Math.PI / 180) * lat;
lng = (Math.PI / 180) * lng;
double eSinLatitude = eLamb * Math.Sin(lat);
double TanZDemi = (Math.Tan((Math.PI / 4) - (lat / 2))) * (Math.Pow(((1 + (eSinLatitude)) / (1 - (eSinLatitude))), (eSur2)));
double RLamb = KLamb * (Math.Pow((TanZDemi), nLamb));
double Teta = nLamb * (lng - LongRef);
double x = 0;
double y = 0;
x = 150000 + 0.01256 + RLamb * Math.Sin(Teta - 0.000142043);
y = 5400000 + 88.4378 - RLamb * Math.Cos(Teta - 0.000142043);
return new Lambert72(x, y);
}
public static LatLon Lambert72_To_LatLon(Lambert72 lb72)
{
double X = lb72.X;
double Y = lb72.Y;
double LongRef = 0.076042943;
//=4°21'24"983
double nLamb = 0.7716421928;
double aCarre = Math.Pow(6378388, 2);
double bLamb = 6378388 * (1 - (1 / 297));
double eCarre = (aCarre - Math.Pow(bLamb, 2)) / aCarre;
double KLamb = 11565915.812935;
double eLamb = Math.Sqrt(eCarre);
double eSur2 = eLamb / 2;
double Tan1 = (X - 150000.01256) / (5400088.4378 - Y);
double Lambda = LongRef + (1 / nLamb) * (0.000142043 + Math.Atan(Tan1));
double RLamb = Math.Sqrt(Math.Pow((X - 150000.01256), 2) + Math.Pow((5400088.4378 - Y), 2));
double TanZDemi = Math.Pow((RLamb / KLamb), (1 / nLamb));
double Lati1 = 2 * Math.Atan(TanZDemi);
double eSin = 0;
double Mult1 = 0;
double Mult2 = 0;
double Mult = 0;
double LatiN = 0;
double Diff = 0;
double lat = 0;
double lng = 0;
do {
eSin = eLamb * Math.Sin(Lati1);
Mult1 = 1 - eSin;
Mult2 = 1 + eSin;
Mult = Math.Pow((Mult1 / Mult2), (eLamb / 2));
LatiN = (Math.PI / 2) - (2 * (Math.Atan(TanZDemi * Mult)));
Diff = LatiN - Lati1;
Lati1 = LatiN;
} while (Math.Abs(Diff) > 2.77777E-08);
lat = (LatiN * 180) / Math.PI;
lng = (Lambda * 180) / Math.PI;
return new LatLon(lat, lng);
}
I am the author of the page you mention in your post.
I don't know if you have resolved your problem but the Lambert coordinates you give are not correct. I think that you have to divide them by 1000. That gives x=151488.250 and y=170492.909 which are possible coordinates and corresponding to a street in... Brussels.
Be careful to the choice of the datum when converting to and from lat/lng values.
I'm trying to generate some points at random distances away from a fixed point using GPS.
How can I add distance in meters to a GPS coordinate?
I've looked at UTM to GPS conversion but is there a simpler method to achieve this?
I'm working on Android platform just in case.
Cheers,
fgs
P0(lat0,lon0) : initial position (unit : degrees)
dx,dy : random offsets from your initial position in meters
You can use an approximation to compute the position of the randomized position:
lat = lat0 + (180/pi)*(dy/6378137)
lon = lon0 + (180/pi)*(dx/6378137)/cos(lat0)
This is quite precise as long as the random distance offset is below 10-100 km
Edit: of course in Java Math.cos() expects radians so do use Math.cos(Math.PI/180.0*lat0) if lat0 is in degrees as assumed above.
To take a square I'm using this:
private double[] getBoundingBox(final double pLatitude, final double pLongitude, final int pDistanceInMeters) {
final double[] boundingBox = new double[4];
final double latRadian = Math.toRadians(pLatitude);
final double degLatKm = 110.574235;
final double degLongKm = 110.572833 * Math.cos(latRadian);
final double deltaLat = pDistanceInMeters / 1000.0 / degLatKm;
final double deltaLong = pDistanceInMeters / 1000.0 / degLongKm;
final double minLat = pLatitude - deltaLat;
final double minLong = pLongitude - deltaLong;
final double maxLat = pLatitude + deltaLat;
final double maxLong = pLongitude + deltaLong;
boundingBox[0] = minLat;
boundingBox[1] = minLong;
boundingBox[2] = maxLat;
boundingBox[3] = maxLong;
return boundingBox;
}
This returns an array with 4 coordinates, with them you can make a square with your original point in center.
A detailed outline is given at http://www.movable-type.co.uk/scripts/latlong.html.
If you, somewhere, need to interconvert longitude/latitude to UTM coordinates (the ones used in GPS) you may want to have a look at http://www.uwgb.edu/dutchs/UsefulData/UTMFormulas.htm
If you want to go east or north or west or south you can use this:
#SuppressLint("DefaultLocale")
public static double go_mock_loc(double xx_lat,double xx_long,double xx_dinstance,String Direction)
{
// double xx_lat= 45.815005;
// double xx_long= 15.978501;
// int xx_dinstance=500;
int equator_circumference=6371000;
int polar_circumference=6356800;
double m_per_deg_long = 360 / polar_circumference;
double rad_lat=(xx_lat* (Math.PI) / 180);
double m_per_deg_lat = 360 / ( Math.cos(rad_lat) * equator_circumference);
double deg_diff_long = xx_dinstance * m_per_deg_long;
double deg_diff_lat = xx_dinstance * m_per_deg_lat;
double xx_north_lat = xx_lat + deg_diff_long;
//double xx_north_long= xx_long;
double xx_south_lat = xx_lat - deg_diff_long;
//double xx_south_long= xx_long;
//double xx_east_lat = xx_lat;
double xx_east_long= xx_long + deg_diff_lat;
//double xx_west_lat = xx_lat;
double xx_west_long= xx_long - deg_diff_lat;
if (Direction.toUpperCase().contains("NORTH")) {
return xx_north_lat;
} else if (Direction.toUpperCase().contains("SOUTH"))
{
return xx_south_lat;
} else if (Direction.toUpperCase().contains("EAST"))
{
return xx_east_long;
} else if (Direction.toUpperCase().contains("WEST"))
{
return xx_west_long;
}
else
return 0;
}
I found that solution of #Bogdan Khrystov is very well.
So here is C# version of his solution.
public enum GeoDirection
{
NORTH = 1, SOUTH = 2, EAST = 3, WEST = 4
}
public static Tuple<double, double> AddDistanceInMeters(double latitude, double longitude, int distanceInMeters, GeoDirection direction)
{
var equatorCircumference = 6371000;
var polarCircumference = 6356800;
var mPerDegLong = 360 / (double)polarCircumference;
var radLat = latitude * Math.PI / 180;
var mPerDegLat = 360 / (Math.Cos(radLat) * equatorCircumference);
var degDiffLong = distanceInMeters * mPerDegLong;
var degDiffLat = distanceInMeters * mPerDegLat;
var xxNorthLat = latitude + degDiffLong;
var xxSouthLat = latitude - degDiffLong;
var xxEastLong = longitude + degDiffLat;
var xxWestLong = longitude - degDiffLat;
switch (direction)
{
case GeoDirection.NORTH:
return new Tuple<double, double>(xxNorthLat, longitude);
case GeoDirection.SOUTH:
return new Tuple<double, double>(xxSouthLat, longitude);
case GeoDirection.EAST:
return new Tuple<double, double>(latitude, xxEastLong);
case GeoDirection.WEST:
return new Tuple<double, double>(latitude, xxWestLong);
default:
return null;
}
}
rewrite #Ersin Gülbahar answer in Kotlin:
object LocationUtil {
enum class Direction {
NORTH, SOUTH, EAST, WEST
}
fun addDistanceInMeters(
latitude: Double,
longitude: Double,
distanceInMeters: Int,
direction: Direction
): Pair<Double, Double> {
val equatorCircumference = 6371000
val polarCircumference = 6356800
val mPerDegLong = (360 / polarCircumference.toDouble())
val radLat = latitude * Math.PI / 180
val mPerDegLat = 360 / (Math.cos(radLat) * equatorCircumference)
val degDiffLong = distanceInMeters * mPerDegLong
val degDiffLat = distanceInMeters * mPerDegLat
val xxNorthLat = latitude + degDiffLong
val xxSouthLat = latitude - degDiffLong
val xxEastLong = longitude + degDiffLat
val xxWestLong = longitude - degDiffLat
return when (direction) {
Direction.NORTH -> Pair(xxNorthLat, longitude)
Direction.SOUTH -> Pair(xxSouthLat, longitude)
Direction.EAST -> Pair(latitude, xxEastLong)
Direction.WEST -> Pair(latitude, xxWestLong)
}
}
}
This code splits the line between two coordinates in n segments. Replace the delta calculation by your fixed distance
#Override
public void split(Coordinates p1, Coordinates p2, int segments) {
double φ1 = Math.toRadians(p1.getLat());
double λ1 = Math.toRadians(p1.getLon());
double φ2 = Math.toRadians(p2.getLat());
double λ2 = Math.toRadians(p2.getLon());
double xDelta = (φ2 - φ1) / segments;
double yDelta = (λ2 - λ1) / segments;
for (int i = 0; i < segments; i++){
double x = φ1 + i * xDelta;
double y = λ1 + i * yDelta;
double xc = Math.toDegrees(x);
double yc = Math.toDegrees(y);
System.out.println(xc+","+yc);
}
}
Combining answers from #Ersin Gülbahar and #Stéphane above, I came up with this solution in Flutter/Dart:
import 'dart:math' as math;
enum Direction { north, south, east, west }
double moveCoordinate(
double latitude, double longitude, double distanceToMoveInMeters, Direction directionToMove) {
const earthEquatorRadius = 6378137;
final latitudeOffset = (180 / math.pi) * (distanceToMoveInMeters / earthEquatorRadius);
final longitudeOffset = (180 / math.pi) *
(distanceToMoveInMeters / earthEquatorRadius) /
math.cos(math.pi / 180 * latitude);
switch (directionToMove) {
case Direction.north:
return latitude + latitudeOffset;
case Direction.south:
return latitude - latitudeOffset;
case Direction.east:
return longitude + longitudeOffset;
case Direction.west:
return longitude - longitudeOffset;
}
return 0;
}
This works, tested. The code is C# but you can easily change it to another language
private PointLatLng NewPositionBasedOnDistanceAngle(PointLatLng org, double distance, double bearing)
{
double rad = bearing * Math.PI / 180; //to radians
double lat1 = org.Lat * Math.PI / 180; //to radians
double lng1 = org.Lng * Math.PI / 180; //to radians
double lat = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / 6378137) + Math.Cos(lat1) * Math.Sin(distance / 6378137) * Math.Cos(rad));
double lng = lng1 + Math.Atan2(Math.Sin(rad) * Math.Sin(distance / 6378137) * Math.Cos(lat1), Math.Cos(distance / 6378137) - Math.Sin(lat1) * Math.Sin(lat));
return new PointLatLng(lat * 180 / Math.PI, lng * 180 / Math.PI); // to degrees
}