0

I have data that has numerics outside the boundary and I am trying to get non-weighted and weighted images using the following code but the result is a very tiny image I can see the data getting plotted in there but its very small:

rm(list=ls())
setwd("~/Desktop/Wildlife")
library('spatstat')
library('sp')
library('raster')
library('maptools')
library('rgeos')
data1<-read.table("AllObs.csv",sep=",",header=TRUE)
coordinates(data1)<- ~ utm_x+utm_y
class(data1)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

Import Shapefiles

bound<-readShapePoly("~/Desktop/Wildlife/Bounding_Box.shp")
building<-readShapePoly("~/Desktop/Wildlife/BuildingFootsprints.shp")
water<-readShapePoly("~/Desktop/Wildlife/Water_Bodies.shp")

Spatial lines

Roads_uw<-readShapeLines("~/Desktop/Wildlife/Roads_uw.shp")
creeks<-readShapeLines("~/Desktop/Wildlife/Creeks.shp")
Roads<-readShapeLines("~/Desktop/Wildlife/Roads.shp")

Plot Spatial Data

par(mar=c(0.1,0.1,0.1,0.1))
plot(bound)
box()
data1 <- data1[!is.na(over(x=data1, y= as(bound,"SpatialPolygons"))),]
plot(building,add=TRUE, col="pink", cex=0.3,pch=16)
plot(Roads,add=TRUE, col="black", cex=0.3,pch=16)
plot(Roads_uw,add=TRUE, cex=0.3,pch=16)
plot(water, add=TRUE, col="cyan", lwd=1)
plot(creeks, add=TRUE, col="red", lwd=1)
legend("topright", inset=0.001, cex=0.7,
+ c("Building", "Roads", "Roads_uw", "Water", "Creeks"), pch=c(16,NA), lty=c(NA,1), col=c("pink","black","black","cyan","red"))

Clip extra by study area

building_cl<-gIntersection(spgeom1=building,spgeom2=bound)
Roads_cl<-gIntersection(spgeom1=Roads,spgeom2=bound)
Roads_uw<-gIntersection(spgeom1=Roads_uw,spgeom2=bound)
water_cl<-gIntersection(spgeom1=water,spgeom2=bound)
creeks_cl<-gIntersection(spgeom1=creeks,spgeom2=bound)

plot spatial data

par(mar=c(0.1,0.1,0.1,0.1))
plot(bound)
box()
plot(building_cl,add=TRUE, col="pink", cex=0.3,pch=16)
plot(Roads_cl,add=TRUE, col="black", cex=0.3,pch=16)
plot(Roads_uw,add=TRUE, cex=0.3,pch=16)
plot(water_cl, add=TRUE, col="cyan", lwd=1)
plot(creeks_cl, add=TRUE, col="red", lwd=1)
legend("topright", inset=0.001, cex=0.7,
+ c("Building", "Roads", "Roads_uw", "Water", "Creeks"), pch=c(16,NA), lty=c(NA,1), col=c("pink","black","black","cyan","red"))

point pattern analysis

data2<-read.table("PPP.csv",sep=",",header=TRUE)
data2win<-owin(c(min(data2$utm_x)-10000,max(data2$utm_x)+10000),
+ c(min(data2$utm_y)-10000,max(data2$utm_y)+10000))
data2win
window: rectangle = [525488, 547767] x [4801386, 4844192] units
data2pp<-ppp(x=data2$utm_x,y=data2$utm_y,window=data2win)
data2pp
planar point pattern: 106 points
window: rectangle = [525488, 547767] x [4801386, 4844192] units
sigma <- bw.ppl(data2pp)
data2_nw<-density(data2pp, sigma=sigma)
data2_w<-density(data2pp, sigma=sigma, weights=data2$num_indiv)

convert non-weighted to raster

data2_nwr<-raster(data2_nw)
data2_nwr<-mask(data2_nwr,bound)

convert weighted image to raster

data2_wr<-raster(data2_w)
data2_wr<-mask(data2_wr,bound)

set up two panel plot

par(mfrow=c(1,1),mar=c(1,1,1,1))

plot non-weighted in upper panel

plot(data2_nwr, axes=FALSE, main="Non-Weighted")
plot(bound, add=TRUE)
box()
points(data2$utm_x,data2$utm_y,pch=16,cex=0.3)
plot(building_cl,add=TRUE, col="pink", cex=0.2,pch=16)
plot(Roads_cl,add=TRUE, col="black", cex=0.2,pch=16)
plot(Roads_uw,add=TRUE, cex=0.2,pch=16)
plot(water_cl, add=TRUE, col="cyan", lwd=0.2)
plot(creeks_cl, add=TRUE, col="red", lwd=0.2)

plot weighted in lower panel

plot(data2_wr,axes=FALSE, main="Weighted")
plot(bound, add=TRUE)
points(data2$utm_x,data2$utm_y,pch=16,cex=0.2)
plot(building_cl,add=TRUE, col="pink", cex=0.2,pch=16)
plot(Roads_cl,add=TRUE, col="black", cex=0.2,pch=16)
plot(Roads_uw,add=TRUE, cex=0.2,pch=16)
plot(water_cl, add=TRUE, col="cyan", lwd=0.2)
plot(creeks_cl, add=TRUE, col="red", lwd=0.2)
legend("topright", inset=0.001, cex=0.7,
+ c("Building", "Roads", "Roads_uw", "Water", "Creeks"), pch=c(16,NA), lty=c(NA,1), col=c("pink","black","black","cyan","red"))

We were also given this code by our professor, which is suppose to eliminate the error:

data2<- data2[!is.na(over(x=data2, y= as(bound,"SpatialPolygons"))),]

but when I try using this I get this error:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘over’ for signature ‘"data.frame", "SpatialPolygons"’

Thank you for your help!

GileBrt
  • 1,830
  • 3
  • 20
  • 28
Divya
  • 1
  • You're much more likely to get a helpful answer if your question includes a self-contained example that will allow anyone else to reproduce your problem (see [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for help on that). We don't have any of your shp files... – Ben Mar 15 '15 at 23:30
  • Could you tell me if there is a way I can get rid of this error: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘over’ for signature ‘"data.frame", "SpatialPolygons"’ – Divya Mar 15 '15 at 23:44
  • See my first comment... – Ben Mar 16 '15 at 02:18

0 Answers0