How to extract longitude and latitude from a link

Question:

From the following link, I am trying to extract the longitude and latitude. I found similar posts but not one with the same format. I’m new to regex/text manipulation and would appreciate any guidance on how I might do this using Python. The output I’d like to have from this example is

latitude = 40.744221 
longitude = -73.982854

Many thanks in advance.

https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315×150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw

Asked By: chachacha

||

Answers:

I’m guessing that this expression might return our desired output:

center=(-?d+.d+)%2C(-?d+.d+)

Test with re.findall

import re

regex = r"center=(-?d+.d+)%2C(-?d+.d+)"

test_str = "https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw"

print(re.findall(regex, test_str))

Test with re.finditer

import re

regex = r"center=(-?d+.d+)%2C(-?d+.d+)"

test_str = "https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

The expression is explained on the top right panel of this demo, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.

Answered By: Emma

Use a simple re.search on the string with tuple packing:

lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()

where s is your string (link).

Example:

import re

s = 'https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw'

lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()

print(lattitude)  # 40.744221
print(longitude)  # -73.982854
Answered By: Austin

Python has a module for parsing URLs in the standard library

from urllib import parse

# Split off the query
_, query_string = parse.splitquery("https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw")

# Parse the query into a dict
query = parse.parse_qs(query_string)

# You can now access the query using a dict lookup
latlng = query["center"]

# And to get the values (selecting 0 as it is valid for a query string to contain the same key multiple times).
latitude, longitude = latlng[0].split(",")

For this usecase I would avoid regular expressions. The urllib module is more explicit, will handle all aspects of URL encoding and is well tested.

Another great third party module for handling URLs is the excellent YARL.

Answered By: Tim
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.