S
S
Sergey Ilyin2020-05-01 16:19:19
Python
Sergey Ilyin, 2020-05-01 16:19:19

Geopy / how to find the closest point from a list?

there are two dictionaries:

income = {'': 'Error', 'Kaliningrad (Konigsberg), ': (54.710454, 20.512733), 'Krasnojarsk, Тельмана, 36': (56.05031, 92.967305), 'Moscow > Moskva, ': (55.755814, 37.617635), 'Moscow > Moskva, Багратионовский пр. д.7 кор.20Б': (55.743137, 37.504232)}
# в income могут быть строки!

etalon = {'115035, г. Москва, М-35, ул. Балчуг, 2': (55.747459, 37.625271), '308000, г. Белгород, пр-т Славы, 74': (50.598079, 36.585839), '241050, г. Брянск, ул. Горького, 34': (53.244984, 34.365707), '600000, г. Владимир, ул. Большая Московская, 29': (56.129745, 40.406446)}

x_values = income.values()
y_values = etalon.values()


I want to cycle through income, take a geopair (latitude, longitude) and compare it with each geopair in etalon. the result should be a list like:
[address in income, address in etalon]
or a dictionary:
{address in income: address in etalon}

from geopy.distance import geodesic

for x in x_values:
    if type(x) == str:
        None
    else:
        distance = 10000000 # выбираем какую-то от балды дистанцию побольше
        for y in y_values():
            try:
                distance_new = geodesic(x, y, ellipsoid='WGS-84')
                distance_new = geodesic(x_values, y_values, ellipsoid='WGS-84')
                    if distance_new < distance:
                        distance = distance_new
                        total = {income.keys(): etalon.keys()}
            except:
                print('Error')


does not work ((

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-05-01
@sunsexsurf

dict.keys() returns Dictionary view objects (dict_keys), and this object is mutable and therefore cannot be a key in a dictionary.

F
freeExec, 2020-05-01
@freeExec

Don't reinvent the wheel. Spatial indexes and nearest lookup have been around for a long time.
Google:

K nearest neighbors (kNN)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question