Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HI, I am having issues with my geolocation python project. Some parts will not run and I cannot figure out what is wrong. Below I

HI, I am having issues with my geolocation python project. Some parts will not run and I cannot figure out what is wrong. Below I have attached what I have done so far. If you need more information about the project, feel free to ask me and I will provide the missing information. Thank you in advance for your help.

geolocation.py

image text in transcribed

-------------------------------------------------------------------------------------------------------------------------------------------

geolocator.py

# This class provides a single static method called find that provides access

# to the Google Maps api. It takes a search string and returns the latitude

# and longitude of the top hit for the search. Returns null if the search

# produces no results or if no internet connection is available.

#

# Sample calls:

# location = GeoLocator.find("space needle")

# location = GeoLocator.find_place("space needle")

import urllib

import urllib.request

import urllib.parse

import xml.etree.ElementTree as et

from place_information import *

from geolocation import *

class GeoLocator:

# Given a query string, returns a GeoLocation representing the coordinates

# of Google's best guess at what the query string represents.

#

# Returns None if there are no results for the given query, or if we

# cannot connect to the Maps API.

@staticmethod

def find(location):

root = GeoLocator.__get_xml(location)

if(root == None):

return None

result = root.find('result').find('geometry').find('location')

lat = float(result.find('lat').text)

lng = float(result.find('lat').text)

return GeoLocation(lat, lng)

# Given a query string, returns a PlaceInformation representing the data

# of Google's best guess at what the query string represents.

#

# Returns None if there are no results for the given query, or if we

# cannot connect to the Maps API.

@staticmethod

def find_place(location):

root = GeoLocator.__get_xml(location)

if(root == None):

return None

result = root.find('result').find('geometry').find('location')

lat = float(result.find('lat').text)

lng = float(result.find('lat').text)

address = root.find('result').find('formatted_address').text

name = root.find('result').find('address_component').find('long_name').text

#tag = " ".join(root.find('result').findall('type'))

tag = root.find('result').find('type').text

return PlaceInformation(name, address, tag, lat, lng)

# takes a location string to search the Google Maps API for as a parameter

# returns an XML object with data about the search

@staticmethod

def __get_xml(location):

url_string = ("https://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" +

urllib.parse.quote_plus(location)+

"&key=PUT YOUR KEY HERE")

try:

# Get the first result in the XML document, bail if no results

f = urllib.request.urlopen(url_string)

xml = f.read()

root = et.fromstring(xml)

result = root.find('result').find('geometry').find('location')

if (None == result or "" == result):

# No results found for query.

return None

return root

except RuntimeError:

print(Error)

# Given any failures trying to retrieve results...

return None

-------------------------------------------------------------------------------------------------------------------------------------------

geolocation_client.py

from geolocation import GeoLocation def main(): stash = GeoLocation(34.988889, -106.614444) ABQstudio = GeoLocation(34.989978, -106.614357) FBIbuilding = GeoLocation(35.131281, -106.61263) print("the stash is at ", stash.__str__()) print("ABQ stuido is at ", ABQstudio.__str__()) print("FBI building is at ", FBIbuilding.__str__()) print("distance in miles between:") stash_studio = stash.distance_from(ABQstudio) stash_fbi = stash.distance_from(FBIbuilding) print(" stash/studio = ", stash_studio) print(" stash/fbi = ", stash_fbi) if __name__ == "__main__": main()

-------------------------------------------------------------------------------------------------------------------------------------------

place_information_client.py

from place_information import * from geolocation import * def main(): data = [PlaceInformation("Mount Lemmon", "Tucson", "tourist, park", 32.44380, -110.77134), PlaceInformation("Student Union Building", "University of Arizona campus", "restaurant", 32.23262, -110.95206), PlaceInformation("Gould-Simpson Hall", "University of Arizona campus", "university, ua", 32.22976, -110.95501)] london = GeoLocation(51.5112139, -0.1198244) old_main = GeoLocation(32.23189, -110.95342) for info in data: print("name : " + info.get_name()) print("address : " + info.get_address()) print("tags : " + info.get_tag()) print("__str__ : " + str(info)) print("London : " + str(info.distance_from(london))) print("Old Main: " + str(info.distance_from(old_main))) print()

-------------------------------------------------------------------------------------------------------------------------------------------

search_near.py

# This program can be used to search for places of interest near a given

# location. Read the give_intro method for more details. It depends on a file

# called places.txt for the data.

from place_information import *

from geolocator import *

def main():

give_intro()

lines = open("places.txt").readlines()

data = read_file(lines)

target = input("target? ")

target = GeoLocator.find_place(target)

if (target == None):

print("no matches for that search string.")

else:

print("found " + target.get_name())

show_matches(data, target)

# introduces the program to the user

def give_intro():

print("This program allows you to search for places of")

print("interest near a target location. For each")

print("search, you will be asked for a target radius,")

print("and a search string for name and tags. If you")

print("don't want to filter on name and/or tag, simply")

print("hit enter to see all results.")

print()

# Reads data from the given file to construct a list of

# PlaceInformation objects. Assumes the data is in standard format

# (tab-delimited lines with name, address, tag, latitude, longitude).

def read_file(lines):

entries = []

for line in lines:

line = line.split("\t")

name = line[0]

address = line[1]

tag = line[2]

latitude = float(line[3])

longitude = float(line[4])

info = PlaceInformation(name, address, tag, latitude, longitude)

entries.append(info)

return entries

# Prompts the user for a radius and search strings and shows matching

# entries from data and their distance from the given target.

def show_matches(data, target):

radius = 42 # not a real radius, priming the while loop

while (radius > 0):

print()

radius = float(input("radius (0 to quit)? "))

if (radius > 0):

show_entries(data, target, radius)

