create_osm_tables.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/osm_pois.lua \
  42. --prefix=osm \
  43. /tmp/estonia-latest.osm.pbf
  44. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.highways;
  45. DROP TABLE IF EXISTS ${destination_schema}.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/highways.lua \
  58. --prefix=osm \
  59. /tmp/estonia-latest.osm.pbf
  60. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.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/buildings.lua \
  73. --prefix=osm \
  74. /tmp/estonia-latest.osm.pbf
  75. psql ${CONNECTION_NAME} -c "DROP TABLE IF EXISTS ${destination_schema}.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/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}.administrative ADD COLUMN weight_poi integer;"
  93. psql ${CONNECTION_NAME} -c "
  94. UPDATE ${destination_schema}.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}.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. #./batch_create_links.sh
  110. # maakonna links tabelite ühendamine
  111. ./merge_links.sh
  112. # testimiseks Valga valla links
  113. #./test_links.sh
  114. # Kontrollime ridade arvusid tabelites link_*
  115. ./generate_links_table_stats.sh
  116. # b) nodes loomine
  117. #psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/create_nodes.sql
  118. # c) links: source, target, cost
  119. ./update_links_sources_targets_costs.sh
  120. # Täiendav isoleeritud lõikude eemaldamine + reverse_cost lisamine
  121. # d) PGROUTING ettevalmistus
  122. psql ${CONNECTION_NAME} -c "
  123. SELECT pgr_createVerticesTable('${destination_schema}.links', 'geom', 'source', 'target');
  124. SELECT pgr_analyzeGraph('${destination_schema}.links', 0.001, the_geom := 'geom', id := 'id', source := 'source', target := 'target');
  125. "
  126. ./update_links_reverse_cost.sh
  127. # Kustuta isoleeritud lõigud
  128. ./pgr_prepare_batchwise.sh
  129. # Puhasta "intersection detected" lõigud
  130. ./detect_and_split_intersections_batchwise_fixed.sh
  131. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/delete_links_covered_by_split.sql
  132. psql ${CONNECTION_NAME} -c "
  133. SELECT pgr_createVerticesTable('${destination_schema}.links', 'geom', 'source', 'target');
  134. SELECT pgr_analyzeGraph('${destination_schema}.links', 0.001, the_geom := 'geom', id := 'id', source := 'source', target := 'target');
  135. "
  136. # -------------- Siiani oleks nagu h½sti ---------------
  137. # NOTICE: Isolated segments: 0
  138. # NOTICE: Dead ends: 111179
  139. # NOTICE: Potential gaps found near dead ends: 0
  140. # NOTICE: Intersections detected: 6
  141. # NOTICE: Ring geometries: 0
  142. # Teistkordne probleemsete kohtade leidmine läbi tippude
  143. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/detect_unconnected_intersections_batchwise_fixed.sql
  144. # ---------------- Siit on pooleli --------------
  145. # Teistkordne puhastamine
  146. # Järgmine päring andis tühja tulemuse. Uuri. Struktuuri oleks hea kasutada.
  147. #psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/detect_unconnected_intersections_batchwise.sql
  148. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/delete_links_from_intersections_unconnected.sql
  149. # Loome tabeli tippudest kus on "intersection detected" (0 rida)
  150. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/export_remaining_intersections_batchwise.sql
  151. # Skripti, mis logib kõik lõigud, mis lõikuvad geomeetriliselt, kuid pole ühendatud — sõltumata geomeetria tüübist.
  152. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/log_all_unconnected_intersections.sql
  153. # Kustutamine
  154. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/delete_links_by_intersections_all_geom.sql
  155. # Peale eelmist käsku tekkis 2 tk "Potential gaps found near dead ends"
  156. psql ${CONNECTION_NAME} -c "
  157. SELECT pgr_createVerticesTable('${destination_schema}.links', 'geom', 'source', 'target');
  158. SELECT pgr_analyzeGraph('${destination_schema}.links', 0.001, the_geom := 'geom', id := 'id', source := 'source', target := 'target');
  159. "
  160. # Parandame vea "Potential gaps found near dead ends"
  161. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/repair_links_nearby_vertices.sql
  162. psql ${CONNECTION_NAME} -c "
  163. SELECT pgr_createVerticesTable('${destination_schema}.links', 'geom', 'source', 'target');
  164. SELECT pgr_analyzeGraph('${destination_schema}.links', 0.001, the_geom := 'geom', id := 'id', source := 'source', target := 'target');
  165. "
  166. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/fix_small_gaps.sql
  167. psql ${CONNECTION_NAME} -c "
  168. SELECT pgr_createVerticesTable('${destination_schema}.links', 'geom', 'source', 'target');
  169. SELECT pgr_analyzeGraph('${destination_schema}.links', 0.001, the_geom := 'geom', id := 'id', source := 'source', target := 'target');
  170. "
  171. # Abitabelite kustutamised
  172. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/drop_links_tables.sql
  173. # Tag'ide lisamine: isoleeritud ja lõikuvad
  174. #./tag_links_batchwise.sh
  175. # e) paranda graafi linke
  176. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/fix_gaps_and_isolated.sql
  177. psql ${CONNECTION_NAME} -v schema=${destination_schema} -d ${destination_dbname} -f sql/repair_graph_components.sql
  178. ## -----------------------------------------------------------------------------------