Skip to content

Instantly share code, notes, and snippets.

@FlowerWrong
Created August 20, 2020 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlowerWrong/62f412299831910726351ad3013442ad to your computer and use it in GitHub Desktop.
Save FlowerWrong/62f412299831910726351ad3013442ad to your computer and use it in GitHub Desktop.
China region
# rails g model ChinaRegion name parent_id lng lat
class AddChinaRegionData < ActiveRecord::Migration[6.0]
def up
# https://github.com/pfinal/city
ChinaRegion.destroy_all
ActiveRecord::Base.connection.reset_pk_sequence!(ChinaRegion.table_name)
data = []
File.open(Rails.root.join('db', 'data', 'region.txt')).each do |line|
next unless /\((.*)\).*/ =~ line
ld = Regexp.last_match(1).split(',').map { |e| e.delete("'") }
data << {
id: ld[0].to_i,
name: ld[1],
parent_id: (ld[2].present? ? ld[2].to_i : nil),
lnglat: Point.new(ld[3], ld[4]).to_s
}
end
data.each do |r|
ChinaRegion.create!(r)
end
data = []
end
def down
ChinaRegion.destroy_all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment