Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module for storing and querying geographical coordinates

Is there a Python module where I can create objects with a geographical location coordinate (latitude and longitude), and query all the objects for ones which are within a 5km distance (i.e. radius) of a given coordinate?

I've been trying to store the latitude and longitude as keys in dictionaries (as they're indexed by key) and use some distance finding algorithms to query them. But this feels like a horrible hack.

Essentially something like PostGIS for PostgreSQL, but all within my Python app's memory.

like image 379
Jon Cox Avatar asked Jan 31 '12 11:01

Jon Cox


People also ask

How do I get geo coordinates in python?

We can get GPS coordinates from python using geopy if the location is provided. The location can be provided using Nominatim from geopy and then latitude and longitude can be extracted. To know if the library is successfully installed or not, the following output will be shown on the terminal.


4 Answers

Yes, try geopy.

import geopy
import geopy.distance

pt1 = geopy.Point(48.853, 2.349)
pt2 = geopy.Point(52.516, 13.378)

dist = geopy.distance.distance(pt1, pt2).km
# 878.25

afterwards you can query your lists of points:

[pt for pt in points if geopy.distance.distance(orig, pt).km < 5.]
like image 168
eumiro Avatar answered Oct 25 '22 08:10

eumiro


I know this isn't exactly what you meant, but you could use GeoDjango with an in-memory SQLite database. It's a full set of GIS tools exposed as a Web application, which makes it a Swiss Army knife for rapidly developing GIS applications, especially for small ad hoc queries.

like image 21
Filip Dupanović Avatar answered Oct 25 '22 07:10

Filip Dupanović


The usual approach in GIS is to create a buffer around the point of interest and query the intersection. As @RyanDalton suggests, if you plan to do a lot of geolocation stuff, use Shapely, the GIS API for Python. It is good to know about Shapely even if you still want a spatial index (see below). Here is how to create buffers in Shapely:

distance = 3
center = Point(1, 1)
pts = [Point(1.1, 1.2),Point(1.2,1.2)]
center_buf = a.buffer(distance)
#filters the points list according to whether they are contained in the list
contained = filter(center_buf.contains,pts)

You can index your points yourself (let's say by longitude for example) if you don't have many. Otherwise you can also use the Rtree package, check the link called Using Rtree as a cheapo spatial database!

like image 20
Vladtn Avatar answered Oct 25 '22 07:10

Vladtn


Your dictionary idea doesn't sound that bad, though you will need to check points that fall under 'neighbouring' dictionary keys as well.

If you can't find the right tool, and like coding algorithms, you could implement a binary space partition tree which afaik is a less hacky way of achieving a similar thing.

like image 43
Sideshow Bob Avatar answered Oct 25 '22 09:10

Sideshow Bob