Postgis 数据库:如何获取指定时间戳和指定区域之间的所有 gps 点?

Postgis Database: How can I get all gps points between specified timestamps and specified region?

我对 DBMS 完全陌生。我在每个用户的不同 csv 文件中都有车辆痕迹。格式:名称,时间戳,纬度,经度,randomId。 eg:user0,2008-10-2309:42:25,441972.694217,4428508.5117,2704942289

1) 如何实现 RANGE 查询,它要求在时间戳 (t1) 和 t2 之间看到的所有车辆的所有 gps 点在范围 (center= lat,lon; radius =r公里)。

因为我在所有 csv 中有数十亿行。我创建了一个基本的 table

  CREATE TABLE userDataBase1
(
    gid serial NOT NULL,
    name character varying(50),
    time_stamp TIMESTAMPTZ // postgresql doesn't have this datatype
    latitude numeric(12,8),// Don't know the data type for UTM points
    longitude numeric(12,8),
    pseudonym integer,
    the_geom geometry
);

要这样直接复制吗?

\copy landmarks(name,time_stamp,landmark,latitude,longitude) FROM '/local/path/to/Individual_Landmarks.csv' DELIMITERS ',' CSV HEADER;

2) 复制和构建数据库的最佳方法是什么,以便我的 RANGE 查询(如上定义)returns 有效地从数十亿条轨迹中获取数据。

Atleast 基本实现,效果也不错。

因为我是 DBMS 新手。小片段的解释真的很有帮助。太感谢了! P.S: 我正在使用 postgre 9.5、postgis 2.2、windows 10、pgAdmin III

仅供参考:我已通过 Python 脚本成功连接到数据库。

import psycopg2
conn = psycopg2.connect(database="postgis_unistuttgart", user="postgres", password="vishnu", host="127.0.0.1", port="5432")
print "Opened database successfully"

编辑1: 问题的小变化。我已经将纬度和经度更改为 UTM,就像使用 python 脚本一样。

import utm
import os
def gpsToUtm(latDeg,lonDeg):
    #print "gpsToUtm:",latDeg,lonDeg
    lat,lon,zoneNo,Zoneletter = utm.from_latlon(latDeg, lonDeg)    
    return lat,lon

例如:现在我在 UTM 中有这样的位置值 (441972.694217,4428508.5117)。

1) PostgreSQL Table 中 UMT position(Meters) 的数据类型应该是什么? 2) TIMESTAMPTZ 在我的 postgresql 版本中不可用。那么这种格式的正确数据类型应该是什么

2008-10-2309:42:25

.

鉴于你有数十亿行,你最好使用 table 像这样:

CREATE TABLE userDataBase1
(
    gid serial NOT NULL,
    name character varying(50),
    pseudonym integer,
    the_geom geography
);

请注意,lat/long 可从地理列中获得,因此无需将它们作为常规列再次存储在 table 中。要按照评论中的建议加载 table,您将批量加载到 table,然后从中加载到永久 table,如下所示:

CREATE TEMPORARY TABLE load_table
(
    name character varying(50),
    ts timestamptz,
    latitude numeric(12,8),
    longitude numeric(12,8)
);
\copy load_table FROM '/path/to/file' WITH CSV;

然后您可以使用查询将数据从 'load_table' 复制到永久 table,例如:

INSERT INTO userDataBase1 (name, ts, the_geom) SELECT name, ts, st_setsrid(st_makepoint(longitude, latitude),4326)::geography FROM load_table;

加载数据后,您将创建要点索引以加快查找速度:

CREATE INDEX userDataBase1_the_geom_idx ON userDataBase1 USING GIST (the_geom);

加载数据并建立索引后,您可以发出查询以提取您感兴趣的记录:

SELECT * FROM userDataBase1 WHERE ts BETWEEN _timestamp1_ and _timestamp2_ AND st_dwithin(st_setsrid(st_makepoint(_longitutde_, _latitude_), 4326)::geography, the_geom), _meters_);

请注意,这将使用几何索引,并且期望结果受到充分限制,这要归功于该索引,它接受table简单地扫描该区域内的所有记录以查找满足时间戳标准的记录.如果查询的时间戳部分在 table 上具有高度选择性,则可以使用以下方法在时间戳列上创建标准 btree 索引:

CREATE INDEX userDataBase1_ts_idx ON userDataBase1 (ts);

PostgreSQL 然后会根据 table 的统计分析和传递到查询中的特定值来选择要使用的索引(或者可能同时使用位图索引扫描)。

如果您有数十亿行,请使用 table inheritance 来加快查询性能和数据加载过程。

如评论中所述,首先将输入数据拆分为较小的数据集。 您首先创建一个 parent table 然后再创建那么多 child table 文件是如何输入的。在示例中,我使用 landmarks_child_1 作为 table 名称。其他 table 可以命名为 landmarks_child_2landmarks_child_3 等。

-- Create a parent table landmarks
CREATE TABLE landmarks (
  id serial primary key,
  name text,
  time_stamp timestamp,
  landmark text,
  latitude double precision,
  longitude double precision,
  geom geometry(Point, 4326)
);

现在创建并填充 child table landmarks_child_1。对所有其他 child tables.

重复此步骤
-- Create and fill the child table landmarks_child_1
CREATE TABLE landmarks_child_1 () INHERITS (landmarks);
ALTER TABLE landmarks_child_1 ADD PRIMARY KEY (id);   

-- create index for better performance.
CREATE INDEX landmarks_child_1_gist_geom ON landmarks_child_1 USING GIST (geom);        
CREATE INDEX landmarks_child_1_timestamp_index ON landmarks_child_1 ( time_stamp)


-- copy data
\copy landmarks_child_1(name,time_stamp,landmark,latitude,longitude) FROM '/local/path/to/Individual_Landmarks.csv' DELIMITERS ',' CSV HEADER;

-- create postgis geometries based on longitude and latitude
UPDATE landmarks_child_1 SET geom = St_SetSrid(ST_Point(longitude, latitude),4326);

如果您有 UTM 坐标而不是全球坐标 long/lag,只需更改 srid。 IE。在北京你会使用 srid 32650

UPDATE landmarks_child_1 SET geom = St_SetSrid(ST_Point(longitude, latitude),32650);

现在您的数据库中有数据,可以请求数据了。

示例查询

在此示例查询中,我请求了坐标 116.32015799999、40.004775000971(中国北京)周围 100 米半径内以及时间戳 2016-01-01 01:00:00 和 2016-01-01 [ 之间的所有点=58=](一小时)。

SELECT * FROM landmarks 
WHERE ST_DWithin(geom::geography, ST_Point(116.32015799999, 40.004775000971)::geography, 100)
AND time_stamp BETWEEN '2016-01-01 01:00:00'::timestamp AND '2016-01-01 02:00:00'::timestamp;

如果您有 UTM 坐标,只需使用 ST_SetSrid(),不要转换为地理坐标。

...
WHERE ST_DWithin(geom, ST_SetSrid(ST_Point(441972.694217,4428508.5117),32650), 100)
...

为什么要继承?

主要是因为性能更好。如果您有数百万行,您的查询将使用继承更快,因为您将在单个 table 中存储十亿行。 您可以查询 parent table 并将返回所有 child table 的结果(根据您的 WHERE 子句)。

您不需要知道您的数据实际位于 child table 中。 Table 继承将为您做到这一点。 (有关详细信息:请参阅 inheritance

重要 Postgis 中的坐标是 longitude/latitude,也是 x/y。在google地图和大部分地图网api中,坐标的表示顺序倒序:latitude/longitude(y/x)。使用正确的顺序!