Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vaclavdekanovsky/6465b0f5700a5215296997e158c18e86 to your computer and use it in GitHub Desktop.

Select an option

Save vaclavdekanovsky/6465b0f5700a5215296997e158c18e86 to your computer and use it in GitHub Desktop.
Getting geospatial coordinates with GeoPy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# https://geopy.readthedocs.io/en/stable/\n",
"from geopy.geocoders import Nominatim\n",
"geolocator = Nominatim(user_agent=\"sample app\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"data = geolocator.geocode(\"1 Apple Park Way, Cupertino, CA\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extracting the data from the raw response can be tidious, because each provider return different data struture"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('37.3337572', '-122.0113815')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.raw.get(\"lat\"), data.raw.get(\"lon\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"using the `.point`, `.point.latitude` and `.point.longitude` makes it easy no matter what provider you are asking. The third tribute is altitude."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Point(37.3337572, -122.0113815, 0.0)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.point"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(37.3337572, -122.0113815)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.point.latitude, data.point.longitude"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`.reverse` helps you find the address based on the geolocation"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Location(Apple Park, 1, Apple Park Way, Monta Vista, Cupertino, Santa Clara County, California, 94087, United States of America, (37.3348469, -122.01139215737962, 0.0))"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"geolocator.reverse('37.3337572, -122.0113815')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment