chrisvwn / Rnightlights

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

Unable to extract district level nightlight information of India #40

Closed prashgeo87 closed 4 years ago

prashgeo87 commented 4 years ago

Hi Chrisvwn, Thanks for the coding... I am trying to replicate your mentioned codes in R software. But I am getting the following error,

install.packages(“Rnightlights”)

install.packages("lubridate")

install.packages("reshape2")

install.packages("ggplot2")

install.packages("plotly")

library(Rnightlights) library(lubridate) library(reshape2)

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

CPU cores to use in parallel

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

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

download and process monthly VIIRS stats at the highest admin level

highestAdmLevelStats <- getCtryNlData(ctryCode = "IND",

  • admLevel = "gadm36_IND_2",
  • nlType = "VIIRS.M",
  • nlPeriods = nlRange("201801", "201812"),
  • nlStats = list("sum","median",na.rm=TRUE),
  • ignoreMissing=FALSE) 2019-10-09 17:36:48: NlRange autodetected nlType: VIIRS.M Processing missing data: IND:VIIRS.M:VCMCFG:201801:sum, VIIRS.M:VCMCFG:201802:sum, VIIRS.M:VCMCFG:201803:sum, VIIRS.M:VCMCFG:201804:sum, VIIRS.M:VCMCFG:201805:sum, VIIRS.M:VCMCFG:201806:sum, VIIRS.M:VCMCFG:201807:sum, VIIRS.M:VCMCFG:201808:sum, VIIRS.M:VCMCFG:201809:sum, VIIRS.M:VCMCFG:201810:sum, VIIRS.M:VCMCFG:201811:sum, VIIRS.M:VCMCFG:201812:sum. This may take a while. Note: Set 'ignoreMissing=TRUE' to return only data found or 'ignoreMissing=NULL' to return NULL if not all the data is found 2019-10-09 17:36:48: START PROCESSING: ctryCodes=IND, admLevels=list(IND = "gadm36_IND_2"), nlTypes=VIIRS.M, configNames=VCMCFG, multiTileStrategy=all, multiTileMergeFun=mean, removeGasFlares=TRUE, nlPeriods=c("201801", "201802", "201803", "201804", "201805", "201806", "201807", "201808", "201809", "201810", "201811", "201812"), nlStats=list(list("sum", "median", na.rm = TRUE)), custPolyPath=NULL, gadmVersion=3.6, gadmPolyType=shpZip, downloadMethod=auto, cropMaskMethod=rast, extractMethod=rast 2019-10-09 17:36:48: Downloading country polygons ... 2019-10-09 17:36:48: Downloading polygon: IND 2019-10-09 17:36:48: Downloading ctry poly: IND 2019-10-09 17:36:48: Downloading ctry shpZip: IND 2019-10-09 17:36:48: Polygon dir for IND:3.6 already exists 2019-10-09 17:36:48: Downloading country polygons ... DONE 2019-10-09 17:36:48: PROCESSING nlType:VIIRS.MconfigName: VCMCFG nlPeriod:201801 2019-10-09 17:36:48: Checking tiles required for VIIRS.M 201801 2019-10-09 17:36:48: IND: Stats missing. Adding tiles 2019-10-09 17:36:48: numTiles: 1, Required tiles: 75N060E 2019-10-09 17:36:48: Cropped raster C:\Users\Prashant\Documents/.Rnightlights/outputrasters/NL_IND_VIIRS.M_201801_VCMCFG-MTSALL-MEAN-RGFT_GADM-3.6-SHPZIP.tif already exists. Skipping tile download 2019-10-09 17:36:48: ProcessNlCountry: ctryCode=IND, admLevel=gadm36_IND_2, nlType=VIIRS.M, configName=VCMCFG, multiTileStrategy=all, multiTileMergeFun=mean, removeGasFlares=TRUE, nlPeriod=201801, nlStats=list(list("sum", "median", na.rm = TRUE)), downloadMethod=auto, cropMaskMethod=rast, extractMethod=rast, gadmVersion=3.6, gadmPolyType=shpZip, custPolyPath=NULL**** 2019-10-09 17:36:48: Check for existing data file 2019-10-09 17:36:48: Load polygon layer for crop 2019-10-09 17:36:49: Data file not found. Creating ... 2019-10-09 17:36:52: Data file not found. Creating ... DONE 2019-10-09 17:36:52: Begin processing 201801 2019-10-09 17:36:52: C:\Users\Prashant\Documents/.Rnightlights/outputrasters/NL_IND_VIIRS.M_201801_VCMCFG-MTSALL-MEAN-RGFT_GADM-3.6-SHPZIP.tif already exists 2019-10-09 17:36:52: Begin extracting data from the raster
    |=============================================================================================================================================================| 100%Error in { : task 1 failed - "invalid 'type' (closure) of argument"

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), : object 'highestAdmLevelStats' not found

extract date from the NL col names

highestAdmLevelStats$nlPeriod <- substr(highestAdmLevelStats$nlPeriod, 12, 17) Error in substr(highestAdmLevelStats$nlPeriod, 12, 17) : object 'highestAdmLevelStats' not found

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) : object 'highestAdmLevelStats' not found

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, : object 'highestAdmLevelStats' not found

print(g) Error in print(g) : object 'g' not found

quick conversion to interactive map with plotly

ggplotly(g)

Please help me solve this issue...

Thanks and Regards

chrisvwn commented 4 years ago

Hi @prashgeo87, you're welcome.

This looks like a problem with the way the nlStats are defined. You have:

nlStats=list(list("sum", "median", na.rm = TRUE))

which is translated as a call to the sum function with arguments median and na.rm=TRUE which is incorrect. For multiple nlStats specify a list with each function in its own sub-list with corresponding arguments. Try this instead to calculate mean and median:

nlStats = list(list("sum", "na.rm = TRUE"), list("median", na.rm = TRUE))
prashgeo87 commented 4 years ago

Hi Chrisvwn, Thanks a lot, I have updated the code but now I am stuck with new error, please help me.

Optionally plot the data

library(ggplot2) library(plotly)

Attaching package: ‘plotly’

The following object is masked from ‘package:ggplot2’:

last_plot

The following object is masked from ‘package:stats’:

filter

The following object is masked from ‘package:graphics’:

layout

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), : object 'highestAdmLevelStats' not found

extract date from the NL col names

highestAdmLevelStats$nlPeriod <- substr(highestAdmLevelStats$nlPeriod, 12, 17) Error in substr(highestAdmLevelStats$nlPeriod, 12, 17) : object 'highestAdmLevelStats' not found

extract date from the NL col names

highestAdmLevelStats$nlPeriod <- substr(highestAdmLevelStats$nlPeriod, 12, 17) Error in substr(highestAdmLevelStats$nlPeriod, 12, 17) : object 'highestAdmLevelStats' not found

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) : object 'highestAdmLevelStats' not found

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, : object 'highestAdmLevelStats' not found

print(g) Error in print(g) : object 'g' not found

quick conversion to interactive map with plotly

ggplotly(g)

Thanks and Regards

chrisvwn commented 4 years ago

Please post the whole output beginning from the getCtryNlData command

prashgeo87 commented 4 years ago

Hi Chrisvwn, I have attached the codes in txt format, Looking forward to your support. Thanks and Regards, Code_Share.txt

chrisvwn commented 4 years ago

Hi @prashgeo87 this looks like the main error here:

Error in melt(highestAdmLevelStats, id.vars = grep("NL_", names(highestAdmLevelStats),  : could not find function "melt"

Check that you have reshape2 installed and run library(reshape2) first.

chrisvwn commented 4 years ago

Closing this issue. Please reopen if you are still having a problem.