Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KML Importer #362

Open
ut opened this issue Jan 27, 2025 · 0 comments
Open

KML Importer #362

ut opened this issue Jan 27, 2025 · 0 comments

Comments

@ut
Copy link
Member

ut commented Jan 27, 2025

Approach


require 'nokogiri'

module KMLImporter
  class Importer
    def self.import_kml(file_path)
      kml_data = File.read(file_path)
      doc = Nokogiri::XML(kml_data)

      # Process the KML data here and save it to your application's model
      # Example: Parse the KML data and save it to a Location model
      locations = []

      doc.css('Placemark').each do |placemark|
        name = placemark.css('name').text
        coordinates = placemark.css('coordinates').text.split(',').map(&:strip)
        lat, lon = coordinates[1], coordinates[0] # KML uses (lon, lat) order

        # Create or update Location records with parsed data
        location = Location.find_or_initialize_by(name: name)
        location.latitude = lat.to_f
        location.longitude = lon.to_f
        locations << location
      end

      # Save all parsed locations
      Location.transaction do
        locations.each(&:save)
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant