Created
August 20, 2020 09:22
-
-
Save FlowerWrong/62f412299831910726351ad3013442ad to your computer and use it in GitHub Desktop.
China region
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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