route-relations.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 multi-stage processing to bring tags from
  5. -- relations into ways.
  6. -- This will only import ways tagged as highway. The 'rel_refs' text column
  7. -- will contain a comma-separated list of all ref tags found in parent
  8. -- relations with type=route and route=road. The 'rel_ids' column will be
  9. -- an integer array containing the relation ids. These could be used, for
  10. -- instance, to look up other relation tags from the 'routes' table.
  11. local tables = {}
  12. tables.highways = osm2pgsql.define_way_table('highways_relations', {
  13. { column = 'tags', type = 'jsonb' },
  14. { column = 'rel_refs', type = 'text' }, -- for the refs from the relations
  15. { column = 'rel_ids', sql_type = 'int8[]' }, -- array with integers (for relation IDs)
  16. { column = 'geom', type = 'linestring', not_null = true },
  17. }, {
  18. schema = schema,
  19. proj = 3301
  20. })
  21. -- Tables don't have to have a geometry column
  22. tables.routes = osm2pgsql.define_relation_table('routes_relations', {
  23. { column = 'tags', type = 'jsonb' },
  24. }, {
  25. schema = schema,
  26. proj = 3301
  27. })
  28. -- This will be used to store information about relations queryable by member
  29. -- way id. It is a table of tables. The outer table is indexed by the way id,
  30. -- the inner table indexed by the relation id. This way even if the information
  31. -- about a relation is added twice, it will be in there only once. It is
  32. -- always good to write your osm2pgsql Lua code in an idempotent way, i.e.
  33. -- it can be called any number of times and will lead to the same result.
  34. local w2r = {}
  35. function osm2pgsql.process_way(object)
  36. -- We are only interested in highways
  37. if not object.tags.highway then
  38. return
  39. end
  40. -- Data we will store in the "highways" table always has the tags from
  41. -- the way
  42. local row = {
  43. tags = object.tags,
  44. geom = object:as_linestring()
  45. }
  46. -- If there is any data from parent relations, add it in
  47. local d = w2r[object.id]
  48. if d then
  49. local refs = {}
  50. local ids = {}
  51. for rel_id, rel_ref in pairs(d) do
  52. refs[#refs + 1] = rel_ref
  53. ids[#ids + 1] = rel_id
  54. end
  55. table.sort(refs)
  56. table.sort(ids)
  57. row.rel_refs = table.concat(refs, ',')
  58. row.rel_ids = '{' .. table.concat(ids, ',') .. '}'
  59. end
  60. tables.highways:insert(row)
  61. end
  62. -- This function is called for every added, modified, or deleted relation.
  63. -- Its only job is to return the ids of all member ways of the specified
  64. -- relation we want to see in stage 2 again. It MUST NOT store any information
  65. -- about the relation!
  66. function osm2pgsql.select_relation_members(relation)
  67. -- Only interested in relations with type=route, route=road and a ref
  68. if relation.tags.type == 'route' and relation.tags.route == 'road' and relation.tags.ref then
  69. return { ways = osm2pgsql.way_member_ids(relation) }
  70. end
  71. end
  72. -- The process_relation() function should store all information about way
  73. -- members that might be needed in stage 2.
  74. function osm2pgsql.process_relation(object)
  75. if object.tags.type == 'route' and object.tags.route == 'road' and object.tags.ref then
  76. tables.routes:insert({
  77. tags = object.tags
  78. })
  79. -- Go through all the members and store relation ids and refs so they
  80. -- can be found by the way id.
  81. for _, member in ipairs(object.members) do
  82. if member.type == 'w' then
  83. if not w2r[member.ref] then
  84. w2r[member.ref] = {}
  85. end
  86. w2r[member.ref][object.id] = object.tags.ref
  87. end
  88. end
  89. end
  90. end