mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 17:21:38 -05:00
Add lonlat to places
This commit is contained in:
parent
5521d8e6b2
commit
c2f6224421
9 changed files with 67 additions and 10 deletions
13
app/jobs/data_migrations/migrate_places_lonlat_job.rb
Normal file
13
app/jobs/data_migrations/migrate_places_lonlat_job.rb
Normal 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
|
||||||
|
|
@ -1,17 +1,27 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Place < ApplicationRecord
|
class Place < ApplicationRecord
|
||||||
DEFAULT_NAME = 'Suggested place'
|
include Nearable
|
||||||
reverse_geocoded_by :latitude, :longitude
|
include Distanceable
|
||||||
|
|
||||||
validates :name, :longitude, :latitude, presence: true
|
DEFAULT_NAME = 'Suggested place'
|
||||||
|
|
||||||
|
validates :name, :lonlat, presence: true
|
||||||
|
|
||||||
has_many :visits, dependent: :destroy
|
has_many :visits, dependent: :destroy
|
||||||
has_many :place_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 }
|
enum :source, { manual: 0, photon: 1 }
|
||||||
|
|
||||||
|
def lon
|
||||||
|
lonlat.x
|
||||||
|
end
|
||||||
|
|
||||||
|
def lat
|
||||||
|
lonlat.y
|
||||||
|
end
|
||||||
|
|
||||||
def async_reverse_geocode
|
def async_reverse_geocode
|
||||||
return unless DawarichSettings.reverse_geocoding_enabled?
|
return unless DawarichSettings.reverse_geocoding_enabled?
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,8 +79,7 @@ class ReverseGeocoding::Places::FetchData
|
||||||
return found_place if found_place.present?
|
return found_place if found_place.present?
|
||||||
|
|
||||||
Place.find_or_initialize_by(
|
Place.find_or_initialize_by(
|
||||||
latitude: place_data['geometry']['coordinates'][1].to_f.round(5),
|
lonlat: "POINT(#{place_data['geometry']['coordinates'][0].to_f.round(5)} #{place_data['geometry']['coordinates'][1].to_f.round(5)})"
|
||||||
longitude: place_data['geometry']['coordinates'][0].to_f.round(5)
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -97,7 +96,7 @@ class ReverseGeocoding::Places::FetchData
|
||||||
|
|
||||||
def reverse_geocoded_places
|
def reverse_geocoded_places
|
||||||
data = Geocoder.search(
|
data = Geocoder.search(
|
||||||
[place.latitude, place.longitude],
|
[place.lat, place.lon],
|
||||||
limit: 10,
|
limit: 10,
|
||||||
distance_sort: true,
|
distance_sort: true,
|
||||||
radius: 1,
|
radius: 1,
|
||||||
|
|
|
||||||
|
|
@ -351,8 +351,7 @@ class Visits::SmartDetect
|
||||||
nearby_organizations.each do |org|
|
nearby_organizations.each do |org|
|
||||||
Place.create!(
|
Place.create!(
|
||||||
name: org[:name],
|
name: org[:name],
|
||||||
latitude: org[:latitude],
|
lonlat: "POINT(#{org[:longitude]} #{org[:latitude]})",
|
||||||
longitude: org[:longitude],
|
|
||||||
city: org[:city],
|
city: org[:city],
|
||||||
country: org[:country],
|
country: org[:country],
|
||||||
geodata: org[:geodata],
|
geodata: org[:geodata],
|
||||||
|
|
|
||||||
13
db/data/20250303194123_migrate_places_lonlat.rb
Normal file
13
db/data/20250303194123_migrate_places_lonlat.rb
Normal 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
|
||||||
7
db/migrate/20250303194009_add_lonlat_to_places.rb
Normal file
7
db/migrate/20250303194009_add_lonlat_to_places.rb
Normal 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
|
||||||
9
db/migrate/20250303194043_add_lonlat_index_to_places.rb
Normal file
9
db/migrate/20250303194043_add_lonlat_index_to_places.rb
Normal 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
4
db/schema.rb
generated
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_catalog.plpgsql"
|
enable_extension "pg_catalog.plpgsql"
|
||||||
enable_extension "postgis"
|
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 "reverse_geocoded_at"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_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
|
end
|
||||||
|
|
||||||
create_table "points", force: :cascade do |t|
|
create_table "points", force: :cascade do |t|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe DataMigrations::MigratePlacesLonlatJob, type: :job do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue