Calculate distance between cities & find surrounding cities based on GeoPT, in Python on Google App Engine
By : Hamed Rabiei
Date : March 29 2020, 07:55 AM
it should still fix some issue This works perfect but is a lil slow : Function to Calculate Distance. The Arguments passed to this function are tuples of latitude and longitude of a location or a Geopt(): code :
def HaversineDistance(location1, location2):
"""Method to calculate Distance between two sets of Lat/Lon."""
lat1, lon1 = location1
lat2, lon2 = location2
earth = 6371 #Earth's Radius in Kms.
#Calculate Distance based in Haversine Formula
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = earth * c
return d
def get_closest_cities(self, kms):
cities = []
#Find surrounding Cities of a given city within a given radius
allcities = self.country.city_set
for city in allcities:
distance = HaversineDistance((self.location.lat, self.location.lon),(city.location.lat, city.location.lon))
if not distance >= kms:
cities.append((city.name, int(distance)))
cities.remove(cities[0])
return cities
|
Calculate the distance between two Geographical Coordinates in Java
By : Grégoire Krieg
Date : November 01 2020, 04:09 PM
it should still fix some issue The distance function will return the distance between two points in meteres code :
public double distance() {
double lat1 = 44.40239182909422;
double lon1 = 8.930511474608954;
double lat2 = 30.297017883371236;
double lon2 = 122.3822021484364;
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 1.609344 * 1000;
return (dist); // 134910.69784909734
}
/* The function to convert decimal into radians */
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/* The function to convert radians into decimal */
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
|
Calculate distance between two geographical locations in sql server
By : Jiacheng Wang
Date : March 29 2020, 07:55 AM
To fix this issue You have 2 issues here. The first is not working the GEOGRAPHY datatypes and working with GEOMETRY instead, when you are actually working with GPS coordinates. Geography will consider the shape of the earth and will return more precise values for this scenario. The second and biggest problem is the ring orientation of your Japan polygon. The order the coordinates are placed on the hard-coded string is very important to determine the resulting polygon. Sometimes SQL Server can't properly determine which side of the edges between our points we actually mean as the polygon. code :
DECLARE @g_metry GEOMETRY = GEOMETRY::STGeomFromText('POLYGON ((139.999722222222 40.0513888888889, 141.401111111111 40.0513888888889, 141.398333333333 39.3494444444444, 140.765 38.1005555555556, 140.766111111111 37.3333333333333, 140.314722222222 36.8161111111111, 140.316111111111 36.2841666666667, 139.050277777778 36.2847222222222, 139.049444444444 35.1669444444444, 138.499444444444 35.1669444444444, 138.499444444444 35.5852777777778, 136.816944444444 35.5830555555556, 136.034166666667 34.8980555555556, 135.732222222222 34.8997222222222, 135.732222222222 35.3827777777778, 136.431944444444 35.8013888888889, 137.365 36.5341666666667, 138.165555555556 36.9333333333333, 139.415 37.8152777777778, 140.285833333333 39.4497222222222, 140.285833333333 39.8652777777778, 139.915555555556 39.8652777777778, 139.915555555556 39.9669444444444, 139.999722222222 40.0513888888889))', 4326);
DECLARE @g GEOGRAPHY = @g_metry.MakeValid().STUnion(@g_metry.STStartPoint()).STAsText()
DECLARE @h GEOGRAPHY = GEOGRAPHY::STGeomFromText('POINT(-1.9335937499142 53.956085529457)', 4326);
SELECT
@g.STDistance(@h)/1609.344 as DistanceInMiles,
@h.STDistance(@g) as DistanceInMeters
DistanceInMiles DistanceInMeters
5574.53292757983 8971341.11980304
|
How to find all towns/cities within a driving distance of a particular city?
By : Kevin Van Haecke
Date : March 29 2020, 07:55 AM
Does that help I'm not quite sure why you would do something like that, but you have multiple ways to do so. Easy way:
|
calculate the distance between two cities
By : 钟元武
Date : March 29 2020, 07:55 AM
With these it helps You will need to convert your address or partial address into valid latitude and longitude. There are several web services/APIs which can do this for you. Google has an API for this: https://developers.google.com/maps/documentation/geocoding/start?csw=1You will then need to do some math to calculate the distance, something like the Haversine formula. If you google C# Haversine formula you'll find several articles, including some Stackoverflow questions about how to implement this.
|