dleutnant / swmmr

R Interface for US EPA's SWMM
https://cran.r-project.org/package=swmmr
39 stars 15 forks source link

coordinates of outfalls are ignored in `shp_to_inp` #39

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hello all, I tried to convert a .inp file of a sewer network (see below for the file) to a shape file but I get this error: "Error in valid_numeric_matrix(x) : !anyNA(x) is not TRUE" and the script only creates the shapefile of the polygons. I tried with all the sample files in the package and the code worked. Can you please help me? Thank you very much. Enrico

The code that I used is the following:

if (!require(swmmr)) install.packages(swmmr) library(tidyverse) library(swmmr) library(purrr) library(dplyr) library(sf)

inp_file <- "C:/Users/Enrico/Tesi/Paullo/R/Paullo.inp" out_dir <- "C:/Users/Enrico/Tesi/Paullo/R"

Paullo <- read_inp(x = inp_file) summary(Paullo)

inp_to_files(x = Paullo, name = "Paullo", path_out = out_dir)

list.files(out_dir) c("shp", "txt", "dat") %>% map( ~ file.path(out_dir, .)) %>% map(list.files)

Paullo Script.txt

dleutnant commented 4 years ago

It seems that you have some conduits without coordinates.

library(tidyverse)
library(swmmr)

inp_file <- "/Users/dominik/Downloads/Paullo.inp"

Paullo <- read_inp(x = inp_file)
summary(Paullo)
#> 
#> ** summary of swmm model structure ** 
#> infiltration   :     horton 
#> flow_units     :        cms 
#> flow_routing   :    dynwave 
#> start_date     : 06/22/1993 
#> end_date       : 06/23/1993 
#> raingages      :          1 
#> subcatchments  :        835 
#> aquifers       :          0 
#> snowpacks      :          0 
#> junctions      :        909 
#> outfalls       :         18 
#> dividers       :          0 
#> storages       :          0 
#> conduits       :        958 
#> pumps          :          0 
#> orifices       :          0 
#> weirs          :          0 
#> outlets        :          0 
#> controls       :          0 
#> pollutants     :          0 
#> landuses       :          0 
#> lid_controls   :          0 
#> treatment      :          0 
#> *************************************

dplyr::left_join(x = Paullo$conduits, 
                 y = Paullo$coordinates, 
                 by = c(`To Node` = "Node")) %>% 
  dplyr::filter(is.na(`X-Coord`) | is.na(`Y-Coord`)) %>% 
  dplyr::select(Name, `From Node`, `To Node`, `X-Coord`, `Y-Coord`)
#> # A tibble: 18 x 5
#>    Name  `From Node` `To Node` `X-Coord` `Y-Coord`
#>    <chr> <chr>       <chr>         <dbl>     <dbl>
#>  1 C9    942         944              NA        NA
#>  2 C163  851         979              NA        NA
#>  3 C166  756         754              NA        NA
#>  4 C172  540         951              NA        NA
#>  5 C239  506         959              NA        NA
#>  6 C254  417         418              NA        NA
#>  7 C371  679         709              NA        NA
#>  8 C588  924         923              NA        NA
#>  9 C598  921         922              NA        NA
#> 10 C643  753         937              NA        NA
#> 11 C700  400         991              NA        NA
#> 12 C701  502         503              NA        NA
#> 13 C943  693         946              NA        NA
#> 14 C966  908         909              NA        NA
#> 15 C991  932         714              NA        NA
#> 16 C1001 939         943              NA        NA
#> 17 C1017 870         871              NA        NA
#> 18 C1033 771         945              NA        NA

Created on 2020-01-23 by the reprex package (v0.3.0)

ghost commented 4 years ago

Thanks for your quick reply, the problem is related to the outfall nodes, in fact the conduits without coordinates are those that connect a node to an outfall, however from QGIS the connections are correct. I am attaching a screenshot of the shapefile relating to the outfalls in which I add the coordinate values. Thanks again Enrico 1

ghost commented 4 years ago

I also tried to manually enter the coordinates of the outfalls in SWMM and then use the command inp_to_files and obtain the shapefiles again, but even if I use those obtained in my shp_to_inp script, the coordinates of the outfalls are not entered when I open the .inp file in SWMM. I don't know what to do, I attach the .inp file with the coordinates of the drains entered manually and the shp_to_inp script I use, thanks very much for your help, Enrico

