Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: elegant way of finding the GPS coordinates of a circle around a certain GPS location

I have a set of GPS coordinates in decimal notation, and I'm looking for a way to find the coordinates in a circle with variable radius around each location.

Here is an example of what I need. It is a circle with 1km radius around the coordinate 47,11.

What I need is the algorithm for finding the coordinates of the circle, so I can use it in my kml file using a polygon. Ideally for python.

like image 840
otmezger Avatar asked Apr 08 '13 19:04

otmezger


People also ask

How do I find the nearest location in Python?

Use reverse_geocode library in python to get nearest city with country. To add one do this coordinates = (-37.81, 144.96), It needs comma in the end, otherwise it gives error.

How do I get the distance between two latitude and longitude in Python?

The math. dist() method returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point.


2 Answers

see also Adding distance to a GPS coordinate for simple relations between lat/lon and short-range distances.

this works:

import math

# inputs
radius = 1000.0 # m - the following code is an approximation that stays reasonably accurate for distances < 100km
centerLat = 30.0 # latitude of circle center, decimal degrees
centerLon = -100.0 # Longitude of circle center, decimal degrees

# parameters
N = 10 # number of discrete sample points to be generated along the circle

# generate points
circlePoints = []
for k in xrange(N):
    # compute
    angle = math.pi*2*k/N
    dx = radius*math.cos(angle)
    dy = radius*math.sin(angle)
    point = {}
    point['lat']=centerLat + (180/math.pi)*(dy/6378137)
    point['lon']=centerLon + (180/math.pi)*(dx/6378137)/math.cos(centerLat*math.pi/180)
    # add to list
    circlePoints.append(point)

print circlePoints
like image 88
Stéphane Avatar answered Oct 12 '22 01:10

Stéphane


Use the formula for "Destination point given distance and bearing from start point" here:

http://www.movable-type.co.uk/scripts/latlong.html

with your centre point as start point, your radius as distance, and loop over a number of bearings from 0 degrees to 360 degrees. That will give you the points on a circle, and will work at the poles because it uses great circles everywhere.

like image 37
Spacedman Avatar answered Oct 12 '22 02:10

Spacedman