cnregion

package module
v2.0.0-...-49787b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 11 Imported by: 1

README

cnregion

Test Go version PkgGoDev codecov License

历年统计用区域和城乡划分代码,数据来源于 https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/。 符合国家标准 GB/T 2260 与 GB/T 10114。

关于版本号,主版本号代码不兼容性更改,次版本号代码最后一次生成的数据年份,BUG 修正和兼容性的功能增加则增加修订版本号。

v, err := cnregion.LoadFile("./data/regions.db", "-", 2020)

p := v.Provinces() // 返回所有省列表
cities := p[0].Items() // 返回该省下的所有市
counties := cities[0].Items() // 返回该市下的所有县
towns := counties[0].Items() // 返回所有镇
villages := towns[0].Items() // 所有村和街道信息

d := v.Districts() // 按以前的行政大区进行划分
provinces := d[0].Items() // 该大区下的所有省份

list := v.Search(&SearchOptions{Text: "温州"}) // 按索地名中带温州的区域列表

对采集的数据进行了一定的加工,以减少文件的体积,文件保存在 ./data/regions.db 中。

安装

go get github.com/issue9/cnregion/v2

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package cnregion 中国区域划分代码

中国行政区域五级划分代码,包含了省、市、县、乡和村五个级别。 数据规则以及数据来源

Index

Constants

View Source
const Version = 1

Version 数据文件的版本号

Variables

View Source
var ErrIncompatible = errors.New("数据文件版本不兼容")

ErrIncompatible 数据文件版本不兼容

当数据文件中指定的版本号与当前的 Version 不相等时,返回此错误。

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB 区域数据库信息

数据格式:

1:[versions]:{id:name:yearIndex:size{}}

- 1 表示数据格式的版本,采用当前包的 Version 常量;
- versions 表示当前数据文件中的数据支持的年份列表,以逗号分隔;
- id 当前区域的 ID;
- name 当前区域的名称;
- yearIndex 此条数据支持的年份列表,每一个位表示一个年份在 versions 中的索引值;
- size 表示子元素的数量;

func Load

func Load(data []byte, separator string, compress bool, version ...int) (*DB, error)

Load 将数据内容加载至 DB 对象

version 仅加载指定年份的数据,如果为空,则加载所有数据;

func LoadFS

func LoadFS(f fs.FS, file, separator string, compress bool, version ...int) (*DB, error)

LoadFS 从数据文件加载数据

func LoadFile

func LoadFile(file, separator string, compress bool, version ...int) (*DB, error)

LoadFile 从数据文件加载数据

func NewDB

func NewDB() *DB

NewDB 返回空的 DB 对象

func (*DB) AddItem

func (db *DB) AddItem(regionID, name string, ver int) error

AddItem 添加一条子项

func (*DB) AddVersion

func (db *DB) AddVersion(ver int) (ok bool)

AddVersion 添加新的版本号

func (*DB) Districts

func (db *DB) Districts() []*Region

Districts 按行政大区划分

NOTE: 大区划分并不统一,按照各个省份的第一个数字进行划分。

func (*DB) Dump

func (db *DB) Dump(file string, compress bool) error

Dump 输出到文件

func (*DB) Find

func (db *DB) Find(regionID string) *Region

Find 查找指定 ID 对应的信息

func (*DB) Provinces

func (db *DB) Provinces() []*Region

Provinces 省份列表

func (*DB) Search

func (db *DB) Search(opt *Options) []*Region

Search 简单的搜索功能

func (*DB) Versions

func (db *DB) Versions() []int

Version 当前这份数据支持的年份列表

type Options

type Options struct {
	// 表示你需要搜索的地名需要包含的内容
	//
	// 不能是多个名称的组合,比如"浙江温州",直接写"温州"就可以。
	// 也不要提供类似于"居委会"这种无实际意义的地名;
	Text string

	// 上一级的区域 ID
	//
	// 为空表示不限制。
	Parent string

	// 搜索的城市类型
	//
	// 该值取值于 github.com/issue9/cnregion/v2/id.Level 类型。 多个值可以通过或运算叠加。
	// 0 表示所有类型。
	Level id.Level

	// 最大的搜索数量。0 表示不限制数量。
	Max int
	// contains filtered or unexported fields
}

Options 搜索选项

type Region

type Region struct {
	// contains filtered or unexported fields
}

Region 表示单个区域

func (*Region) FullID

func (r *Region) FullID() string

func (*Region) FullName

func (r *Region) FullName() string

func (*Region) ID

func (r *Region) ID() string

func (*Region) IsSupported

func (r *Region) IsSupported(ver int) bool

IsSupported 当前数据是否支持该年份

func (*Region) Items

func (r *Region) Items() []*Region

func (*Region) Name

func (r *Region) Name() string

func (*Region) Versions

func (r *Region) Versions() []int

Directories

Path Synopsis
Package id 针对 ID 的一些操作函数
Package id 针对 ID 的一些操作函数
Package version 提供版本的相关信息
Package version 提供版本的相关信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL