Skip to content

Instantly share code, notes, and snippets.

@alexcpn
Created November 27, 2015 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcpn/eba81b50259ee75455cf to your computer and use it in GitHub Desktop.
Save alexcpn/eba81b50259ee75455cf to your computer and use it in GitHub Desktop.
Creating a random point at an approximate distance from a given latitude and longitude
# Testing simlation of generating random points
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
def create_random_point(x0,y0,distance):
"""
Utility method for simulation of the points
"""
r = distance/ 111300
u = np.random.uniform(0,1)
v = np.random.uniform(0,1)
w = r * np.sqrt(u)
t = 2 * np.pi * v
x = w * np.cos(t)
x1 = x / np.cos(y0)
y = w * np.sin(t)
return (x0+x1, y0 +y)
fig = plt.figure()
ax = host_subplot(111, axes_class=AA.Axes)
#ax.set_ylim(76,78)
#ax.set_xlim(13,13.1)
ax.set_autoscale_on(True)
latitude1,longitude1 = 13.04738626,77.61946793
ax.plot(latitude1,longitude1,'ro')
for i in range(1,20):
x,y = create_random_point(latitude1,longitude1 ,500 )
ax.plot(x,y,'bo')
dist = haversine(x,y,latitude1,longitude1)
print "Distance between points is " ,dist # a value approxiamtely less than 500 meters
plt.show()
@alexcpn
Copy link
Author

alexcpn commented Nov 27, 2015

Distance between points is 0.288044147914
Distance between points is 0.409557451806
Distance between points is 0.368260305716
Distance between points is 0.340720560546
Distance between points is 0.453773334731
Distance between points is 0.460608754561
Distance between points is 0.497188825576
Distance between points is 0.603178188859
Distance between points is 0.628898384307
Distance between points is 0.416297587754
Distance between points is 0.503691568896
Distance between points is 0.175153349209
Distance between points is 0.195149463735
Distance between points is 0.424094009858
Distance between points is 0.286807741494
Distance between points is 0.558049206307
Distance between points is 0.498612171417
Distance between points is 0.047344718215
Distance between points is 0.484232497086
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment