Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include recipient_region attribute in geojson? #28

Open
cc50liu opened this issue Apr 10, 2023 · 2 comments
Open

Include recipient_region attribute in geojson? #28

cc50liu opened this issue Apr 10, 2023 · 2 comments

Comments

@cc50liu
Copy link

cc50liu commented Apr 10, 2023

I'd like to subset the data to projects in Africa, and am struggling to do so. Would you consider including a recipient_region attribute in the geojson so users can quickly subset the data based on that?

Here's why I'm asking:
If I use the dataset in the .xlsx format, there is a Recipient Region field I can use to easily limit observations to Africa. Then, I need to convert the geoJSON URL DL fields into a sf object, ideally with one observation for each geoJSON URL DL. Doing this in R works well for only one observation (using either geojson_sf(geojson_url_dl) or read_sf(geojson_url_dl)), but I am struggling to do this for more than one observation. The closest I've gotten is this:
two_proj_sf <- lapply(as.list(two_proj$geojson_url_dl),function(x) geojson_sf(x))
which creates a list. When I attempt to convert the list into a dataframe using this code
two_proj_sf <- bind_rows(two_proj_sf, .id="column_label")
I receive an error because the implementation start year was interpreted as a character in the first instance and as a double in the second instance.

Error in bind_rows():
! Can't combine 1$Implementation Start Year and 2$Implementation Start Year .

Since I was struggling to do this using the data you provided in the .xlsx format, I switched to reading your complete geojson file and filtering on the attributes there instead. However, those attributes do not include a Recipient Region, so I need to figure out how to get a list of all the African countries to limit based on Recipient, or to get a geographic representation of the African continent to subset on that.

I looked through your examples, but didn't notice any R code examples there.

I imported the geoBoundariesCGAZ_ADM0.topojson file also available from AidData to get country boundaries, but that also does not have regions, so I would need to find a definitive list of African country ISO codes to be able to subset using those boundaries.

It occurs to me that I could use the .xlsx data to create a list of project ids I want to include and then subset the geojson data by those project ids. I'll work on that, but in the meantime also wanted to see if you have other suggestions or ideas, or could consider including the recipient region in the geojson file.

@sgoodm
Copy link
Member

sgoodm commented Apr 10, 2023

@cc50liu, I'd suggest one of two approaches for achieving your goal with the current data format:

1 - Join the raw data

  • Load the full tabular data (xlsx file) into a dataframe and the full geospatial data (complete GeoJSON file) into a spatial dataframe
  • Filter the tabular data by region
  • Remove redundant fields from the spatial dataframe (i.e., all but project id and spatial feature)
  • Join the dataframe and spatial dataframe on project id (inner join)

2 - Dynamically load individual GeoJSONs and manage fields

  • You could utilize the same approach you attempted (two_proj_sf <- lapply(as.list(two_proj$geojson_url_dl),function(x) geojson_sf(x))) but with a custom function to manage the GeoJSON attributes
  •  prepare_feature <- function(url) {
         raw_sdf <- geojson_sf(url)
         sdf <- subset(raw_sdf, select = c(<geom_field>, <id_field>) )
         return(sdf)
     }
    
  • two_proj_sf <- lapply(as.list(two_proj$geojson_url_dl), prepare_feature(x))

I'll leave this issue open so we can incorporate region into the fields included with the GeoJSONs for the next release.

@cc50liu
Copy link
Author

cc50liu commented Apr 10, 2023

Thanks for your detailed answer @sgoodm, especially for the example code and ideas on how to move forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants