Last active
March 28, 2023 08:15
-
-
Save mkhatib/5641314 to your computer and use it in GitHub Desktop.
A small class that represents a basic geolocation point and have a method to generate random points around that point given a radius and count.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A simple GeoPoint object to generate random points nearby.""" | |
import math | |
import random | |
class GeoPoint(object): | |
"""Represents a geolocation point with latitude and longitude.""" | |
def __init__(self, lat, lng): | |
self.lat = lat | |
self.lng = lng | |
def __str__(self): | |
return self.__repr__() | |
def __repr__(self): | |
return 'GeoPoint(lat: {}, lng: {})'.format(self.lat, self.lng) | |
def random_nearby_points(self, radius=1000, count=1): | |
"""Generate number of random points in a radius. | |
Args: | |
radius: Maximum distance from the point. | |
count: Number of points to generate. | |
Returns: | |
List of GeoPoint objects. | |
""" | |
points = [] | |
for i in xrange(count): | |
points.append(self.random_nearby_point(radius)) | |
return points | |
def random_nearby_point(self, radius=1000): | |
"""Generate a random point in a radius. | |
Reference URL: http://goo.gl/KWcPE. | |
Args: | |
radius: Maximum distance from the point. | |
Returns: | |
GeoPoint object. | |
""" | |
# Convert Radius from meters to degrees. | |
rd = radius / 111300.0 | |
u = random.random() | |
v = random.random() | |
w = rd * math.sqrt(u) | |
t = 2.0 * math.pi * v | |
x = w * math.cos(t) | |
y = w * math.sin(t) | |
xp = x / math.cos(self.lat) / 1.0 | |
return GeoPoint(y + self.lat, xp + self.lng) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment