-1

I am creating a programme for a project that can recommend places to visit in a city.

If I had a bunch of restaurants with their locations on my database and I had the user input their location, what would be the best way of recommending restaurants closed to them. I wouldn't be recommending based on location alone but I just don't know the best methods to go about doing this. I am willing to learn I just don't know where to start!

Having the user input their location (such as post code) would be enough - I don't know if I have enough technical skill to use gps, I am fine with APIs etc.

grey150
  • 3
  • 1

2 Answers2

0

There are a few questions in here.

  • First, you need to use a consistent co-ordinate system to store the restaurant locations, EPSG:3857 is the simplest in this case, as the units are in meters rather than degrees. This system still distorts lengths, but the restaurants are close to the person so this is fine here. The PyProj Python library is the standard for transforming co-ordinate locations, using pyproj.Transformer.

  • Get the person's location using a web-API. For this, you can use the Geopy Python library to look-up an address, and use Nominatim to resolve these in testing and demonstration; you need to move to a paid service rather than Nominatim if you want frequent look-ups.

  • Then, do a "nearest neighbour lookup", using an algorithm rather than just looping through sites and using the distance formula you know from geometry. There are many algorithms to choose from for this; the sklearn Python library can do this.

Guy Keogh
  • 549
  • 2
  • 5
  • 15
0

Using the formula for Euclidean distance:

euclid-dist

Where

euclid-dist

is simply coded in python as

x**0.5

if you want to read about the performance of x**0.5 vs math.sqrt(x) read this

And where

euclid-dist

is simply coded in python as

x**2

Then,

You could use a this very basic instructive python code:

#location in tuple (x,y)
user_location = (8,9)

#This is dictionary with:
#keys being names of Restaurants
#values being tuples with the position (x,y)
locations={"MacDonald's":    (-4,-8),
           "TGI Fridays":    (1,1),
           "Bembos":         (2,3),
           "Burger King":    (5,6),
           "El Limeño":      (7,8),
           "Astrid y Gastón":(-1,10),
           "Central":        (1,9),
           "Johnny Rockets": (6,1)
           }

#Extract x,y value from tuple user_location
user_x,user_y = user_location
dist_min=float('inf')


for key in locations:
    x,y=locations[key]
    #Euclidean's Distance Expression presented earlier
    dist=((x-user_x)**2+(y-user_y)**2)**0.5
    print(f"Location:({x},{y}) Name:{key} / Distance:{dist:4.3f}" )
    if dist<dist_min:
        dist_min = dist
        best_key = key

print("\n")
print(f"The selected location is {best_key} since it has th smallest distance to the user:{dist_min:4.3f}")

Copy and paste this code, it simply works.

I hope it helps in some way.

parafacundo
  • 93
  • 1
  • 4