if (!require(swmmr)) install.packages(swmmr) library(tidyverse) library(swmmr) library(purrr) library(dplyr) library(sf)

out_dir <- "C:/Users/Enrico/Tesi/Paullo/R"

Paullo_simulazione <- shp_to_inp( path_options = file.path(out_dir,"txt/Paullo_options.txt"), path_timeseries = file.path(out_dir,"dat/Paullo_timeseries_TS1.dat"), path_polygon = file.path(out_dir,"shp/Paullo_polygon.shp"), path_line = file.path(out_dir,"shp/Paullo_link.shp"), path_point = file.path(out_dir,"shp/Paullo_point.shp"), path_outfall = file.path(out_dir,"shp/Paullo_outfall.shp") )

summary(Paullo_simulazione)

dir.create(file.path(out_dir, "Nuovo_inp")) write_inp(Paullo_simulazione, file.path(out_dir, "Nuovo_inp", "Paullo_simulazione.inp"))

Paullo.txt

ghost commented 4 years ago

Sorry if I insist, probably the problem is related to the fact that I'm using version 0.9.0 of the package and not the latest version, that is 0.9.0.9000, but I can't install it since I can't find the .targz archive. I tried to edit the script of the shp_to_inp function manually with the indicated corrections relating to the junctions but it is read-only. Can someone help me? I use Rstudio and I'm not very good at programming, thank you very much.

dleutnant commented 4 years ago

Thanks for spotting this! Apparently, coordinates from outfalls are ignored within shp_to_inp. I need to double check this, though.

Here's a quick fix in which I i) manually extract the coordinates of the outfalls and ii) row bind them later to the other coordinates...

library(swmmr)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0

inp_file <- "/Users/dominik/Downloads/Paullo.txt"

Paullo <- read_inp(x = inp_file)

inp_to_files(x = Paullo, name = "Paullo", path_out = "/Users/dominik/Downloads/Paullo")
#> Warning: All elements of `...` must be named.
#> Did you want `geometry = c(`X-Coord`, `Y-Coord`)`?
#> Warning in abbreviate_shapefile_names(obj): Field names abbreviated for ESRI
#> Shapefile driver
#> Writing layer `Paullo_polygon' to data source `/Users/dominik/Downloads/Paullo/shp/Paullo_polygon.shp' using driver `ESRI Shapefile'
#> Writing 836 features with 22 fields and geometry type Polygon.
#> Warning: All elements of `...` must be named.
#> Did you want `geometry = c(`X-Coord`, `Y-Coord`)`?
#> Writing layer `Paullo_link' to data source `/Users/dominik/Downloads/Paullo/shp/Paullo_link.shp' using driver `ESRI Shapefile'
#> Writing 958 features with 16 fields and geometry type Line String.
#> Warning: All elements of `...` must be named.
#> Did you want `geometry = c(`X-Coord`, `Y-Coord`)`?
#> Writing layer `Paullo_point' to data source `/Users/dominik/Downloads/Paullo/shp/Paullo_point.shp' using driver `ESRI Shapefile'
#> Writing 909 features with 6 fields and geometry type Point.
#> Warning: All elements of `...` must be named.
#> Did you want `geometry = c(`X-Coord`, `Y-Coord`)`?
#> Writing layer `Paullo_outfall' to data source `/Users/dominik/Downloads/Paullo/shp/Paullo_outfall.shp' using driver `ESRI Shapefile'
#> Writing 18 features with 6 fields and geometry type Point.
#> section weirs is missing
#> section orifices is missing
#> section pumps is missing
#> section storage is missing
#> *.shp files were written to /Users/dominik/Downloads/Paullo/shp
#> *.txt file was written to /Users/dominik/Downloads/Paullo/txt
#> section curves is missing
#> timeseries.dat files were written to /Users/dominik/Downloads/Paullo/dat

out_dir <- "/Users/dominik/Downloads/Paullo"

Paullo_simulazione <- shp_to_inp(
  path_options = file.path(out_dir,"txt/Paullo_options.txt"),
  path_timeseries = file.path(out_dir,"dat/Paullo_timeseries_TS1.dat"),
  path_polygon = file.path(out_dir,"shp/Paullo_polygon.shp"),
  path_line = file.path(out_dir,"shp/Paullo_link.shp"),
  path_point = file.path(out_dir,"shp/Paullo_point.shp"),
  path_outfall = file.path(out_dir,"shp/Paullo_outfall.shp")
)

# manually get Name and coordinates from outfalls
tmp_outfalls <- file.path(out_dir,"shp/Paullo_outfall.shp") %>% 
  sf::st_read(stringsAsFactors = F) %>% 
  dplyr::bind_cols(sf::st_drop_geometry(.), 
                   tibble::as_tibble(sf::st_coordinates(.))) %>% 
  dplyr::select(Name, X,  Y) %>% 
  sf::st_drop_geometry() %>% 
  tibble::as_tibble()
#> Reading layer `Paullo_outfall' from data source `/Users/dominik/Downloads/Paullo/shp/Paullo_outfall.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 18 features and 6 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 530399.1 ymin: 5028844 xmax: 532649.5 ymax: 5029711
#> epsg (SRID):    NA
#> proj4string:    NA

# add outfalls to coorinates
Paullo_simulazione$coordinates  <- 
  dplyr::bind_rows(Paullo_simulazione$coordinates, tmp_outfalls)

# rename
colnames(Paullo_simulazione$coordinates) <- c("Node", "X-Coord", "Y-Coord")

# final check 
dplyr::left_join(x = Paullo_simulazione$outfalls, 
                 y = Paullo_simulazione$coordinates, 
                 by = c("Name" = "Node"))
#> # A tibble: 18 x 8
#>    Name  Elevation Type  StageData Gated RouteTo `X-Coord` `Y-Coord`
#>    <chr>     <dbl> <chr>     <int> <chr> <chr>       <dbl>     <dbl>
#>  1 S418       92.2 FREE         NA NO    " "       531990.  5029404.
#>  2 S503       92.5 FREE         NA NO    " "       531979.  5029693.
#>  3 S754       92.0 FREE         NA NO    " "       532000.  5029347.
#>  4 S951       92.1 FREE         NA NO    " "       531997.  5029372.
#>  5 S959       92.2 FREE         NA NO    " "       531989.  5029429.
#>  6 S979       94   FREE         NA NO    " "       532267.  5029371.
#>  7 S991       92.7 FREE         NA NO    " "       531975.  5029711.
#>  8 S922       93.8 FREE         NA NO    " "       530760.  5029481.
#>  9 S714       90.2 FREE         NA NO    " "       530873.  5029063.
#> 10 S943       90.7 FREE         NA NO    " "       530865.  5029088.
#> 11 S871       93   FREE         NA NO    " "       532408.  5029391.
#> 12 S709       92.1 FREE         NA NO    " "       531295.  5029077.
#> 13 S945       91.7 FREE         NA NO    " "       531948.  5028919.
#> 14 S946       91.3 FREE         NA NO    " "       531660.  5028844.
#> 15 S937       91.9 FREE         NA NO    " "       531997.  5029299.
#> 16 S944       91.8 FREE         NA NO    " "       530911.  5029182.
#> 17 S909       93   FREE         NA NO    " "       532650.  5029332.
#> 18 S923       94   FREE         NA NO    " "       530399.  5029515.

dir.create(file.path(out_dir, "Nuovo_inp"))
write_inp(Paullo_simulazione, file.path(out_dir, "Nuovo_inp", "Paullo_simulazione.inp"))

Created on 2020-01-27 by the reprex package (v0.3.0)

dleutnant commented 4 years ago

@DoeringA Are you already "back in business"? ;)

ghost commented 4 years ago

Thanks a lot for the fix, I also solved the problem by inserting the outfalls into the shapefile relating to the junctions (in this way the coordinates are read) and then going to eliminate the outfalls after the function shp_to_inp in this way:

Paullo_swmm$junction <- Paullo_swmm$junction[Paullo_swmm$junction$Name != "S418",]

... although I realize that it is not very "elegant" One last question: do you have a tar.gz archive of the latest version of the package 0.9.0.9000? because if I try to install the developer version using:

remotes :: install_github ( "dleutnant / swmmr")

and selecting the option "CRAN packages only", the installation is not completed with error:

Error: Failed to install 'swmmr' from GitHub:    (converted from warning) cannot remove prior installation of package 'rlang' In addition: Warning messages: 1: In untar2 (tarfile, files, list, exdir):    skipping pax global extended headers 2: In untar2 (tarfile, files, list, exdir):    skipping pax global extended headers

Could you kindly help me? because otherwise I can't correct the code related to the shp_to_inp function related to the number of junctions. Many thanks

Enrico

dleutnant commented 4 years ago

Could you run update.packages() before remotes::install_github("dleutnant/swmmr")?

ghost commented 4 years ago

I get this message:

Warning: cannot remove prior installation of package ‘tidyselect’ Warning: restored ‘tidyselect’ package ‘vctrs’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\Temp\Rtmp8aDc1s\downloaded_packages Warning message: In unlink(instPath, recursive = TRUE) : cannot get info on 'C:\Program Files\R\R-3.6.2\library/tidyselect/libs/x64', reason 'Accesso negato'

And then, if I try:

remotes :: install_github ( "dleutnant / swmmr")

I get this one:

Error: Failed to install 'unknown package' from GitHub: attempt to set an attribute on NULL

dleutnant commented 4 years ago

Are you behind a firewall? Do you have full (read and write) access to your R package directory?

ghost commented 4 years ago

yes, it is my personal notebook with administrator privileges so I have full access to the directory, do I try to uninstall R? otherwise I wouldn't know what to try ..

dleutnant commented 4 years ago

Did you eventually inserted some white spaces? Compare the following:

remotes :: install_github ( "dleutnant / swmmr")
#> Error: Failed to install 'unknown package' from GitHub:
#>   Versuch ein Attribut von NULL zu setzen

remotes::install_github("dleutnant/swmmr")
#> Skipping install of 'swmmr' from a github remote, the SHA1 (1654d3c4) has not changed since last install.
#>   Use `force = TRUE` to force installation

Created on 2020-01-28 by the reprex package (v0.3.0)

ghost commented 4 years ago

I uninstalled and reinstalled R, Rstudio and Rtools completely from scratch, I updated all the packages and launched the command without spaces, but I still get errors. I report the complete results of the console, while the script that I launch is as follows:

update.packages() if (!require(swmmr)) remotes::install_github("dleutnant/swmmr") library(tidyverse) library(swmmr) library(purrr) library(dplyr) library(sf)

update.packages() remotes::install_github("dleutnant/swmmr") Downloading GitHub repo dleutnant/swmmr@master Installing 29 packages: dplyr, purrr, readr, Rcpp, tibble, tidyr, xts, zoo, assertthat, glue, magrittr, pkgconfig, R6, rlang, tidyselect, BH, plogr, hms, clipr, crayon, cli, fansi, pillar, ellipsis, stringi, vctrs, lifecycle, utf8, digest

There are binary versions available but the source versions are later: binary source needs_compilation rlang 0.4.2 0.4.3 TRUE tidyselect 0.2.5 1.0.0 TRUE stringi 1.4.4 1.4.5 TRUE

Binaries will be installed trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/dplyr_0.8.3.zip' Content type 'application/zip' length 3224479 bytes (3.1 MB) downloaded 3.1 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/purrr_0.3.3.zip' Content type 'application/zip' length 426130 bytes (416 KB) downloaded 416 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/readr_1.3.1.zip' Content type 'application/zip' length 1566540 bytes (1.5 MB) downloaded 1.5 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/Rcpp_1.0.3.zip' Content type 'application/zip' length 2989801 bytes (2.9 MB) downloaded 2.9 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/tibble_2.1.3.zip' Content type 'application/zip' length 336919 bytes (329 KB) downloaded 329 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/tidyr_1.0.2.zip' Content type 'application/zip' length 1295893 bytes (1.2 MB) downloaded 1.2 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/xts_0.12-0.zip' Content type 'application/zip' length 967328 bytes (944 KB) downloaded 944 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/zoo_1.8-7.zip' Content type 'application/zip' length 1108029 bytes (1.1 MB) downloaded 1.1 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/assertthat_0.2.1.zip' Content type 'application/zip' length 55211 bytes (53 KB) downloaded 53 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/glue_1.3.1.zip' Content type 'application/zip' length 173456 bytes (169 KB) downloaded 169 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/magrittr_1.5.zip' Content type 'application/zip' length 157848 bytes (154 KB) downloaded 154 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/pkgconfig_2.0.3.zip' Content type 'application/zip' length 22405 bytes (21 KB) downloaded 21 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/R6_2.4.1.zip' Content type 'application/zip' length 59293 bytes (57 KB) downloaded 57 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/rlang_0.4.2.zip' Content type 'application/zip' length 1113118 bytes (1.1 MB) downloaded 1.1 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/tidyselect_0.2.5.zip' Content type 'application/zip' length 624335 bytes (609 KB) downloaded 609 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/BH_1.72.0-3.zip' Content type 'application/zip' length 18270741 bytes (17.4 MB) downloaded 17.4 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/plogr_0.2.0.zip' Content type 'application/zip' length 18869 bytes (18 KB) downloaded 18 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/hms_0.5.3.zip' Content type 'application/zip' length 109073 bytes (106 KB) downloaded 106 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/clipr_0.7.0.zip' Content type 'application/zip' length 52070 bytes (50 KB) downloaded 50 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/crayon_1.3.4.zip' Content type 'application/zip' length 750374 bytes (732 KB) downloaded 732 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/cli_2.0.1.zip' Content type 'application/zip' length 389474 bytes (380 KB) downloaded 380 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/fansi_0.4.1.zip' Content type 'application/zip' length 224040 bytes (218 KB) downloaded 218 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/pillar_1.4.3.zip' Content type 'application/zip' length 184643 bytes (180 KB) downloaded 180 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/ellipsis_0.3.0.zip' Content type 'application/zip' length 44589 bytes (43 KB) downloaded 43 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/stringi_1.4.4.zip' Content type 'application/zip' length 15307078 bytes (14.6 MB) downloaded 14.6 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/vctrs_0.2.2.zip' Content type 'application/zip' length 944451 bytes (922 KB) downloaded 922 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/lifecycle_0.1.0.zip' Content type 'application/zip' length 84774 bytes (82 KB) downloaded 82 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/utf8_1.1.4.zip' Content type 'application/zip' length 215363 bytes (210 KB) downloaded 210 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/digest_0.6.23.zip' Content type 'application/zip' length 249164 bytes (243 KB) downloaded 243 KB

package ‘dplyr’ successfully unpacked and MD5 sums checked package ‘purrr’ successfully unpacked and MD5 sums checked package ‘readr’ successfully unpacked and MD5 sums checked package ‘Rcpp’ successfully unpacked and MD5 sums checked package ‘tibble’ successfully unpacked and MD5 sums checked package ‘tidyr’ successfully unpacked and MD5 sums checked package ‘xts’ successfully unpacked and MD5 sums checked package ‘zoo’ successfully unpacked and MD5 sums checked package ‘assertthat’ successfully unpacked and MD5 sums checked package ‘glue’ successfully unpacked and MD5 sums checked package ‘magrittr’ successfully unpacked and MD5 sums checked package ‘pkgconfig’ successfully unpacked and MD5 sums checked package ‘R6’ successfully unpacked and MD5 sums checked package ‘rlang’ successfully unpacked and MD5 sums checked package ‘tidyselect’ successfully unpacked and MD5 sums checked package ‘BH’ successfully unpacked and MD5 sums checked package ‘plogr’ successfully unpacked and MD5 sums checked package ‘hms’ successfully unpacked and MD5 sums checked package ‘clipr’ successfully unpacked and MD5 sums checked package ‘crayon’ successfully unpacked and MD5 sums checked package ‘cli’ successfully unpacked and MD5 sums checked package ‘fansi’ successfully unpacked and MD5 sums checked package ‘pillar’ successfully unpacked and MD5 sums checked package ‘ellipsis’ successfully unpacked and MD5 sums checked package ‘stringi’ successfully unpacked and MD5 sums checked package ‘vctrs’ successfully unpacked and MD5 sums checked package ‘lifecycle’ successfully unpacked and MD5 sums checked package ‘utf8’ successfully unpacked and MD5 sums checked package ‘digest’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\Temp\Rtmp84gtIO\downloaded_packages Running R CMD build...

Thanks again for your patience and availability, Enrico

dleutnant commented 4 years ago

You need to have Rtools to be installed.

Package swmmr has compiled code, but no suitable compiler(s) were found. Installation will likely fail. Install Rtools (https://cran.r-project.org/bin/windows/Rtools/).Then use the pkgbuild package, or make sure that Rtools in the PATH.

ghost commented 4 years ago

Yes I installed correctly Rtools, but I don't understand what I have to do..

dleutnant commented 4 years ago

Please try the "devtools " package to install from GitHub.

  1. install.packages("devtools")
  2. devtools::find_rtools()
  3. devtools::install_github("dleutnant/swmmr")
ghost commented 4 years ago

Finally(using devtools) IT WORKS CORRECTLY!! Many thanks for your help and your package! Enrico

ghost commented 4 years ago

Hello, maybe I should open a new topic but I try to write here in case it was my mistake. I have to insert two storage elements within my network using the shp_to_inp function and to do this I started from an example .inp file contained in the package archive and using the function inp_to_files I obtained the .shp, .txt and .dat files, in which the storage elements are contained in a shapefile of point elements. For this reason I recreated the same shapefile with my data, but when using the shp_to_inp function on my network, the storage is not inserted. Reading the package manual, however, it is reported under the "path_storage" section that the storage elements must be inserted using a .txt file, for this I cannot understand how I have to do to insert them. I report my code in case I was making mistakes and the example .inp file that I used. Thanks again for the help and availability. Enrico Example3.txt

# aggiorno i pacchetti di R e controllo che Rtools sia attivo
library(usethis)
library(devtools)
devtools::find_rtools()
update.packages()

#installo swmmr ed attivo le librerie necessarie
if (!require(swmmr)) devtools::install_github("dleutnant/swmmr")
library(tidyverse)
library(swmmr)
library(purrr)
library(dplyr)
library(sf)

#salvo nella cartella del progetto di R
out_dir <- "C:/Users/Enrico/Tesi/Paullo"

#lancio il comando "shape to inp" del pacchetto swmmr che converte shp e file di testo in file .inp
PAULLO <- shp_to_inp(
  path_options = file.path(out_dir,"SWMM/Paullo_Opzioni.txt"),
  path_timeseries = file.path(out_dir,"PIOGGE/Paullo_TS1.dat"),
  path_polygon = file.path(out_dir,"SWMM/Paullo_polygon.shp"),
  path_line = file.path(out_dir,"SWMM/Paullo_link.shp"),
  path_point = file.path(out_dir,"SWMM/Paullo_point.shp"),
  path_outfall = file.path(out_dir,"SWMM/Paullo_outfall.shp"),
  path_pumps = file.path(out_dir,"SWMM/Paullo_pumps.shp"),
  path_pump_curve = file.path(out_dir,"SWMM/Paullo_Pcurva1.txt"),
  path_storage = file.path(out_dir,"SWMM/Paullo_storages.shp"),
  path_storage_curve = file.path(out_dir,"SWMM/Paullo_Scurva1.txt")
)

# ricavo manualmente Nomi e coordinate degli scarichi diversamente non letti dalla funzione shp_to_inp
tmp_outfalls <- file.path(out_dir,"SWMM/Paullo_outfall.shp") %>% 
  sf::st_read(stringsAsFactors = F) %>% 
  dplyr::bind_cols(sf::st_drop_geometry(.), 
                   tibble::as_tibble(sf::st_coordinates(.))) %>% 
  dplyr::select(Name, X,  Y) %>% 
  sf::st_drop_geometry() %>% 
  tibble::as_tibble()

#aggiungo le coordinate degli scarichi al file .inp creato e rinomino i campi secondo swmm
PAULLO$coordinates  <- 
  dplyr::bind_rows(PAULLO$coordinates, tmp_outfalls)
colnames(PAULLO$coordinates) <- c("Node", "X-Coord", "Y-Coord")

#controllo la struttura del file convertito
summary(PAULLO)

#salvo il file .inp in una nuova cartella nella directory di output
#dir.create(file.path(out_dir, "PAULLO_Inp"))
write_inp(PAULLO, file.path(out_dir, "PAULLO.inp"))

1

ghost commented 4 years ago

Unfortunately I have other problems to report and I apologize once again for the inconvenience. I try to explain myself by points:

1 - The package manual probably reports an error on page 12 in the "path_storage" section as a .shp file with point features is required and not a .txt file (simple writing error).

73350049-a176e600-428c-11ea-96fc-362825f4c6d0 2 - Trying to insert the storage elements, these are detected but the corresponding coordinates are not loaded correctly by the shp_to_inp function, therefore I proceeded by copying the same correction code suggested by you for the outfalls.

3 - The storage elements are also not correctly identified by the read_inp function as the "summary" section reports 0 under "storages" as the following, while these are correctly inserted in the .inp file:

** summary of swmm model structure ** 
infiltration   :     horton 
flow_units     :        cms 
flow_routing   :    dynwave 
start_date     : 06/22/1993 
end_date       : 06/23/1993 
raingages      :          1 
subcatchments  :        818 
aquifers       :          0 
snowpacks      :          0 
junctions      :        908 
outfalls       :         18 
dividers       :          0 
storages       :          0 
conduits       :        955 
pumps          :          4 
orifices       :          0 
weirs          :          0 
outlets        :          0 
controls       :          0 
pollutants     :          0 
landuses       :          0 
lid_controls   :          0 
treatment      :          0 
*************************************

I report again my complete code and the .inp file complete with all the necessary data so that you can check what I have written above. Thanks again for the availability. Enrico PAULLO.txt

out_dir <- "C:/Users/Enrico/Tesi/Paullo"

PAULLO <- shp_to_inp(
  path_options = file.path(out_dir,"SWMM/Paullo_Opzioni.txt"),
  path_timeseries = file.path(out_dir,"PIOGGE/Paullo_TS1.dat"),
  path_polygon = file.path(out_dir,"SWMM/Paullo_polygon.shp"),
  path_line = file.path(out_dir,"SWMM/Paullo_link.shp"),
  path_point = file.path(out_dir,"SWMM/Paullo_point.shp"),
  path_outfall = file.path(out_dir,"SWMM/Paullo_outfall.shp"),
  path_pumps = file.path(out_dir,"SWMM/Paullo_pumps.shp"),
  path_pump_curve = file.path(out_dir,"SWMM/Paullo_Pcurva1.txt"),
  path_storage = file.path(out_dir,"SWMM/Paullo_storages.shp"),
)

tmp_outfalls <- file.path(out_dir,"SWMM/Paullo_outfall.shp") %>% 
  sf::st_read(stringsAsFactors = F) %>% 
  dplyr::bind_cols(sf::st_drop_geometry(.), 
                   tibble::as_tibble(sf::st_coordinates(.))) %>% 
  dplyr::select(Name, X,  Y) %>% 
  sf::st_drop_geometry() %>% 
  tibble::as_tibble()

tmp_storages <- file.path(out_dir,"SWMM/Paullo_storages.shp") %>% 
  sf::st_read(stringsAsFactors = F) %>% 
  dplyr::bind_cols(sf::st_drop_geometry(.), 
                   tibble::as_tibble(sf::st_coordinates(.))) %>% 
  dplyr::select(Name, X,  Y) %>% 
  sf::st_drop_geometry() %>% 
  tibble::as_tibble()

PAULLO$coordinates  <- 
  dplyr::bind_rows(PAULLO$coordinates, tmp_outfalls)
colnames(PAULLO$coordinates) <- c("Node", "X-Coord", "Y-Coord")

PAULLO$coordinates  <- 
  dplyr::bind_rows(PAULLO$coordinates, tmp_storages)
colnames(PAULLO$coordinates) <- c("Node", "X-Coord", "Y-Coord")

summary(PAULLO)

write_inp(PAULLO, file.path(out_dir, "PAULLO.inp"))
ghost commented 4 years ago

Hello again, I'm sorry to disturb but I wanted to know if by chance he had managed to find a fix to make the read_inp and the shp_to_inp functions read the number of storage in the file. Even if you try with the Example3 file contained in its package, the number of storage units is not detected. Thank you very much Enrico Example3.txt

dleutnant commented 4 years ago

The recent development version covers your reported issue. Thank you!