inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

mexdolphins data: polygon seems incorrect #53

Closed dill closed 3 years ago

dill commented 5 years ago

The polygon in mexdolphin$ppoly seems wrong to me.

In dsm the polygon has 45 vertices. In inlabru it has 158. This in itself is of course not really an issue, but the polygon vertices seem to loop back on themselves. Perhaps it's easier to show this in graphics/code:

data(mexdolphin, package="inlabru")
# avoid sp stuff...
poly <- mexdolphin$ppoly@polygons[[1]]@Polygons[[1]]@coords

plot(poly[1:79,], type="l", asp=1)

screen shot 2018-11-20 at 14 45 51

Okay, fine, then we can append the first vertex as the last and make a polygon, but what about the other 79 points?

plot(poly[80:158,], type="l", asp=1)

screen shot 2018-11-20 at 14 47 18

We get almost but not quite the same thing again. Plotting everyting and zooming-in, we see there is a weird overlap:

plot(poly, type="l", asp=1, xlim=c(-50,0), ylim=c(-1500, -1450))

screen shot 2018-11-20 at 14 48 59

Apologies if there is some INLA-y reason that you do this (if there is I'm keen to know!), it's caused havoc for me doing point-in-polygon using mgcv::inSide this afternoon.

finnlindgren commented 5 years ago

Looks like the whole polygon is duplicated? I have a non functioning laptop at the moment. Can you plot the x and y coordinates a sfunctions of index, so one can see if the loops go the same direction or if the duplicate is in the opposite direction? I’m not sure where the poly data originated.

dill commented 5 years ago

Well I think they might have come from me originally... but I don't know how they went from the 45 vertex versions to these... Perhaps something strange happened during projection? (Version in dsm is in lat/long.)

As requested:

par(mfrow=c(1,2))
plot(poly[,1], ylab="x")
plot(poly[,2], ylab="y")

screen shot 2018-11-20 at 15 09 23

finnlindgren commented 5 years ago

It appears that the code that converted the data from dsm is here: https://github.com/cran/inlabru/blob/master/R/data.mexdolphin.R Unfortunately it can’t be run anymore due to a removed function, but the code might reveal the source of the problem. In any case, it’s clear that the polygon info is wrong!

finnlindgren commented 5 years ago

This is the culprit: polyloc = as.data.frame(mexdolphin$mesh$loc[mexdolphin$mesh$segm$int$idx,c(1,2)]) It assumed incorrectly that segm$int$idx would be a vector, but it’s normally a two-column matrix.

The increase from 45 to 79 was due to the dsm polygon being used as input to the mesh refinement, and then extracted from the resulting mesh.

dill commented 5 years ago

Aha! Okay, I was going to volunteer to fix this but given that the function is missing I'll shy away from that task...

On 20/11/2018 15:52, Finn Lindgren wrote:

This is the culprit: |polyloc = as.data.frame(mexdolphin$mesh$loc[mexdolphin$mesh$segm$int$idx,c(1,2)])| It assumed incorrectly that segm$int$idx would be a vector, but it’s normally a two-column matrix. The increase from 45 to 79 was due to the dsm polygon being used as input to the mesh refinement, and then extracted from the resulting mesh.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fbachl/inlabru/issues/53#issuecomment-440322846, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAHodcSkj7vons-PLB_g0LXppwau4-mks5uxCVGgaJpZM4YrQyZ.