dipetkov / eems

Estimating Effective Migration Surfaces
GNU General Public License v2.0
102 stars 28 forks source link

Error in rEEMSplots #40

Open rewilson75 opened 4 years ago

rewilson75 commented 4 years ago

Hello,

I am trying to work through the example to get a handle on using the program.

I had issues installing the plotting package following the instructions in the manual. But was able to successfully install the package using the following library(devtools) install_github("dipetkov/eems/plotting/rEEMSplots")

When I tried making plots with one of the provided example data sets I get the following.... Error in bitmap(paste0(plotpath, "%02d.png"), type = "png16m", res = plot.params$res, : GhostScript was not found

I have since installed GhostScript on my computer but was wondering if you could provide advice on how to get R to recognize it as I still get the error when trying to make pngs. I haven't been able to find a good solution through a google search. I am however able to create pdfs only.

Thank you

dipetkov commented 4 years ago

eems.plots generates a bunch of figures, either as PNGs (with the grDevices::bitmap function) or PDFs (with the grDevices::pdf function). The default is png.

The error is thrown by grDevices::bitmap. Try the following:

bitmap("create-png-test.png", type = "png16m",
       res = 600, units = "in",
       height = 6, width = 6)
plot(cars)
dev.off()

This should throw the same ghostscript error.

Instead you can specify the out.png = FALSE option to generate PDFs instead. You can test it first with:

pdf("create-pdf-test.pdf",
    height = 6, width = 6, onefile = FALSE)
plot(cars)
dev.off()
rewilson75 commented 4 years ago

Thank you for the response.  Switching to making pdf worked.  Thank you for your help on this issue.

I do have another question regarding the boundary file .outer. I have samples that span Beringia.  When I try to make a polygon to encompass my sampling localities across the Arctic I get "Error: Specify a valid ring habitat (a simple closed polygon)".**   I thought this is because I was going back and forth between W and E longitude and EEMS was trying to "correct" the polygon as it couldn't tell if it was going counterclockwise or not.  However,  I am able to draw a polygon if I start in Arctic Alaska and go all the way to Arctic Russia via Greenland and Europe and than back to Alaska.  So with that I am jumping between E and W longitude as well so I am not sure how to draw a polygon that goes across the Bering Strait. 

This polygon works valid habitat

I have tried multiple ways of drawing the polygon in terms of starting points (e.g. in Alaska or in Russia or in Greenland) and tried going in both directions while making sure my first and last coordinate are always the same.  I have my coords listed as Latitude Longitude. I have also tried listing as longitude latitude and it didn't make a difference.

Below is a simplified example habitat that failed.  I have also tried using the same starting point and going in the opposite direction and got the same error.  Input habitat: POLYGON((74.7931 81.1978,75.7318 84.8892,76.8957 89.1079,78.0392 104.225,77.4421 109.674,76.9751 113.893,75.7318 117.057,74.9764 120.221,74.6075 127.077,73.4471 140.436,72.8873 147.643,72.309 156.959,71.0383 168.913,69.9829 -179.837,69.8622 -172.455,71.6013 -159.095,71.6567 -149.251,71.8219 -138.88,72.1481 -121.83,71.7119 -109.525,71.6567 -98.8022,71.4341 -73.8413,70.576 -56.7905,69.6188 -45.189,68.0978 -37.103,65.0998 -36.0483,59.0291 -43.9585,59.3891 -70.3257,59.9219 -83.6851,60.4463 -105.658,60.5328 -120.072,59.6565 -133.08,56.9808 -143.275,56.4988 -164.896,57.5511 171.725,57.5511 155.729,58.7567 146.061,60.6192 138.151,63.7338 122.858,65.6853 106.686,66.679 92.0962,69.9829 80.4946,73.4471 77.3306,74.7931 81.1978))

This one doesn't Not valid

I have been making my polygons using http://www.birdtheme.org/useful/v3tool.html

Any advice or letting me know if it is even possible to use a polygon across this region would be appreciated.

Thanks

dipetkov commented 4 years ago

I think this has to do with the negative longitudes.

library("rgeos")

# I substituted POLYGON with LINESTRING so that I can use
# `rgeos::gIsRing` to check if the geometry is a ring.
# See more with `?rgeos::gIsRing`.
habitat <- "LINESTRING(74.7931 81.1978,75.7318 84.8892,76.8957 89.1079,78.0392 104.225,77.4421 109.674,76.9751 113.893,75.7318 117.057,74.9764 120.221,74.6075 127.077,73.4471 140.436,72.8873 147.643,72.309 156.959,71.0383 168.913,69.9829 -179.837,69.8622 -172.455,71.6013 -159.095,71.6567 -149.251,71.8219 -138.88,72.1481 -121.83,71.7119 -109.525,71.6567 -98.8022,71.4341 -73.8413,70.576 -56.7905,69.6188 -45.189,68.0978 -37.103,65.0998 -36.0483,59.0291 -43.9585,59.3891 -70.3257,59.9219 -83.6851,60.4463 -105.658,60.5328 -120.072,59.6565 -133.08,56.9808 -143.275,56.4988 -164.896,57.5511 171.725,57.5511 155.729,58.7567 146.061,60.6192 138.151,63.7338 122.858,65.6853 106.686,66.679 92.0962,69.9829 80.4946,73.4471 77.3306,74.7931 81.1978)"
habitat <- readWKT(habitat)

# The answer to `gIsRing` is No because
# the geometry is assumed to be planar and there are self-intersections
plot(habitat)
title(paste("gIsRing = ", gIsRing(habitat)))

# The issue is with the negative longitudes.

# Let's get the coordinates from the geometry
outer <- habitat@lines[[1]]@Lines[[1]]@coords
plot(outer, type = "l")

# The coordinates are in (lat, long) order
lat <- outer[, 1]
long <- outer[, 2]

# We can fix this by adding 360 degrees when the longitude is negative
long <- ifelse(long < 0, long + 360, long)
outer <- cbind(lat, long)
plot(outer, type = "l")

Note: Since this habitat covers such a large area, you might consider using the geodesic (great circle) distance: specify distance = greatcirc in the parameter .ini file. (Though not sure if it would make that much of a difference.)