# Prompts the user for search strings and shows matching entries from data

# that are within the given distance of the given target.

def show_entries(data, target, radius):

name_filter = input("name filter (enter for none)? ")

tag_filter = input("tag filter (enter for none)? " )

for entry in data:

name_match = match(entry.get_name(), name_filter)

tag_match = match(entry.get_tag(), tag_filter)

if (name_match and tag_match):

distance = entry.distance_from(target.get_location())

print(distance)

if (distance

print(str(distance) + " miles: " + entry.get_name() + " " +

entry.get_address())

# returns true if the bigger string contains the smaller string ignoring

# case

def match(big, small):

return small.lower() in big.lower()

main()

-------------------------------------------------------------------------------------------------------------------------------------------

places.txt (this is the shorter version)

Mount Lemmon Tucson tourist, park 32.44380 -110.77134

Student Union Building University of Arizona campus restaurant 32.23262 -110.95206

Gould-Simpson Hall University of Arizona campus university, ua 32.22976

-------------------------------------------------------------------------------------------------------------------------------------------

distance_finder.py

image text in transcribed

------------------------------------------------------------------------------------------------------------------------------------------

place_information.py

image text in transcribed

# This class stores information about a location on Earth. Locations are # specified using latitude and longitude. The class includes a method for # computing the distance between two locations. from math import * RADIUS = 3963.1676 # Earth radius in miles class GeoLocation: # constructs a geo location object with given latitude and longitude _init__(self, latitude, longitude): self._latitude - float(latitude) self._longitude float(longitude) def # returns the latitude of this geo location def get_latitude (self): return self._latitude # returns the longitude of this geo location def get_longitude (self): return self._longitude # returns a string representation of this geo location def __str_(self): return "latitude: " + str(self._latitude) + ", longitude: + str(self._longitude) # WRITE THIS METHOD FOR AN A # returns the distance in miles between this geo location and the given # other geo location def distance_from(self, other): return theAnswer # This class provides a sample usage of the Geocoder.find_place method and the # PlaceInformation and GeoLocation classes. It prompts the user for two # locations and reports the distance between them. from geolocator import def main(): print("This program finds the distance between two") print("places using Google Maps data.") print) first = input("first location? ") one = GeoLocator.find_place(first) if (one == None): print("no matches for that search string.") else: print("found at " + str(one)) second = input("second location?") two = GeoLocator.find_place (second) if (two == None): print("no matches for that search string.") else: print("found at + str(two)) print(str(one.distance_from(two.get_location())) + show_match(1, one) show_match(2, two) # displays information about the passed in place(info) # labeling it as the number passed in def show_match(number, info): print) print("Place #" + str(number)) print(" + info.get_name) print(" address: + info.get_address()) print(" tags + info.get_tag() print(" location: + str(info.get_location()) miles apart") name . . main() from geolocation import GeoLocation class PlaceInformation: def init__(self, name, address, tag, latitude, Longitude): self.__name = name;. self.__address = address; self.__tag = tagi. self.__location Geolocation (latitude, longitude) def. get.name (self): return self.__name def get_address(self): return self.__address;. def get_tag (self): return self.__tag: def stwa.(self): return 'Name = 'self.-_name + Location = + str(self.__location); E def get_location(self): return self.__location: - def distance from(self.spot): distance = self.get_location().compute_distance (spot.latitude, spot. Longitude); return str(round(distance, 2)) + miles'; # To display the distance in Kilometer, uncomment the next line #return str (round (distance 2)+kilometers # This class stores information about a location on Earth. Locations are # specified using latitude and longitude. The class includes a method for # computing the distance between two locations. from math import * RADIUS = 3963.1676 # Earth radius in miles class GeoLocation: # constructs a geo location object with given latitude and longitude _init__(self, latitude, longitude): self._latitude - float(latitude) self._longitude float(longitude) def # returns the latitude of this geo location def get_latitude (self): return self._latitude # returns the longitude of this geo location def get_longitude (self): return self._longitude # returns a string representation of this geo location def __str_(self): return "latitude: " + str(self._latitude) + ", longitude: + str(self._longitude) # WRITE THIS METHOD FOR AN A # returns the distance in miles between this geo location and the given # other geo location def distance_from(self, other): return theAnswer # This class provides a sample usage of the Geocoder.find_place method and the # PlaceInformation and GeoLocation classes. It prompts the user for two # locations and reports the distance between them. from geolocator import def main(): print("This program finds the distance between two") print("places using Google Maps data.") print) first = input("first location? ") one = GeoLocator.find_place(first) if (one == None): print("no matches for that search string.") else: print("found at " + str(one)) second = input("second location?") two = GeoLocator.find_place (second) if (two == None): print("no matches for that search string.") else: print("found at + str(two)) print(str(one.distance_from(two.get_location())) + show_match(1, one) show_match(2, two) # displays information about the passed in place(info) # labeling it as the number passed in def show_match(number, info): print) print("Place #" + str(number)) print(" + info.get_name) print(" address: + info.get_address()) print(" tags + info.get_tag() print(" location: + str(info.get_location()) miles apart") name . . main() from geolocation import GeoLocation class PlaceInformation: def init__(self, name, address, tag, latitude, Longitude): self.__name = name;. self.__address = address; self.__tag = tagi. self.__location Geolocation (latitude, longitude) def. get.name (self): return self.__name def get_address(self): return self.__address;. def get_tag (self): return self.__tag: def stwa.(self): return 'Name = 'self.-_name + Location = + str(self.__location); E def get_location(self): return self.__location: - def distance from(self.spot): distance = self.get_location().compute_distance (spot.latitude, spot. Longitude); return str(round(distance, 2)) + miles'; # To display the distance in Kilometer, uncomment the next line #return str (round (distance 2)+kilometers

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions