Skip to content

Commit 93c4450

Browse files
authored
Merge pull request #1233 from joto/doc-create-only-column-for-postprocessing
Add docs about using `create_only` columns for postprocessed data
2 parents 725cd56 + 0c96f69 commit 93c4450

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/flex-advanced-topics.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,34 @@ data using osm2pgsql, use `CREATE UNIQUE INDEX` to create one. You can also use
6464
`ALTER TABLE` to make the column an "official" primary key column. See the
6565
PostgreSQL docs for details.
6666

67+
## Using `create_only` columns for postprocessed data
68+
69+
Sometimes it is useful to have data in table rows that osm2pgsql can't create.
70+
For instance you might want to store the center of polygons for faster
71+
rendering of a label.
72+
73+
To do this define your table as usual and add an additional column, marking
74+
it `create_only`. In our example case the type of the column should be the
75+
PostgreSQL type `GEOMETRY(Point, 3857)`, because we don't want osm2pgsql to
76+
do anything special here, just create the column with this type as is.
77+
78+
```
79+
polygons_table = osm2pgsql.define_area_table('polygons', {
80+
{ column = 'tags', type = 'hstore' },
81+
{ column = 'geom', type = 'geometry' },
82+
{ column = 'center', type = 'GEOMETRY(Point, 3857)', create_only = true },
83+
{ column = 'area', type = 'area' },
84+
})
85+
```
86+
87+
After running osm2pgsql as usual, run the following SQL command:
88+
89+
```
90+
UPDATE polygons SET center = ST_Centroid(geom) WHERE center IS NULL;
91+
```
92+
93+
If you are updating the data using `osm2pgsql --append`, you have to do this
94+
after each update. When osm2pgsql inserts new rows they will always have a
95+
`NULL` value in `center`, the `WHERE` condition makes sure that we only do
96+
this (possibly expensive) calculation once.
97+

0 commit comments

Comments
 (0)