Draft exporting as GeoJSON

This commit is contained in:
Eugene Burmakin 2024-09-02 20:51:34 +02:00
parent 41f0a713ea
commit 80b2f8831d
5 changed files with 58 additions and 5 deletions

View file

@ -3,7 +3,7 @@
class ExportJob < ApplicationJob
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)
Exports::Create.new(export:, start_at:, end_at:, format:).call

View 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

View 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

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Exports::Create
def initialize(export:, start_at:, end_at:, format: :json)
def initialize(export:, start_at:, end_at:, format: :geojson)
@export = export
@user = export.user
@start_at = start_at.to_datetime
@ -18,7 +18,13 @@ class Exports::Create
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.open(file_path, 'w') { |file| file.write(data) }
@ -62,7 +68,8 @@ class Exports::Create
).call
end
def process_json_export(points)
def process_geojson_export(points)
Points::GeojsonSerializer.new(points).call
end
def process_gpx_export(points)

View file

@ -22,7 +22,7 @@
</div>
<div class="w-full md:w-2/12">
<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 class="w-full md:w-2/12"> %>