mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Draft exporting as GeoJSON
This commit is contained in:
parent
41f0a713ea
commit
80b2f8831d
5 changed files with 58 additions and 5 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
class ExportJob < ApplicationJob
|
class ExportJob < ApplicationJob
|
||||||
queue_as :exports
|
queue_as :exports
|
||||||
|
|
||||||
def perform(export_id, start_at, end_at, format: :json)
|
def perform(export_id, start_at, end_at, format: :geojson)
|
||||||
export = Export.find(export_id)
|
export = Export.find(export_id)
|
||||||
|
|
||||||
Exports::Create.new(export:, start_at:, end_at:, format:).call
|
Exports::Create.new(export:, start_at:, end_at:, format:).call
|
||||||
|
|
|
||||||
17
app/serializers/point_serializer.rb
Normal file
17
app/serializers/point_serializer.rb
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class PointSerializer
|
||||||
|
EXCLUDED_ATTRIBUTES = %w[created_at updated_at visit_id id import_id user_id].freeze
|
||||||
|
|
||||||
|
def initialize(point)
|
||||||
|
@point = point
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
point.attributes.except(*EXCLUDED_ATTRIBUTES)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :point
|
||||||
|
end
|
||||||
29
app/serializers/points/geojson_serializer.rb
Normal file
29
app/serializers/points/geojson_serializer.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Points::GeojsonSerializer
|
||||||
|
def initialize(points)
|
||||||
|
@points = points
|
||||||
|
end
|
||||||
|
|
||||||
|
# rubocop:disable Metrics/MethodLength
|
||||||
|
def call
|
||||||
|
{
|
||||||
|
type: 'FeatureCollection',
|
||||||
|
features: points.map do |point|
|
||||||
|
{
|
||||||
|
type: 'Feature',
|
||||||
|
geometry: {
|
||||||
|
type: 'Point',
|
||||||
|
coordinates: [point.longitude, point.latitude]
|
||||||
|
},
|
||||||
|
properties: PointSerializer.new(point).call
|
||||||
|
}
|
||||||
|
end
|
||||||
|
}.to_json
|
||||||
|
end
|
||||||
|
# rubocop:enable Metrics/MethodLength
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :points
|
||||||
|
end
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Exports::Create
|
class Exports::Create
|
||||||
def initialize(export:, start_at:, end_at:, format: :json)
|
def initialize(export:, start_at:, end_at:, format: :geojson)
|
||||||
@export = export
|
@export = export
|
||||||
@user = export.user
|
@user = export.user
|
||||||
@start_at = start_at.to_datetime
|
@start_at = start_at.to_datetime
|
||||||
|
|
@ -18,7 +18,13 @@ class Exports::Create
|
||||||
|
|
||||||
Rails.logger.debug "====Exporting #{points.size} points"
|
Rails.logger.debug "====Exporting #{points.size} points"
|
||||||
|
|
||||||
data = ::ExportSerializer.new(points, user.email).call
|
data =
|
||||||
|
case format
|
||||||
|
when :geojson then process_geojson_export(points)
|
||||||
|
when :gpx then process_gpx_export(points)
|
||||||
|
else raise ArgumentError, "Unsupported format: #{format}"
|
||||||
|
end
|
||||||
|
|
||||||
file_path = Rails.root.join('public', 'exports', "#{export.name}.#{format}")
|
file_path = Rails.root.join('public', 'exports', "#{export.name}.#{format}")
|
||||||
|
|
||||||
File.open(file_path, 'w') { |file| file.write(data) }
|
File.open(file_path, 'w') { |file| file.write(data) }
|
||||||
|
|
@ -62,7 +68,8 @@ class Exports::Create
|
||||||
).call
|
).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_json_export(points)
|
def process_geojson_export(points)
|
||||||
|
Points::GeojsonSerializer.new(points).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_gpx_export(points)
|
def process_gpx_export(points)
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="w-full md:w-2/12">
|
<div class="w-full md:w-2/12">
|
||||||
<div class="flex flex-col space-y-2 text-center">
|
<div class="flex flex-col space-y-2 text-center">
|
||||||
<%= link_to 'Export points', exports_path(start_at: @start_at, end_at: @end_at), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure? This will start background process of exporting points withing timeframe, selected between #{@start_at} and #{@end_at}", turbo_method: :post }, class: "px-4 py-2 bg-green-500 text-white rounded-md join-item" %>
|
<%= link_to 'Export GeoJSON', exports_path(start_at: @start_at, end_at: @end_at, file_format: :geojson), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure? This will start background process of exporting points withing timeframe, selected between #{@start_at} and #{@end_at}", turbo_method: :post }, class: "px-4 py-2 bg-green-500 text-white rounded-md join-item" %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<%# <div class="w-full md:w-2/12"> %>
|
<%# <div class="w-full md:w-2/12"> %>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue