python + faker Randomly generate a point with a radius of 100m from a certain point

I referred to this site for the calculation. http://mononofu.hatenablog.com/entry/20090324/1237894846

Use python's faker.

There is Japanese locale in faker, so use it.

For example, generate 10 points in a circle with a radius of 100m centered on Tokyo Station.

#!/usr/bin/env python

from faker import Factory
fake = Factory.create('ja_JP')

import math
import csv

##
# 
samples       = 10
limit_samples = 10000
csvfile       = "data.csv"

# Tokyo Station(35.681382, 139.766084)
centerlat  = 35.681382
centerlong = 139.766084
radius_m   = 100

#########
earthradius = 6378137
lat1radm   = ((2*math.pi*earthradius)/360)
latradius  = radius_m/lat1radm
long1radm  = ((earthradius*math.cos(centerlat/180*math.pi)*2*math.pi)/360)
longradius = radius_m/long1radm

with open(csvfile, "w+") as f:
    csv_writer = csv.writer(f)
    
    counter = 0
    for _ in range(0,limit_samples):
        geolat  = fake.geo_coordinate(center=centerlat , radius=latradius)
        geolong = fake.geo_coordinate(center=centerlong, radius=longradius)
        
        r = math.sqrt(math.pow((float(geolat)-centerlat)*lat1radm,2)+math.pow((float(geolong)-centerlong)*long1radm,2))
        if r < radius_m:       
            d = [geolat, geolong]
            #print d
            csv_writer.writerow(d)
            counter = counter + 1
        
        if counter >= samples:
            break

Since it is difficult to use as it is, make it correspond so that it can be specified by an argument.

## args
import argparse
parser = argparse.ArgumentParser(description='Generater of coordinate points.')
parser.add_argument('--samples', type=int, nargs='?', default=10, help='number of points')
parser.add_argument('--csv', type=str, nargs='?', default="dummydata.csv", help='csv file name')
parser.add_argument('--radius', type=int, nargs='?', default=100, help='radius(m)')
# Tokyo Station(35.681382, 139.766084)
parser.add_argument('--latitude', type=float, nargs='?', default=35.681382, help='center latitude of the circle')
parser.add_argument('--longitude', type=float, nargs='?', default=139.766084, help='center longitude of the circle')
args = parser.parse_args()
   
##
# 
samples       = args.samples
limit_samples = samples * samples
csvfile       = args.csv
centerlat     = args.latitude
centerlong    = args.longitude
radius_m      = args.radius
$ python genpointsbygeo.py -h
usage: genpointsbygeo.py [-h] [--samples SAMPLES] [--csv CSV]
                         [--radius RADIUS] [--latitude LATITUDE]
                         [--longitude LONGITUDE]

Generater of coordinate points.

optional arguments:
  -h, --help            show this help message and exit
  --samples SAMPLES     number of points
  --csv CSV             csv file name
  --radius RADIUS       radius(m)
  --latitude LATITUDE   center latitude of the circle
  --longitude LONGITUDE
                        center longitude of the circle

$ python genpointsbygeo.py --samples 100 --csv data1.csv --radius 1000
$ wc -l data1.csv 
     100 data1.csv

It is very convenient because you can plot and check multiple points! Thank you! http://www.tree-maps.com/prot/

Recommended Posts

python + faker Randomly generate a point with a radius of 100m from a certain point
[Python] Randomly generate a large number of English names
Hash with python and escape from a certain minister's egosa
Generate Japanese test data with Python faker
Generate a class from a string in Python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
A memorandum of calling Python from Common Lisp
Solve A ~ D of yuki coder 247 with python
Generate an insert statement from CSV with Python.
Create a decision tree from 0 with Python (1. Overview)
Read line by line from a file with Python
Extract data from a web page with Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Perform a Twitter search from Python and try to generate sentences with Markov chains.
A memo connected to HiveServer2 of EMR with python
Learn Nim with Python (from the beginning of the year).
Recommendation of building a portable Python environment with conda
Generate a guy like two masters of a certain program
Make a copy of a Google Drive file from Python
Generate a vertical image of a novel from text data
Generate a random sentence from your tweet with trigram
From buying a computer to running a program with python
A addictive point in "Bayesian inference experience with Python"
[Basics of data science] Collecting data from RSS with python
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
A memo that reads data from dashDB with Python & Spark
Extract template of EML file saved from Thunderbird with python3.7
Acquisition of 3D point cloud with Softbank Pepper (Choregraphe, Python)
A collection of competitive pro techniques to solve with Python
Different from the import type of python. from A import B meaning
I want to start a lot of processes from python
Get a list of purchased DMM eBooks with Python + Selenium
Detect objects of a specific color and size with Python
Python: Create a dictionary from a list of keys and values
Python> Read from a multi-line string instead of a file> io.StringIO ()
Create a pixel art of Levi Captain with Python programming!
I tried a stochastic simulation of a bingo game with Python
Turn an array of strings with a for statement (Python3)
Generate XML (RSS) with Python
Randomly generate a complete permutation
Make a fortune with Python
Create a directory with python
With skype, notify with skype from python!
Create an instance of a predefined class from a string in Python
Save the result of the life game as a gif with python
Trial of voice recognition using Azure with Python (input from microphone)
[python, ruby] fetch the contents of a web page with selenium-webdriver
[3rd] I tried to make a certain authenticator-like tool with python
From a book that programmers can learn ... (Python): Review of arrays
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
I made a lot of files for RDP connection with Python
The story of making a standard driver for db with python.
[Python] Get the update date of a news article from HTML
Batch download images from a specific URL with python Modified version
The idea of feeding the config file with a python file instead of yaml
[4th] I tried to make a certain authenticator-like tool with python
[Python] Correlation is below a certain level ・ Maximum number of features
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
[1st] I tried to make a certain authenticator-like tool with python
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Pass a list by reference from Python to C ++ with pybind11