user3964336
user3964336

Reputation:

Generate random data with lat-long

I am trying to generate random data with range of specific latitude and longitude. The code below executes with no errors and also generates output but not fully what I am expecting.

Program:

import random
import sys
import math

latitude = 19.99
longitude = 73.78
output_file = 'filename'

def generate_random_data():
    with open(output_file, 'w') as output:
        hex1 = '%012X' % random.randint(0,100)
        flt = float(random.randint(0,100))
        latitude = math.acos(random.random() * 2 - 1)
        longitude = random.random() * math.pi * 2

        output.write('%s %.1f %.6f %.6f \n' % (hex1.lower(), flt, longitude, latitude))

if __name__ == '__main__':
    generate_random_data()

The above code runs but gives only one row (which could be indentation problem) But this does not give me random lat & long in required Geo location as mentioned above.

Output generated:

000000000020 95.0 3.929691 1.749931

Output Expected: 000000000020 95.0 19.999691 73.799931

And this generates only one row where as I am trying to have million rows

Upvotes: 9

Views: 16072

Answers (4)

user_dhrn
user_dhrn

Reputation: 597

This is my version of this using NumPy:

import numpy as np

def random_lat_lon(n=1, lat_min=-90., lat_max=90., lon_min=-180., lon_max=180.):
    """
    this code produces an array with pairs lat, lon
    """
    lat = np.random.uniform(lat_min, lat_max, n)
    lon = np.random.uniform(lon_min, lon_max, n)

    return np.array(tuple(zip(lat, lon)))

Upvotes: 0

joshi123
joshi123

Reputation: 865

I modified @Scott's response to create a CSV instead of a txt file:

import random
import sys
import math

latitude = 0.794501
longitude = -0.752568
file_n = 'random_lat_lon.csv'

def generate_random_data(lat, lon, num_rows, file_name):
    with open(file_name, 'w') as output:
        for _ in xrange(num_rows):
            hex1 = '%012x' % random.randrange(16**12)                
            flt = float(random.randint(0,100))
            dec_lat = random.random()/100
            dec_lon = random.random()/100
            output.write('%s,%.1f,%.6f,%.6f \n' % (hex1.lower(), flt, lon+dec_lon, lat+dec_lat))

generate_random_data(latitude, longitude, 600, file_n)

Upvotes: 1

Scott
Scott

Reputation: 6389

I think this is what you are trying to do. I understand you are writing to a file; I took that off here just for the sake of this example.

import random
import sys
import math

latitude = 19.99
longitude = 73.78

def generate_random_data(lat, lon, num_rows):
    for _ in xrange(num_rows):
        hex1 = '%012x' % random.randrange(16**12) # 12 char random string
        flt = float(random.randint(0,100))
        dec_lat = random.random()/100
        dec_lon = random.random()/100
        print '%s %.1f %.6f %.6f \n' % (hex1.lower(), flt, lon+dec_lon, lat+dec_lat)

generate_random_data(latitude, longitude, 5)

Prints:

31d831302b7f 99.0 73.789561 19.997404 
c6ef41c70ebb 0.0 73.780732 19.994279 
4bc2df5388e3 77.0 73.785531 19.994191 
e7648f673475 40.0 73.786610 19.993679 
a11502c744a6 32.0 73.784650 19.997702 

EDIT: So your function including writing to file would then be the following

import random
import sys
import math

latitude = 19.99
longitude = 73.78
file_n = 'random_lat_lon.txt'

def generate_random_data(lat, lon, num_rows, file_name):
    with open(file_name, 'w') as output:
        for _ in xrange(num_rows):
            hex1 = '%012x' % random.randrange(16**12)                
            flt = float(random.randint(0,100))
            dec_lat = random.random()/100
            dec_lon = random.random()/100
            output.write('%s %.1f %.6f %.6f \n' % (hex1.lower(), flt, lon+dec_lon, lat+dec_lat))

generate_random_data(latitude, longitude, 5, file_n)

Which generates the data posted above the EDIT into a file named 'random_lat_lon.txt'.

Upvotes: 11

David Dahan
David Dahan

Reputation: 11162

You need to call your fonction somewhere.

Generally, in Python you do something like:

if __name__ == '__main__':
  generate_random_data()

Upvotes: 2

Related Questions