I've built an 2-dimensional spatialite db with millions of polygons inside. What I want to do is to find the polygon which is the nearest from given (x,y) coordinate.
This is what I want to do.
from pyspatialite.dbapi2 import dbcon = db.connect(spatialite_file_path)con.execute("SELECT InitSpatialMetaData();")con.execute("""CREATE TABLE `polygons`( `id` INTEGER PRIMARY KEY);""")con.execute("SELECT AddGeometryColumn('polygons','polygon',3857,'POLYGON',2);")con.execute("SELECT CreateSpatialIndex('polygons','polygon');") # R-Tree may help?con.execute("""INSERT INTO `polygons`(`id`,`polygon`)VALUES(1,GeomFromText('POLYGON((-13587213.2154 4569316.48944,-13584216.7174 4569316.48944,-13584216.7174 4575716.39583,-13587213.2154 4575716.39583))',3857));"""):# I wanna start my iteration...But I found out that my task, even finding the nearest MBR from (x,y), I had no idea what to start from. Would please someone help me filling the rest of this query?
أكثر...
This is what I want to do.
- Find the polygon with nearest MBR from (x,y). Let's name it polygon#1.
- Calculate the actual distance from (x,y) to polygon#1. Set the actual nearest distance to d1.
- Find the polygon with second nearest MBR from (x,y). Let's name it polygon#2.
- If polygon#2's MBR distance from (x,y) is over d1, stop iterating. d1 is the smallest, which means that polygon#1 is the nearest polygon. Done.
- Calculate the actual distance from (x,y) to polygon#2. Set the actual nearest distance to d2, update the nearest distance if d1 > d2.
- Continue
from pyspatialite.dbapi2 import dbcon = db.connect(spatialite_file_path)con.execute("SELECT InitSpatialMetaData();")con.execute("""CREATE TABLE `polygons`( `id` INTEGER PRIMARY KEY);""")con.execute("SELECT AddGeometryColumn('polygons','polygon',3857,'POLYGON',2);")con.execute("SELECT CreateSpatialIndex('polygons','polygon');") # R-Tree may help?con.execute("""INSERT INTO `polygons`(`id`,`polygon`)VALUES(1,GeomFromText('POLYGON((-13587213.2154 4569316.48944,-13584216.7174 4569316.48944,-13584216.7174 4575716.39583,-13587213.2154 4575716.39583))',3857));"""):# I wanna start my iteration...But I found out that my task, even finding the nearest MBR from (x,y), I had no idea what to start from. Would please someone help me filling the rest of this query?
أكثر...