LoginSignup
1
1

More than 5 years have passed since last update.

python + faker ある地点から半径100mの点をランダムに生成する

Last updated at Posted at 2015-11-02

計算はこちらのサイトを参考にさせて頂きました。
http://mononofu.hatenablog.com/entry/20090324/1237894846

python の faker を使います.

fakerには日本ロケールがあるので、それを利用。

例えば、東京駅を中心に半径100mの円の中に点を10個生成

#!/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

このままだと使いづらいので、引数で指定できるように対応させる。

## 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

複数の点をプロットして確認できるため非常に便利です!
ありがとうございます!
http://www.tree-maps.com/prot/

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1