Pages

Thursday 18 December 2014

Python : Generate Random Latitude and longitude


Take any Lat-Long Position across the globe.

Example
Center of NewYork,NY,USA
LATITUDE = 40.750422
LONGITUDE = -73.996328



Python code to generate 100 random Lat Long positions:


=====================================================================
import random
import sys
import math

outfile = open("output\randomLatLong_NY.txt", "w")

radius = 10000                         #Choose your own radius
radiusInDegrees=radius/111300            
r = radiusInDegrees
x0 = 40.84
y0 = -73.87

for i in range(1,100):                 #Choose number of Lat Long to be generated

  u = float(random.uniform(0.0,1.0))
  v = float(random.uniform(0.0,1.0))

  w = r * math.sqrt(u)
  t = 2 * math.pi * v
  x = w * math.cos(t) 
  y = w * math.sin(t)
  
  xLat  = x + x0
  yLong = y + y0
  outfile.write (str(xLat) + "," + str(yLong) + '\n')

=====================================================================

To verify: Plot randomly generated Lat Longs on Map -








4 comments:

Anonymous said...

Does not work

Kavitha said...



Well Done ! the blog is great and Interactive it is Python : Generate Random Latitude and longitude it is useful for students and Python Developers for more updates on python follow the link

Python Online Training Bangalore

For more info on other technologies go with below links

tableau online Training

ServiceNow Online Training

mulesoft Online Training

Unknown said...

It's A Great Pleasure reading your Article, learned a lot of new things, we have to keep on updating it Mulesoft certification training Thanks for posting.

Ananad said...
This comment has been removed by the author.