Reputation: 173
I have a set of lat/lon coordinates for different nodes.
Since I have to represent these positions in a simulator, I have to convert them to cartesian coordinates.
For this I use the python package pyproj.
lat1 = 50.0
lon1 = 10.0
lat2 = 50.5
lon2 = 10.5
x1, y1, z1 = wgs84_to_utm32(lat1, lon1, 0.0)
x2, y2, z2 = wgs84_to_utm32(lat2, lon2, 0.0)
print(distance_haversine(lat1, lon1, lat2, lon2))
print(distance_cartesian(x1, y1, x2, y2))
the ouput is:
66012.5130481
102485.874713
that is a difference of over 36 km.
So my question is, how can I convert lat/lon coordinates so that the distances are preserved. I don't care for minimal error.
EDIT:
#
# Convert WGS84 (Geographic) coordinates to UTM32 (Germany)
#
def wgs84_to_utm(lat, lon, alt):
wgs84 = Proj(init='epsg:4326')
utm_de = Proj(init='epsg:32632') # utm germany = 32
return transform(wgs84, utm_de, lat, lon, alt)
EDIT2:
Okay to be clear, I know that I am trying to compare a distance on a sphere to a distance on a flat surface.
But since I am simulating WLAN nodes, the distance between those nodes is crucial. But I have no other information than their lat/lon coordinates.
How would I go about representing those lat/lon coordinates on a flat surface, so that the distances are preserved?
EDIT3:
def distance_haversine(lat1, lon1, lat2, lon2):
# approximate radius of earth in km
R = 6373.0 * 1000
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return distance
def distance_cartesian(x1, y1, x2, y2):
dx = x1 - x2
dy = y1 - y2
return sqrt(dx * dx + dy * dy)
Upvotes: 2
Views: 4820
Reputation: 2510
There is a mistake somewhere in the conversion to utm. Try the utm module instead
import utm
lat1 = 50.0
lon1 = 10.0
lat2 = 50.5
lon2 = 10.5
x1, y1, z1, u = utm.from_latlon(lat1, lon1)
x2, y2, z2, u = utm.from_latlon(lat2, lon2)
print(distance_haversine(lat1, lon1, lat2, lon2))
print(distance_cartesian(x1, y1, x2, y2))
Output:
66012.51304805529
66047.84250743943
Upvotes: 2