Storing and Querying GPS Coordinates Effectively

后端 未结 8 1941
一整个雨季
一整个雨季 2021-02-02 17:31

I want to create a large database of GPS coordinates that can be queried by saying \"Return all coordinates that are within \'n\' metres of [this coordinate]\".

I need i

相关标签:
8条回答
  • 2021-02-02 18:00

    I typically do this sort of query using lat/lon. Using spherical geometry, you can put a bounding box around a specific point. For example, say you have a point (X,Y) that you want all coordinates within 1 mile (conversion to meters I'll leave as an exercise for the reader). You can determine a bounding box of (X-1,Y-1),(X+1,Y+1). Then you query your points database using the BETWEEN operator (SELECT foo FROM bar WHERE LAT BETWEEN X-1 AND X+1 AND LON BETWEEN Y-1 AND Y+1). Then you do your detail distance calculation to "round the corners" of your bounding box.

    The caveat is that longitude lines are closer together at the top of the sphere, so you'll get skewed results the further away you are from the equator. But it still serves to quickly filter down your results sets.

    Google "Great Circle Distance" for the calculations.

    EDIT: There are 0.167469 degrees of longitude per mile (it actually ranges from 0.167469 to 0.014564), and 0.014483 degrees of latitude per mile. So your bounding box is (lat - (miles * 0.014483), lon - (miles * 0.167469)), (lat + (miles * 0.014483), lon + (miles * 0.167469))

    0 讨论(0)
  • 2021-02-02 18:02

    GIS databases (e.g. PostgreSQL with PostGIS) actually provide data structures for two- or three dimensional region-searches (spatial indices). The simplest one is the grid index, then the different search trees (kd-tree, quad-tree) with R-tree as the most frequently used (a generalized B-tree for more dimensions). These methods seem adequate.

    A basic grid-index (partitioning the space into grid-cells, and searching only in the nearby cells) can be implemented easily and can reduce the search time significantly. Search trees are a bit harder to implement, but there are lots of open-source implementations for lots of programming languages and DBs (like PostGIS or Geopandas etc.). Using them for problems like this one usually pays off.

    0 讨论(0)
提交回复
热议问题