import-countries.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 file is part of the examples for using the locator function of
  5. -- osm2pgsql. It is used to import all country boundaries into a database.
  6. local countries = osm2pgsql.define_relation_table('countries', {
  7. -- For the ISO3166-1 Alpha-2 country code
  8. -- https://en.wikipedia.org/wiki/ISO_3166-1
  9. { column = 'code', type = 'text', not_null = true },
  10. -- Because we want to use the geometries for the locator feature they
  11. -- must be in 4326! We use a polygon type here and will later split
  12. -- multipolygons into their parts.
  13. { column = 'geom', type = 'polygon', not_null = true, projection = 4326 },
  14. }, {
  15. schema = schema,
  16. proj = 3301
  17. })
  18. function osm2pgsql.process_relation(object)
  19. local t = object.tags
  20. if t.boundary == 'administrative' and t.admin_level == '2' then
  21. local code = t['ISO3166-1']
  22. -- Ignore entries with syntactically invalid ISO code
  23. if not code or not string.match(code, '^%u%u$') then
  24. return
  25. end
  26. for geom in object:as_multipolygon():geometries() do
  27. countries:insert({
  28. code = code,
  29. geom = geom,
  30. })
  31. end
  32. end
  33. end