Commit b9e4136f authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Use width and depth to do projections

parent 69719bdf
......@@ -34,33 +34,31 @@ def lat_lon_distance(lat1, lon1, lat2, lon2):
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return 6371e3 * c
def longitude_to_X(lon, map_size):
def longitude_to_X(lon, width):
"""Mercator projection of longitude."""
return (map_size / 360) * (180 + lon)
return (width / 360) * (180 + lon)
def latitude_to_Y(lat, map_size):
def latitude_to_Y(lat, depth):
"""Mercator projection latitude."""
return (map_size / 180) * (90 - lat)
return (depth / 180) * (90 - lat)
def convert_to_local_coordinates(latitude, longitude, map_info, map_size):
def convert_to_local_coordinates(latitude, longitude, map_info):
"""Convert coordinates into local values."""
x = longitude_to_X(longitude, map_size)
y = latitude_to_Y(latitude, map_size)
local_x = ((x - map_info['min_x']) / (map_info['max_x'] - map_info['min_x'])) \
* 1000 - map_size / 2
local_y = ((y - map_info['min_y']) / (map_info['max_y'] - map_info['min_y'])) \
* 1000 - map_size / 2
x = longitude_to_X(longitude, map_info['width'])
y = latitude_to_Y(latitude, map_info['depth'])
local_x = (((x - map_info['min_x']) / (map_info['max_x'] - map_info['min_x'])) - 0.5) * map_info['width']
local_y = (((y - map_info['min_y']) / (map_info['max_y'] - map_info['min_y'])) - 0.5) * map_info['depth']
return local_x, local_y
def convert_to_geo_coordinates(x, y, map_info, map_size):
def convert_to_geo_coordinates(x, y, map_info):
"""Convert local values to coordinates."""
lon = (x + map_size / 2) / 1000
lon = (x / map_info['width']) + 0.5
lon = lon * (map_info['max_x'] - map_info['min_x']) + map_info['min_x']
lon = lon / (map_size / 360) - 180
lon = lon / (map_info['width'] / 360) - 180
lat = (y + map_size / 2) / 1000
lat = (y / map_info['depth']) + 0.5
lat = lat * (map_info['max_y'] - map_info['min_y']) + map_info['min_y']
lat = 90 - lat / (map_size / 180)
lat = 90 - lat / (map_info['depth'] / 180)
return lon, lat
......@@ -111,9 +109,9 @@ def rotate(x, y, x_o, y_o, rotation):
)
def build_rectangle(root, lat, lon, length, width, height, rotation,
style_map_id, map_info, map_size):
style_map_id, map_info):
"""Build a rectangle."""
local_x, local_y = convert_to_local_coordinates(lat, lon, map_info, map_size)
local_x, local_y = convert_to_local_coordinates(lat, lon, map_info)
min_x = local_x - length / 2
max_x = local_x + length / 2
min_y = local_y - width / 2
......@@ -122,7 +120,7 @@ def build_rectangle(root, lat, lon, length, width, height, rotation,
polygone_coordinates = []
for x, y in ((min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y), (min_x, min_y)):
rotated_x, rotated_y = rotate(x, y, local_x, local_y, rotation)
corner_lon, corner_lat = convert_to_geo_coordinates(rotated_x, rotated_y, map_info, map_size)
corner_lon, corner_lat = convert_to_geo_coordinates(rotated_x, rotated_y, map_info)
polygone_coordinates.append((corner_lat, corner_lon, height))
add_polygone(root, polygone_coordinates, style_map_id)
......@@ -146,17 +144,17 @@ def write_kml(filename, map_dict):
(map_dict['min_lat'], map_dict['min_lon'], 0),
), 'terrain')
map_size = math.ceil(max(
lat_lon_distance(
map_dict['min_lat'], map_dict['min_lon'], map_dict['min_lat'], map_dict['max_lon']),
lat_lon_distance(
map_dict['min_lat'], map_dict['min_lon'], map_dict['max_lat'], map_dict['min_lon']),
))
depth = lat_lon_distance(map_dict['min_lat'], map_dict['min_lon'],
map_dict['max_lat'], map_dict['min_lon'])
width = lat_lon_distance(map_dict['min_lat'], map_dict['min_lon'],
map_dict['min_lat'], map_dict['max_lon'])
map_info = {
'min_x': longitude_to_X(map_dict['min_lon'], map_size),
'min_y': latitude_to_Y(map_dict['min_lat'], map_size),
'max_x': longitude_to_X(map_dict['max_lon'], map_size),
'max_y': latitude_to_Y(map_dict['max_lat'], map_size),
'depth': depth,
'min_x': longitude_to_X(map_dict['min_lon'], width),
'min_y': latitude_to_Y(map_dict['min_lat'], depth),
'max_x': longitude_to_X(map_dict['max_lon'], width),
'max_y': latitude_to_Y(map_dict['max_lat'], depth),
'width': width,
}
for obstacle in map_dict['obstacle_list']:
......@@ -172,13 +170,12 @@ def write_kml(filename, map_dict):
- obstacle['rotation']['z'] * math.pi / 180,
'obstacle',
map_info,
map_size,
)
for flag in map_dict['flag_list']:
position = flag['position']
build_rectangle(document, position['latitude'], position['longitude'],
1, 1, position['altitude'], 0, 'flag', map_info, map_size)
1, 1, position['altitude'], 0, 'flag', map_info)
tree = ET.ElementTree(root)
ET.indent(tree, ' ')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment