bbox.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. -- This config example file is released into the Public Domain.
  2. --
  3. -- This config shows how to use the get_bbox() function to get the bounding
  4. -- boxes of features.
  5. local tables = {}
  6. -- Määrame skeemi keskkonnamuutujast või vaikimisi 'public'
  7. local schema = os.getenv("PGSCHEMA") or "public"
  8. tables.pois = osm2pgsql.define_node_table('pois', {
  9. { column = 'tags', type = 'jsonb' },
  10. { column = 'bbox', type = 'text', sql_type = 'box2d' },
  11. { column = 'geom', type = 'point' },
  12. }, {
  13. schema = schema,
  14. proj = 3301
  15. })
  16. tables.ways = osm2pgsql.define_way_table('ways', {
  17. { column = 'tags', type = 'jsonb' },
  18. { column = 'bbox', type = 'text', sql_type = 'box2d' },
  19. { column = 'geom', type = 'linestring' },
  20. }, {
  21. schema = schema,
  22. proj = 3301
  23. })
  24. tables.polygons = osm2pgsql.define_area_table('polygons', {
  25. { column = 'tags', type = 'jsonb' },
  26. { column = 'bbox', type = 'text', sql_type = 'box2d' },
  27. { column = 'geom', type = 'geometry' }
  28. }, {
  29. schema = schema,
  30. proj = 3301
  31. })
  32. tables.boundaries = osm2pgsql.define_relation_table('boundaries', {
  33. { column = 'type', type = 'text' },
  34. { column = 'tags', type = 'jsonb' },
  35. { column = 'bbox', type = 'text', sql_type = 'box2d' },
  36. { column = 'geom', type = 'multilinestring' },
  37. }, {
  38. schema = schema,
  39. proj = 3301
  40. })
  41. -- Helper function that looks at the tags and decides if this is possibly
  42. -- an area.
  43. local function has_area_tags(tags)
  44. if tags.area == 'yes' then
  45. return true
  46. end
  47. if tags.area == 'no' then
  48. return false
  49. end
  50. return tags.aeroway
  51. or tags.amenity
  52. or tags.building
  53. or tags.harbour
  54. or tags.historic
  55. or tags.landuse
  56. or tags.leisure
  57. or tags.man_made
  58. or tags.military
  59. or tags.natural
  60. or tags.office
  61. or tags.place
  62. or tags.power
  63. or tags.public_transport
  64. or tags.shop
  65. or tags.sport
  66. or tags.tourism
  67. or tags.water
  68. or tags.waterway
  69. or tags.wetland
  70. or tags['abandoned:aeroway']
  71. or tags['abandoned:amenity']
  72. or tags['abandoned:building']
  73. or tags['abandoned:landuse']
  74. or tags['abandoned:power']
  75. or tags['area:highway']
  76. end
  77. -- Format the bounding box we get from calling get_bbox() on the parameter
  78. -- in the way needed for the PostgreSQL/PostGIS box2d type.
  79. local function format_bbox(object)
  80. local xmin, ymin, xmax, ymax = object:get_bbox()
  81. if xmin == nil then
  82. return nil
  83. end
  84. return 'BOX(' .. tostring(xmin) .. ' ' .. tostring(ymin)
  85. .. ',' .. tostring(xmax) .. ' ' .. tostring(ymax) .. ')'
  86. end
  87. function osm2pgsql.process_node(object)
  88. tables.pois:insert({
  89. tags = object.tags,
  90. bbox = format_bbox(object),
  91. geom = object:as_point()
  92. })
  93. end
  94. function osm2pgsql.process_way(object)
  95. -- A closed way that also has the right tags for an area is a polygon.
  96. if object.is_closed and has_area_tags(object.tags) then
  97. tables.polygons:insert({
  98. tags = object.tags,
  99. bbox = format_bbox(object),
  100. geom = object:as_polygon()
  101. })
  102. else
  103. tables.ways:insert({
  104. tags = object.tags,
  105. bbox = format_bbox(object),
  106. geom = object:as_linestring()
  107. })
  108. end
  109. end
  110. function osm2pgsql.process_relation(object)
  111. local relation_type = object:grab_tag('type')
  112. -- Store boundary relations as multilinestrings
  113. if relation_type == 'boundary' then
  114. tables.boundaries:insert({
  115. type = object.tags.boundary,
  116. bbox = format_bbox(object),
  117. tags = object.tags,
  118. geom = object:as_multilinestring()
  119. })
  120. return
  121. end
  122. -- Store multipolygon relations as polygons
  123. if relation_type == 'multipolygon' then
  124. tables.polygons:insert({
  125. bbox = format_bbox(object),
  126. tags = object.tags,
  127. geom = object:as_multipolygon()
  128. })
  129. end
  130. end