Compare Latitude and Longitude in Python

Asked

Viewed 2,331 times

3

I have an application where an Android client sends the coordinates to a Python server, which should put such information in a Mysql Database if the client is within an area (an 8-vertex polygon, to be exact), if not within the area, it does not put in the database.

I just need to know how to make this comparison whether or not the customer is within the specified area, I imagine using the values of Latitude and Longitude.

  • Let’s understand..... Android sends the coordinates of the point where it is. Right? And you want to know if that coordinate is within what limits?

  • https://answall.com/questions/55669/identificar-se-conjuo-coordinatorsadas-est%C3%A1-within-a-radius-on-android? Rq=1

  • @Rodolfodonate: This "area" is an irregular opoligono ? a circle ?

  • I ended up forgetting the shape of the area. An irregular polygon even, of 8 vertices

  • https://stackoverflow.com/questions/13950062/checking-if-a-longitude-latitude-coordinate-resides-inside-a-complex-polygon-in

2 answers

3


You can use an object handling library on the Cartesian plane called Shapely:

from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

def area_contem_cliente( a, c ):
    ponto = Point(c)
    poligono = Polygon(a)
    return poligono.contains(ponto)


cliente_brasilia = ( -15.7801, -47.9292 )
cliente_goiania = ( -16.6799, -49.255 )
cliente_paris = ( 48.85522811, 2.3493576 )
cliente_moscou = ( 55.75223582, 37.62182236 )


brasil = [ ( -8.046177, -34.584961 ),
         ( -20.784877, -40.737305 ),
         ( -33.713374, -53.481445 ),
         ( -30.285635, -57.612305 ),
         ( -16.370743, -60.249023 ),
         ( -7.436552, -73.696289 ),
         ( 4.403373, -64.907227 ),
         ( 4.228090, -51.547852 ) ]


print area_contem_cliente( brasil, cliente_brasilia )
print area_contem_cliente( brasil, cliente_goiania )
print area_contem_cliente( brasil, cliente_paris )
print area_contem_cliente( brasil, cliente_moscou )

Assuming that an 8-vertex Poligono circumvents the Brazilian territory on the terrestrial globe:

inserir a descrição da imagem aqui

Exit:

True
True
False
False
  • The server was made in Python3, so I transferred this code to it, I just wanted to point out that when I went to install the library for Python 3.6, it does not support, the latest version supported is Python 3.5. Stay alert for other users. Best answer impossible, thank you!

0

I suggest replacing the polygonal area of 8 vertices with a circle.

You can use the Haversine’s formula to calculate the distance between two geographical coordinates:

inserir a descrição da imagem aqui Once the distance between the customer and the calculated reference point, you are able to assess whether the customer is within the "radius" of the specified area.

from math import radians, cos, sin, asin, sqrt

# Formula de Haversine
def haversine( a, b ):
    # Raio da Terra em Km
    r = 6371

    # Converte coordenadas de graus para radianos
    lon1, lat1, lon2, lat2 = map(radians, [ a['longitude'], a['latitude'], b['longitude'], b['latitude'] ] )

    # Formula de Haversine
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    hav = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    d = 2 * r * asin( sqrt(hav) )

    return d


brasilia = {'latitude': -15.7801, 'longitude': -47.9292 }
goiania = {'latitude': -16.6799, 'longitude': -49.255}
paris = {'latitude': 48.85522811, 'longitude': 2.3493576 }
moscou = {'latitude': 55.75223582, 'longitude': 37.62182236 }

print "Brasilia-DF x Goiania-GO: " + str( haversine( brasilia, goiania) ) + " Km"
print "Brasilia-DF x Paris-Franca: " + str( haversine( brasilia, paris ) ) + " Km"
print "Moscou-Russia x Paris-Franca: " + str( haversine( moscou, paris ) ) + " Km"
print "Goiania-GO x Moscou-Russia: " + str( haversine( goiania, moscou ) ) + " Km"

Exit:

Brasilia-DF x Goiania-GO: 173.336761581 Km
Brasilia-DF x Paris-Franca: 8725.7318322 Km
Moscou-Russia x Paris-Franca: 2486.76670169 Km
Goiania-GO x Moscou-Russia: 11341.7186759 Km

Browser other questions tagged

You are not signed in. Login or sign up in order to post.