【发布时间】:2012-06-28 16:32:08
【问题描述】:
我正在使用 CartoDB 作为我的应用程序的 PostGIS 服务器。
我需要存储轨迹并为每个坐标对轨迹点关联一个时间戳,以便进行give me the distance traveled between 12AM and 12PM on day X 之类的查询。
在 CartoDB 中,我只能创建 3 种类型的表:MULTIPOINT、MULTILINESTRING 和 MULTIPOLYGON。要插入曲目,我使用的是 MULTILINESTRING 表。
我的第一次尝试是插入时间戳作为 MULTILINESTRING 中每个点的 Z 索引,但我总是得到一个 ERROR: Geometry has Z dimension but column does not.
这是如何在“普通” PostGIS 数据库中实现的,以及如何在 CartoDB PostGIS 实现中实现它?
更新 1:
所以在 jatorre 回答之后,我创建了一个 JSFiddle 示例作为练习。 但是我得到了不连贯的结果。
在上面的示例中,我有两个具有相同两个数据集的表。然而,一个是MULTILINESTRING 表,其中每一行代表一个segment,另一个是MULTIPOINT 表,我在其中存储每个segment 坐标集。
然后我查询这两个表以获取total distance of segments according to transport mode。我想我很清楚地知道了 jattore 的想法,但是我不明白为什么 Car 和 Walk 总距离会得到不同的结果。有什么提示吗?
解决方案:
距离的微小差异是因为我需要在对segment 进行分组之前对points 表进行排序。
这是我当前的查询,以获取根据运输方式行驶的总距离和时间:
WITH segments AS
(SELECT ST_Makeline(pts.the_geom_webmercator) as the_geom, (MAX(pts.timestamp) - MIN(pts.timestamp)) AS time, paths.transport_mode, paths.cartodb_id AS id
FROM (SELECT * FROM points ORDER BY track_id, path_id, timestamp ASC) AS pts JOIN paths ON pts.path_id=paths.cartodb_id
WHERE paths.user_id=1
GROUP BY id, transport_mode)
SELECT SUM(ST_Length(segments.the_geom)) AS distance, SUM(segments.time), segments.transport_mode
FROM segments
GROUP BY segments.transport_mode
ORDER BY distance
【问题讨论】: