create_osm_tables.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env bash
  2. # Lae keskkonnamuutujad
  3. source env.sh
  4. # Liigume ajutisse kausta
  5. #cd /tmp
  6. # Laadime alla Eesti OSM andmed ajutisse kausta
  7. #wget https://download.geofabrik.de/europe/estonia-latest.osm.pbf -P /tmp
  8. #wget https://raw.githubusercontent.com/openstreetmap/osm2pgsql/master/default.style -O default.style
  9. # Impordime andmed PostGIS andmebaasi
  10. # pbf failist luuakse lähtetabelid 'osm_point', 'osm_line', 'osm_polygon', 'osm_roads'
  11. # Ülejäänud tabelid luuakse lua failide abil.
  12. # https://github.com/osm2pgsql-dev/osm2pgsql/blob/master/flex-config/bbox.lua
  13. # First use the import-countries.lua to import country boundaries,
  14. # then use these with the other config files.
  15. PGSCHEMA=${destination_schema} osm2pgsql \
  16. --create \
  17. --slim \
  18. --cache 2000 \
  19. --number-processes 4 \
  20. --database "${destination_dbname}" \
  21. --username "${destination_user}" \
  22. --host "${destination_host}" \
  23. --port "${destination_port}" \
  24. --middle-schema="${destination_schema}" \
  25. --output=flex \
  26. --style=lua/import-countries.lua \
  27. --prefix=osm \
  28. /tmp/estonia-latest.osm.pbf
  29. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.osm_pois;" && \
  30. PGSCHEMA=${destination_schema} osm2pgsql \
  31. --create \
  32. --slim \
  33. --cache 2000 \
  34. --number-processes 4 \
  35. --database "${destination_dbname}" \
  36. --username "${destination_user}" \
  37. --host "${destination_host}" \
  38. --port "${destination_port}" \
  39. --middle-schema="${destination_schema}" \
  40. --output=flex \
  41. --style=lua/ardok_osm_pois.lua \
  42. --prefix=osm \
  43. /tmp/estonia-latest.osm.pbf
  44. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.ardok_highways;
  45. DROP TABLE IF EXISTS ${destination_schema}.ardok_routes;" && \
  46. PGSCHEMA=${destination_schema} osm2pgsql \
  47. --create \
  48. --slim \
  49. --cache 2000 \
  50. --number-processes 4 \
  51. --database "${destination_dbname}" \
  52. --username "${destination_user}" \
  53. --host "${destination_host}" \
  54. --port "${destination_port}" \
  55. --middle-schema="${destination_schema}" \
  56. --output=flex \
  57. --style=lua/ardok_highways.lua \
  58. --prefix=osm \
  59. /tmp/estonia-latest.osm.pbf
  60. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.ardok_buildings;" && \
  61. PGSCHEMA=${destination_schema} osm2pgsql \
  62. --create \
  63. --slim \
  64. --cache 2000 \
  65. --number-processes 4 \
  66. --database "${destination_dbname}" \
  67. --username "${destination_user}" \
  68. --host "${destination_host}" \
  69. --port "${destination_port}" \
  70. --middle-schema="${destination_schema}" \
  71. --output=flex \
  72. --style=lua/ardok_buildings.lua \
  73. --prefix=osm \
  74. /tmp/estonia-latest.osm.pbf
  75. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.ardok_administrative;" && \
  76. PGSCHEMA=${destination_schema} osm2pgsql \
  77. --create \
  78. --slim \
  79. --cache 2000 \
  80. --number-processes 4 \
  81. --database "${destination_dbname}" \
  82. --username "${destination_user}" \
  83. --host "${destination_host}" \
  84. --port "${destination_port}" \
  85. --middle-schema="${destination_schema}" \
  86. --output=flex \
  87. --style=lua/ardok_administrative.lua \
  88. --prefix=osm \
  89. /tmp/estonia-latest.osm.pbf
  90. # Kaalude lisamine administratiiv polügoonidele
  91. psql ${CONNECTION_NAME} -c "
  92. ALTER TABLE ${destination_schema}.ardok_administrative ADD COLUMN weight_poi integer;"
  93. psql ${CONNECTION_NAME} -c "
  94. UPDATE ${destination_schema}.ardok_administrative p
  95. SET weight_poi = sub.poi_count
  96. FROM (
  97. SELECT
  98. a.relation_id,
  99. COUNT(poi.*) AS poi_count
  100. FROM ${destination_schema}.ardok_administrative a
  101. JOIN ${destination_schema}.osm_pois poi
  102. ON ST_Intersects(a.geom, poi.geom)
  103. GROUP BY a.relation_id
  104. ) AS sub
  105. WHERE p.relation_id = sub.relation_id;"
  106. ## ---------------------- GRAAFI LOOMINE ---------------------------
  107. # a) links loomine
  108. echo "$COUNTIES" | parallel -j4 "county={}; bash batch_create_links.sh"
  109. bash merge_links.sh
  110. #psql ${CONNECTION_NAME} -v schema=${destination_schema} -d data -f sql/create_links.sql
  111. # Kontrollime ridade arvusid tabelites link_*
  112. bash generate_links_table_stats.sh
  113. # b) nodes loomine
  114. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d data -f sql/create_nodes.sql
  115. # c) links: source, target, cost
  116. ./update_links_sources_targets_costs.sh
  117. # Täiendav isoleeritud lõikude eemaldamine + reverse_cost lisamine
  118. ./update_links_reverse_cost.sh
  119. # d) PGROUTING ettevalmistus
  120. psql ${CONNECTION_NAME} -c "
  121. SELECT pgr_createVerticesTable('${destination_schema}.links', 'geom', 'source', 'target');
  122. SELECT pgr_analyzeGraph('${destination_schema}.links', 0.001, the_geom := 'geom', id := 'id', source := 'source', target := 'target');
  123. "
  124. # Puhaste isoleeritud lõigud
  125. ./pgr_prepare_batchwise.sh
  126. ./detect_and_split_intersections_batchwise_fixed.sh
  127. ./convert_links_split_to_graph.sh
  128. ./finalize_road_graph.sh
  129. # Tag'ide lisamine: isoleeritud ja lõikuvad
  130. #./tag_links_batchwise.sh
  131. # e) paranda graafi linke
  132. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d data -f sql/fix_gaps_and_isolated.sql
  133. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d data -f sql/repair_graph_components.sql
  134. ## -----------------------------------------------------------------------------------