batch_create_links.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash
  2. set -e
  3. source env.sh
  4. # Kontrolli ühenduse muutujat
  5. echo "🔗 Kasutan CONNECTION_NAME=${CONNECTION_NAME}"
  6. # Võta kõik maakonnad admin_taseme 6 järgi
  7. COUNTIES=$(psql ${CONNECTION_NAME} -At -d "$destination_dbname" -c "SELECT relation_id FROM ${destination_schema}.administrative WHERE admin_level = '6';")
  8. #IFS=$'\n'
  9. for county in $COUNTIES; do
  10. safe_name=$(echo "$county" | tr ' ' '_' )
  11. echo "📦 Töötlen $county → links_$safe_name"
  12. psql ${CONNECTION_NAME} -v schema=${destination_schema} -v county="'$county'" -v links_table="links_$safe_name" -d ${destination_dbname} <<'EOF'
  13. \set ON_ERROR_STOP on
  14. DROP TABLE IF EXISTS test_highways;
  15. CREATE TEMP TABLE test_highways AS
  16. SELECT h.*
  17. FROM :schema.highways h
  18. JOIN :schema.administrative a ON ST_Intersects(h.geom, a.geom)
  19. WHERE a.relation_id = :county AND a.admin_level = '6';
  20. DROP TABLE IF EXISTS :schema._noded_geom;
  21. CREATE UNLOGGED TABLE :schema._noded_geom AS
  22. SELECT ST_Node(ST_Collect(geom)) AS geom FROM test_highways;
  23. DROP TABLE IF EXISTS :schema._raw_segments;
  24. CREATE UNLOGGED TABLE :schema._raw_segments AS
  25. SELECT (ST_Dump(geom)).geom AS geom
  26. FROM :schema._noded_geom;
  27. DROP TABLE IF EXISTS :schema.:links_table;
  28. CREATE UNLOGGED TABLE :schema.:links_table AS
  29. SELECT
  30. h.id AS eid,
  31. h.hwtype AS highway,
  32. h.surface,
  33. h.oneway,
  34. h.maxspeed,
  35. COALESCE(NULLIF(h.name, ''), NULLIF(h.hwtype, ''), 'Unnamed road') AS name,
  36. s.geom
  37. FROM :schema._raw_segments s
  38. JOIN LATERAL (
  39. SELECT *
  40. FROM test_highways h
  41. WHERE h.geom && s.geom AND ST_Intersects(h.geom, s.geom)
  42. ORDER BY ST_Length(ST_Intersection(h.geom, s.geom)) DESC
  43. LIMIT 1
  44. ) h ON TRUE;
  45. EOF
  46. echo -e "\n✅ Töödeldud links_$safe_name"
  47. done