Fix hexagons

This commit is contained in:
Eugene Burmakin 2025-09-12 22:23:17 +02:00
parent 09174de6e9
commit 52a69ecf3d
2 changed files with 21 additions and 5 deletions

View file

@ -69,12 +69,10 @@ class Api::V1::Maps::HexagonsController < ApiController
end
def set_user_and_dates
if params[:uuid].present?
set_public_sharing_context
else
return set_public_sharing_context if params[:uuid].present?
set_authenticated_context
end
end
def set_public_sharing_context
@stat = Stat.find_by(sharing_uuid: params[:uuid])

View file

@ -41,8 +41,26 @@ class Maps::HexagonGrid
generate_hexagons
end
def area_km2
@area_km2 ||= calculate_area_km2
end
private
def calculate_area_km2
width = (max_lon - min_lon).abs
height = (max_lat - min_lat).abs
# Convert degrees to approximate kilometers
# 1 degree latitude ≈ 111 km
# 1 degree longitude ≈ 111 km * cos(latitude)
avg_lat = (min_lat + max_lat) / 2
width_km = width * 111 * Math.cos(avg_lat * Math::PI / 180)
height_km = height * 111
width_km * height_km
end
def validate_bbox_order
errors.add(:base, 'min_lon must be less than max_lon') if min_lon >= max_lon
errors.add(:base, 'min_lat must be less than max_lat') if min_lat >= max_lat