administrative.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -- Määrame skeemi keskkonnamuutujast või vaikimisi 'public'
  2. local schema = os.getenv("PGSCHEMA") or "public"
  3. local tables = {}
  4. -- Define the "countries" locator and get all country geometries from the
  5. -- database. Use the import-countries.lua file to import them first, before
  6. -- you run this.
  7. local countries = osm2pgsql.define_locator({name = "countries"})
  8. countries:add_from_db("SELECT code, ST_Subdivide(geom, 200) FROM " .. schema .. ".countries")
  9. -- Funktsioon, mis eemaldab soovimatud võtmed tagidest
  10. local function clean_tags(tags)
  11. local keys_to_remove = {
  12. "border_zone"
  13. }
  14. for _, key in ipairs(keys_to_remove) do
  15. tags[key] = nil
  16. end
  17. return next(tags) == nil
  18. end
  19. -- Defineerime relatsioonide tabeli, kuhu salvestame halduspiirid
  20. tables.boundaries =
  21. osm2pgsql.define_relation_table(
  22. "administrative",
  23. {
  24. {column = "country", type = "text"},
  25. {column = "type", type = "text"},
  26. {column = "admin_level", type = "int"},
  27. {column = "name", type = "text"},
  28. {column = "code", type = "text"},
  29. {column = "countycode", type = "text"},
  30. {column = "parishcode", type = "text"},
  31. {column = "tags", type = "jsonb"},
  32. {column = "geom", type = "geometry", not_null = true} -- lubame erinevad geomeetriatüübid
  33. },
  34. {
  35. schema = schema,
  36. proj = 3301
  37. }
  38. )
  39. -- Peamine funktsioon, mis töötleb OSM relatsioone
  40. function osm2pgsql.process_relation(object)
  41. local relation_type = object.tags["type"]
  42. local geom = object:as_multipolygon()
  43. if not geom then
  44. return
  45. end
  46. -- Kontrollime, kas geomeetria asub Eestis
  47. local cc = countries:first_intersecting(geom)
  48. if cc ~= "EE" then
  49. return
  50. end
  51. -- Töötleme ainult boundary-relatsioone
  52. if relation_type == "boundary" then
  53. local name = object.tags["name"]
  54. local type = object.tags["boundary"]
  55. local admin_level = object.tags.admin_level
  56. local code = object.tags["EHAK:code"]
  57. local countycode = object.tags["EHAK:countycode"]
  58. local parishcode = object.tags["EHAK:parishcode"]
  59. -- Eemaldame soovimatud tagid
  60. if clean_tags(object.tags) then
  61. return
  62. end
  63. -- Koostame rea andmebaasi jaoks
  64. local row = {
  65. country = cc,
  66. type = type,
  67. admin_level = admin_level,
  68. name = name,
  69. code = code,
  70. countycode = countycode,
  71. parishcode = parishcode,
  72. tags = object.tags,
  73. geom = geom
  74. }
  75. tables.boundaries:insert(row)
  76. end
  77. end