Incorrect Plot when plotting SpatialPointsDataFrame - r

I am new to R and am having trouble plotting a SpatialPointsDataFrame, with eventual hopes of creating minimum convex polygons. I've been trying for a few days but can't find anything to fix this problem.
I load my excel data as TXT. File has 3 columns (Latitude, Longitude, ID) and 549 rows of observations. Then I enter the following code:
# Create coordinates variable
coords <- cbind(LAT = as.numeric(as.character(multi$LAT)), LONG = as.numeric(as.character(multi$LONG)))
# Create the SpatialPointsDataFrame
multi_SPDF <- SpatialPointsDataFrame(coords, data = data.frame(multi$ID), proj4string = CRS("+init=epsg:4326"))
#Plot the points
plot(multi_SPDF)
When I enter this, it produces a plot that looks like this:
I made this code from a similar code found at this link: http://www.alex-singleton.com/R-Tutorial-Materials/7-converting-coordinates.pdf
If anyone is able to help me to make this work I would really appreciate it. Hopefully I provided all of the necessary information.
EDIT
In an attempt to provide a reproducible example, I extracted the head of the data to copy into the comment, as follows:
LAT LONG ID
1 -41.30853 174.7342 7
2 -41.30481 174.7353 6
3 -41.30681 174.7363 7
4 -41.30660 174.7360 10
5 -41.31400 174.7329 10
6 -41.31059 174.7350 6
When I ran the above code on these 6 rows alone, it produced a plot with points distributed vertically and horizontally (exactly what I wanted!)
However, the same code still does not work on my entire data set. So I think the problem may be in my excel file and not my code.

Related

Spatstat: Creating a pixel image object from a database I can't transform into a matrix

Hey people of Stackoverflow!
I'm trying to see which factors increases the incidence of fire caused by lightnings, but I'm having problems creating a pixel image object using the im() function of the library spatstat.
The thing is the data I have is in the shape of the area and not a rectangle or square, so I can't transform the data into a matrix.
I tried to create a window with the function owin() and the poly argument, but I have ALL the points (including border and filling) of the area, so I can't get the polygon of the area.
So I need help to get ideas to a) create an pixel image object directly from my database or b) adding points to create a rectangle to then transform my data into a matrix and create with it the pixel image object.
I hope you can help me and if you need more info, please let me know.
Edit: Sorry for not putting an example of the data before.
So my data looks like this:
no. lon lat elev exp slope veg
1 700.5380 984.4786 548 -1 0 1
2 704.0483 984.4786 518 135 0 1
3 707.5586 984.4786 548 -1 0 1
4 711.0689 984.4786 569 254 4 1
5 714.5791 984.4786 590 178 5 1
6 697.0277 981.9342 518 -1 0 1
You can see a plot of the data here.
The other datafile I have only has the data of the lightnings.
Hope you can help me and thanks for everything!
Also, I can't use the im() because I can't make my data into a matrix and I also tried to use owin(poly=data) but it makes me a shape made of lines with the data, also I read more and I think owin wasn't the solution I needed either...
I'm reading right now other libraries to see if I can do the raster with other library instead of spatstat.
I think this recipe should work for you to create a SpatialPointsDataframe and an image (in the example with the raster package) from you data:
https://gis.stackexchange.com/questions/20018/how-can-i-convert-data-in-the-form-of-lat-lon-value-into-a-raster-file-using-r
Then you can convert the spatial df to spatstat with:
library(maptools)
library(spatstat)
spatialPointsDF<-as(spatialPointsDataFrame, "ppp")
plot(spatialPointsDF)
See the maptools functions:
https://www.rdocumentation.org/packages/maptools/versions/0.9-2/topics/as.ppp
To create an im object from raster, see again the maptools spatstat API (as.im.RasterLayer):
https://www.rdocumentation.org/packages/maptools/versions/0.9-2/topics/as.ppp

spatial join on two simple features {sf} with over 1 mil. entries as fast as possible

I hope this is not too trivial but I really can't find an answer and I'm too new to the topic to come up with alternatives myself. So here is the Problem:
I have two shapefiles x and y that represent different processing levels of a Sentinel2 satellite image.
x contains about 1.300.000 polygons/Segments completely covering the image extend without any further vital information.
y contains about 500 polygons representing the cloud-free area of the image (also covering most of the image except for a few "cloud-holes") as well as information about the used image in 4 columns (Sensor, Time...)
I'm trying to add the image information to x in places x is covered by y. pretty simple? I just can't find a way to make it happen without taking days.
I read x in as a simple feature {sf}, as reading it with shapefile / readOGR takes ages.
I tried different things with y
when I try merge(x,y) I can only take one sf as merge doesn't support two sf's.
merging x (as sf) and y (as shp) gives me the error "cannot allocate vector of size 13.0 Gb"
so I tried sf::st_join(x,y), which supports both Variables to be sf but still didn't finish for 28 hours now
sf::st_intersect(x,y) took about 9 minutes for a 10.000 segment subset, so that might not be a lot faster for the whole piece.
could subsetting x to a few smaller pieces solve the whole thing or is there another simple solution? could I do something with my workspace to make the merge work or is there simply no shortcut to joining that amount of polygons?
Thanks a lot in advance and I hope my description isn't too fuzzy!
my tiny work station:
win 7 64 bit
8 GB RAM
intel i7-4790 # 3,6 GHz
I often face this kind of problems and as #manotheshark2 afirms, I prefer to work in a loop subseting my vector layer. Here is my advice:
Load your data
library(raster)
library(rgdal)
x <- readOGR('C:/', 'sentinelCovers')
y <- readOGR('C:/', 'cloudHoles')
Assign an y ID for identify which x polygons intersects y polygons and create the column in x table
x$xyID <- NA # Answer col
y$yID <- 1:nrow(y#data) # ID col
Run a loop subseting x
for (posX in 1:nrow(x#data)){
pol.x <- x[posX, ]
intX <- raster::intersect(pol.x, y)
# x$xyID[posX] <- intX#data$yID ## Run this if there's unique y polygons
# x$xyID[posX] <- paste0(intX#data$yID, collapse = ',') ## Run this if there's multiple y polygons
}
You can check if is better to run the loop on x o y layer
x$xyID <- NA # Answer col
x$xID <- 1:nrow(x#data) # ID Col
for (posY in 1:nrow(y#data)){
pol.y <- y[posY, ]
intY <- tryCatch(raster::intersect(pol.y, x), finally = 'NULL')
if (is.null(intY)) next
x$xyID[x#data$xID %in% intY#data$xID] <- pol.y$yID
}

How to have a precise map of a country with R

I usually went to the site :
http://gadm.org
to find the contours of the country I wanted to plot with R.
But today, this website is closed, and I don't know how to proceed to have a really accurate map...
When I try :
library(maps)
map("world", xlim=c(15,23), ylim=c(45.5,49))
The contours of the country are not very accurate...
I try to find an other website which could give the contours, but I didn't find.
Somebody can help me ?
Ok, let's say you want to plot France contours using data from Natural Earth:
# First download the data in your working directory
download.file("http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_0_countries.zip", "countries.zip")
# Then unzip
unzip("countries.zip")
# Load maptools
library(maptools)
# Read in the shapefile
world <- readShapeSpatial("ne_10m_admin_0_countries.shp")
# Plot France
plot(world[world$ADMIN=="France",1])
# Column 1 is the id column, column "ADMIN" contains the country names

R storing different columns in different vectors to compute conditional probabilities

I am completely new to R. I tried reading the reference and a couple of good introductions, but I am still quite confused.
I am hoping to do the following:
I have produced a .txt file that looks like the following:
area,energy
1.41155882174e-05,1.0914586287e-11
1.46893363946e-05,5.25011714434e-11
1.39244046855e-05,1.57904991488e-10
1.64155121046e-05,9.0815757601e-12
1.85202830392e-05,8.3207522281e-11
1.5256036289e-05,4.24756620609e-10
1.82107587343e-05,0.0
I have the following command to read the file in R:
tbl <- read.csv("foo.txt",header=TRUE).
producing:
> tbl
area energy
1 1.411559e-05 1.091459e-11
2 1.468934e-05 5.250117e-11
3 1.392440e-05 1.579050e-10
4 1.641551e-05 9.081576e-12
5 1.852028e-05 8.320752e-11
6 1.525604e-05 4.247566e-10
7 1.821076e-05 0.000000e+00
Now I want to store each column in two different vectors, respectively area and energy.
I tried:
area <- c(tbl$first)
energy <- c(tbl$second)
but it does not seem to work.
I need to different vectors (which must include only the numerical data of each column) in order to do so:
> prob(energy, given = area), i.e. the conditional probability P(energy|area).
And then plot it. Can you help me please?
As #Ananda Mahto alluded to, the problem is in the way you are referring to columns.
To 'get' a column of a data frame in R, you have several options:
DataFrameName$ColumnName
DataFrameName[,ColumnNumber]
DataFrameName[["ColumnName"]]
So to get area, you would do:
tbl$area #or
tbl[,1] #or
tbl[["area"]]
With the first option generally being preferred (from what I've seen).
Incidentally, for your 'end goal', you don't need to do any of this:
with(tbl, prob(energy, given = area))
does the trick.

convert Zip3 and zip5 level shapefiles to x-y coordinates

I am not sure how to start this, as my GIS playing in R has been to plot things using ggplot2 and other packages using latlong coordinates. What I need to do now, is to use a visualization component in Microstrategy that uses a shapefile in the form of an HTML file containing x-y coordinates for the plot (ie. top left is 0,0). An example of a state level file is:
<HTML><HEAD><TITLE>untitled</TITLE></HEAD><BODY>
<IMG SRC="" USEMAP="#myMap" WIDTH="812" HEIGHT="713" BORDER="0" />
<MAP NAME="myMap">
<AREA SHAPE="POLY" HREF="#" ALT="Texas" COORDS="299,363,299,360,....." />
</MAP></BODY></HTML>
The points listed in 'coords' are the X and Y points with respect to a 812 by 713 'image' that is plotted and colored on the fly.
I have shp, shx, dbf files for Zip3 and Zip5 from http://www.vdstech.com/usa-data.aspx but am unsure of where to even start the conversion! I don't mind doing the grunt work of formatting the HTML file by hand, it is the X-Y conversion that I am stuck at (rusty, not touched GIS for quite a while):
The following code imports the shapefile into R
library(rgdal)
zip3 <- readOGR(dsn = '/Users/adempsey/Downloads/zip3'), layer = 'zip3')
After which I am stuck and currently hunting for tutorial of how to extract zip3 + x-y coordinates into a dataframe that I can then use to create my final file with
update 2
using the following, I ca convert to a data frame, but I am unable to pull across the associated zip3 code, which appeared to be stored in the associated dbf file
Row long lat order hole piece group id
1 -151.0604 70.41873 1 FALSE 1 0.1 0
2 -150.7620 70.49722 2 FALSE 1 0.1 0
Yes, this is beyond my current rusty R
update3
This code dumps the zip codes into a data frame
zip3.codes <- as.data.frame(zip3)
Which should be combinable with something like
zip3.df <- fortify(zip3#polygons[[1000]])
Where the 1000 would be replaced with all the rows zip3.codes associated with a particular zip3
You can use fastshp package to load the data:
install.packages("fastshp",,"http://rforge.net")
library(fastshp)
s <- read.shp("zip5.shp", format="polygon")
s is now a list of all ZIP shapes. You're interested in the x and y components
- for example to plot the first ZIP simply use something like
plot(s[[1]]$x, s[[1]]$y, asp=1.25)
polygon(s[[1]]$x, s[[1]]$y, col="#eeeeee")
To match the names, use read.dbf from foreign:
library(foreign)
d <- read.dbf("zip5.dbf", as.is=TRUE)
names(s) <- d$ZIP5
See ?read.shp for more details on the available formats. The "polygon" one uses NA to separate individual polygons, "list" uses indexing to give you the parts.
BTW the dataset is somewhat dubious, you may want to look into TIGER/Line census ZCTA5 data (most recent is 2010).

Resources