5

All I need is the ability to create random points within a radius of another point.

so given self.fence.latitude and self.fence.longitude and self.fence.radius I should be able to generate random points with the following code (i'm writing in python/django and using numpy but all these calcs can be done using the standard math library)

for whatever reason my points are showing up in a line. I need random points.

# angle for this random point
angle = numpy.float128(2 * numpy.pi * numpy.random.random_sample())
# radius from fence center
radius = numpy.float128(self.fence.radius) * numpy.random.random_sample()

# diff from ceneter in radians
latitudinal_diff_rad = radius * numpy.sin(angle) / numpy.float128(settings.EARTH_RADIUS)
longitudinal_diff_rad = radius * numpy.cos(angle) / numpy.float128(settings.EARTH_RADIUS)

# radians to degrees
latitudinal_diff = latitudinal_diff_rad * 180 / numpy.pi
longitudinal_diff = longitudinal_diff_rad * 180 / numpy.pi

try :
     # create the item
    item = Item(
        latitude = numpy.float128(self.fence.latitude) + latitudinal_diff,
        longitude = numpy.float128(self.fence.longitude) + latitudinal_diff,
    )
    item.save()

except IntegrityError, e :
    continue
    print e

stupid line

What am i doing wrong?

Edit

# this was the math I was using before I simplified it and switched to numpy lib
distance_from_point = float(self.fence.radius) * random.random()
unit_length = distance_from_point / settings.EARTH_RADIUS
longitudinal_diff_rad = ((unit_length) * (2 * random.random() - 1))
latitudinal_diff_rad = math.acos(math.cos(unit_length) / math.cos(longitudinal_diff_rad))
3
  • 2
    I see three errors right off: (1) you won't get a circle with this approach due to the metric distortions in using latitude and longitude; (2) you appear to confound meters with degrees in the calculation of the radius; and (3) there are some (obvious) typos in the last calculations. Your code could be approximately fixed if you know you will be making small circles (a few tens of kilometers or less) at points far from the poles. Would that be the case?
    – whuber
    Jun 18, 2013 at 20:13
  • lets start back at the beginning of the problem. I am trying to find random points within a certain radius of another point. The distribution doesn't have to be perfectly random, the fence radius will be between 10 to 200km roughly. I wasn't feeling great about my math when I tossed this up here, how would you fix it? Jun 18, 2013 at 20:21
  • Do you have the ability to project your data, such as a general projection library? Re the edit, if you want to generate points within a disk uniformly at random (rather than just along its circular boundary), then you need to make the distance equal to the square root of a uniform value: that's because there isn't too much area close to the center; most of the circle's area is near its perimeter.
    – whuber
    Jun 18, 2013 at 21:47

1 Answer 1

2

i found my error...

longitude = numpy.float128(self.fence.longitude) + latitudinal_diff,

should be

longitude = numpy.float128(self.fence.longitude) + longitudinal_diff,

explains my 45 degree angle.

3
  • Great. Please mark this as the accepted answer when you can (may have to wait a day). That way future readers will know what worked to fix the issue.
    – user3461
    Jun 19, 2013 at 13:46
  • yea i do have to wait a day to accept it. I'm not sure how helpful this will be for future folks as it was just a copy paste error. Jun 19, 2013 at 13:54
  • If anything it serves as a reminder to check closely for copy/paste (and syntax) errors. There are many similar examples in the questions on this site.
    – user3461
    Jun 19, 2013 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.