Home Backend Development Python Tutorial How to implement geocoding with Python

How to implement geocoding with Python

Nov 21, 2016 pm 05:52 PM
python

Taking the logistics industry as an example, this paper analyzes the application methods of PostgreSQL and Greenplum in the logistics industry in terms of geographical location information processing, optimal path algorithms, and machine learning. It mentioned the problem of converting addresses into coordinates. A more professional term should be "geocoding", that is, knowing an address, such as No. 10, Shangdi 10th Street, Haidian District, Beijing, how to obtain the corresponding longitude and latitude location information ( 40,116), or vice versa.

Geocoding concept

Many map-related vendors provide related APIs, and we can directly use these APIs to obtain this information. For example, Baidu's Geocoding API.

Geocoding API is a type of interface used to provide conversion services from addresses to longitude and latitude coordinates or from longitude and latitude coordinates to addresses. Users can use development languages ​​such as C#, C++, and Java to send requests and receive return data in JSON and XML. Geocoding API includes address parsing and reverse geocoding functions:

How to implement geocoding with Python

Borrowing a more intuitive picture from the ESRI document

How to implement geocoding with Python

Geocoding: that is, address parsing, obtaining Baidu longitude and latitude information from structured addresses as detailed as streets , for example: the result of address resolution of "No. 27, Zhongguancun South Street, Haidian District, Beijing" is "lng:116.31985,lat:39.959836". At the same time, geocoding also supports direct parsing of the names of places of interest and landmark buildings to return Baidu latitude and longitude. For example, the result of address parsing of "Baidu Building" is "lng:116.30815,lat:40.056885".

Reverse geocoding: that is, reverse geocoding, which obtains structured address information from Baidu longitude and latitude information. For example: "lat:31.325152,lng:120.558957" The result of reverse geocoding is "318 Tayuan Road, Huqiu District, Suzhou City, Jiangsu Province Number".

However, one thing that needs to be explained is that if you want to use Baidu’s API, you need to have a Baidu account and apply for the corresponding Key. In fact, in addition to Baidu, Google, ESRI, Microsoft's Bing, etc. all have similar geocoding services. However, most of these services do not have libraries specifically for Python and their Json structures are inconsistent with each other. Therefore, the Python master who specializes in dealing with dissatisfaction made a special geocoding tool geocoder to integrate and unify the services of these different manufacturers.

Geocoder

First take a look at which companies' geocoding services it supports:

How to implement geocoding with Python

Installation

pip install geocoder
Copy after login

Geocoding

import geocoder
g = geocoder.google("1403 Washington Ave, New Orleans, LA 70130")
g = geocoder.arcgis(u"北京市海淀区上地十街10号")
g.latlng
Copy after login

The output is

[29.9287839, -90.08421849999999]
Copy after login

You can also view the complete geojson

g.geojson
Copy after login

output For

{'bbox': [-90.0855674802915,
  29.9274349197085,
  -90.0828695197085,
  29.9301328802915],
 'geometry': {'coordinates': [-90.08421849999999, 29.9287839],
  'type': 'Point'},
 'properties': {'accuracy': u'ROOFTOP',
  'address': u'1403 Washington Ave, New Orleans, LA 70130, USA',
  'bbox': [-90.0855674802915,
   29.9274349197085,
   -90.0828695197085,
   29.9301328802915],
  'city': u'New Orleans',
  'confidence': 9,
  'country': u'US',
  'county': u'Orleans Parish',
  'encoding': 'utf-8',
  'housenumber': u'1403',
  'lat': 29.9287839,
  'lng': -90.08421849999999,
  'location': '1403 Washington Ave, New Orleans, LA 70130',
  'neighborhood': u'Garden District',
  'ok': True,
  'place': u'ChIJGyFHWc2lIIYRYSoneaXAUiw',
  'postal': u'70130',
  'provider': 'google',
  'quality': u'street_address',
  'state': u'LA',
  'status': 'OK',
  'status_code': 200,
  'street': u'Washington Ave'},
 'type': 'Feature'}
Copy after login

, when trying to query the Chinese address directly using Google, it failed

g = geocoder.google(u"北京市海淀区上地十街10号")
g.ok
Copy after login

The output is

False
Copy after login

It should be fine using Baidu, but I did not apply for the corresponding key. Switch to arcgis and be able to successfully encode

g = geocoder.arcgis(u"北京市海淀区上地十街10号")
g.latlng
Copy after login

The output is

[40.050934, 116.30079]
Copy after login

Reverse geocoding

g = geocoder.google([29.9287839, -90.08421849999999], method='reverse')

print g.address
print g.city
print g.state
print g.country
Copy after login

The output is

1403 Washington Ave, New Orleans, LA 70115, USA
New Orleans
LA
US
Copy after login

Change to the Chinese address

g = geocoder.google([40.050934, 116.30079], method='reverse')
print g.address
print g.city
print g.state
print g.country
Copy after login

The output is

Bai Du Da Sha, Haidian Qu, Beijing Shi, China, 100193
Beijing
Beijing Shi
CN
Copy after login

Try using the arcgis service

g = geocoder.arcgis([40.050934, 116.30079], method='reverse')
print g.address
print g.city
print g.state
print g.country
Copy after login

The output is

None
北京市
北京市
CHN
Copy after login

Google converts it into English, but the address is relatively complete. Although arcgis is in Chinese, the detailed address is actually output as None, which is useful.

Others

geocoder’s functions don’t stop there, it can also check IPs (including your own).

g = geocoder.ip('199.7.157.0')
print g.latlng
print g.city
g = geocoder.ip('me')
print g.latlng
print g.city
Copy after login

The output is

[43.6934, -79.4857]
Toronto
[51.05, 13.75]
Dresden
Copy after login

Querying the spatial bounding box of a city

g = geocoder.arcgis(u"山东")
g.bbox
Copy after login

The output is

{'northeast': [38.976997, 121.976998], 'southwest': [33.022997, 116.022998]}
Copy after login

Summary

Spatial information can be described by text information such as administrative divisions and natural geographical areas, or it can be identified by coordinate systems and numbers (zip codes, etc.). Geocoding technology can be used to associate geolocation elements of spatial information with corresponding text information. This article mainly introduces the geocoder geocoding tool, which can conveniently and quickly use the geocoding services provided by maps and other related manufacturers to convert the location described in text into longitude and latitude on the map, or obtain the corresponding coordinates through a certain location on the map. Text description of the location information.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

How to use AWS Glue crawler with Amazon Athena How to use AWS Glue crawler with Amazon Athena Apr 09, 2025 pm 03:09 PM

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to view server version of Redis How to view server version of Redis Apr 10, 2025 pm 01:27 PM

Question: How to view the Redis server version? Use the command line tool redis-cli --version to view the version of the connected server. Use the INFO server command to view the server's internal version and need to parse and return information. In a cluster environment, check the version consistency of each node and can be automatically checked using scripts. Use scripts to automate viewing versions, such as connecting with Python scripts and printing version information.

See all articles