1

我有一个由伦敦的 OSM 地图数据组成的 PostgreSQL 数据库。我使用 osm2psql 导入了这些数据。我想:

  • 遍历planet_osm_line 表中的每一行
  • 将线条分成单独的线段
  • 计算每个线段的值(在本例中将值设置为 0.5)
  • 将线段作为新条目写入新表。

下面的 python 代码似乎实现了这一点,但有一个问题。它似乎只是访问整个数据库的一小部分。

import psycopg2

conn = psycopg2.connect("dbname=db user=username")
maincur = conn.cursor()
readcur = conn.cursor()
writecur = conn.cursor()

maincur.execute("DROP TABLE lines_red")
maincur.execute("CREATE TABLE lines_red (osm_id bigint, name text, way geometry, value float);")

maincur.execute("SELECT osm_id, ST_NPOINTS(way) FROM planet_osm_line")
    for record in maincur:
    pointlist = []
    for i in range(0,record[1]):
        readcur.execute("SELECT ST_ASTEXT(ST_POINTN(way, %s+1)) FROM planet_osm_line WHERE osm_id=%s;",(i,record[0]))
        output = readcur.fetchone()
        pointlist.append(output[0])
    for i in range(0,record[1]-1):  
        if pointlist[i+1] != None:
            value = 0.5
            writecur.execute("INSERT INTO lines_red (name, way, value) VALUES ('testname', ST_Makeline(%s, %s), %s);", (pointlist[i],pointlist[i+1],value))

conn.commit()
maincur.close()
readcur.close()
writecur.close()
conn.close()

为了说明下图显示了以灰色显示的完整planet_osm_line 表和以红色显示的查询结果。红线应该覆盖整个地图,因为代码应该遍历整个planet_osm_line 表。我正在使用 tilemill 来显示结果。

Result of trying to iterate over the entire planet_osm_line table

4

1 回答 1

0

The problem in this case was with my use of TileMill.

When a new layer is created the default setting for its extent is to pre-calculate based on the database table used as input. This means that the layer will only ever be the size of the dataset with which it was initially created.

In this case I had iterated over a subset of my data as a test of my code and the extents were calculated based on the results. When I iterated over the whole data set the code iterated over the entire database but only displayed the results within the previously calculated constraints.

The solution is to create a new layer or set the extents setting in the layer to 'dynamic' rather than 'pre-calculate'

于 2013-02-11T10:43:29.523 回答