4

I've successfully used Shapely with python to check if a point is within one of a set of polygons in a .shp file but now would like to check if a point is within a circle of radius x miles from another point - how can I achieve this with Shapely?

I'd imagine I have to create a circle with radius x miles from my reference point and then check if each of my other points intersects with that new circle - I'm ok with checking the intersection but how do I create a circle with radius x miles given my coordinates are in lat/long?

I read a question that suggests I need to change projections but not sure what this means or how to apply it to my problem: creating circle with radius in metres

My example is in Africa btw, not the US (the above question mentions the US local state plane as a 'reference system' but not sure what that means)...

4
  • How about using the distance method? object.distance(other)
    – songololo
    Commented May 5, 2016 at 6:45
  • Shapely doesn't deal with projections, so you'll need to make sure that both your objects are already in the same projected coordinate reference system appropriate for the local position on the globe.
    – songololo
    Commented May 5, 2016 at 6:47
  • would really appreciate any pointers on how I can go about creating a circle in gps coordinates space with a given number of km radius - like how do I project km into gps space and create the circle?
    – alxcnwy
    Commented May 6, 2016 at 22:25
  • Performing these sorts of calculations in a geographic coordinate space can be done via PostGIS, but this involves setting up a database, but could be worth it in the long run depending on your use case. Otherwise, converting the coordinates to a projected system with pyproj for performing the operations using shapely is a good way to go.
    – songololo
    Commented May 7, 2016 at 10:18

1 Answer 1

10

To effectively use shapely it is important to first project your coordinates into a projected coordinate system that is appropriate to your region, for example, epsg:27700 if you are based in the UK. A good way to do this is using pyproj:

import pyproj as proj

# setup your projections
crs_wgs = proj.Proj(init='epsg:4326') # assuming you're using WGS84 geographic
crs_bng = proj.Proj(init='epsg:27700') # use a locally appropriate projected CRS

# then cast your geographic coordinate pair to the projected system
x, y = proj.transform(crs_wgs, crs_bng, input_lon, input_lat)

You can then go about using your projected coordinate pairs to create circles and test for point within, or you could simply use the distance method. [EDIT: Remember that units are now those of your projected coordinate system.]

from shapely import geometry

# create your two points
point_1 = geometry.Point(x_1, y_1)
point_2 = geometry.Point(x_2, y_2)

# create your circle buffer from one of the points
distance = 1000
circle_buffer = point_1.buffer(distance)

# and you can then check if the other point lies within
if point_2.within(circle_buffer):
    print('point 2 is within the distance buffer of point 1')
# or similarly
if circle_buffer.contains(point_2):
    print('circle buffer contains point 2')

# but a simpler method is to simply check the distance
if point_1.distance(point_2) < distance:
    print('point 1 is within the distance of point 2')
0

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.