According to the longitude and latitude coordinates, the city name of the administrative division of the province, city, city and county is obtained, and the self-built database java python php c# .net is applicable

In LBS applications, it is a very common function to analyze and obtain the corresponding city based on coordinates. For example, the app automatically selects a city through mobile phone positioning; Most of the databases that support the spatial data type ( ) support the corresponding provinces and municipalities Spatial, including but not limited to: MySQL, SQL Server, Oracle, PostgreSQLetc.; the development language is not limited, as long as the database query can be performed, it can be used Java, , Python, PHP, C#etc. Very simple to implement.

Online preview and download of provincial and urban boundary data: https://xiangyuecn.gitee.io/areacity-jsspider-statsgov/
GitHub address: https://github.com/xiangyuecn/AreaCity-JsSpider-StatsGov

Obtain the address through coordinates: the interface provided by Baidu Maps is called " address inverse analysis ", and the interface provided by Gaode Map is called " geographical inverse coding ". Both of their open platforms provide front-end and back-end interfaces, and the data can be obtained by sending an http request , please read the platform development documentation by yourself for related interface calls, it is very convenient to use.

Of course, this article will not introduce how to get the complete addresses of street house numbers, nor how to call other people's interfaces, but only introduce the acquisition of the names of provinces and cities corresponding to the coordinates, and write SQL for spatial queries in self-built databases, completely by yourself Implementation is relatively more complicated than adjusting the open platform interface.

Since there are many changes in districts and counties across the country every year, the boundary data of provinces and cities needs to be maintained synchronously. Fortunately, this open source library has continuous long-term maintenance, and it is relatively easy to update after new data is released. Since the update and maintenance data of the open source library is relatively timely, as long as the open source library is not closed, the extraction method introduced in this article is always applicable; it is much better than those data that are uploaded to the download platform and have not been updated for ten thousand years.

Query effect display:
Database query effect

A coordinate taken at random from here:
Coordinate position

The intuitive effect is as shown in the picture above. Just click on the Baidu map (or the coordinates obtained by App positioning) to get a coordinate, and then use the spatial query SQL in the database to query the city where the coordinate is located.

Step 1. Download the provincial and urban boundary data

Go to the open source library to download the latest ok_geo.csv.7zfile (13M size, unzip 130M+), click here to download . After downloading, unzip it and get it ok_geo.csv. This file contains the latest coordinate boundary vector data of all provinces, cities and counties in the country.

Note: This file only contains the data of the third level (provinces, municipalities) and does not include the fourth level (townships and streets). If you need the coordinate boundary data of townships, please click here to download ok_geo4_*.csv the file (90MB+ compressed package and 300M+ after export).

Step 2. Parse the CSV file and import it into the database

The downloaded file ok_geo.csvis a plain text file, which can be parsed by writing a script by yourself, and then imported into the database. It is more complicated to parse and process by yourself, please refer to the documentation in the open source library; a format conversion tool is provided in the open source library, which supports CSV The data is imported into the database, so we download the tool directly when downloading the data, click here to download .

In addition to ok_geo.csvimporting into the database, this conversion tool also supports exporting: sql, shp, geojson, and coordinate system conversion; it can also execute custom JavaScript scripts to expand rich functions; the software is Windows version, if you need to use it in MacOs, you can use virtual machine.

import operation

The conversion tool performs the import database operation:

  1. Click 选择ok_geo.csv文件the button to select the decompressed CSV file;
  2. In the database setting, select the type of database to be imported, here is selected MySQL, and then fill in the database connection, including: port, database name, account password;
  3. Click 导入数据库the button, wait for a while, about 3 minutes, all the data will be imported into the table created by today's date in the database.

Note: The boundary data in the csv file defaults to the Amap GCJ-02Mars coordinate system. If you need other coordinate systems, such as Baidu BD-09or GPS WGS-84, you can convert them through the coordinate system conversion plug-in in the advanced script. After selecting the corresponding plug-in, Just click Apply, and the coordinate system conversion will be performed automatically when importing the database.

Note: This tool is limited to exporting only one city and its next-level data for each operation. It is still very easy to export a small amount of data, so we can operate several times to import all the city data we need into the database; for example, Shenzhen All district and county data: import all provinces in the country in the first pass, fill in the city name prefix 广东省(with a space at the end) in the second pass and import all cities in Guangdong, and fill in the city name prefix in the third pass 广东省 深圳市(with a space at the end) Import all districts in Shenzhen. If you fill in the key in the key input box, this tool does not have these restrictions. The open source library will issue keys from time to time for welfare, and you can export all the three-level data of provinces and municipalities in the country with one click.

Table structure and spatial fields (MySQL version, similar to other databases):

CREATE TABLE Areacity_Geo_20220216 (
  id int NOT NULL, --城市id
  pid int NOT NULL, --上级城市id
  deep int NOT NULL, --层级:0省、1市、2区
  name varchar(250) NOT NULL, --城市名称:`深圳市`
  ext_path varchar(255) NOT NULL, --省市区三级完整名称:广东省 深圳市 罗湖区
  geo geometry NOT NULL, --城市中心坐标,空间数据格式
                --,ST_AsText转成WKT文本后:`POINT EMPTY`、`POINT (123.456 34.567)`
  polygon geometry NOT NULL --城市边界范围图形,空间数据格式
                --,ST_AsText转成WKT文本后:`POLYGON EMPTY`、`POLYGON ((123.456 34.567,...))`、`MULTIPOLYGON (((123.456 34.567,...)),...)`
)

对空间字段的查询,需要用`ST_AsText()`方法才能查询出字符串文本(WKT: Well Known Text),否则查询出来的是二进制数据
-- MySQL版:
SELECT id, name, ST_AsText(geo) AS geo, ST_AsText(polygon) AS polygon FROM 表名
-- SQL Server版:
SELECT id, name, geo.STAsText() AS geo, polygon.STAsText() AS polygon FROM 表名

Step 3. Obtain the city according to the coordinate analysis in the program

After the provincial and urban boundaries are imported into the database, we can query the database in Java, , Python, PHPand other programs, and use the SQL spatial calculation function to query which boundaries a coordinate is within, and then we can get the corresponding provincial and urban information up.C#ST_Intersects

Spatial query SQL statement

比如要查询坐标`lng:113.929976 lat:22.529497`是在哪个城市
-- MySQL版:
SELECT id,deep,name FROM 表名 WHERE ST_Intersects(polygon, ST_GeomFromText('POINT(113.929976 22.529497)',0))=1
-- SQL Server版:
SELECT id,deep,name FROM 表名 WHERE polygon.STIntersects(geometry::STGeomFromText('POINT(113.929976 22.529497)',0))=1

Example of query results (MySQL version, similar to other databases)
MySQL query effect

The program code is connected to the database, and after the database data is queried through the above SQL, the province and city information can be obtained, and the deepfields can be used to distinguish which data is the province (deep=0), city (deep=1), district and county (deep=2).

Through the above three steps, we have completely realized the function of analyzing and obtaining the corresponding city according to the latitude and longitude coordinates.

【END】

Guess you like

Origin blog.csdn.net/xiangyuecn/article/details/122961085