Solved: Generate GPS Coordinates in Python [With Examples]

This article is about how to generate GPS coordinates using python. Python is a vast language that supports many APIs and libraries and finding GPS coordinates is one of the applications pythons provides.

 

Install Python

Before you get into the code of how to generate GPS coordinates using python, you first need to check if the latest python version is installed in your system or not. To check that, type the following command into your windows command line terminal

Advertisement
python  --version

The output must be something like this

generate gps coordinates using python

If the command line does not recognize python in your system, you need to download it from the website. After installation, you are good to go.

 

How to generate GPS coordinates using python by using urllib?

In this section, we will learn How to generate GPS coordinates using python by using a python library urllib.

First of all install geopy using the following command on your terminal

pip install urllib

To know that the library is successfully installed, the following output must be shown on the terminal

Collecting urllib3
Downloading urllib3-1.26.10-py2.py3-none-any.whl (139 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.2/139.2 kB 588.8 kB/s eta 0:00:00
Installing collected packages: urllib3
Successfully installed urllib3-1.26.10

You can also download this package from GitHub, make sure that the latest version is installed.

Advertisement

Then use the following approach to generate GPS coordinates using python

  1. Import urllib urlopen
  2. Use the URL “http://ipinfo.io/json” to get the current address.
  3. Load the data
  4. Extract the latitude and longitude lines.

The code will be as follows

# get gps coordinates from geopy
import json

# import urlopen from urllib.request
from urllib.request import urlopen

# open following url to get ipaddress
urlopen("http://ipinfo.io/json")

# load data into array
data = json.load(urlopen("http://ipinfo.io/json"))

# extract lattitude
lat = data['loc'].split(',')[0]

# extract longitude
lon = data['loc'].split(',')[1]

print(lat, lon)

 

The output of this code is

33.5973 73.0479

 

How to get GPS coordinates from python using geopy?

We can get GPS coordinates from python using geopy if the location is provided. The location can be provided using Nominatim from geopy and then latitude and longitude can be extracted.

First of all download the geopy library by writing the following command on your terminal

pip install geopy

To know if the library is successfully installed or not, the following output will be shown on the terminal.

Advertisement
Collecting geopy
Using cached geopy-2.2.0-py3-none-any.whl (118 kB)
Requirement already satisfied: geographiclib<2,>=1.49 in c:\users\azka\appdata\local\programs\python\python310\lib\site-packages (from geopy) (1.52)
Installing collected packages: geopy
Successfully installed geopy-2.2.0

This package can also be installed using the GitHub link, for that the following command will be written on the terminal

pip install git+https://github.com/geopy/geopy

Get the location and then extract the GPS coordinates.

The approach used here is

  1. Get the geolocator using Nominatim.
  2. And get geocode of the address given.
  3. The geocode is used to extract latitude and longitude information.
  4. You can either pass the address information yourself or get the location via a URL opener.
  5. In this code, we provided the information ourselves.

 

The code will be as follows

#generate gps coordinates using python
from geopy.geocoders import Nominatim

import pandas as pd

#get the coordinates of the address

def get_coordinates(address):
    geolocator = Nominatim(user_agent="myapplication")
    location = geolocator.geocode(address)
    return location.latitude, location.longitude

# print the coordinates of the address
def print_coordinates(address):
    lat, lon = get_coordinates(address)
    print("Latitude: ", lat)
    print("Longitude: ", lon)

get_coordinates("1600 Amphitheatre Parkway, Mountain View, CA")
print_coordinates("1600 Amphitheatre Parkway, Mountain View, CA")

The output of this code is :

Latitude:  37.42248575
Longitude:  -122.08558456613565

 

Conclusion

In this article, we have learned two ways how to generate GPS coordinates using python. The first way is to get an IP address using a URL opener to open the address link and extract the information. Another way is to use geopy library and extract the information of the address provided to it.

Advertisement

 

Further Reading

Google Maps API
Python Web Applications

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X