2020-08-13 13:36:35 +02:00
|
|
|
namespace :after_party do
|
|
|
|
desc 'Deployment task: fix_geo_areas_geometry'
|
|
|
|
task fix_geo_areas_geometry: :environment do
|
|
|
|
puts "Running deploy task 'fix_geo_areas_geometry'"
|
|
|
|
|
|
|
|
geometry_collections = GeoArea.where("geometry -> 'type' = '\"GeometryCollection\"'")
|
|
|
|
multi_polygons = GeoArea.where("geometry -> 'type' = '\"MultiPolygon\"'")
|
|
|
|
|
2020-08-13 17:00:05 +02:00
|
|
|
def valid_geometry?(geometry)
|
|
|
|
RGeo::GeoJSON.decode(geometry.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
progress = ProgressReport.new(geometry_collections.count)
|
2020-08-13 13:36:35 +02:00
|
|
|
geometry_collections.find_each do |geometry_collection|
|
|
|
|
geometry_collection.geometry['geometries'].each do |geometry|
|
2020-08-13 17:00:05 +02:00
|
|
|
if valid_geometry?(geometry)
|
|
|
|
geometry_collection.champ.geo_areas.create!(geometry: geometry, source: 'selection_utilisateur')
|
|
|
|
end
|
2020-08-13 13:36:35 +02:00
|
|
|
end
|
2020-08-13 17:00:05 +02:00
|
|
|
|
|
|
|
geometry_collection.destroy
|
|
|
|
progress.inc
|
2020-08-13 13:36:35 +02:00
|
|
|
end
|
2020-08-13 17:00:05 +02:00
|
|
|
progress.finish
|
2020-08-13 13:36:35 +02:00
|
|
|
|
2020-08-13 17:00:05 +02:00
|
|
|
progress = ProgressReport.new(multi_polygons.count)
|
2020-08-13 13:36:35 +02:00
|
|
|
multi_polygons.find_each do |multi_polygon|
|
|
|
|
multi_polygon.geometry['coordinates'].each do |coordinates|
|
2020-08-13 17:00:05 +02:00
|
|
|
geometry = {
|
2020-08-13 13:36:35 +02:00
|
|
|
type: 'Polygon',
|
|
|
|
coordinates: coordinates
|
2020-08-13 17:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if valid_geometry?(geometry)
|
|
|
|
multi_polygon.champ.geo_areas.create!(geometry: geometry, source: 'selection_utilisateur')
|
|
|
|
end
|
2020-08-13 13:36:35 +02:00
|
|
|
end
|
|
|
|
|
2020-08-13 17:00:05 +02:00
|
|
|
multi_polygon.destroy
|
|
|
|
progress.inc
|
|
|
|
end
|
|
|
|
progress.finish
|
2020-08-13 13:36:35 +02:00
|
|
|
|
|
|
|
# Update task as completed. If you remove the line below, the task will
|
|
|
|
# run with every deploy (or every time you call after_party:run).
|
|
|
|
AfterParty::TaskRecord
|
|
|
|
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
|
|
|
|
end
|
|
|
|
end
|