inbo / fish-tracking

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

Fix 79 #80

Closed damianooldoni closed 8 months ago

damianooldoni commented 8 months ago

This PR should fix #79. The error was due to a mix of polygons (row 1) and multipolygons (row2) in ws_bpns area. Extracting coordinates of mixed elements (called collections) is not allowed as they have different (number of) columns. But we were interested only in first two columns (the X and Y columns) so the solution is to extract coordinates for each row and then binding the rows together via rbind as actually we already did for binding the coordinates of two study areas.

I could run the code in prepare_dataset.R:

projections.locations.receivers <- find.projections.receivers(
  shape.study.area = michimit,
  receivers = locations.receivers,
  projection = coordinate_epsg,
  shape.study.area2 = ws_bpns, 
  shape.study.area_merged = study.area
)

Let me know if you also can and if results make sense 🔍

damianooldoni commented 8 months ago

Probably something for another issue, still found while running the script prepare_dataset.R on this branch.

One receiver, s-9a, is not projected on the line: see screenshot below.

image

This results in Inf values while calculating distances of course. Something strange occurring in gidstance::dist2Line function in find.projections.receivers(), I am afraid. I will 🔍 further

damianooldoni commented 8 months ago

Ok, I think I got it. I am now using terra::nearest() function instead of geosphere::dist2Lines() to find the projection of the receivers. This has multiple benefits:

  1. no need to transform data to lat/lon (WGS84, EPSG 4326) behind the screen and transform it back to original planar CRS before returning the output
  2. the code is incredibly faster than before as terra::nearest() works in x-y Euclidean space

Notice that the original projection is not needed anymore and so the argument projection has been removed. The modified find.projections.receivers() requires a planar CRS, which is actually always the case in this analysis. If not, a conversion is needed before using the function.

Screenshot about the wrong, now correct, projection of the receivers: image

PieterjanVerhelst commented 8 months ago

Issue solved 👏 !