-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapboxTest.py
83 lines (59 loc) · 2.24 KB
/
MapboxTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from geopy.geocoders import MapBox
import requests
def get_country_from_coordinates(latitude, longitude):
url = f"https://api.mapbox.com/geocoding/v5/mapbox.places/{longitude},{latitude}.json"
params = {
"access_token": "pk.eyJ1IjoiZ28taWZyYyIsImEiOiJja3E2bGdvb3QwaXM5MnZtbXN2eGtmaWgwIn0.llipq3Spc_PPA2bLjPwIPQ",
"types": "country"
}
response = requests.get(url, params=params)
data = response.json()
# Extract the country from the response
if "features" in data and len(data["features"]) > 0:
country = data["features"][0]["text"]
return country
return None
# Example usage
latitude = 0.08637
longitude = 120.050538
country = get_country_from_coordinates(latitude, longitude)
if country:
print(f"The country at ({latitude}, {longitude}) is: {country}")
else:
print("No country found for the given coordinates.")
# import requests
# def geocode(address):
# endpoint = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'
# access_token = 'pk.eyJ1IjoiZ28taWZyYyIsImEiOiJja3E2bGdvb3QwaXM5MnZtbXN2eGtmaWgwIn0.llipq3Spc_PPA2bLjPwIPQ'
# # Format the address and construct the API request URL
# formatted_address = address.replace(' ', '%20')
# url = f"{endpoint}{formatted_address}.json?access_token={access_token}"
# # Send the API request
# response = requests.get(url)
# data = response.json()
# # Extract the coordinates from the response
# if 'features' in data and len(data['features']) > 0:
# location = data['features'][0]['geometry']['coordinates']
# return location
# else:
# return None
# # Example usage
# address = '089210'
# coordinates = geocode(address)
# if coordinates:
# latitude, longitude = coordinates
# print(f"Latitude: {latitude}")
# print(f"Longitude: {longitude}")
# else:
# print("Location not found.")
# Initialize the Mapbox geocoder
geocoder = MapBox(api_key='YOUR_MAPBOX_API_KEY')
# NUTS3 code to geocode
nuts3_code = 'FR718'
# Geocode the NUTS3 code
location = geocoder.geocode(nuts3_code)
# Extract latitude and longitude from the geocoded location
latitude = location.latitude
longitude = location.longitude
# Print the result
print(f'Latitude: {latitude}, Longitude: {longitude}')