forests.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Lua config demonstrates how to use (some of) the generalization
  5. -- functionality. In this case we are generating a forest layer and create
  6. -- several levels of generalized data from it.
  7. --
  8. -- NOTE THAT THE GENERALIZATION SUPPORT IS EXPERIMENTAL AND MIGHT CHANGE
  9. -- WITHOUT NOTICE!
  10. -- We want to do three levels of generalized data for different zoom levels
  11. -- or scales: small, medium, and large.
  12. local gen_levels = { 's', 'm', 'l' }
  13. -- And these are the zoom levels we do the generalization in. This doesn't
  14. -- mean that these are the zoom levels for which the generalization is shown!
  15. local zoom_levels = { s = 7, m = 8, l = 10 }
  16. local expire_outputs = {}
  17. -- These defines the expire outputs for three levels of generalized data.
  18. -- See flex-config/expire.lua for details on how this works.
  19. for _, level in ipairs(gen_levels) do
  20. expire_outputs['exp_' .. level] = osm2pgsql.define_expire_output({
  21. maxzoom = zoom_levels[level],
  22. table = 'exp_' .. level,
  23. schema = schema
  24. })
  25. end
  26. local tables = {}
  27. -- This is the table for the original data.
  28. tables.forests = osm2pgsql.define_area_table('forests', {
  29. -- We'll keep the name tag
  30. { column = 'name', type = 'text' },
  31. -- We'll also keep all other tags, just because we can
  32. { column = 'tags', type = 'jsonb' },
  33. -- Geometries can be polygons or multipolygons
  34. { column = 'geom', type = 'geometry', not_null = true,
  35. expire = {
  36. { output = expire_outputs.exp_s },
  37. { output = expire_outputs.exp_m },
  38. { output = expire_outputs.exp_l },
  39. }
  40. },
  41. -- If the forest has a name and isn't too small, this will be a point
  42. -- where we can put a label.
  43. { column = 'labelpoint', type = 'point' },
  44. -- The area (in Web Mercator units) for lager forests, otherwise NULL.
  45. { column = 'area', type = 'real' },
  46. }, {
  47. schema = schema,
  48. proj = 3301
  49. })
  50. -- These defines the tables for three levels of generalized data. In this
  51. -- case, the geometries generated by the generalization are always polygons.
  52. for _, level in ipairs(gen_levels) do
  53. tables[schema..'.forests_' .. level] = osm2pgsql.define_table({
  54. name = 'forests_' .. level,
  55. schema = schema,
  56. -- Define the x and y integer columns and add an index for them.
  57. ids = { type = 'tile' },
  58. columns = {
  59. { column = 'geom', type = 'polygon', not_null = true }
  60. }
  61. })
  62. end
  63. -- This is where the data is actually inserted into the forests table. If
  64. -- the forest has a name tag and the area is larger than a defined minimum
  65. -- we'll also add the name and a point for the label.
  66. local minimum_area_for_label = 0.001
  67. local function insert_forest(tags, geom)
  68. local attrs = {
  69. tags = tags,
  70. geom = geom,
  71. }
  72. if tags.name then
  73. local area = geom:area()
  74. if area >= minimum_area_for_label then
  75. attrs.name = tags.name
  76. attrs.area = area
  77. attrs.labelpoint = geom:pole_of_inaccessibility()
  78. end
  79. end
  80. tables.forests:insert(attrs)
  81. end
  82. function osm2pgsql.process_way(object)
  83. if not object.is_closed then
  84. return
  85. end
  86. local tags = object.tags
  87. if tags.natural == 'wood' or tags.landuse == 'forest' then
  88. insert_forest(tags, object:as_polygon())
  89. end
  90. end
  91. function osm2pgsql.process_relation(object)
  92. if object.tags.type ~= 'multipolygon' then
  93. return
  94. end
  95. local tags = object.tags
  96. if tags.natural == 'wood' or tags.landuse == 'forest' then
  97. insert_forest(tags, object:as_multipolygon())
  98. end
  99. end
  100. -- This function is called in the generalization step. We define three levels
  101. -- of generalization that should go into the already created tables.
  102. function osm2pgsql.process_gen()
  103. for _, level in ipairs(gen_levels) do
  104. osm2pgsql.run_gen('raster-union', {
  105. name = 'forests_' .. level, -- name (for logging)
  106. debug = false, -- set to true to get more details debug output
  107. src_table = schema..'.forests',
  108. dest_table = schema..'.forests_' .. level,
  109. zoom = zoom_levels[level],
  110. geom_column = 'geom',
  111. margin = 0.1, -- 10% overlap at tile boundaries
  112. expire_list = schema..'.exp_' .. level, -- where to get expired tiles in append mode
  113. make_valid = true -- make sure geometries are valid
  114. })
  115. end
  116. end