buildings.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. -- NOTE: Beautification tool not available. Original content preserved.
  2. -- Määrame skeemi keskkonnamuutujast või vaikimisi 'public'
  3. local schema = os.getenv("PGSCHEMA") or "public"
  4. local tables = {}
  5. -- Define the "countries" locator and get all country geometries from the
  6. -- database. Use the import-countries.lua file to import them first, before
  7. -- you run this.
  8. local countries = osm2pgsql.define_locator({name = "countries"})
  9. countries:add_from_db("SELECT code, ST_Subdivide(geom, 200) FROM " .. schema .. ".countries")
  10. tables.buildings =
  11. osm2pgsql.define_way_table(
  12. "buildings",
  13. {
  14. -- Add a SERIAL column and tell osm2pgsql not to fill it (PostgreSQL will
  15. -- do that for us)
  16. {column = "id", sql_type = "serial", create_only = true},
  17. -- We always need a highway type, so we can declare the column as NOT NULL
  18. -- This will contain the country code of the first matching country
  19. -- (which can be any of the countries because there is no order here).
  20. {column = "country", type = "text"},
  21. {column = "city", type = "text"},
  22. {column = "street", type = "text"},
  23. {column = "housenumber", type = "text"},
  24. {column = "housename", type = "text"},
  25. {column = "postcode", type = "text"},
  26. {column = "name", type = "text"},
  27. {column = "place", type = "text"},
  28. {column = "amenity", type = "text"},
  29. {column = "height", type = "int"},
  30. {column = "use", type = "text"},
  31. {column = "levels", type = "int"},
  32. {column = "tags", type = "jsonb"},
  33. {column = "geom", type = "polygon", not_null = true}
  34. },
  35. {
  36. schema = schema,
  37. proj = 3301
  38. }
  39. )
  40. -- Helper function to remove some of the tags we usually are not interested in.
  41. -- Something like this can be useful if you are writing all tags to the
  42. -- database in a JSON(B) column and don't want that cluttered with lots of tags
  43. -- nobody cares about. Returns true if there are no tags left.
  44. local function clean_tags(tags)
  45. local keys_to_remove = {
  46. "shelter"
  47. }
  48. for _, key in ipairs(keys_to_remove) do
  49. tags[key] = nil
  50. end
  51. return next(tags) == nil
  52. end
  53. -- Mittevajalike tag'ide eemaldamine
  54. local function hide_tags(tags)
  55. local keys_to_hide = {
  56. "odbl", "created_by", "source", "source:ref", "source:addr",
  57. "addr:country", "addr:city", "addr:street", "addr:housenumber", "addr:housename",
  58. "addr:postcode", "height", "building", "maaamet:ETAK", "building:use",
  59. "building:levels", "old_addr:street", "old_addr:housenumber", "source:addr:2023",
  60. "maaamet:orig_tunnus", "wikidata", "old_addr:housename", "old_addr:place",
  61. "name", "phone", "internet_access", "wheelchair", "roof:shape",
  62. "name:en", "name:ru", "brand", "fuel", "website", "alt_name",
  63. "capacity", "operator", "fuel:diesel", "self_service", "opening_hours",
  64. "brand:wikidata", "fuel:octane_95", "fuel:octane_98", "brand:wikipedia",
  65. "roof:material", "email", "name:et", "roof", "stars", "fee",
  66. "old_name", "name:fi", "description:ru", "url", "name:lt",
  67. "addr:place", "roof:levels", "addr:name", "name:pl", "name:lv",
  68. "payment:credit_cards", "payment:debit_cards", "amenity", "tourism",
  69. "shop", "man_made", "addr:old_street", "operator:wikidata",
  70. "fuel:adblue", "contact:email", "leisure", "contact:phone",
  71. "contact:website", "healthcare", "maaamet:ADS", "name:vro",
  72. "check_date", "building:material", "owner:ru", "int_name", "wikipedia", "power",
  73. "fuel:HGV_diesel", "branch", "EE:EHIS:id", "operator:type", "operator:type",
  74. "layer"
  75. }
  76. for _, key in ipairs(keys_to_hide) do
  77. tags[key] = nil
  78. end
  79. return next(tags) == nil
  80. end
  81. function osm2pgsql.process_way(object)
  82. -- Get the type of "building" and remove it from the tags
  83. local building = object.tags.building
  84. -- Only with geometry
  85. local geom = object:as_polygon()
  86. if not geom then
  87. return
  88. end
  89. -- Only Estonia
  90. local cc = countries:first_intersecting(geom)
  91. if cc ~= "EE" then
  92. return
  93. end
  94. if building then
  95. local city = object.tags["addr:city"]
  96. local street = object.tags["addr:street"]
  97. local housenumber = object.tags["addr:housenumber"]
  98. local housename = object.tags["addr:housename"]
  99. local postcode = object.tags["addr:postcode"]
  100. local name = object.tags["name"] or object.tags["addr:name"]
  101. local place = object.tags["addr:place"]
  102. local amenity = object.tags["amenity"] or object.tags["tourism"] or object.tags["shop"]
  103. or object.tags["man_made"] or object.tags["leisure"] or object.tags["healthcare"]
  104. or object.tags["power"]
  105. local use = object.tags["building:use"]
  106. local levels = object.tags["building:levels"]
  107. local tags = object.tags
  108. local height = object.tags.height
  109. -- the way
  110. local row = {
  111. country = cc,
  112. city = city,
  113. street = street,
  114. housenumber = housenumber,
  115. housename = housename,
  116. postcode = postcode,
  117. name = name,
  118. place = place,
  119. amenity = amenity,
  120. height = height,
  121. use = use,
  122. levels = levels,
  123. tags = tags,
  124. geom = geom
  125. }
  126. if clean_tags(object.tags) then
  127. return
  128. end
  129. -- hide_tags(object.tags)
  130. tables.buildings:insert(row)
  131. end
  132. end