Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersect the contour and polygon in R

I have been trying for a few days to create the contour and then plot the shapefile and contour on the same file. Now, that I am able to create the contour and shapefile on the same plot. I want to clip the contour with the shapefile an only show the shapefile.

The data temp.csv can be found on this link https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv Shapefile can be found on the following location: https://www.dropbox.com/sh/ztvmibsslr9ocmc/YOtiwB8p9p

The script file image.scale.R can be found on the following location "https://www.dropbox.com/s/2f5s7cc02fpozk7/image.scale.R "

The code that I have used so far is as follows:

## Required packages 
library(maptools) 
library(rgdal) 
library(sp) 
library(maptools) 
library(sm) 
require(akima) 
require(spplot) 
library(raster) 
library(rgeos)

## Set Working Directory 
setwd("C:\\Users\\jdbaba\\Documents\\R working folder\\shape")

## Read Data from a file 
age2100 <- read.table("temp.csv",header=TRUE,sep=",")

x <- age2100$x 
y <- age2100$y   
z <- age2100$z

####################################
##Load the shape file 
##################################### 
shapefile <- readShapePoly("Export_Output_4.shp")

fld <- interp(x,y,z)

par(mar=c(5,5,1,1)) filled.contour(fld)

###Import the image.scale 
source source("image.scale.R")

# http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html

x11(width=8, height=7) 
layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(6,1), height=6, respect=TRUE) 
layout.show(2)

par(mar=c(4,4,1,2)) 
image(fld,axes=T) 
contour(fld, add=TRUE)
#points(age2100$x,age2100$y, pch=".", cex=2,legend=F) 
plot(shapefile,add=T,lwd=2) 
box()
par(mar=c(4,0,1,4)) 
image.scale(fld, xlab="Eastings", ylab="Northings", xaxt="n", yaxt="n", horiz=FALSE)

axis(4)
mtext("Salinity", side=4, line=2.5)

The output of the above code is as follows:

enter image description here

Now, I want to get rid of the colored gradients and the contours from the polygon shapefile and only leave the intersection part.

Any help is highly appreciated.

Research: I found this link https://gis.stackexchange.com/questions/25112/clip-depth-contour-with-spatial-polygon on Stack exchange Gis and I tried to follow this method I always get error while creating the contour.

I found another similar thread on https://stat.ethz.ch/pipermail/r-sig-geo/2009-May/005793.html . But I couldn't make it work on my dataset.

I would like to acknowledge Marc in the box for helping me in getting to this point.

Thanks.

like image 674
Jd Baba Avatar asked Oct 06 '22 08:10

Jd Baba


1 Answers

Indeed, @baptiste gave you a strong hint for the solution, the recent paper by Paul Murrell. Paul was generous to provide us with the code for his entire paper manuscript, which you can get from his personal website. On the side topic, Paul shows beautiful example of reproducible research with this paper. Generally, I took the following approach:

  • extract latitude and longitude coordinates from the shapefile (a function to do this is here, by Paul Hiemstra),
  • plot everything with your code,
  • and use polypath to remove everything outside the boundaries defined by shapefile, using extracted coordinates as a baseline.

    #function to extract coordinates from shapefile (by Paul Hiemstra)
    allcoordinates_lapply = function(x) { 
        polys = x@polygons 
        return(do.call("rbind", lapply(polys, function(pp) { 
               do.call("rbind", lapply(pp@Polygons, coordinates)) 
               }))) 
    } 
    q = allcoordinates_lapply(shapefile)
    
    #extract subset of coordinates, otherwise strange line connections occur...
    lat = q[110:600,1]
    long = q[110:600,2]
    
    #define ranges for polypath
    xrange <- range(lat, na.rm=TRUE)
    yrange <- range(long, na.rm=TRUE)
    xbox <- xrange + c(-20000, 20000)
    ybox <- yrange + c(-20000, 20000)
    
    #plot your stuff
    plot(shapefile, lwd=2)
    image(fld, axes=F, add=T)
    contour(fld, add=T)
    #and here is the magic 
    polypath(c(lat, NA, c(xbox, rev(xbox))),
             c(long, NA, rep(ybox, each=2)),
             col="white", rule="evenodd")
    

enter image description here

like image 160
Geek On Acid Avatar answered Oct 13 '22 12:10

Geek On Acid