|
| 1 | +-- This config example file is released into the Public Domain. |
| 2 | + |
| 3 | +-- This file shows how to use a locator to tag all buildings with the country |
| 4 | +-- they are in. |
| 5 | + |
| 6 | +-- Define the "countries" locator and get all country geometries from the |
| 7 | +-- database. Use the import-countries.lua file to import them first, before |
| 8 | +-- you run this. |
| 9 | +local countries = osm2pgsql.define_locator({ name = 'countries' }) |
| 10 | + |
| 11 | +-- The SELECT query must return the regions the locator will use. The first |
| 12 | +-- column must contain the name of the region, the second the geometry in |
| 13 | +-- WGS84 lon/lat coordinates. |
| 14 | +-- To improve the efficiency of the country lookup, we'll subdivide the |
| 15 | +-- country polygons into smaller pieces. (If you run this often, for instance |
| 16 | +-- when doing updates, do the subdivide first in the database and store the |
| 17 | +-- result in its own table.) |
| 18 | +countries:add_from_db('SELECT code, ST_Subdivide(geom, 200) FROM countries') |
| 19 | + |
| 20 | +-- You have to decide whether you are interested in getting all regions |
| 21 | +-- intersecting with any of the objects or only one of them. |
| 22 | +-- |
| 23 | +-- * Getting all regions makes sure that you get everything, even if regions |
| 24 | +-- overlap or objects straddle the border between regions. Use the function |
| 25 | +-- all_intersecting() for that. |
| 26 | +-- * Getting only one region is faster because osm2pgsql can stop looking |
| 27 | +-- for matches after the first one. Use first_intersecting() for that. |
| 28 | +-- This makes sense if you only have a single region anyway or if your |
| 29 | +-- regions don't overlap or you are not so concerned with what happens at |
| 30 | +-- the borders. |
| 31 | +-- |
| 32 | +-- Just for demonstration, we do both in this example, in the "country" and |
| 33 | +-- "countries" columns, respectively. |
| 34 | +local buildings = osm2pgsql.define_area_table('buildings', { |
| 35 | + |
| 36 | + -- This will contain the country code of the first matching country |
| 37 | + -- (which can be any of the countries because there is no order here). |
| 38 | + { column = 'country', type = 'text' }, |
| 39 | + |
| 40 | + -- This array will contain the country codes of all matching countries. |
| 41 | + { column = 'countries', sql_type = 'text[]' }, |
| 42 | + { column = 'tags', type = 'jsonb' }, |
| 43 | + { column = 'geom', type = 'polygon', not_null = true }, |
| 44 | +}) |
| 45 | + |
| 46 | +local function add(geom, tags) |
| 47 | + buildings:insert({ |
| 48 | + country = countries:first_intersecting(geom), -- or use geom:centroid() |
| 49 | + |
| 50 | + -- We have to create the format that PostgreSQL expects for text |
| 51 | + -- arrays. We assume that the region names do not contain any special |
| 52 | + -- characters, otherwise we would need to do some escaping here. |
| 53 | + countries = '{' .. table.concat(countries:all_intersecting(geom), ',') .. '}', |
| 54 | + |
| 55 | + tags = tags, |
| 56 | + geom = geom, |
| 57 | + }) |
| 58 | +end |
| 59 | + |
| 60 | +function osm2pgsql.process_way(object) |
| 61 | + if object.tags.building then |
| 62 | + add(object:as_polygon(), object.tags) |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +function osm2pgsql.process_relation(object) |
| 67 | + if object.tags.building then |
| 68 | + local geom = object:as_multipolygon() |
| 69 | + for p in geom:geometries() do |
| 70 | + add(p, object.tags) |
| 71 | + end |
| 72 | + end |
| 73 | +end |
| 74 | + |
0 commit comments