Add lonlat to places

This commit is contained in:
Eugene Burmakin 2025-03-03 20:50:49 +01:00
parent 5521d8e6b2
commit c2f6224421
9 changed files with 67 additions and 10 deletions

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
class DataMigrations::MigratePlacesLonlatJob < ApplicationJob
queue_as :default
def perform(user_id)
user = User.find(user_id)
# rubocop:disable Rails/SkipsModelValidations
user.places.update_all('lonlat = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)')
# rubocop:enable Rails/SkipsModelValidations
end
end

View file

@ -1,17 +1,27 @@
# frozen_string_literal: true
class Place < ApplicationRecord
DEFAULT_NAME = 'Suggested place'
reverse_geocoded_by :latitude, :longitude
include Nearable
include Distanceable
validates :name, :longitude, :latitude, presence: true
DEFAULT_NAME = 'Suggested place'
validates :name, :lonlat, presence: true
has_many :visits, dependent: :destroy
has_many :place_visits, dependent: :destroy
has_many :suggested_visits, through: :place_visits, source: :visit
has_many :suggested_visits, -> { distinct }, through: :place_visits, source: :visit
enum :source, { manual: 0, photon: 1 }
def lon
lonlat.x
end
def lat
lonlat.y
end
def async_reverse_geocode
return unless DawarichSettings.reverse_geocoding_enabled?

View file

@ -79,8 +79,7 @@ class ReverseGeocoding::Places::FetchData
return found_place if found_place.present?
Place.find_or_initialize_by(
latitude: place_data['geometry']['coordinates'][1].to_f.round(5),
longitude: place_data['geometry']['coordinates'][0].to_f.round(5)
lonlat: "POINT(#{place_data['geometry']['coordinates'][0].to_f.round(5)} #{place_data['geometry']['coordinates'][1].to_f.round(5)})"
)
end
@ -97,7 +96,7 @@ class ReverseGeocoding::Places::FetchData
def reverse_geocoded_places
data = Geocoder.search(
[place.latitude, place.longitude],
[place.lat, place.lon],
limit: 10,
distance_sort: true,
radius: 1,

View file

@ -351,8 +351,7 @@ class Visits::SmartDetect
nearby_organizations.each do |org|
Place.create!(
name: org[:name],
latitude: org[:latitude],
longitude: org[:longitude],
lonlat: "POINT(#{org[:longitude]} #{org[:latitude]})",
city: org[:city],
country: org[:country],
geodata: org[:geodata],

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
class MigratePlacesLonlat < ActiveRecord::Migration[8.0]
def up
User.find_each do |user|
DataMigrations::MigratePlacesLonlatJob.perform_later(user.id)
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddLonlatToPlaces < ActiveRecord::Migration[8.0]
def change
add_column :places, :lonlat, :st_point, geographic: true
end
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class AddLonlatIndexToPlaces < ActiveRecord::Migration[8.0]
disable_ddl_transaction!
def change
add_index :places, :lonlat, using: :gist, algorithm: :concurrently
end
end

4
db/schema.rb generated
View file

@ -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_194509) do
ActiveRecord::Schema[8.0].define(version: 2025_03_03_194043) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "postgis"
@ -125,6 +125,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_21_194509) do
t.datetime "reverse_geocoded_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.geography "lonlat", limit: {srid: 4326, type: "st_point", geographic: true}
t.index ["lonlat"], name: "index_places_on_lonlat", using: :gist
end
create_table "points", force: :cascade do |t|

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe DataMigrations::MigratePlacesLonlatJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
end