inbo / fish-tracking

🐟 Collection of scripts for processing and analysing fish tracking data
3 stars 0 forks source link

Add validate_waterbody() function and use it before gUnion #71

Closed damianooldoni closed 3 years ago

damianooldoni commented 3 years ago

This PR will fix #70.

A function called validate_waterbody() has been written in order to check whether the water body shapefile is has a valid geometry. The function works by converting the sp Spatial object to sf. The sf functions st_is_valid() is used to check the validity and st_make_valid() to reconstruct a valid geometry in case previous function returns FALSE. A sp spatial object is returned back.

I updated prepare_dataset() too by adding the validation step wherever water bodies are combined. Maybe we could think to move the validation within the load.shapefile() when moving this routine to a public package.

PieterjanVerhelst commented 3 years ago

Thanks 👍 ! Indeed, I would argue as well to add this to load.shapefile(). When running the code, R stops working at the code

projections.locations.receivers <- find.projections.receivers(
  shape.study.area = study.area,
  receivers = locations.receivers,
  projection = coordinate.string
)

image

Do you have the same problem?

damianooldoni commented 3 years ago

Some questions:

  1. Which study.area are you using?
  2. Does it stop working for any study.area?
  3. Have you already tried to close and restart RStudio? If not try it again and let me know.
damianooldoni commented 3 years ago

Update: no problems at all while projecting receivers on study.area, where study.area is a combination of rivers, nete, westerschelde and sea and the receivers are the ones from receivernetwork_2015_phd_verhelst_eel.csv.

PieterjanVerhelst commented 3 years ago

Indeed, forgot to mention the used data. I get the error when running the code for the shapefiles pbarn_freshwater.shp and ws_bpns. The receiver locations that belong to these rivers are in receivernetwork_2015_phd_verhelst_eel.csv.

I did restart Rstudio a few times, but it keeps running stuck.

damianooldoni commented 3 years ago

I found a very stupid typo which I solved now. I don't know if it has to do with your bug. Can you please check it again?

PieterjanVerhelst commented 3 years ago

Nope, still the same. Does R also shut down with you, btw?

damianooldoni commented 3 years ago

I get this error:

Error: C stack usage  15923648 is too close to the limit

which seems something related to recursion issues: https://stackoverflow.com/questions/14719349/error-c-stack-usage-is-too-close-to-the-limit

I am investigating further. I have R 4.1.0. Do you?

PieterjanVerhelst commented 3 years ago

I have 4.0.3 ("Bunny-Wunnies Freak Out"), but will update.

damianooldoni commented 3 years ago

Meanwhil, I found that gUnion of a spatialLineDataFrame and a spatialPolygonDataFram is an object of class SpatialCollections. This makes things horribly bad as a spatial collection is not convertible to sf and many things are done in sf.

My proposal is to combine lines and polygons using st_union() from sf. But then we cannot return back to sp anymore! This means I have to check whether we can do all what we do after this step using sf objects instead of sp objects. I think so as we convert to rasters and we use rasters afterwards. Conversion to rasters seems ok

If you agree, we can merge this PR as this issue has nothing to do with #70 which has been tackled by validating the geometry. I mentioned this PR in a new issue: https://github.com/inbo/fish-tracking/issues/72: solving it will be moved to a new PR

PieterjanVerhelst commented 3 years ago

Agreed to merge the pull request and open a new one for the union-conflict.

damianooldoni commented 3 years ago

Can you then officially approve this PR and merge it? Thanks.