Generate random data with lat-long

11,041

Solution 1

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'.

Solution 2

You need to call your fonction somewhere.

Generally, in Python you do something like:

if __name__ == '__main__':
  generate_random_data()

Solution 3

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)
Share:
11,041

Related videos on Youtube

pnuts
Author by

pnuts

Updated on July 28, 2022

Comments

  • pnuts
    pnuts almost 2 years

    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

    • Scott
      Scott almost 9 years
      It could help if in your post you create some expected output (range of lat, lon, how many rows, etc.).
    • Boggio
      Boggio almost 9 years
      Why not use a specialized package to generate fake data, so you can focus on other more important parts. I would recommend using Faker
  • Admin
    Admin almost 9 years
    Output_file is not defined error after this function called
  • David Dahan
    David Dahan almost 9 years
    So..define it. output_file = "toto.txt" Plus the random() functiontakes no argument.
  • Admin
    Admin almost 9 years
    Thanks .. initial issue solved program is edited pls check once
  • Scott
    Scott almost 9 years
    @SitzBlogz One last thing: If your intention is that hex1 is a unique identifier you won't quite get that with '%012X' % random.randint(0,100) . If you want 12 character random strings do this instead: '%012x' % random.randrange(16**12). In fact I'll update my answer to include that.
  • Admin
    Admin almost 9 years
    can we have time series also randomly generated? Like start time HH:MM:SS and end time HH:MM:SS ? I mean to say in two new columns.
  • Scott
    Scott almost 9 years
    I started writing something but then searched and found stackoverflow.com/a/553320/4663466. So you could do something like s = '07:00:00' e = '16:45:00' f = '%H:%M:%S' and strTimeProp(s, e, f, random.random()). Which for example could give you 12:39:14. If you have any trouble integrating this into the function above post it as a new question and I'll try to answer before someone else :)