From 217d6249e2ea146abbc04dbb0e50ff06eab297ed Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sun, 23 Feb 2025 22:59:14 +0100 Subject: [PATCH] Use lonlat in maps controller --- CHANGELOG.md | 2 +- app/controllers/map_controller.rb | 4 ++-- app/models/concerns/distanceable.rb | 2 -- ...at_to_points.rb => 20250221181805_add_lonlat_to_points.rb} | 0 ...4430_remove_points_latitude_longitude_uniqueness_index.rb} | 0 db/schema.rb | 2 +- 6 files changed, 4 insertions(+), 6 deletions(-) rename db/migrate/{20250221181905_add_lonlat_to_points.rb => 20250221181805_add_lonlat_to_points.rb} (100%) rename db/migrate/{20250221201930_remove_points_latitude_longitude_uniqueness_index.rb => 20250221194430_remove_points_latitude_longitude_uniqueness_index.rb} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index af345dfa..8b6cac6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## TODO: - Realtime broadcast for importing progress -- Frontend update to use `lonlat` column. ## Fixed @@ -25,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Points are now using `lonlat` column for storing longitude and latitude. - Semantic history points are now being imported much faster. - GPX files are now being imported much faster. +- Distance calculation are now using Postgis functions and expected to be more accurate. # 0.24.1 - 2025-02-13 diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index bad160d5..cd1f73d1 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -7,8 +7,8 @@ class MapController < ApplicationController @points = points.where('timestamp >= ? AND timestamp <= ?', start_at, end_at) @coordinates = - @points.pluck(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id, :country) - .map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] } + @points.pluck(:lonlat, :battery, :altitude, :timestamp, :velocity, :id, :country) + .map { |lonlat, *rest| [lonlat.y.to_f, lonlat.x.to_f, *rest.map(&:to_s)] } @distance = distance @start_at = Time.zone.at(start_at) @end_at = Time.zone.at(end_at) diff --git a/app/models/concerns/distanceable.rb b/app/models/concerns/distanceable.rb index 9a220aad..6b2d1546 100644 --- a/app/models/concerns/distanceable.rb +++ b/app/models/concerns/distanceable.rb @@ -105,8 +105,6 @@ module Distanceable RGeo::Geographic.spherical_factory(srid: 4326).point(point[1], point[0]) when self.class point.lonlat - else - nil end end end diff --git a/db/migrate/20250221181905_add_lonlat_to_points.rb b/db/migrate/20250221181805_add_lonlat_to_points.rb similarity index 100% rename from db/migrate/20250221181905_add_lonlat_to_points.rb rename to db/migrate/20250221181805_add_lonlat_to_points.rb diff --git a/db/migrate/20250221201930_remove_points_latitude_longitude_uniqueness_index.rb b/db/migrate/20250221194430_remove_points_latitude_longitude_uniqueness_index.rb similarity index 100% rename from db/migrate/20250221201930_remove_points_latitude_longitude_uniqueness_index.rb rename to db/migrate/20250221194430_remove_points_latitude_longitude_uniqueness_index.rb diff --git a/db/schema.rb b/db/schema.rb index 02e602bc..757a2f3b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_02_21_151930) do +ActiveRecord::Schema[8.0].define(version: 2025_02_21_194509) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "postgis"