import geopy # used to get location
from geopy.geocoders import Nominatim
import pandas as pd
from pyproj import Transformer
def get_user_location(): # user location
geolocator = Nominatim(user_agent="Everywhere") # name of app
user_input = input("Please enter your city or town ") # tirng by default
global location
location = geolocator.geocode(user_input)
print(location.latitude,location.longitude) # # output long and lats of user
longitudes = []
latitudes = []
def east_northing_to_lat_long(filepath):
df = pd.read_csv(filepath, encoding= 'latin1') # encoding makes file readable
t = Transformer.from_crs(crs_from="27700",crs_to="4326", always_xy=True) # instance of transformer class
df['longitude'], df['latitude'] = t.transform((df['Easting'].values), (df['Northing'].values)) # new
# get the lats and long in lists
for i,row in df.iterrows(): #loops through every row
l = row['longitude']
la = row['latitude']
longitudes.append(float(l)) # instance long
latitudes.append(float(la)) # instance for lat
for column,column1 in zip(longitudes,latitudes):
global nearest_latitude
global nearest_longitude
nearest_longitude = min((longitudes), key = lambda x: abs(location.longitude-x)) # comparing the user's longitude with longitude
nearest_latitude = min((latitudes), key = lambda y: abs(location.longitude-y))
return nearest_longitude and nearest_longitude
Я создаю систему поиска школ, в которой ближайшая школа находится с использованием восточного и северного направлений пользователя, которые были преобразованы в широту и долготу с помощью библиотеки pyproj.
Приведенный выше код показывает, как я получил ближайшие долготу и широту к местоположению пользователя. Чтобы завершить внутреннюю часть моего проекта, мне нужно получить название школы. Однако я изо всех сил пытался найти решение, как получить «EstablishmentName» из неизвестной строки в CSV-файле, используя известную «широту» школы (nearest_latitude) из той же строки.
Вот что я пробовал:
for i,row in enumerate(df): # find the row that cooordinates are in
if nearest_latitude in row:
#name_of_school = row['EstablishmentName']
print(f'it is in this ',{row})
Подробнее здесь: https://stackoverflow.com/questions/797 ... n-data-fro