简体   繁体   English

如何从特定区域内的数据库中获取所有纬度和经度的列表?

[英]How do i get a list of all the latitudes and longitudes from a database within a specific area?

I have a table in a Postgres DB containing places and their corresponding latitude and longitude values.我在 Postgres 数据库中有一个表,其中包含地点及其相应的纬度和经度值。

Places (id, name, lat, lng)  # primary-key(id)

I will get an input of a pair of latitudes and longitudes forming a rectangle.我将得到一对形成一个矩形的纬度和经度的输入。

{
  "long_ne": 12.34, 
  "lat_ne": 34.45, 
  "long_sw": 15.34, 
  "lat_sw": 35.56
}

I want to get all the rows that fall inside the rectangle.我想获取落在矩形内的所有行。

The rows can't be sorted based on their lat-lng values as that will cause trouble while inserting new values.无法根据它们的 lat-lng 值对行进行排序,因为这会在插入新值时造成麻烦。

What would be the best way to go about solving this to optimize queries to get the result? go 关于解决此问题以优化查询以获得结果的最佳方法是什么?

I can obviously do it using the WHERE clause, but would it be the ost optimized solution?我显然可以使用WHERE子句来做到这一点,但它会是最优化的解决方案吗? There would be a massive number of rows in the table and is there a way this query can be optimized to speed up the result?表中会有大量的行,有没有办法可以优化这个查询来加快结果?

It this what you want?这是你想要的吗?

select *
from places
where 
    lat between :lat_no and :lat_sw
    and lng between :long_no and :long_sw

Where :lat_no , :lat_ws :long_no and :long_sw are the input parameters to the query.其中:lat_no:lat_ws :long_no:long_sw是查询的输入参数。

This gives you all rows whose latitude and longitude fall between the square boundaries.这将为您提供纬度和经度在正方形边界之间的所有行。 Note that between considers the inteval inclusive of their bounds on both ends.请注意, between考虑了包含它们两端的边界的 inteval。 You can change this as needed with inequality conditions, for example, this makes the match inclusive on the lower bound and exclusive on the upper bound:您可以根据需要使用不等式条件更改此设置,例如,这使得匹配在下限上包含而在上限上排除:

where
    lat >= :lat_no and lat < :lat_sw
    and lng >= :long_no and lng < :long_sw

Won't a simple SQL query suffice here?一个简单的 SQL 查询在这里不够吗?

SELECT *
FROM Places
WHERE   (lat > lat_sw) 
    AND (long > long_sw)
    AND (lat < lat_ne)
    AND (long < long_ne)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM