atmoschem / EmissV

Create and processing emissions for numeric air quality models
https://atmoschem.github.io/EmissV/
Other
35 stars 10 forks source link

streetDist problems #20

Closed mpadge closed 6 years ago

mpadge commented 6 years ago

This is a brilliant package that we would like to incorporate within our analyses. To do that, we would like to use the streetDist function which does not appear to work in current form. Reproducible code follows:

library (EmissV)
library (osmdata)
library (sf)
city <- "accra"
bb <- getbb (city)
dat <- opq (bbox = city) %>%
    add_osm_feature (key = "highway") %>%
    osmdata_sf (quiet = FALSE) %>%
    osmdata::osm_poly2line () %>%
    magrittr::extract2 ("osm_lines")
#saveRDS (dat, file = "accra-hw.Rds")
utm <- 32630 # for Accra

# Get a raster grid of population density to use for the emission distribution:
url <- paste0 ("https://github.com/ATFutures/who-data/releases/download/",
               "v0.0.2-worldpop-tif-gha-npl/accra.2fpopdens.2fGHA15adj_040213.tif")
download.file (url, "accra-pop.tif", mode = "wb")
ras <- raster::raster ("accra-pop.tif") %>%
    raster::crop (raster::extent (bb)) %>%
    as ("SpatialPolygons") %>%
    st_as_sf ()

#dat <- readRDS (file = "accra-hw.Rds")
dat <- dat [dat$highway %in% c ("motorway", "trunk", "primary",
                                "secondary", "teritary"), ]

s <- streetDist (emission = 1, dist = c (1, 0, 0, 0, 0), grid = ras,
                 osm = dat, epsg = utm)
# Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
# In addition: Warning message:
# attribute variables are assumed to be spatially constant throughout all geometries

Any help appreciated!


Note also the use of the osmdata package which might be of general use within this package to very easily obtain OSM street networks.

Schuch666 commented 6 years ago

Hey @ibarraespinosa can you take a look on this error?

Schuch666 commented 6 years ago

Hi @mpadge , thanks for the feedback

Sorry for the delay in my reply, I did not receive notification of this issue.

Alternatively the lineSource function can be used to make this task in the code below with the type = "sf" or "sp" (in test phase):

sf_output <- lineSource(dat, ras, as_raster = F, type = "sf")
raster_output <- lineSource(dat, ras, as_raster = T, type = "sf", gcol = 150, grow = 150)

if you use raster, you will probably want to use the gcol and grow arguments (number of grid cols and grid rows of the output raster)

Schuch666 commented 6 years ago

@mpadge osmdata is great, it's a good idea to include this in the package

we will include this in the future in a vignette for an application outside Brazil

ibarraespinosa commented 6 years ago

This function assumed that the grid as a column "id" for each feature. Now this commit add a columns "id". Can you confirm @mpadge @Schuch666 it is fixed? Best

Schuch666 commented 6 years ago

Thanks @ibarraespinosa

The function lineSource create a id too