chrisvwn / Rnightlights

R package to extract data from satellite nightlights.
GNU General Public License v3.0
47 stars 14 forks source link

Erro in rgdal #73

Closed fabinho80 closed 3 years ago

fabinho80 commented 3 years ago

Hi there,

I'm having some problem to run nightlights data from Brazil based on Ishara Data Blog. How can I sove the following error: Error in rgdal::ogrListLayers(dsn = path.expand(path = getPolyFnamePath(ctryCode = ctryCodes, : Cannot open data source

Thanks for your time

chrisvwn commented 3 years ago

Hi,

Please post the code you are running and the full output.

fabinho80 commented 3 years ago

Code: library(Rnightlights) library(lubridate) library(reshape2)

(Optional performance enhancement if you have aria2c and gdal installed)

pkgOptions(downloadMethod = "aria", cropMaskMethod = "gdal", extractMethod = "gdal", deleteTiles = TRUE)

Optional performance enhancement. If extractMethod="rast" you can specify the number of

CPU cores to use in parallel

pkgOptions(extractMethod = "rast", numCores=4)

ctryCodeToName("BRA")

ctry <- "BRA" #replace to run for any other country

download and process monthly VIIRS stats at the highest admin level

highestAdmLevelStats <- getCtryNlData(ctryCode = ctry, admLevel = "highest", nlType = "VIIRS.M", nlPeriods = nlRange("201509", "201512"), nlStats = list("sum",na.rm=TRUE), ignoreMissing=FALSE)

Optionally plot the data

library(ggplot2) library(plotly)

melt the stats into key-value format for easy multi-line plotting with ggplot2

highestAdmLevelStats <- melt(highestAdmLevelStats, id.vars = grep("NL_", names(highestAdmLevelStats), invert=TRUE), variable.name = "nlPeriod", value.name = "radiancesum")

extract date from the NL col names

highestAdmLevelStats$nlPeriod <- substr(highestAdmLevelStats$nlPeriod, 12, 17)

format period as date

highestAdmLevelStats$nlPeriod <- ymd(paste0(substr(highestAdmLevelStats$nlPeriod, 1,4), "-",substr(highestAdmLevelStats$nlPeriod, 5,6), "-01"))

plot 2nd admin level sums for the year

g <- ggplot(data = highestAdmLevelStats, aes(x=nlPeriod, y=radiancesum, color=highestAdmLevelStats[[2]])) + scale_x_date(date_breaks = "1 month", date_labels = "%Y-%m")+ geom_line()+geom_point() + labs(color = names(highestAdmLevelStats)[2]) + xlab("Month") + ylab("Sum of Radiances") + ggtitle(paste0(unique(names(highestAdmLevelStats)[2]), " sum of radiances for ", ctry))

print(g)

quick conversion to interactive map with plotly

ggplotly(g)

Full output

library(Rnightlights) library(lubridate) library(reshape2) ctryCodeToName("BRA") BRA "Brazil" ctry <- "BRA" #replace to run for any other country

download and process monthly VIIRS stats at the highest admin level

highestAdmLevelStats <- getCtryNlData(ctryCode = ctry,

  • admLevel = "highest",
  • nlType = "VIIRS.M",
  • nlPeriods = nlRange("201509", "201512"),
  • nlStats = list("sum",na.rm=TRUE),
  • ignoreMissing=FALSE) 2021-04-12 19:37:25: NlRange autodetected nlType: VIIRS.M Error in rgdal::ogrListLayers(dsn = path.expand(path = getPolyFnamePath(ctryCode = ctryCodes, : Cannot open data source

    Optionally plot the data

    library(ggplot2) library(plotly)

    melt the stats into key-value format for easy multi-line plotting with ggplot2

    highestAdmLevelStats <- melt(highestAdmLevelStats,

  • id.vars = grep("NL_", names(highestAdmLevelStats),
  • invert=TRUE),
  • variable.name = "nlPeriod",
  • value.name = "radiancesum") Error in melt(highestAdmLevelStats, id.vars = grep("NL_", names(highestAdmLevelStats), : objeto 'highestAdmLevelStats' não encontrado

    extract date from the NL col names

    highestAdmLevelStats$nlPeriod <- substr(highestAdmLevelStats$nlPeriod, 12, 17) Error in substr(highestAdmLevelStats$nlPeriod, 12, 17) : objeto 'highestAdmLevelStats' não encontrado

    format period as date

    highestAdmLevelStats$nlPeriod <- ymd(paste0(substr(highestAdmLevelStats$nlPeriod, 1,4),

  • "-",substr(highestAdmLevelStats$nlPeriod, 5,6), "-01")) Error in substr(highestAdmLevelStats$nlPeriod, 1, 4) : objeto 'highestAdmLevelStats' não encontrado

    plot 2nd admin level sums for the year

    g <- ggplot(data = highestAdmLevelStats,

  • aes(x=nlPeriod, y=radiancesum,
  • color=highestAdmLevelStats[[2]])) +
  • scale_x_date(date_breaks = "1 month", date_labels = "%Y-%m")+
  • geom_line()+geom_point() + labs(color = names(highestAdmLevelStats)[2]) +
  • xlab("Month") +
  • ylab("Sum of Radiances") +
  • ggtitle(paste0(unique(names(highestAdmLevelStats)[2]), " sum of radiances for ", ctry)) Error in ggplot(data = highestAdmLevelStats, aes(x = nlPeriod, y = radiancesum, : objeto 'highestAdmLevelStats' não encontrado print(g) Error in print(g) : objeto 'g' não encontrado

    quick conversion to interactive map with plotly

    ggplotly(g) Error in ggplotly(g) : objeto 'g' não encontrado

chrisvwn commented 3 years ago

Hi,

So this looks like 2 problems. First is a problem accessing the polygons and this may be due to the gadm site being down. Second problem here is the extraction of the date from the stats column name. This example needs to be updated to match the latest stat column names. You can probably do this manually or try using nlPeriodToDate instead.

Please try again and see if the first error goes away. Then if you need help with the second part we can discuss.

chrisvwn commented 3 years ago

Closing this issue for now. Please reopen if the resolution did not work above did not work.