ropensci / osmdata

R package for downloading OpenStreetMap data
https://docs.ropensci.org/osmdata
314 stars 45 forks source link

RStudio is unable to find proj.db file when using trim_osmdata() #326

Closed mlucassc closed 1 year ago

mlucassc commented 1 year ago

I am trying to trim all data points that are not within a certain district of the city I am researching, but when R runs the trim_osmdata() function, I get thousands of error messages saying that the proj.db file cannot be found.

proj_create_operation_factory_context: Cannot find proj.db pj_obj_create: Cannot find proj.db pj_obj_create: Cannot find proj.db proj_as_proj_string: Cannot find proj.db proj_as_proj_string: Cannot find proj.db

This error repeats thousands of times. When it's done trimming, I check to see if it still works, there is a plausible amount of datapoints left and the district appears to be trimmed, but it takes 4 to 5 minutes and sometimes R just freezes. Also, I am not sure if the error message comes up for every datapoint or just some and they are not trimmed.

So far, I have tried setting the PROJ_LIB environment variable to each of the six proj.db files I found on my system, but this did not solve the problem. Screenshot of all proj.db file locations

I also tried updating Rtools and installing GDAL using the OSGeo4W installer, which did not solve the problem either. When I run the sample code given in the trim_osmdata() documentation, I get the same error message.

Interestingly I also could not get other peoples code running, the error is not bound to my computer, I get the same error messages on different PCs with the same code and the example code given in the package documentation.

I am not super expierenced with how all of the code works on a low level and I am not even sure what the proj.db file does, so please be nice if I did something stupid.

Here's my code:

library(osmdata)
library(sf)
library(tidyverse)
library(geosphere)
library(ggmap)

##getting the district polygon
lindenhofbb <- getbb("Mannheim Neckarau", format_out = "polygon")
##Query for every building for the district, but not yet trimmed to the actual borders 
mb <- getbb("Mannheim Neckarau") %>% 
  opq() %>% 
  add_osm_feature(key = 'building') %>% 
  osmdata_sf()
##trimming the data i got from the query 
mbtrimmed <- trim_osmdata(dat = mb, bb_poly = lindenhofbb)

##turining the list into an df for further analysis 
mbdf <- data.frame(mbtrimmed$osm_points)

I am also using ggmap() and geomsf() to create a map with all the data points plotted to check if the trimming was successful.

Do I need to install another package? I found PROJ and proj4, but i am unsure what they do.

If you have any ideas as to why this is happening, please share your wisdom :)

jmaspons commented 1 year ago

Perhaps you can use the new features in osmdata 0.2 that allows server side filtering by relation id (format_out = "osm_type_id")

library(osmdata)

mb <- getbb("Mannheim Neckarau", format_out = "osm_type_id") %>% 
  opq() %>% 
  add_osm_feature(key = 'building') %>% 
  osmdata_sf()

Anyway, your code works for me. Thus, it can be a problem specific for your systems. Which OS are you working on?

mlucassc commented 1 year ago

The server side fitering works !!! Thank you so much, this was a huge problem for my bachelors thesis!!

I am working on Windows 11, but I tried it on the University computers aswell and they run Windows 10.