├── .gitignore
├── .travis.yml
├── CNAME
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
├── assembly
│ └── release.xml
├── java
│ └── tech
│ │ └── spiro
│ │ └── addrparser
│ │ ├── common
│ │ ├── ContainPointJudge.java
│ │ ├── ContainPointJudgeFactory.java
│ │ ├── NotImplementedException.java
│ │ ├── Point.java
│ │ ├── RegionConverter.java
│ │ ├── RegionDTO.java
│ │ ├── RegionInfo.java
│ │ └── RegionLevel.java
│ │ ├── crawler
│ │ ├── DataResp.java
│ │ ├── GetRegionException.java
│ │ ├── RegionDataCrawler.java
│ │ ├── RegionResp.java
│ │ └── RestClient.java
│ │ ├── io
│ │ ├── IOPipeline.java
│ │ ├── RegionDataInput.java
│ │ ├── RegionDataOutput.java
│ │ ├── RegionDataReport.java
│ │ ├── file
│ │ │ ├── BinaryFileRegionDataOutput.java
│ │ │ ├── JSONFileRegionDataInput.java
│ │ │ └── JSONFileRegionDataOutput.java
│ │ └── rdbms
│ │ │ ├── RdbmsRegionDTOWrapper.java
│ │ │ ├── RdbmsRegionDataInput.java
│ │ │ ├── RdbmsRegionDataOutput.java
│ │ │ └── RdbmsSQL.java
│ │ ├── parser
│ │ ├── DefaultContainPointJudge.java
│ │ ├── Location.java
│ │ ├── LocationParserEngine.java
│ │ └── ParserEngineException.java
│ │ └── tool
│ │ ├── CrawlerServer.java
│ │ ├── JSONFile2MySQL.java
│ │ ├── MapTools.java
│ │ └── MySQLTableInit.java
└── script
│ ├── crawler.bat
│ ├── crawler.sh
│ ├── jsonfile2mysql.bat
│ └── jsonfile2mysql.sh
└── test
└── java
└── tech
└── spiro
└── addrparser
└── io
└── rdbms
└── RdbmsSQLTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
24 | target
25 | .idea
26 | *.iml
27 |
28 | !china-region.zip
29 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - openjdk8
4 |
--------------------------------------------------------------------------------
/CNAME:
--------------------------------------------------------------------------------
1 | addrparser.spiro.tech
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/hsp8712/addrparser)
2 | [](https://maven-badges.herokuapp.com/maven-central/tech.spiro/addrparser/)
3 | [](https://github.com/hsp8712/addrparser/releases)
4 | [](https://www.apache.org/licenses/LICENSE-2.0.html)
5 |
6 | # addrparser
7 | 离线高效的解析中国范围内的经纬度为省市区信息,省市区信息包括: 行政区划编码、行政区划中文名称、行政区域的中心点经纬度,行政区域的边界点经纬度集合。
8 |
9 | # 依赖安装
10 | ***依赖 jdk1.8***
11 | ## Maven
12 | ```xml
13 |
14 | tech.spiro
15 | addrparser
16 | 1.1
17 |
18 | ```
19 | ## Jar文件
20 | [addrparser-1.1](http://repo1.maven.org/maven2/tech/spiro/addrparser/1.1/addrparser-1.1.jar)
21 |
22 | # 使用说明-解析地址
23 | ## 下载数据文件
24 | [china-region-20190902.zip](https://github.com/hsp8712/addrparser/releases/download/addrparser-1.0.1/china-region-20190902.zip)
25 | 解压后得到文件:china-region.json,文件中包括了所有的省市区行政区域信息,包括编码、名称、中心点、边界点集合。
26 |
27 | **经纬度数据基于GCJ-02坐标系**
28 |
29 | ## 方式一 (文件)
30 | 直接使用china-region.json文件作为基础数据。
31 |
32 | 示例代码:
33 | ```java
34 |
35 | // china-region.json文件作为基础数据
36 | RegionDataInput regionDataInput = new JSONFileRegionDataInput("path/china-region.json");
37 |
38 | // 创建并初始化位置解析引擎,一般配置为全局单例
39 | LocationParserEngine engine = new LocationParserEngine(regionDataInput);
40 | // 初始化,加载数据,比较耗时
41 | engine.init();
42 |
43 | // 执行解析操作
44 | Location location = engine.parse(118.750934,32.038634);
45 |
46 | // 获取省市区信息
47 | RegionInfo provInfo = location.getProv();
48 | RegionInfo cityInfo = location.getCity();
49 | RegionInfo districtInfo = location.getDistrict();
50 | ```
51 |
52 | ## 方式二 (MySQL)
53 | 将数据导入mysql数据库,使用mysql中数据作为基础数据。
54 |
55 | 下载数据导入工具:
56 | [addrparser-1.1.tar.gz](https://github.com/hsp8712/addrparser/releases/download/addrparser-1.1/addrparser-1.1.tar.gz)
57 |
58 | 或通过源码编译工具: `mvn clean package -P tool`
59 |
60 |
61 | 解压后,进入addrparser/bin目录,执行jsonfile2mysql.sh (Linux) 或 jsonfile2mysql.bat (Windows)
62 |
63 | ```
64 | usage: JSONFile2MySQL
65 | -a,--password MySQL password
66 | -d,--db MySQL database
67 | -f,--file Json region data file path 上述china-region.json文件的路径
68 | -h,--host MySQL host
69 | -i,--init Init table schema
70 | -p,--port MySQL port, default 3306
71 | -t,--table-name Default: 'region_data', Region data table name
72 | -u,--user MySQL user
73 | ```
74 |
75 | 如果不指定-t,使用默认表名region_data
76 |
77 | 示例代码:
78 | ```java
79 | // javax.sql.DataSource 数据源
80 | DataSource ds = ...;
81 |
82 | // 关系型数据库区域数据输入
83 | RegionDataInput regionDataInput = new RdbmsRegionDataInput(ds);
84 |
85 | // 如果上述命令如果指定了定制的表名,则该处也要指定表名。
86 | // RegionDataInput regionDataInput = new RdbmsRegionDataInput(ds, customTableName);
87 |
88 | // 创建并初始化位置解析引擎,一般配置为全局单例
89 | LocationParserEngine engine = new LocationParserEngine(regionDataInput);
90 | // 初始化,加载数据,比较耗时
91 | engine.init();
92 |
93 | // 执行解析操作
94 | Location location = engine.parse(118.750934,32.038634);
95 |
96 | // 获取省市区信息
97 | RegionInfo provInfo = location.getProv();
98 | RegionInfo cityInfo = location.getCity();
99 | RegionInfo districtInfo = location.getDistrict();
100 | ```
101 |
102 | # 使用说明-地图工具
103 | `tech.spiro.addrparser.tool.MapTools` 类中包含了常用的地图工具方法:
104 |
105 | ## getPoint2PointDistance
106 | 获取点到点的距离
107 |
108 | ## getPoint2LineDistance
109 | 获取点到直线的距离
110 |
111 | ## inCircleArea
112 | 判断点是否在圆形范围内
113 |
114 | ## inRectangleArea
115 | 判断点是否在矩形范围内
116 |
117 | ## inPolygonArea
118 | 判断点是否在多边形范围内
119 |
120 | ## yawing
121 | 判断点是否偏离一个轨迹
122 |
123 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | tech.spiro
4 | addrparser
5 | 1.2-SNAPSHOT
6 | ${project.groupId}:${project.artifactId}
7 | Tool for parsing longitude/latitude to region info in china.
8 | https://github.com/hsp8712/addrparser
9 |
10 |
11 | The Apache Software License, Version 2.0
12 | http://www.apache.org/licenses/LICENSE-2.0.txt
13 |
14 |
15 |
16 |
17 | Spiro Huang
18 | hsp8712@qq.com
19 |
20 |
21 |
22 | scm:git:git@github.com:hsp8712/addrparser.git
23 | scm:git:git@github.com:hsp8712/addrparser.git
24 | https://github.com/hsp8712/addrparser/tree/master
25 | HEAD
26 |
27 |
28 |
29 | ossrh
30 | https://oss.sonatype.org/content/repositories/snapshots
31 |
32 |
33 | ossrh
34 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
35 |
36 |
37 |
38 |
39 | UTF-8
40 |
41 |
42 |
43 |
44 | junit
45 | junit
46 | 4.13.1
47 | test
48 |
49 |
50 | org.slf4j
51 | slf4j-api
52 | 1.7.25
53 |
54 |
55 | com.alibaba
56 | fastjson
57 | 1.2.83
58 |
59 |
60 | org.apache.httpcomponents
61 | httpclient
62 | 4.5.13
63 | provided
64 |
65 |
66 | commons-cli
67 | commons-cli
68 | 1.4
69 | provided
70 |
71 |
72 | mysql
73 | mysql-connector-java
74 | 8.0.28
75 | provided
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | org.apache.maven.plugins
84 | maven-compiler-plugin
85 | 3.3
86 |
87 | 1.8
88 | 1.8
89 | UTF-8
90 |
91 |
92 |
93 | org.apache.maven.plugins
94 | maven-resources-plugin
95 | 2.7
96 |
97 | UTF-8
98 |
99 |
100 |
101 | org.apache.maven.plugins
102 | maven-jar-plugin
103 | 2.5
104 |
105 |
106 | db
107 |
108 |
109 |
110 |
111 | org.apache.maven.plugins
112 | maven-release-plugin
113 | 2.5.3
114 |
115 | true
116 | false
117 | release
118 | deploy
119 |
120 |
121 |
122 |
123 |
124 |
125 | release
126 |
127 |
128 |
129 | org.apache.maven.plugins
130 | maven-source-plugin
131 | 2.2.1
132 |
133 |
134 | attach-sources
135 |
136 | jar-no-fork
137 |
138 |
139 |
140 |
141 |
142 | org.apache.maven.plugins
143 | maven-javadoc-plugin
144 | 2.9.1
145 |
146 |
147 | attach-javadocs
148 |
149 | jar
150 |
151 |
152 |
153 |
154 |
155 | org.apache.maven.plugins
156 | maven-gpg-plugin
157 | 1.5
158 |
159 |
160 | sign-artifacts
161 | verify
162 |
163 | sign
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 | tool
173 |
174 |
175 | org.apache.httpcomponents
176 | httpclient
177 | 4.5.13
178 |
179 |
180 | commons-cli
181 | commons-cli
182 | 1.4
183 |
184 |
185 | mysql
186 | mysql-connector-java
187 | 8.0.28
188 |
189 |
190 | org.slf4j
191 | slf4j-simple
192 | 1.7.25
193 |
194 |
195 |
196 |
197 |
198 | org.apache.maven.plugins
199 | maven-assembly-plugin
200 | 2.4.1
201 |
202 |
203 | make-assembly-release
204 | package
205 |
206 | single
207 |
208 |
209 |
210 | src/main/assembly/release.xml
211 |
212 | addrparser
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
--------------------------------------------------------------------------------
/src/main/assembly/release.xml:
--------------------------------------------------------------------------------
1 |
4 | ${project.version}
5 |
6 | tar.gz
7 |
8 | true
9 |
10 |
11 |
12 | false
13 | lib
14 | runtime
15 | true
16 | true
17 | true
18 |
19 |
20 |
21 |
22 |
23 | ${project.basedir}/src/main/resources
24 |
25 | *.xml
26 | *.properties
27 |
28 | conf
29 |
30 |
31 | ${project.basedir}/src/main/script
32 |
33 | *.sh
34 | *.bat
35 |
36 | 755
37 | unix
38 | bin
39 |
40 |
41 |
42 | ${project.basedir}/target
43 |
44 | *.jar
45 |
46 | lib
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/ContainPointJudge.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Interface to judge whether a point in polygons
7 | * @author Spiro Huang
8 | * @since 1.0
9 | */
10 | public interface ContainPointJudge {
11 |
12 | /**
13 | * Initialize polygons
14 | * @param polyline Multi polygons point list
15 | */
16 | void initPolygons(List> polyline);
17 |
18 | boolean contain(Point point);
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/ContainPointJudgeFactory.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | import tech.spiro.addrparser.parser.DefaultContainPointJudge;
4 |
5 | /**
6 | * @author Spiro Huang
7 | * @since 1.0
8 | */
9 | public class ContainPointJudgeFactory {
10 |
11 | public static ContainPointJudge create() {
12 | return new DefaultContainPointJudge();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/NotImplementedException.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | /**
4 | * Some code have not implemented already, throw this exception.
5 | * @author Spiro Huang
6 | * @since 1.0
7 | */
8 | public class NotImplementedException extends RuntimeException {
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/Point.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | /**
4 | * Point with longitude/latitude pair.
5 | * @author Spiro Huang
6 | * @since 1.0
7 | */
8 | public class Point
9 | {
10 | private final double lon;
11 | private final double lat;
12 |
13 | public Point(double lon, double lat) {
14 | this.lon = lon;
15 | this.lat = lat;
16 | }
17 |
18 | public double getLon() {
19 | return lon;
20 | }
21 |
22 | public double getLat() {
23 | return lat;
24 | }
25 |
26 | @Override
27 | public String toString() {
28 | return "Point{" +
29 | "lon=" + lon +
30 | ", lat=" + lat +
31 | '}';
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/RegionConverter.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * Convert {@link RegionDTO} to {@link RegionInfo}
8 | * @author Spiro Huang
9 | * @since 1.0
10 | */
11 | public class RegionConverter {
12 |
13 | public static RegionInfo convert(RegionDTO regionDTO) {
14 | RegionInfo.Builder builder = new RegionInfo.Builder();
15 |
16 | // parentCode
17 | builder.parentCode(Integer.valueOf(regionDTO.getParentCode()));
18 | // code
19 | builder.code(Integer.valueOf(regionDTO.getCode()));
20 | // name
21 | builder.name(regionDTO.getName());
22 |
23 | // level
24 | builder.level(regionDTO.getLevel());
25 |
26 | // center
27 | String[] center = regionDTO.getCenter().split(",");
28 | Point centerPoint = new Point(Double.valueOf(center[0]), Double.valueOf(center[1]));
29 | builder.center(centerPoint);
30 |
31 | // polyline
32 | String polyline = regionDTO.getPolyline();
33 | String[] blocks = polyline.split("\\|");
34 |
35 | for (String block : blocks) {
36 | List blockObj = new ArrayList<>();
37 | String[] points = block.split(";");
38 |
39 | for (String point : points) {
40 | String[] lonLat = point.split(",");
41 | Point pointObj = new Point(Double.valueOf(lonLat[0]), Double.valueOf(lonLat[1]));
42 | blockObj.add(pointObj);
43 | }
44 | builder.addPolyline(blockObj);
45 | }
46 |
47 | return builder.build();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/RegionDTO.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | /**
4 | * Region data object for I/O.
5 | * @author Spiro Huang
6 | * @since 1.0
7 | */
8 | public class RegionDTO {
9 | private int parentCode;
10 | private int code;
11 | private String name;
12 |
13 | /**
14 | * Available value: 1: "province", 2: "city", 3: "district", 4: "street"
15 | */
16 | private RegionLevel level;
17 |
18 | /**
19 | * Format: "[longitude],[latitude]"
20 | */
21 | private String center;
22 |
23 | /**
24 | * Maybe represent multi polygons separated by "|".
25 | * Format: "[longitude1],[latitude1];[longitude2],[latitude2];[longitude3],[latitude3]|[longitude4],[latitude4];..."
26 | */
27 | private String polyline;
28 |
29 | public int getParentCode() {
30 | return parentCode;
31 | }
32 |
33 | public void setParentCode(int parentCode) {
34 | this.parentCode = parentCode;
35 | }
36 |
37 | public int getCode() {
38 | return code;
39 | }
40 |
41 | public void setCode(int code) {
42 | this.code = code;
43 | }
44 |
45 | public String getName() {
46 | return name;
47 | }
48 |
49 | public void setName(String name) {
50 | this.name = name;
51 | }
52 |
53 | public RegionLevel getLevel() {
54 | return level;
55 | }
56 |
57 | public void setLevel(RegionLevel level) {
58 | this.level = level;
59 | }
60 |
61 | public String getCenter() {
62 | return center;
63 | }
64 |
65 | public void setCenter(String center) {
66 | this.center = center;
67 | }
68 |
69 | public String getPolyline() {
70 | return polyline;
71 | }
72 |
73 | public void setPolyline(String polyline) {
74 | this.polyline = polyline;
75 | }
76 |
77 | @Override
78 | public String toString() {
79 | return "RegionDTO{" +
80 | "parentCode=" + parentCode +
81 | ", code=" + code +
82 | ", name='" + name + '\'' +
83 | ", level=" + level +
84 | ", center='" + center + '\'' +
85 | ", polyline='...'" +
86 | '}';
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/main/java/tech/spiro/addrparser/common/RegionInfo.java:
--------------------------------------------------------------------------------
1 | package tech.spiro.addrparser.common;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 |
7 | /**
8 | * Region data object for calculating.
9 | * @author Spiro Huang
10 | * @since 1.0
11 | */
12 | public class RegionInfo {
13 |
14 | private final int parentCode;
15 | private final int code;
16 | private final String name;
17 | private final RegionLevel level;
18 | private final Point center;
19 | private final List> polyline;
20 |
21 | private final ContainPointJudge containPointJudge = ContainPointJudgeFactory.create();
22 |
23 | private RegionInfo(Builder builder) {
24 | this.parentCode = builder.parentCode;
25 | this.code = builder.code;
26 | this.name = builder.name;
27 | this.level = builder.level;
28 | this.center = builder.center;
29 | this.polyline = Collections.unmodifiableList(builder.polyline);
30 | containPointJudge.initPolygons(this.polyline);
31 | }
32 |
33 | public int getParentCode() {
34 | return parentCode;
35 | }
36 |
37 | public int getCode() {
38 | return code;
39 | }
40 |
41 | public String getName() {
42 | return name;
43 | }
44 |
45 | public RegionLevel getLevel() {
46 | return level;
47 | }
48 |
49 | public Point getCenter() {
50 | return center;
51 | }
52 |
53 | public List> getPolyline() {
54 | return polyline;
55 | }
56 |
57 | public boolean contain(Point point) {
58 | return this.containPointJudge.contain(point);
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "RegionInfo{" +
64 | "parentCode=" + parentCode +
65 | ", code=" + code +
66 | ", name='" + name + '\'' +
67 | ", level=" + level +
68 | '}';
69 | }
70 |
71 | public static class Builder {
72 | private int parentCode;
73 | private int code;
74 | private String name;
75 | private RegionLevel level;
76 | private Point center;
77 | private List> polyline = new ArrayList<>();
78 |
79 | public Builder() {
80 | }
81 |
82 | public Builder code(int code) {
83 | this.code = code;
84 | return this;
85 | }
86 |
87 | public Builder parentCode(int parentCode) {
88 | this.parentCode = parentCode;
89 | return this;
90 | }
91 |
92 | public Builder name(String name) {
93 | this.name = name;
94 | return this;
95 | }
96 |
97 | public Builder level(RegionLevel level) {
98 | this.level = level;
99 | return this;
100 | }
101 |
102 | public Builder center(Point center) {
103 | this.center = center;
104 | return this;
105 | }
106 |
107 | public Builder addPolyline(List polyline) {
108 | if (polyline != null && (!polyline.isEmpty())) {
109 | this.polyline.add(Collections.unmodifiableList(polyline));
110 | }
111 | return this;
112 | }
113 |
114 | public RegionInfo build() {
115 |
116 | if (this.name == null) {
117 | throw new IllegalArgumentException(" cannot be null.");
118 | }
119 |
120 | if (this.level == null) {
121 | throw new IllegalArgumentException(" cannot be null.");
122 | }
123 |
124 | if (this.center == null) {
125 | throw new IllegalArgumentException("