Skip to content

Latest commit

 

History

History
74 lines (52 loc) · 2.16 KB

Python_Locate_city_on_map.md

File metadata and controls

74 lines (52 loc) · 2.16 KB



Template request | Bug report | Generate Data Product

Tags: #python #geocoding #city #coordinates #location #api

Author: Florent Ravenel

Description: This notebook will show how to get coordinates from a city name using Python and display it on a map.

References:

Input

Import libraries

try:
    import geocoder
except:
    !pip install geocoder --user
    import geocoder
import pandas as pd
import plotly.express as px

Setup Variables

  • city: name of the city to get coordinates from
  • country: name of the country to help find the city
city = "Paris"
country = "France"

Model

Get coordinates from city

Using the geocoder library, we can get the coordinates from a city name.

g = geocoder.arcgis(f"{city}, {country}")
lat, lng = g.latlng
df = pd.DataFrame([{"name": city, "latitude": lat, "longitude": lng, "size": 1}])
print(f"Coordinates of {city}: {lat}, {lng}")

Output

Locate city on map

fig = px.scatter_mapbox(
    df,
    lat='latitude',
    lon='longitude',
    size='size',
    text='name',
    zoom=4,
    mapbox_style='open-street-map'
)
fig.update_layout(
    width=1200,
    height=800,
)
config = {"displayModeBar": False}
fig.show(config=config)