Compare Latitude and Longitude in Python

2

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 (a polygon of 8 vertices, to be exact), if is not within the area, it does not put it in the database.

I just need to know how to make this comparison if the customer is within the specified area, I imagine it is using the Latitude and Longitude values.

    
asked by anonymous 05.10.2017 / 20:44

2 answers

2

You can use an object-manipulation 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 a polygon of 8 vertices circumvents the Brazilian territory in the terrestrial globe:

Output:

True
True
False
False
    
06.10.2017 / 00:58
0

I suggest replacing the 8-vertex polygon area with a circle.

You can use the Haversine Formula to calculate the distance between two geographic coordinates:

Oncewiththedistancebetweentheclientandthecalculatedreferencepoint,youareabletoevaluateiftheclientiswithinthe"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"

Output:

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
    
05.10.2017 / 21:54