buildings.lua 3.1 KB

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