Module: RandomCoordinates
- Defined in:
- lib/random_coordinates.rb,
lib/random_coordinates/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- EARTH_RADIUS =
km
6371
- VERSION =
"0.1.0"
Class Method Summary collapse
Class Method Details
.at(centroid, radius, kml = nil) ⇒ random_lat, random_lng
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/random_coordinates.rb', line 14 def self.at(centroid, radius, kml=nil) max_distance = Math.sqrt(radius * 100 / 3) min_distance = Math.sqrt(radius * 100 / 8) # Latitude north/south distance = Random.rand(min_distance..max_distance) new_lat = distance * 180 / (Math::PI * EARTH_RADIUS) new_lat = Random.rand(0..1).zero? ? new_lat : -new_lat # north (+) or south (-) # Longitude east/west distance = Random.rand(min_distance..max_distance) new_lng = distance * 180 / (Math::PI * EARTH_RADIUS * Math.cos(new_lat * Math::PI / 180)) new_lng = Random.rand(0..1).zero? ? new_lng : -new_lng # east (+) or west (-) random_lat = (centroid[:lat] + new_lat).round(5) # meter precision random_lng = (centroid[:lng] + new_lng).round(5) # meter precision unless kml.nil? boundary = BorderPatrol.parse_kml(File.read(kml)) if !boundary.contains_point?(random_lng, random_lat) at(centroid, kml, radius) end end [random_lat, random_lng] end |