# Finding all latitude and longitudes within X km range from input location using Python

I've close to 4000 location data(latitude and longitude). The task is to find all location points(from the above location data) within the X km range from the input location.

Example

``````input_latitude = 11.2486912
input_longitude = 75.7799088
location_data = [[lat1,long1],[lat2,long2],....]
range = 1 # In Kilometers
returns [[lat,long],...] #These location points within range
``````

My Idea:

1. Taking input location as a center location point
2. Assuming a buffer circle and finding all exterior location points of the circle
3. Comparing whether the location data points fall within the buffer circle or not using center and exterior points of the buffer circle

I'm able to accomplish steps 1&2 using this reference (Creating buffer circle x kilometers from point using Python?) that is getting the exterior locations points from the input location

What my step 3 could be? The solution suggested should be scalable since I might get a large volume of location data in future.

• which library are you using that doesn't implement `st_contains`? Jan 31, 2023 at 8:20
• @IanTurton I'm using shapely python library to find the exterior co-ordinate points. shapely does have contains but all the examples have used polygon(buffer is in polygon shape). For my case how should I use this for a circle?? Jan 31, 2023 at 8:36
• A spheroidal buffer is not a circle, just a polygon. If it's circular, then something is wrong. Jan 31, 2023 at 13:46

One can use the GeoPy library for calculating distances.

So, your code may look like this:

``````from geopy.distance import distance

# 5000 input points
points = [[50.150874, 14.563832], [50.104217, 14.4757], [49.966785, 14.370114], [50.073802, 14.402783], [49.944036, 14.44569], [49.959517, 14.391878], [49.988219, 14.544117], [50.066673, 14.613868], [49.94696, 14.462611], [50.129684, 14.334411], [50.08892, 14.303301], [50.066749, 14.417668], [50.075101, 14.53634], [50.064396, 14.263388], [50.055027, 14.38642], [50.160055, 14.584758], [50.040627, 14.283248], [50.042484, 14.638717], [50.138972, 14.309488], [50.151947, 14.469412], [50.000437, 14.406288], [50.177643, 14.467332], [50.083176, 14.569287], [50.029644, 14.412903], [50.155166, 14.480961], [49.954799, 14.554184], [50.042168, 14.223561], [50.151349, 14.565225], [50.07656, 14.663827], [50.110211, 14.261647], [49.98352, 14.341426], [50.13142, 14.555974], [50.007052, 14.598136], [50.105399, 14.488696], [50.096282, 14.626482], [50.113605, 14.626768], [49.952213, 14.53148], [50.058545, 14.458349], [50.016023, 14.508792], [50.071948, 14.589867], [50.106051, 14.360603], [50.020599, 14.425797], [50.064876, 14.572056], [50.118254, 14.556321], [50.065982, 14.232], [50.045452, 14.217889], [50.135366, 14.335496], [50.120126, 14.633857], [50.16258, 14.41], [49.949706, 14.433877], [49.992574, 14.299198], [49.999167, 14.509118], [49.967147, 14.458644], [50.157903, 14.462958], [50.029415, 14.378698], [50.063549, 14.357676], [50.006606, 14.244327], [50.164178, 14.452357], [49.97892, 14.3269], [50.149529, 14.3472], [50.168217, 14.436196], [50.128047, 14.313571], [50.098681, 14.489209], [50.07064, 14.516238], [50.016935, 14.612858], [50.040573, 14.546343], [49.996446, 14.615458], [50.063094, 14.561846], [50.013674, 14.38807], [50.120068, 14.568918], [50.002958, 14.606296], [50.171301, 14.521588], [50.11462, 14.644043], [50.134498, 14.391158], [50.115566, 14.507894], ... ]

# Prague : https://geohack.toolforge.org/geohack.php?language=en&pagename=Prague&params=50_05_15_N_14_25_17_E_region:CZ-10_type:city_globe:export&title=Prague
input_point = (50.0875, 14.421389)

points_filtered = []

for point in points:
if distance(input_point, point).km < 1:
points_filtered.append(point)

print(points_filtered)
``````

It will result in:

``````[[50.082106, 14.426997], [50.095957, 14.418067], [50.084315, 14.426904], [50.084502, 14.414438], [50.090365, 14.424935], [50.083304, 14.413765], [50.085358, 14.431308], [50.091852, 14.410086], [50.087172, 14.428616], [50.086841, 14.41463], [50.090552, 14.416526], [50.083257, 14.432033], [50.087521, 14.411056], [50.08791, 14.416463], [50.092245, 14.418567], [50.087121, 14.421294], [50.091767, 14.427862], [50.084119, 14.42598], [50.09194, 14.430399], [50.08693, 14.421646], [50.084292, 14.420637], [50.094869, 14.413924]]
``````
• Hi @Taras will this solution work effectively with less time. Asking this since as mentioned in the question the location data volume might be in 1 million to 10 million Jan 31, 2023 at 10:21
• Honestly, I do not know Jan 31, 2023 at 10:29