Geolocation 使用db2查找特定距离内的位置

Geolocation 使用db2查找特定距离内的位置,geolocation,db2,Geolocation,Db2,我正在使用html5地理定位api获取我在纬度和经度上的位置。我希望将它们存储在位置表中,并希望在特定距离内检索这些位置 我当前的纬度和经度存储在变量latval、longval和distance中 我的桌子在哪里 列包括位置、纬度和长度 我使用DB2Express C作为数据库,现在纬度和经度列被设置为双精度类型。我应该使用什么类型来存储这些值,以及使用什么查询来获取距离内的位置名称 谢谢。看起来Express C有一个扩展,包括空间处理。我从未使用过它,目前似乎无法访问,因此我无法与它交谈。

我正在使用html5地理定位api获取我在纬度和经度上的位置。我希望将它们存储在位置表中,并希望在特定距离内检索这些位置

我当前的纬度和经度存储在变量latval、longval和distance中 我的桌子在哪里 列包括位置、纬度和长度 我使用DB2Express C作为数据库,现在纬度和经度列被设置为双精度类型。我应该使用什么类型来存储这些值,以及使用什么查询来获取距离内的位置名称
谢谢。

看起来Express C有一个扩展,包括空间处理。我从未使用过它,目前似乎无法访问,因此我无法与它交谈。我假设您希望使用“查找半径内的所有位置”是一个非常标准的查询

如果出于某种原因您无法使用扩展,我会这样做: 保持表的原样,或者使用浮点数据类型,尽管请使用完整的属性名,但没有理由截断它们。出于简单的需要,“位置”的名称可以存储在表中,但如果同一位置有多个对象,则可能需要为其指定一个数字id,以便实际点只在其中一次。 您还需要覆盖纬度和经度的标记,可能每个方向一个,或者每个列一个

然后,给定起始位置和距离,使用以下查询:

SELECT name, latitude, longitude
FROM location
WHERE (latitude >= :currentLatitude - :distance
       AND latitude <= :currentLatitude + :distance)
AND (longitude >= :currentLongitude - :distance
     AND longitude <= :currentLongitude + :distance)
  -- The previous four lines reduce the points selected to a box.
  -- This is, or course, not completely correct, but should allow
  -- the query to use the indicies to greatly reduce the initial 
  -- set of points evaluated.
  -- You may wish to flip the condition and use ABS(), but
  -- I don't think that would use the index...
AND POWER(latitude - :currentLatitude, 2) + POWER(longitude - :currentLongitude, 2) 
         <= POWER (:distance, 2)
  -- This uses the pythagorean theorem to find all points within the specified
  -- distance.  This works best if the points have been pre-limited in some
  -- way, because an index would almost certainly not be used otherwise.
  -- Note that, on a spherical surface, this isn't completely accurate
  --  - namely, distances between longitude points get shorter the farther
  --       from the equator the latitude is - 
  -- but for your purposes is likely to be fine.

请注意,将距离函数包装在标记为DETERMINISTIC的UDF中将允许将其放置在SELECT和HAVING部分中,但实际上只调用一次,从而消除了对CTE的需要。

感谢您的努力。我正在寻找使用球面公式的距离查询。使用大圆距离公式的查询是什么?非常感谢x-zero。我现在在我的大学里。我回家后会检查它是否工作。我还安装了SpatialExtender。但是我不知道如何在里面存储坐标。如果你知道这方面的任何情况,请告诉我,这将非常有帮助。。您可以在pluginsfordb2页面上找到spatialextender。它返回给我以下错误SQL0206N DIST在使用它的上下文中无效。SQLSTATE=42703 SQL0206N DIST'使用边界坐标公式时,以下查询正在运行,但未显示任何结果。选择名称为Nearest时,ACOSSINRADIANSlatitude*SINRADIANS17.386211+COSRADIANSlatitude*COSRADIANS17.386211*COSRADIANS78.430578-经度*6371作为测试选择的距离*从Nearest WHERE dist请注意,公式中存在一些错误,主要是因为与以下表示相关的不精确性:,因为float/double不能精确地表示某些数字。您确定在该距离内列出了点吗?
WITH Nearby (name, latitude, longitude, dist) as (
 SELECT name, latitdude, longitude,
       ACOS(SIN(RADIANS(latitude)) * SIN(RADIANS(:currentLatitude)) + 
            COS(RADIANS(latitude)) * COS(RADIANS(:currentLatitude)) * 
            COS(RADIANS(:currentLongitude - longitude))) * :RADIUS_OF_EARTH as dist
FROM location 
WHERE (latitude >= :currentLatitude - DEGREES(:distance / :RADIUS_OF_EARTH)
       AND latitude <= :currentLatitude + DEGREES(:distance / :RADIUS_OF_EARTH))
AND (longitude >= :currentLongitude - 
               DEGREES(:distance / :RADIUS_OF_EARTH / COS(RADIANS(:currentLatitude)))
     AND longitude <= :currentLongitude + 
               DEGREES(:distance / :RADIUS_OF_EARTH / COS(RADIANS(:currentLatitude))))
)
SELECT *
FROM Nearby
WHERE dist <= :distance