Solved: Generate GPS Coordinates in Python [With Examples]


Python

Reviewer: Deepak Prasad

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

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.

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.

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.

 

Further Reading

Google Maps API
Python Web Applications

 

Azka Iftikhar

Azka Iftikhar

She is proficient in multiple programming languages, including C++, GO, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can check her professional profile on GitHub which captures her experience and portfolio.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

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 send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment