.Rhistory 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. geometry_type = "PROMOTE_TO_MULTI"
  2. cmd <- sprintf(
  3. paste0(
  4. "export PGCLIENTENCODING=ISO-8859-4; ",
  5. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING ISO-8859-4 -f PostgreSQL ",
  6. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  7. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  8. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  9. "-s_srs %s -t_srs %s -nlt %s"
  10. ),
  11. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  12. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  13. conf$s_srs, conf$t_srs, geometry_type
  14. )
  15. cmd <- sprintf(
  16. paste0(
  17. "export PGCLIENTENCODING=ISO-8859-4; ",
  18. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING ISO-8859-4 -f PostgreSQL ",
  19. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  20. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  21. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  22. "-s_srs %s -t_srs %s -nlt %s"
  23. ),
  24. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  25. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  26. conf$s_srs, conf$t_srs, geometry_type
  27. )
  28. paste0(
  29. "export PGCLIENTENCODING=ISO-8859-4; ",
  30. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING ISO-8859-4 -f PostgreSQL ",
  31. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  32. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  33. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  34. "-s_srs %s -t_srs %s -nlt %s"
  35. )
  36. cmd <- sprintf(
  37. paste0(
  38. "export PGCLIENTENCODING=ISO-8859-4; ",
  39. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING ISO-8859-4 -f PostgreSQL ",
  40. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  41. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  42. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  43. "-s_srs %s -t_srs %s -nlt %s"
  44. ),
  45. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  46. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  47. conf$s_srs, conf$t_srs, geometry_type
  48. )
  49. conf$dbname
  50. conf$host
  51. conf$port
  52. conf$user
  53. conf$password
  54. conf$sslmode
  55. conf$schema
  56. source
  57. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  58. source
  59. source
  60. cmd <- sprintf(
  61. paste0(
  62. "export PGCLIENTENCODING=ISO-8859-4; ",
  63. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING ISO-8859-4 -f PostgreSQL ",
  64. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  65. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  66. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  67. "-s_srs %s -t_srs %s -nlt %s"
  68. ),
  69. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  70. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  71. conf$s_srs, conf$t_srs, geometry_type
  72. )
  73. cmd
  74. system(cmd)
  75. conf$table
  76. print(tbl_names[i])
  77. conf$table <- tolower(tbl_names[i])
  78. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  79. ## Export to postgis database.
  80. conf$primary_key = 'id'
  81. conf$s_srs = "EPSG:3301"
  82. conf$t_srs = "EPSG:3301"
  83. geometry_type = "PROMOTE_TO_MULTI"
  84. # ruut::copy_shp_to_db(
  85. # dir = tmp_dir_kihid, layer = tbl_names[i], conf = conf,
  86. # id = "id", crs_source = "EPSG:3301", crs_target = "EPSG:3301",
  87. # geometry_type = "PROMOTE_TO_MULTI"
  88. # )
  89. cmd <- sprintf(
  90. paste0(
  91. "export PGCLIENTENCODING=ISO-8859-4; ",
  92. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING ISO-8859-4 -f PostgreSQL ",
  93. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  94. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  95. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  96. "-s_srs %s -t_srs %s -nlt %s"
  97. ),
  98. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  99. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  100. conf$s_srs, conf$t_srs, geometry_type
  101. )
  102. system(cmd)
  103. cat(sprintf(
  104. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  105. ls[i], conf$schema, conf$table
  106. ))
  107. cmd <- sprintf(
  108. paste0(
  109. "export PGCLIENTENCODING=ISO-8859-4; ",
  110. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -f PostgreSQL ",
  111. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  112. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  113. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  114. "-s_srs %s -t_srs %s -nlt %s"
  115. ),
  116. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  117. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  118. conf$s_srs, conf$t_srs, geometry_type
  119. )
  120. system(cmd)
  121. cmd <- sprintf(
  122. paste0(
  123. "export PGCLIENTENCODING=UTF-8; ",
  124. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -f PostgreSQL ",
  125. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  126. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  127. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  128. "-s_srs %s -t_srs %s -nlt %s"
  129. ),
  130. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  131. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  132. conf$s_srs, conf$t_srs, geometry_type
  133. )
  134. system(cmd)
  135. styler:::style_active_file()
  136. library(estmap)
  137. encoding <- 'UTF-8'
  138. cmd <- sprintf(
  139. paste0(
  140. "export PGCLIENTENCODING=%s; ",
  141. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  142. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  143. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  144. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  145. "-s_srs %s -t_srs %s -nlt %s"
  146. ), encoding, encoding,
  147. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  148. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  149. conf$s_srs, conf$t_srs, geometry_type
  150. )
  151. cmd
  152. tbl_names
  153. i = 33
  154. print(tbl_names[i])
  155. conf$table <- tolower(tbl_names[i])
  156. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  157. ## Export to postgis database.
  158. conf$primary_key <- "id"
  159. conf$s_srs <- "EPSG:3301"
  160. conf$t_srs <- "EPSG:3301"
  161. geometry_type <- "PROMOTE_TO_MULTI"
  162. encoding <- 'UTF-8'
  163. encoding <- 'CP1257'
  164. cmd <- sprintf(
  165. paste0(
  166. "export PGCLIENTENCODING=%s; ",
  167. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  168. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  169. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  170. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  171. "-s_srs %s -t_srs %s -nlt %s"
  172. ), encoding, encoding,
  173. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  174. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  175. conf$s_srs, conf$t_srs, geometry_type
  176. )
  177. system(cmd)
  178. # Unzip
  179. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  180. print(tbl_names[i])
  181. conf$table <- tolower(tbl_names[i])
  182. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  183. ## Export to postgis database.
  184. conf$primary_key <- "id"
  185. conf$s_srs <- "EPSG:3301"
  186. conf$t_srs <- "EPSG:3301"
  187. geometry_type <- "PROMOTE_TO_MULTI"
  188. encoding <- 'UTF-8'
  189. encoding <- 'CP1257'
  190. cmd <- sprintf(
  191. paste0(
  192. "export PGCLIENTENCODING=%s; ",
  193. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  194. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  195. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  196. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  197. "-s_srs %s -t_srs %s -nlt %s"
  198. ), encoding, encoding,
  199. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  200. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  201. conf$s_srs, conf$t_srs, geometry_type
  202. )
  203. system(cmd)
  204. encoding <- 'ISO-8859-4'
  205. cmd <- sprintf(
  206. paste0(
  207. "export PGCLIENTENCODING=%s; ",
  208. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  209. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  210. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  211. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  212. "-s_srs %s -t_srs %s -nlt %s"
  213. ), encoding, encoding,
  214. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  215. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  216. conf$s_srs, conf$t_srs, geometry_type
  217. )
  218. system(cmd)
  219. encoding <- 'ISO-8859-4'
  220. cmd <- sprintf(
  221. paste0(
  222. "export PGCLIENTENCODING=%s; ",
  223. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  224. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  225. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  226. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  227. "-s_srs %s -t_srs %s -nlt %s"
  228. ), encoding, encoding,
  229. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  230. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  231. conf$s_srs, conf$t_srs, geometry_type
  232. )
  233. system(cmd)
  234. encoding <- 'ISO-8859-4'
  235. cmd <- sprintf(
  236. paste0(
  237. "export PGCLIENTENCODING=%s; ",
  238. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  239. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  240. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  241. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  242. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  243. ), encoding, encoding,
  244. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  245. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  246. conf$s_srs, conf$t_srs, geometry_type
  247. )
  248. system(cmd)
  249. encoding <- 'UTF-8'
  250. cmd <- sprintf(
  251. paste0(
  252. "export PGCLIENTENCODING=%s; ",
  253. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  254. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  255. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  256. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  257. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  258. ), encoding, encoding,
  259. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  260. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  261. conf$s_srs, conf$t_srs, geometry_type
  262. )
  263. system(cmd)
  264. tbl_names
  265. i=15
  266. print(tbl_names[i])
  267. conf$table <- tolower(tbl_names[i])
  268. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  269. ## Export to postgis database.
  270. conf$primary_key <- "id"
  271. conf$s_srs <- "EPSG:3301"
  272. conf$t_srs <- "EPSG:3301"
  273. geometry_type <- "PROMOTE_TO_MULTI"
  274. encoding <- 'UTF-8'
  275. # encoding <- 'ISO-8859-4'
  276. cmd <- sprintf(
  277. paste0(
  278. "export PGCLIENTENCODING=%s; ",
  279. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  280. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  281. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  282. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  283. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  284. ), encoding, encoding,
  285. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  286. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  287. conf$s_srs, conf$t_srs, geometry_type
  288. )
  289. system(cmd)
  290. tbl_names
  291. i = 41
  292. print(tbl_names[i])
  293. conf$table <- tolower(tbl_names[i])
  294. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  295. ## Export to postgis database.
  296. conf$primary_key <- "id"
  297. conf$s_srs <- "EPSG:3301"
  298. conf$t_srs <- "EPSG:3301"
  299. geometry_type <- "PROMOTE_TO_MULTI"
  300. encoding <- 'UTF-8'
  301. # encoding <- 'ISO-8859-4'
  302. cmd <- sprintf(
  303. paste0(
  304. "export PGCLIENTENCODING=%s; ",
  305. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  306. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  307. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  308. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  309. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  310. ), encoding, encoding,
  311. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  312. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  313. conf$s_srs, conf$t_srs, geometry_type
  314. )
  315. system(cmd)
  316. encoding <- 'UTF-16'
  317. # encoding <- 'ISO-8859-4'
  318. cmd <- sprintf(
  319. paste0(
  320. "export PGCLIENTENCODING=%s; ",
  321. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  322. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  323. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  324. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  325. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  326. ), encoding, encoding,
  327. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  328. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  329. conf$s_srs, conf$t_srs, geometry_type
  330. )
  331. system(cmd)
  332. encoding <- 'UTF-16'
  333. # encoding <- 'ISO-8859-4'
  334. cmd <- sprintf(
  335. paste0(
  336. "export PGCLIENTENCODING=%s; ",
  337. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  338. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  339. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  340. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  341. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  342. ), encoding, encoding,
  343. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  344. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  345. conf$s_srs, conf$t_srs, geometry_type
  346. )
  347. system(cmd)
  348. encoding <- 'ISO-8859-4'
  349. cmd <- sprintf(
  350. paste0(
  351. "export PGCLIENTENCODING=%s; ",
  352. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  353. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  354. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  355. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  356. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  357. ), encoding, encoding,
  358. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  359. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  360. conf$s_srs, conf$t_srs, geometry_type
  361. )
  362. system(cmd)
  363. tbl_names[i]
  364. if(tbl_names[i] %in% c('Kirik_p')) encoding <- 'ISO-8859-4' else encoding <- 'UTF-8'
  365. cmd <- sprintf(
  366. paste0(
  367. "export PGCLIENTENCODING=%s; ",
  368. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING %s -f PostgreSQL ",
  369. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  370. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  371. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  372. "-s_srs %s -t_srs %s -nlt %s -skipfailures"
  373. ), encoding, encoding,
  374. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  375. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  376. conf$s_srs, conf$t_srs, geometry_type
  377. )
  378. system(cmd)
  379. styler:::style_selection()
  380. styler:::style_active_file()
  381. url <- "https://geoportaal.maaamet.ee/docs/muld"
  382. # Estonia shapefile (ZIP archive)
  383. map_shapefile <- "Mullakaart_SHP.zip"
  384. # Download and save
  385. url_download <- sprintf("%s", url)
  386. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  387. url_download
  388. url <- "https://geoportaal.maaamet.ee/docs/muld/Mullakaart_SHP.zip"
  389. # Estonia shapefile (ZIP archive)
  390. map_shapefile <- "Mullakaart_SHP.zip"
  391. # Download and save
  392. url_download <- sprintf("%s", url)
  393. url_download
  394. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  395. saveTo
  396. # Temp directory
  397. tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "maaamet_mullakaart")
  398. if (!dir.exists(tmp_dir)) {
  399. dir.create(tmp_dir)
  400. }
  401. # Download link
  402. url <- "https://geoportaal.maaamet.ee/docs/muld/Mullakaart_SHP.zip"
  403. # Estonia shapefile (ZIP archive)
  404. map_shapefile <- "Mullakaart_SHP.zip"
  405. # Download and save
  406. url_download <- sprintf("%s", url)
  407. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  408. saveTo
  409. file.exists(saveTo)
  410. # Unzip
  411. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  412. # List of files
  413. tmp_dir_kihid <- sprintf("%s/%s", tmp_dir, map_shapefile)
  414. ls <- list.files(path = tmp_dir_kihid, pattern = ".dbf")
  415. ls_long <- list.files(path = tmp_dir_kihid, pattern = ".dbf", full.names = T)
  416. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  417. tbl_names
  418. ls
  419. tmp_dir
  420. tmp_dir
  421. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  422. ls
  423. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  424. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  425. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  426. tbl_names
  427. conf$schema <- "eesti"
  428. ruut::db_create_new_schema(conf = conf)
  429. length(tbl_names)
  430. i=1
  431. print(tbl_names[i])
  432. conf$table <- tolower(tbl_names[i])
  433. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  434. ## Export to postgis database.
  435. conf$primary_key <- "id"
  436. conf$s_srs <- "EPSG:3301"
  437. conf$t_srs <- "EPSG:3301"
  438. geometry_type <- "PROMOTE_TO_MULTI"
  439. cmd <- sprintf(
  440. paste0(
  441. "export PGCLIENTENCODING=UTF-8; ",
  442. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -f PostgreSQL ",
  443. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  444. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  445. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  446. "-s_srs %s -t_srs %s -nlt %s"
  447. ),
  448. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  449. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  450. conf$s_srs, conf$t_srs, geometry_type
  451. )
  452. cmd
  453. system(cmd)
  454. print(tbl_names[i])
  455. conf$table <- tolower(tbl_names[i])
  456. source <- sprintf('"%s" "%s"', tmp_dir_kihid, tbl_names[i])
  457. source
  458. conf$primary_key <- "id"
  459. conf$s_srs <- "EPSG:3301"
  460. conf$t_srs <- "EPSG:3301"
  461. geometry_type <- "PROMOTE_TO_MULTI"
  462. cmd <- sprintf(
  463. paste0(
  464. "export PGCLIENTENCODING=UTF-8; ",
  465. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -f PostgreSQL ",
  466. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  467. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  468. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  469. "-s_srs %s -t_srs %s -nlt %s"
  470. ),
  471. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  472. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  473. conf$s_srs, conf$t_srs, geometry_type
  474. )
  475. cmd
  476. source
  477. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  478. source('~/apps/R/packages/estmap/R/maaamet_mullakaart.R', echo=TRUE)
  479. source
  480. print(tbl_names[i])
  481. conf$table <- tolower(tbl_names[i])
  482. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  483. ## Export to postgis database.
  484. conf$primary_key <- "id"
  485. conf$s_srs <- "EPSG:3301"
  486. conf$t_srs <- "EPSG:3301"
  487. geometry_type <- "PROMOTE_TO_MULTI"
  488. cmd <- sprintf(
  489. paste0(
  490. "export PGCLIENTENCODING=UTF-8; ",
  491. "ogr2ogr -progress --config PG_USE_COPY YES --config PGCLIENTENCODING UTF-8 -f PostgreSQL ",
  492. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  493. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  494. "-lco GEOMETRY_NAME=geom -lco FID=%s -nln %s.%s ",
  495. "-s_srs %s -t_srs %s -nlt %s"
  496. ),
  497. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  498. conf$sslmode, conf$schema, source, conf$primary_key, conf$schema, conf$table,
  499. conf$s_srs, conf$t_srs, geometry_type
  500. )
  501. system(cmd)
  502. # Delete unnecessary files.
  503. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
  504. styler:::style_active_file()
  505. library(estmap)
  506. library(estmap)
  507. styler:::style_active_file()
  508. library(estmap)
  509. library(estmap)
  510. library(estmap)
  511. library(estmap)
  512. library(estmap)