Skip to content

Commit

Permalink
Regions of China.
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Jul 3, 2015
1 parent 4af5067 commit 546d220
Show file tree
Hide file tree
Showing 11 changed files with 38,786 additions and 2 deletions.
Empty file added .gitignore
Empty file.
11 changes: 11 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is the official list of regions-of-china authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.

# Names should be added to this file as
# Name or Organization <email address>
# The email address is not required for organizations.

# Please keep the list sorted.

Dong Liu <[email protected]>
19 changes: 19 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This is the official list of people who can contribute
# (and typically have contributed) code to the regions-of-china repository.
# The AUTHORS file lists the copyright holders; this file
# lists people.
#
# The submission process automatically checks to make sure
# that people submitting code are listed in this file (by email address).

# Names should be added to this file like so:
# Name <email address>
#
# An entry with two email addresses specifies that the
# first address should be used in the submit logs and
# that the second address should be recognized as the
# same person when interacting with Rietveld.

# Please keep the list sorted.

Dong Liu <[email protected]>
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2015 The regions-of-china Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Dong Liu <[email protected]>. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# regions-of-china
The administrative divisions of China, privated sql, txt format data.
# Regions of China
The administrative divisions of China, provade CSV, SQL, text, Excel and XML data format.

# Data format
* regions-of-china.csv CSV format.
* regions-of-china.sql SQL format.
* regions-of-china.txt Text format.
* regions-of-china.xlsx Excel format.
* regions-of-china.xml XML format.

# References
* https://en.wikipedia.org/wiki/List_of_regions_of_the_People%27s_Republic_of_China
* https://en.wikipedia.org/wiki/Administrative_divisions_of_China
* https://en.wikipedia.org/wiki/Provinces_of_China
* http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201504/t20150415_712722.html
68 changes: 68 additions & 0 deletions convertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2015 The regions-of-china Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"log"
"bufio"
"os"
"fmt"
"strings"
)

func main() {
fr, err := os.Open("regions-of-china.txt")
if err != nil {
log.Fatal(err)
}

defer fr.Close()

fw, err := os.Create("regions-of-china.sql")
if err != nil {
log.Fatal(err)
}

defer fw.Close()

provinceID := "0"
provinceName := ""
cityID := "0"
cityName := ""
queryTpl := "('%s', '%s', '%s', '%s', '%s', '%s'),"

scanner := bufio.NewScanner(fr)
writer := bufio.NewWriter(fw)

fmt.Fprintln(writer, "INSERT INTO region (`district_id`, `district_name`, `city_id`, `city_name`, `province_id`, `province_name`) VALUES")

for scanner.Scan() {
t := scanner.Text()
if t[0] == '#' {
continue
}

str := ""
nodes := strings.Split(t, " ")
switch len(nodes) {
case 2: // Province
provinceID = nodes[0]
provinceName = nodes[1]
str = fmt.Sprintf(queryTpl, "0", "", "0", "", provinceID, provinceName)
case 3: // City
cityID = nodes[0]
cityName = nodes[2]
str = fmt.Sprintf(queryTpl, "0", "", cityID, cityName, provinceID, provinceName)
case 4: // District
str = fmt.Sprintf(queryTpl, nodes[0], nodes[3], cityID, cityName, provinceID, provinceName)
default: // Err
fmt.Printf("$s\n", t)
}

fmt.Fprintln(writer, str)
}

writer.Flush()
}
Loading

0 comments on commit 546d220

Please sign in to comment.