I have a table named coords with the following columns |name|lat|lon|und|
import sqlite3
database = sqlite3.connect('geoida.db')
cursor = database.cursor()
cursor.execute("select lat, lon, und from coords")
results = cursor.fetchall()
every line in table stores coordinates of one point of g r i d and distance from point to point is always in decimal 0.041667,
what is equal to 2.5”.
What I would like to achieve is to find 4 nearest adjacent points around given latitude and longitude in decimal.
We have to keep in mind that latitude and longitude of these four points have to fill quite simple condition:
excess between lat, lon of adjacent point and lat, lon
of given point must be less/equal 0.041667 on + or –
or we can treat this value as max radius divisive sought neighboring points from the given one.
for example:
for given point 56.02050000 13.02040000
4 nearest adjacent points taken from my coords table are:
56.000000 13.000000
56.000000 13.041667
56.041667 13.000000
56.041667 13.041667
Given points are stored in another database, where C1 is latitude and C2 is longitude
database = sqlite3.connect('F.tsj')
cursor = database.cursor()
cursor.execute("select C1, C2 from tblSoPoints")
results = cursor.fetchall()
How can I put such query using python?
Sorry for code but there’s something wrong with formating.
full example with rtree index
Note: this code doesn’t include boundaries.
For very efficient range queries if there are millions of coordinates you might need SQLite R-Tree index.
For 1000000 entries the above approach takes ~0.16 seconds, but the function that uses rtree requires less than 1ms. For 10000 entries it is 800 µs vs. 20 µs for rtree-based solution for the data from the test. DISCLAIMER: The numbers are for the code I’ve posted that I run on my machine.