Set Coordinates Reference Systems to a list of SpatialPolygon - transformation

I want to set the Coordinates Reference Systems to a list of SpatialPolygon called SpP
library(sp)
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
I want to give a coordinate reference system to each features in the list. CRS("+proj=longlat +datum=WGS84"

For any Spatial* object, you can set the CRS with the proj4string function.
For your example:
proj4string(SpP) <- CRS("+proj=longlat +datum=WGS84")
See ?proj4string and ?CRS for more details.

Related

RMarkdown reformatting base r plot

I'm building an r package and trying to include a plot in my package vignette. Here is the code in my vignette (with options echo=F,warnings=F):
summary_table = data.frame(n_row = rep(seq(100,1000,100),each = 2),mean = c(0.087,0.137,0.278,1.880,0.859,8.742,1.782,28.908,3.928,63.544,6.648,141.373,11.158,252.689,18.409,427.515,28.220,725.520,39.675,1024.007),sd = c(0.021,0.010,0.018,0.170,0.100,0.630,0.161,1.849,0.245,3.927,0.455,12.202,0.806,14.315,1.350,37.318,2.675,63.963,2.983,102.328),package = rep(c("TDAInference","TDA"),10))
# plot table
plot(summary_table$n_row[summary_table$package=="TDA"], summary_table$mean[summary_table$package=="TDA"], type="b",
xlim=range(summary_table$n_row), ylim=range(0,summary_table$mean+1.96*summary_table$sd),xlab = "Points in diagram",ylab = "Mean execution time (sec)",main = "Benchmarking diagram_distance function")
lines(summary_table$n_row[summary_table$package=="TDAInference"], summary_table$mean[summary_table$package=="TDAInference"], col=2, type="b")
legend(x = 200,y = 800,legend = c("TDAInference","TDA"),col = c("red","black"),lty = c(1,1),cex = 0.8)
arrows(summary_table$n_row, summary_table$mean-1.96*summary_table$sd/sqrt(10), summary_table$n_row, summary_table$mean+1.96*summary_table$sd/sqrt(10), length=0.05, angle=90, code=3)
When I run the code chunk in my script it looks like this, Desired plot, which is what I want. However, when I use devtools::build_vignettes() and view the vignette it looks like this: Reformatted plot
Any idea why this is happening and how I could fix the problem?

ggplotly tooltip for geom_text and geom_segment; does not work on Mac

I am working on revising a CRAN package. I have one function call that seems to only cause errors on Mac. It caused an error on r-release-osx-x86_64-mavericks on CRAN. I ran it on El Capitan and Mavericks and it also produced the same error on both of these Mac versions. All other systems checked by CRAN (solaris, linux, windows) did not cause an error.
I created a MWE of the function call as follows (sorry it is long to create the data). Basically, it starts with creating a ggplot2 figure using 3 data frames (edgeDF - all non-path edges, nodeDF - all non-path nodes, pathDF - edges and nodes for path of interest):
set.seed(1)
label=c()
for (i in 1:10){
label[i] = paste(sample(letters,sample(3:10, 1),replace=TRUE),collapse='')
}
nodeDF = data.frame(label = label, x = runif(10,0,10), y = runif(10,0,10))
edgeDF = data.frame()
edgeDF = rbind(edgeDF, c(x=nodeDF[1,]$x, xend=nodeDF[1,]$y, y=nodeDF[2,]$x, yend=nodeDF[2,]$y))
edgeDF = rbind(edgeDF, c(x=nodeDF[5,]$x, xend=nodeDF[5,]$y, y=nodeDF[6,]$x, yend=nodeDF[6,]$y))
colnames(edgeDF) = c("x","y","xend","yend")
r1=runif(1,0,10)
r2=runif(1,0,10)
r3=runif(1,0,10)
r4=runif(1,0,10)
label=c()
for (i in 1:3){
label[i] = paste(sample(letters,sample(3:10, 1),replace=TRUE),collapse='')
}
xend = c(r1, r2, runif(1,0,10))
xstart = c(runif(1,0,10), r1, r2)
yend = c(r3, r4, runif(1,0,10))
ystart = c(runif(1,0,10), r3, r4)
pathDF = data.frame(label=label,xstart=xstart,ystart=ystart,xend=xend,yend=yend,x=xstart,y=ystart)
edgeCol="gray84"
pathEdgeCol="seagreen"
nodeCol="black"
plotTotalImage = ggplot2::ggplot(data = nodeDF, ggplot2::aes(x = x, y = y)) +
ggplot2::geom_segment(data = edgeDF, ggplot2::aes(x=x, y=y, xend=xend, yend=yend), colour = edgeCol) +
ggplot2::geom_segment(data = pathDF, ggplot2::aes(x=xstart, y=ystart, xend=xend, yend=yend), colour = pathEdgeCol) +
ggplot2::geom_text(data = nodeDF, ggplot2::aes(x = x, y = y, label = label), colour = nodeCol)
plotTotalImage = plotTotalImage + ggplot2::geom_text(data = pathDF,ggplot2::aes(x = x, y = y, label = label), fontface= "bold")
I call geom_segment to create the edges (gray ones are off path, green ones are on path). I call geom_text to create node labels (non-bold ones are off path, bold ones are on path). This successfully creates a static image.
At this point, I want to use plotly to add interactivity to the plot. I do not want information to hover for segments; I only want information to hover for node labels. When the mouse hovers over a node label, I want the x value and the label name to be displayed.
The following code works as intended on Windows:
animatePlotTotalImage <- plotly::plotly_build(plotly::ggplotly(plotTotalImage, tooltip = c("x", "label")))
animatePlotTotalImage$data[[1]]$hoverinfo <- "none"
animatePlotTotalImage$data[[2]]$hoverinfo <- "none"
animatePlotTotalImage$data[[3]]$hoverinfo <- c("x+text")
animatePlotTotalImage$data[[4]]$hoverinfo <- c("x+text")
animatePlotTotalImage
However, on Mac, it results in the following Error:
Error in `*tmp*`[[2]] : subscript out of bounds
I am unsure how to go about solving this problem. Any ideas or suggestions would be greatly appreciated!
I might be wrong but I think you have $x missing from your statements:
This works for me (mac Sierra):
animatePlotTotalImage <- plotly::plotly_build(plotly::ggplotly(plotTotalImage, tooltip = c("x", "label")))
animatePlotTotalImage$x$data[[1]]$hoverinfo <- "none"
animatePlotTotalImage$x$data[[2]]$hoverinfo <- "none"
animatePlotTotalImage$x$data[[3]]$hoverinfo <- c("x+text")
animatePlotTotalImage$x$data[[4]]$hoverinfo <- c("x+text")

how to set the scale to logarithmic and linear when i right click on the particular figure in GUI matlab

I have created a Gui in matlab which has three axes and i would like to change the scale to log and linear by right click on the particular figure and select the scale options(1.logarithmic 2.linear). I tried using the UIcontextmenu. But i didn't get the result.
firstcycle_mass = handles.Data1{1,1}(1:6392,1);
firstcycle_ioncurrent = handles.Data1{1,2}(1:6392,1);
replace_with_dot_firstcycle_mass = strrep(firstcycle_mass, ',','.');
X1 = str2double(replace_with_dot_firstcycle_mass);
replace_with_dot_firstcycle_ioncurrent = strrep(firstcycle_ioncurrent, ',','.');
Y1 = str2double(replace_with_dot_firstcycle_ioncurrent);
axes(handles.axes1);
plotline= plot(X1,Y1,'r');
xlabel('Mass [amu]');
ylabel('Ion current [A]');
c=uicontextmenu;
plotline.UIContextMenu = c;
% Create menu items for the uicontextmenu
m1 = uimenu(c,'Label','logarithmic','Callback',#setlinestyle);
m2 = uimenu(c,'Label','linear','Callback',#setlinestyle);
function setlinestyle(source,callbackdata)
switch source.Label
case 'logarithmic'
set(gca,'yscale','log')
case 'linear'
set(gca,'yscale','linear')
end
end

Reverse image search implementation

I am currently trying to make a site which will contain several images with patterns and shapes (Lets say few squares and circles of various colors and shape in each picture). And I am aiming to provide the user a way to upload their images of the pattern and do a reverse image search to check whether similar pattern image already exists in my site or not. So is there any way to implement the same, either by custom code or by using any third party api/widgets etc?
Hi Ashish below is a matlab code for a function which generates signature of a particular binary object's surface, which is nearly size dependent, you can use this concept for matching a shape on different scale.
function sig = signature(bw,prec)
boundry = bwboundaries(bw);
xy = boundry{1};
x = xy(:,1);
y = xy(:,2);
len = length(x);
res = (len/prec);
re = rem(res,2);
if re
res = ceil(res);
end
indexes = 1:res:len;
xnew = x(indexes);
ynew = y(indexes);
cx = round(mean(xnew));
cy = round(mean(ynew));
xn = abs(xnew-cx);
yn = abs(ynew-cy);
sig = (xn.^2+yn.^2);
sig = sig/max(sig);
Following is the example of how to use signature function:
clc
clear all
close all
path = 'E:\GoogleDrive\Mathworks\irisDEt\shapes';
im1 = imread([path,'\3.png']);
gray1 = ((im1));
scales = [1,2,3,4];
gray1 = im2bw(gray1);
for i = 1:length(scales)
im = imresize(gray1,scales(i));
sig = signature(im,25);
figure,plot(sig)
fra = getframe();
image = frame2im(fra);
imwrite(image,['E:\GoogleDrive\Mathworks\irisDEt\shapes\',num2str(i),'.png'])
end
following is the test image and its signature for changing in size od images which looks similar in shape.
All above signatures are generated by the code given above.

How do you map Touch Joysticks in Bluestack?

Ive been trying to setup Bluestack so I can play my phone Terraria saves on my computer with a mouse and keyboard. I own the PC version as well but I just want to use my Pc to renovate a big house as it should be faster with a keyboard and mouse. Ive found Aurora 2 and it was helpful but it has no more description on how to do more complicated inputs then bluestack itself. I've made one setup and it kinda works so I get the just I just don't undersatand a few small details. My current config file looks like this:
#Terraria
#Aurora 2 keymapping tool
[origin]
Author = Dracrius
Rating = Label2
[keys]
W = Tap (8,69)
S = Tap (8,92)
D = Tap (14,80)
A = Tap (1,80)
P = Tap (98,3)
Z = Tap (47,5)
1 = Tap (3,5)
[guidance]
W =
S =
D =
A =
P =
Z =
1 =
[gamepad]
LeftStick_Up = W
LeftStick_Down = S
LeftStick_Left = A
LeftStick_Right = D
Y = Z
B = 1
A = 1
[opensensor]
Mode = Touch1
Now it all works and i can switch items or pause but is i try to use the dpad to walk left and down work because it's the edge of the screen but up and right do not. They both just center the joystick where i set the up and right buttons to. Now I know this is because of how the terraria touch stick works. You have to touch the screen for the joysticks to even show up (where ever you touch) and then drag in the direction you'd like to move. So how do I tell Bluestack to treat it as such? I know lots of touch games use this style of onscreen control so it should be do able but I can't find a description on how or any of the finer details of bluestack key mapping. These are parts of the Template bluestack gives you that I think may be related or I don't understand enough to know!
[OpenSensor]
#
# First, specify a mode for the app.
#
#Mode = TapA *Tap vs Touch?*
#Mode = TapB *What's the difference?*
#Mode = Touch1
#Mode = Touch2 *What's the difference?*
#Mode = Touch1TapA *What does this mean?*
#Mode = Touch1TapB *What's the difference?*
#Mode = Swipe
#Mode = Tilt
#Mode = TiltTapA *What's TiltTap?*
#Mode = TiltTapB *What's the difference?*
#Mode = TiltTouch1 *What's TiltTouch?*
#
# Specify mode specific guidance. *WHATTT?*
#
#Tilt = STilt
#SwipeTap = SSwipeTap *Maybe this has something to do with it!*
#Area1 = (0,33,100,33) STouchArea *I think this has something to do with it!*

Resources