addresses.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. -- Get a location for all objects with addresses. If mapped as a polygon or
  5. -- multipolygon, the centroid will be used.
  6. local addrs = osm2pgsql.define_table({
  7. name = 'addrs',
  8. schema = schema,
  9. proj = 3301,
  10. ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
  11. columns = {
  12. { column = 'name', type = 'text' },
  13. { column = 'country', type = 'text' },
  14. { column = 'state', type = 'text' },
  15. { column = 'postcode', type = 'text' },
  16. { column = 'city', type = 'text' },
  17. { column = 'place', type = 'text' },
  18. { column = 'street', type = 'text' },
  19. { column = 'housenumber', type = 'text' },
  20. { column = 'geom', type = 'point', projection = 3301, not_null = true }
  21. }
  22. })
  23. local function get_address(tags)
  24. local addr = {}
  25. local count = 0
  26. for _, key in ipairs({'housenumber', 'street', 'city', 'postcode', 'country', 'state', 'place'}) do
  27. local a = tags['addr:' .. key]
  28. if a then
  29. addr[key] = a
  30. count = count + 1
  31. end
  32. end
  33. return count > 1, addr
  34. end
  35. function osm2pgsql.process_node(object)
  36. local any, addr = get_address(object.tags)
  37. if any then
  38. addr.name = object.tags.name
  39. addr.geom = object:as_point()
  40. addrs:insert(addr)
  41. end
  42. end
  43. function osm2pgsql.process_way(object)
  44. if object.is_closed then
  45. local any, addr = get_address(object.tags)
  46. if any then
  47. addr.name = object.tags.name
  48. addr.geom = object:as_polygon():centroid()
  49. addrs:insert(addr)
  50. end
  51. end
  52. end
  53. function osm2pgsql.process_relation(object)
  54. if object.tags.type == 'multipolygon' then
  55. local any, addr = get_address(object.tags)
  56. if any then
  57. addr.name = object.tags.name
  58. addr.geom = object:as_multipolygon():centroid()
  59. addrs:insert(addr)
  60. end
  61. end
  62. end