<commit_before>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\"'")

    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)
    geometry_collections.find_each do |geometry_collection|
      geometry_collection.geometry['geometries'].each do |geometry|
        if valid_geometry?(geometry)
          geometry_collection.champ.geo_areas.create!(geometry: geometry, source: 'selection_utilisateur')
        end
      end

      geometry_collection.destroy
      progress.inc
    end<commit_msg>Fix MultiLineString geometries<commit_after>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\"'")
    multi_line_strings = GeoArea.where("geometry -> 'type' = '\"MultiLineString\"'")

    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)
    geometry_collections.find_each do |geometry_collection|
      geometry_collection.geometry['geometries'].each do |geometry|
        if valid_geometry?(geometry)
          geometry_collection.champ.geo_areas.create!(geometry: geometry, source: 'selection_utilisateur')
        end
      end

      geometry_collection.destroy
      progress.inc
    end
    progress.finish

    progress = ProgressReport.new(multi_line_strings.count)
    multi_line_strings.find_each do |multi_line_string|
      multi_line_string.geometry['coordinates'].each do |coordinates|
        geometry = {
          type: 'LineString',
          coordinates: coordinates
        }

        if valid_geometry?(geometry)
          multi_line_string.champ.geo_areas.create!(geometry: geometry, source: 'selection_utilisateur')
        end
      end

      multi_line_string.destroy
      progress.inc
    end
{'fraction_non_alphanumeric': 0.09501557632398754, 'fraction_numerical': 0.0, 'mean_word_length': 3.7137614678899085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1889206', 'n_tokens_mistral': 758, 'n_tokens_neox': 747, 'n_words': 134}