GithubHelp home page GithubHelp logo

guoyu07 / city_demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cheenwe/city_demo

0.0 1.0 0.0 1.58 MB

国家-省-市-县-镇 数据模型及数据

Ruby 74.46% JavaScript 1.76% CoffeeScript 0.56% CSS 5.03% HTML 18.19%

city_demo's Introduction

国家及城市表单的实现

使用同一张表实现:国家-省-市-县-镇 的级联

字段整理如下:

表名:locals

字段名 名称 类型
name 名称 string
type 类型 string
parent_id 父级 integer
abbr 缩写 string

定义读文档方法

def path name
  File.read("#{Rails.root}/db/#{name}")
end

国家信息

# countries = YAML::load(path("country.json"))
countries =  JSON.parse(path("country.json"))

n = 100000
countries.each do |key, value|
  n= n+1
  Local::Contuny.create(id: n, name: value, abbr: key ) unless Local::Contuny.find_by_abbr(key).present?
end

省市县及街道

areas = JSON.parse(path("areas.json"))

streets = areas.values.flatten
contuny_id = Local::Contuny.find_by_abbr("CN").id
streets.each do |street|
  id = street['id']
  text = street['text']
  abbr = Pinyin.t(text) { |letters| letters[0].upcase }

  if id.size == 6    # 省市区
    if id.end_with?('0000')                           # 省
      Local::Province.create(name: text, id: id[0..1], abbr:abbr, parent_id: contuny_id ) unless Local.find_by_id(id[0..1]).present?

    elsif id.end_with?('00')                          # 市
      Local::City.create(name: text, id: id[0..3], abbr:abbr, parent_id: id[0..1] )  unless Local.find_by_id(id[0..3]).present?
    else #区
      Local::District.create(name: text, id: id, abbr:abbr, parent_id: id[0..3] )  unless Local.find_by_id(id).present?
    end
  else
    Local::Street.create(name: text, id: id, abbr:abbr, parent_id: id[0..5] )  unless Local.find_by_id(id).present?
  end
end

用法

1.直接把db/locals.sql 导入到数据库 2.36 sec

2.原始数据: 比较慢 47907 条数据 用了53分钟

rake local:country rake local:area

3.使用json数据

rake local:init

city_demo's People

Contributors

cheenwe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.