Hướng dẫn how do you find latitude and longitude in python? - làm thế nào để bạn tìm thấy vĩ độ và kinh độ trong python?

Trong bài viết này, chúng tôi sẽ viết một kịch bản Python để lấy tên thành phố, tiểu bang và quốc gia bằng cách sử dụng vĩ độ và kinh độ bằng mô -đun địa lý. và các địa danh trên khắp thế giới.

Để cài đặt mô -đun Geopy, hãy chạy lệnh sau trong thiết bị đầu cuối của bạn.

pip install geopy

Approach:

  • Nhập mô -đun địa lý
  • Khởi tạo API nominatim để nhận vị trí từ chuỗi đầu vào.
  • Nhận vị trí với hàm golocator.reverse ().
  • Bây giờ trích xuất dữ liệu từ thể hiện vị trí

Hãy để thực hiện với từng bước:

Bước #1: Nhập mô -đun. Import the module.

Python3

from geopy.geocoders import Nominatim

Bước #2: Tạo một đối tượng Nominatim và khởi tạo API Nominatim & NBSP; với tham số Geoapiexercise. Make a Nominatim object and initialize Nominatim API  with the geoapiExercises parameter.

Python

geolocator = {'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}0=__12

Bước #3: Bây giờ chỉ định vĩ độ và kinh độ vào phương thức golocator.reverse (). Một phương thức đảo ngược () yêu cầu truy vấn đối số và cũng chấp nhận ít nhất đối số chính xác_one, điều này đúng theo mặc định.Now assign the latitude and longitude into a geolocator.reverse() method. A reverse() methods require the argument query, and also accept at least the argument exactly_one, which is True by default.

Python3

{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}4= {'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}6

{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}7= {'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}9

City : Patna State : Bihar Country : India Zip Code : 8000010____99 City : Patna State : Bihar Country : India Zip Code : 8000012City : Patna State : Bihar Country : India Zip Code : 8000013City : Patna State : Bihar Country : India Zip Code : 8000014City : Patna State : Bihar Country : India Zip Code : 8000013City : Patna State : Bihar Country : India Zip Code : 8000016

City : Patna State : Bihar Country : India Zip Code : 8000017City : Patna State : Bihar Country : India Zip Code : 8000018

Output:

[Vị trí (Rajendra Nagar, Patna, Patna Nông thôn, Patna, Bihar, 800001, Ấn Độ, (25.594023552508407, 85.13756080147536, 0.0))] & NBSP;
 

Bước #4: Bây giờ hãy lấy thông tin từ danh sách đã cho và được phân tích cú pháp vào từ điển có chức năng thô ().Now get the information from the given list and parsed into a dictionary with raw function().

Python3

City : Patna State : Bihar Country : India Zip Code : 8000019= City : Patna State : Bihar Country : India Zip Code : 8000011City : Patna State : Bihar Country : India Zip Code : 8000012City : Patna State : Bihar Country : India Zip Code : 8000013

City : Patna State : Bihar Country : India Zip Code : 8000017City : Patna State : Bihar Country : India Zip Code : 8000015

Output:

{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}

Bước #5: Bây giờ đi qua các tên thành phố, tiểu bang và quốc gia.Now traverse the city, state, and country names.

Python3

City : Patna State : Bihar Country : India Zip Code : 8000016= City : Patna State : Bihar Country : India Zip Code : 8000018City : Patna State : Bihar Country : India Zip Code : 8000019from0

from1= City : Patna State : Bihar Country : India Zip Code : 8000018from4from0

from6= City : Patna State : Bihar Country : India Zip Code : 8000018from9from0

geopy.geocoders 1= City : Patna State : Bihar Country : India Zip Code : 8000018geopy.geocoders 4{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}3

geopy.geocoders 6= City : Patna State : Bihar Country : India Zip Code : 8000018geopy.geocoders 9{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}3

City : Patna State : Bihar Country : India Zip Code : 8000017import2import3import4

City : Patna State : Bihar Country : India Zip Code : 8000017import2import7import8

City : Patna State : Bihar Country : India Zip Code : 8000017import2Nominatim1Nominatim2

City : Patna State : Bihar Country : India Zip Code : 8000017import2Nominatim5Nominatim6

Output:

City : Patna State : Bihar Country : India Zip Code : 800001

Thực hiện đầy đủ:

Python3

from geopy.geocoders import Nominatim

geolocator = {'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}0=__12

{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}4= {'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}6

{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}7= {'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}9

City : Patna State : Bihar Country : India Zip Code : 8000010____99 City : Patna State : Bihar Country : India Zip Code : 8000012City : Patna State : Bihar Country : India Zip Code : 8000013City : Patna State : Bihar Country : India Zip Code : 8000014City : Patna State : Bihar Country : India Zip Code : 8000013City : Patna State : Bihar Country : India Zip Code : 8000016

City : Patna State : Bihar Country : India Zip Code : 8000019= City : Patna State : Bihar Country : India Zip Code : 8000011City : Patna State : Bihar Country : India Zip Code : 8000012City : Patna State : Bihar Country : India Zip Code : 8000013

City : Patna State : Bihar Country : India Zip Code : 8000016= City : Patna State : Bihar Country : India Zip Code : 8000018City : Patna State : Bihar Country : India Zip Code : 8000019from0

from1= City : Patna State : Bihar Country : India Zip Code : 8000018from4from0

from6= City : Patna State : Bihar Country : India Zip Code : 8000018from9from0

geopy.geocoders 1= City : Patna State : Bihar Country : India Zip Code : 8000018geopy.geocoders 4{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}3

geopy.geocoders 6= City : Patna State : Bihar Country : India Zip Code : 8000018geopy.geocoders 9{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}3

City : Patna State : Bihar Country : India Zip Code : 8000017import2import3{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}33

City : Patna State : Bihar Country : India Zip Code : 8000017import2import7{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}37

City : Patna State : Bihar Country : India Zip Code : 8000017import2Nominatim1{'suburb': 'Rajendra Nagar', 'city': 'Patna', 'country': 'Patna Rural', 'state_district': 'Patna', 'state': 'Bihar', 'postcode': '800001', 'country': 'India', 'country_code': 'in'}41

Thực hiện đầy đủ:

Output:

City : Patna State : Bihar Country : India Zip Code : 800001

Chủ đề