Advertisement
Sandbird

Random points inside boundaries

Jul 18th, 2022
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. import numpy as np
  2. import random
  3. import csv
  4. from shapely.geometry import Polygon, Point, LineString
  5.  
  6. poly = Polygon([(23.641881537889,37.929053926155), (23.666295612031,37.942899212831),
  7. (23.68803937946,37.941695374075), (23.704824056742,37.915206015202),
  8. (23.750218956531,37.85436479936), (23.775080866712,37.833451364383),
  9. (23.752192655178,37.922010896235), (23.791022107604,37.978829639512),
  10. (23.835081854037,38.006198162822), (23.862523655673,38.033885868582),
  11. (23.822469306444,38.094878864117), (23.823222369487,38.125143309181),
  12. (23.720225543315,38.11373449824), (23.688220971962,38.092972349773),
  13. (23.694610606763,38.064907199052), (23.665842815668,38.034725935164),
  14. (23.649439609277,38.008420474693), (23.603281778183,37.960745968997),
  15. (23.609957492578,37.948408725358), (23.622736730746,37.942239326525),
  16. (23.638758455769,37.950214302858), (23.647532265301,37.944346007546),
  17. (23.636481660717,37.936852529986), (23.641881537889,37.929053926155)])
  18.  
  19.  
  20. def random_points_within(poly, num_points):
  21.     min_x, min_y, max_x, max_y = poly.bounds
  22.  
  23.     points = []
  24.  
  25.     while len(points) < num_points:
  26.         random_point = Point([random.uniform(min_x, max_x), random.uniform(min_y, max_y)])
  27.         if (random_point.within(poly)):
  28.             points.append(random_point)
  29.  
  30.     return points
  31.  
  32. points = [random_points_within(poly, i) for i in range(10)]
  33. #save points to csv line by line (lat, lon)
  34. with open('results.csv', 'w', newline='') as csvfile:
  35.     writer = csv.writer(csvfile, delimiter=',')
  36.     for point in points:
  37.         for p in point:
  38.             writer.writerow([p.y, p.x])
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement