chrisvwn / Rnightlights

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

Using custPolyPath to download radiance using different administrative divisions #67

Closed nreguera closed 3 years ago

nreguera commented 3 years ago

Hi Chris.

I am trying to download the radiances using the following geographical layers instead of the preset GADM:

The formats of files available for the layers are the following ones:

image

I choose the shapefile format. This format contains the following files (same for each division admin0, admin1 and admin3, in the next screenshot showing the ones in the admin3 level):

image

In R (version 4.0.2) with Rstudio (version 1.4.904) and Rnightlights (version 0.2.4.5), I run this code to download the nightlights data:

image

having the custPolyPath file needed to crop the rasters in that folder that you can see in the previous screenshot (and in the same folder where all the other admin levels files are). This compressed file contains all the files I showed you previously, I haven´t modified it.

After running it, this is the ouptut and the error that I get:

image

After the previous process finishes, there are these new files in a local folder created by the process:

image

and inside the POLY_MMR_CUST-mmr_polbnda_250k_adm3_mimu.zip-SHPZIP folder there are these files:

image

It seems that it fails to create the CSV file structure to store the radiances right?

Thanks.

chrisvwn commented 3 years ago

Hi Natxo,

Thank you for trying out the custPolyPath feature and for the detail in this issue.

Yes, it seems to have gone most of the way but is unable to create the CSV file structure.

Let me try it here and get back to you.

chrisvwn commented 3 years ago

Seems the package was able to import it. I used getCtryNlData() so the error at the end is because I used the wrong admLevel which is the same layer name since it is difficult to determine the actual name.

See below: image image

Verifying the structure has been imported:

> Rnightlights:::readCtryStruct("MMR", custPolyPath = "mmr_polbnda_250k_adm3_mimu.zip")
     0_mmr_polbnda_250k_adm3_mimu area_sq_km
  1:                       Ramree   1170.124
  2:                      Toungup   4792.945
  3:                     Kyaukpyu   1168.149
  4:                      Labutta   2492.103
  5:                     Ngapudaw   3210.414
 ---                                        
326:                       Laihka   2797.747
327:                    Mongkaing   3779.654
328:                        Hseni   1241.766
329:                      Kunlong   1174.341
330:                         Muse   1981.160

Looking at the error again, it seems it is a failure to write the file due to some filesystem issue- either it was unable to create the file or the directory did not exist. Not sure what is going on there. Could you try again? If it still doesn't work try deleting the NL_STRUCT_MMR_CUST-mmr_polbnda_250k_adm3_mimu.zip-SHPZIP.csv file and try again.

nreguera commented 3 years ago

So a couple of things here:

image

image

Maybe the error came from that reason?

image

After this, I do have the CSV file structre created in the data folder, and in the polygons folder I have the same files I told you previously.

chrisvwn commented 3 years ago

Yes, that sounds like the problem. For some reason the data directory was not created first time round at installation. I am not sure what might have happened there but glad it worked when you run setupDataPath manually. If this happens again let me know so I can try to track down the problem.

For the admLevel you can run the following to list all admLevels in the polygon:

searchAdmLevel(ctryCode = "MMR", custPolyPath = "mmr_polbnda_250k_adm3_mimu.zip")

Note that to access this particular polygon you must specify the ctryCode and custPolyPath together. For custPolyPath we only look at the filename so you don't have to specify the full path subsequently. This also means that even if you use a custPolyPath with a different path but same filename it will think this is the same one as before.

nreguera commented 3 years ago

So I run that code only once before getCtryNlData?

When you said that I must specify the ctryCode and custPolyPath together, do you mean in the getCtryNlData function?

So I tried again with this code:

searchAdmLevel(ctryCode = "MMR", 
               custPolyPath = "mmr_polbnda_250k_adm3_mimu.zip")

# Download rasters (tiff) and radiance readings (csv)
nl_source <- getCtryNlData(
  ctryCode = ctry, # country
  admLevel = "admin3", # admin level
  custPolyPath = "mmr_polbnda_250k_adm3_mimu.zip",
  nlType = "VIIRS.M", # monthly values
  nlPeriods = nlRange("201204", "201212"),
  ignoreMissing = FALSE, # not ignoring missing values
  nlStats = list("mean", na.rm=FALSE) # aggregation: average 
)

And I got this error:

image

If I set the full path of in the CustPolyPath parameter I get the same error.

Maybe should I remove all the files and try it again?

chrisvwn commented 3 years ago

What I mean is whenever you are referencing this map e.g. getCtryNlData, searchAdmLevel, and any other function that has ctryCode and custPolyPath parameters you need to specify both otherwise it will consider it a different request and you may see it try to re-import the polygon or fail to recognize it.

So searchAdmLevel will give you a list of valid admLevels. Unfortunately, for custPolyPath we cannot determine the real name at import so we will find it has a different admLevel name. When I run it I get:

> Rnightlights::searchAdmLevel(ctryCode = "MMR", custPolyPath = "mmr_polbnda_250k_adm3_mimu.zip")
$mmr_polbnda_250k_adm3_mimu.zip
$mmr_polbnda_250k_adm3_mimu.zip$MMR
[1] "0_mmr_polbnda_250k_adm3_mimu"

This shows there is only 1 admLevel and it is assigned the name "0_mmr_polbnda_250k_adm3_mimu". So this is the admLevel you use in getCtryNlData, not "admin3".

nreguera commented 3 years ago

Does searchAdmLevel does create the CSV as well? And when using custPolyPath we still need to set the admLevelright?

I have run this code:

nl_source <- getCtryNlData(
  ctryCode = ctry, # country
  custPolyPath = "mmr_polbnda_250k_adm3_mimu.zip",
  admLevel = "0_mmr_polbnda_250k_adm3_mimu", # admin level
  nlType = "VIIRS.M", # monthly values
  nlPeriods = nlRange("201204", "201212"),
  ignoreMissing = FALSE, # not ignoring missing values
  nlStats = list("mean", na.rm=FALSE) # aggregation: average 
)

and I get this output with an error:

image

I do have the _NL_STRUCT_MMR_CUST-mmr_polbnda_250k_adm3mimu.zip-SHPZIP.csv file in E:/R/.Rnightlights/data and I can open it. It looks fine for me. I attach it here.

NL_STRUCT_MMR_CUST-mmr_polbnda_250k_adm3_mimu.zip-SHPZIP.zip

chrisvwn commented 3 years ago

So the main error here I think is something went wrong with tile downloads.

It seems the download site now requires registration and login. https://payneinstitute.mines.edu/eog-2/transition-to-secured-data-access/

The second error is because the data file has not been created yet due to the first error. Note the file it is complaining about is not NL_STRUCT_... it is NL_DATA_....

Let me see if I can quickly add the auth required.

chrisvwn commented 3 years ago

Hi @nreguera ,

I have been able to add a preliminary authorization bit. Could you give it a go?

You can update the package and try again.

nreguera commented 3 years ago

Hi Chris,

This is what I get when trying to download them:

image

Should we create our own credentials or it will be managed through the package?

chrisvwn commented 3 years ago

Hi,

You need to register individually at https://eogauth.mines.edu/auth/realms/master/account/ then enter the credentials in the package. The credentials will be saved in the .Rnightlights folder for future use so you only need to do this once.

nreguera commented 3 years ago

OK, done, but I got this error:

image

chrisvwn commented 3 years ago

Not sure what happened there. Could you try again now that the credentials are saved?

nreguera commented 3 years ago

Just tried again, same error, but it doesn´t ask for the credentials

nreguera commented 3 years ago

ok, wait, I just restarted the session and it looks working. hold on because i forgot to install aria2 :) I will write you later

nreguera commented 3 years ago

I think it's working now 👍 Let´s close this issue and if I see any other thing when using them we can reopen it again.

Thanks for your support Chris 🙏🏻

chrisvwn commented 3 years ago

You're welcome