【发布时间】:2014-08-27 00:02:20
【问题描述】:
我使用 R 在 [0,1]x[0,1] 中创建 1'000'000 个均匀随机点的数据集,然后将其导出为 CSV,以便我可以将其加载到 Postgres/PostGIS。
R:
N <- 1000000
df <- data.frame(id=1:N, lon=runif(N), lat=runif(N))
write.table(df,"/media/Volume/temp/random_points.csv",row.names=FALSE,sep=";")
# manually remove header from CSV
head(df)
# id lat lon
#1 1 0.9094180 0.1208861
#2 2 0.8009161 0.8499304
#3 3 0.9800282 0.3757218
#4 4 0.5795991 0.4551454
#5 5 0.8988043 0.7801994
#6 6 0.9456310 0.2343178
邮政地理信息系统:
create table random_points (id serial, lat float, lon float);
ALTER TABLE random_points ADD PRIMARY KEY (id);
# now I import CSV into table
ALTER TABLE random_points ADD COLUMN geom geometry(POINT,4326);
UPDATE random_points SET geom = ST_SetSRID(ST_MakePoint(lon,lat),4326);
CREATE INDEX idx_lon_lat ON random_points USING GIST(geom);
现在我想查询位于边界框内的所有点 - 由索引提供支持:
最小纬度 = 0.342,最大纬度 = 0.352,最小经度 = 0.793,最大经度 = 0.812
SELECT *
FROM random_points
WHERE random_points.geom && ST_MakeEnvelope(0.342, 0.352, 0.793, 0.812, 4326);
但我不仅得到了大小为 207'376 的结果集,这比使用 R (177) 得到的结果集要多得多——该区域的预期值为 190。
第一条记录已经不属于预期的边界框;即使我在某处混淆了经度和纬度:
4;0.579599140677601;0.45514538907446;"...138CE23F"
9;0.618269162718207;0.392739744856954;"...9DCC8E33F"
10;0.742938967887312;0.58326911740005;"...127C6E73F"
17;0.665668761124834;0.475526283029467;"...92284DE53F"
27;0.668456399813294;0.747356393141672;"...ACFE63E53F"
知道我做错了什么吗?
我的目的是在 PostGIS 中使用边界框对查询点进行基准测试,以评估何时从 R 中使用 PostGIS 来实现该目的而不是使用数据框/表格工具。
【问题讨论】:
-
您可能对 PL/R、Postgres 中 R 中的存储过程感兴趣:joeconway.com/plr
标签: postgresql postgis