3

I'm new to working with spatial data and polygons in R.

I have two separate shape files of two polygons that I extract from Google Earth. So basically the first shape file is a location (such as a shopping mall etc.) and the second shape file is a three kilometre radius around the first location. I read both shape files into R as SpatialPolygonsDataFrames. I use the following code:

library(maptools)
library(sp)
library(spatstat)
options(digits=10) 

# Read polygon a

a <- readShapeSpatial(file.choose())
class(a)

spatstat.options(checkpolygons=FALSE)

r <- slot(a,"polygons")
r <- lapply(r, function(a) { SpatialPolygons(list(a)) })
windows <- lapply(r, as.owin)
Ploy_One <- tess(tiles=windows)

# Read polygon b

b <- readShapeSpatial(file.choose())
class(b)

spatstat.options(checkpolygons=FALSE)

s <- slot(b,"polygons")
s <- lapply(s, function(b) { SpatialPolygons(list(b)) })

windows <- lapply(s, as.owin)
Poly_Two <- tess(tiles=windows)

# Read polygon b

Combined_Region <- intersect.tess(Poly_One, Poly_Two)
plot(Combined_Region)

However, I don't get a combined view of the two polygons, (view of the one polygon within the other).

If anybody have some advice on how I can code this the merge of two polygon regions into a single polygon region in R, I'd appreciate it very much!

Carmen
  • 117
  • 1
  • 7
  • 2
    Can you post links to your two polygon shapefiles? – jlhoward Jan 24 '14 at 08:57
  • Hi Jhoward, I hope this works https://skydrive.live.com/?cid=7286ae33f47c4a63&id=7286AE33F47C4A63!115&Bsrc=Share&Bpub=SDX.SkyDrive&authkey=!Ap5RgaKrJN5MYbU https://skydrive.live.com/?cid=7286ae33f47c4a63&id=7286AE33F47C4A63!121&Bsrc=Share&Bpub=SDX.SkyDrive&authkey=!Ap5RgaKrJN5MYbU – Carmen Jan 24 '14 at 09:30
  • When I go to your link SkyDrive wants me to log on. There's an option in SkyDrive to share a file by creating a link to it and posting that. It's explained [here](http://windows.microsoft.com/en-us/skydrive/share-file-folder) under "get a link". Can you do that and post the link? – jlhoward Jan 24 '14 at 20:29
  • Thank you!! Here's the new link https://skydrive.live.com/redir?resid=7286AE33F47C4A63%21127 . I found these course notes on spatstat (http://www.csiro.au/resources/pf16h - the pdf file on pg. 48), it says that the vertices of each polygon should be traversed anticlockwise for external boundaries and clockwise for internal boundaries (holes). I've tried this but I get an error that says "Area of window is negative; check that all polygons were traversed in the right direction". I may be doing something wrong, will let you know if it worked. I appreciate your help! Thank you!! – Carmen Jan 25 '14 at 07:30
  • OK we're making progress. A "shapefile" is not a single file, but a collection of files with the same name and different extensions, usually including .shp, .dbf, .shx, .prj, and .shp.xml (not always all of these). So if your "shapefile" is, for example, `poly`, then the actual files would be `ploy.shp, poly.dbf, poly.shx`, etc. You need to collect all these in a zip file and post that, for both Polygon_One and Polygon_Two. – jlhoward Jan 25 '14 at 16:44
  • https://skydrive.live.com/redir?resid=7286AE33F47C4A63%21131 , sorry!!! Here it is now! – Carmen Jan 25 '14 at 19:08
  • It worked! Using the following code Z <- owin(poly = list(list(x = Poly_One_Long, y = Poly_One_Lat), list(x = Poly_Two_Long, y = Poly_Two_Lat))) with Poly_One plotted in an anticlockwise direction and Poly_Two in a clockwise direction. Poly_Two forms a hole in Poly_One. I didn't import the points in a .shp file but just as a simple .csv file. Importing a circular polygon in an anticlockwise direction isn't easy, I don't think for future and bigger analysis the function will be very effective, but for now I'm just glad I get a pretty picture. If you have any further advice please share thx – Carmen Jan 25 '14 at 19:52

1 Answers1

4

If you're committed to using thespatstat package, this probably will not be very helpful. If not, read on...

If all you want do to is plot the polygons as separate layers, use ggplot

library(ggplot2)
library(sp)
library(maptools)

setwd("<directory with all your files...>")

poly1 <- readShapeSpatial("Polygon_One")
poly2 <- readShapeSpatial("Polygon_Two")
# plot polygons as separate layers,,,
poly1.df <- fortify(poly1)
poly2.df <- fortify(poly2)
ggplot() +
  geom_path(data=poly1, aes(x=long,y=lat, group=group))+
  geom_path(data=poly2, aes(x=long,y=lat, group=group), colour="red")+
  coord_fixed()

If you need to combine them into one spatialPolygonDataFrame, use this. The nuance here is that you can't use spRbind(...) if the two layers have common polygon IDs. So the call to spChFIDs(...) changes the ID of the one polygon in poly2 (the circle) to "R.3km".

# combine polygons into a single shapefile
poly.combined <- spRbind(poly1,spChFIDs(poly2,"R.3km"))
# plot polygons using ggplot aesthetic mapping
poly.df <- fortify(poly.combined)
ggplot(poly.df) + 
  geom_path(aes(x=long, y=lat, group=group, color=group)) + 
  scale_color_discrete("",labels=c("Center", "3km Radius")) +
  coord_fixed()
# plot using plot(...) method for spatialObjects
plot(poly.combined)

You'll notice that, on these plots, the "circle" isn't. This is because we're using long/lat and you're pretty far south of the equator. The data needs to be reprojected. Turns out the appropriate CRS for South Africa is utm-33.

wgs.84 <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
utm.33 <- "+proj=utm +zone=33 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
proj4string(poly.combined) <- CRS(wgs.84)
poly.utm33 <- spTransform(poly.combined,CRS(utm.33))
poly.df    <- fortify(poly.utm33)
ggplot(poly.df) + 
  geom_path(aes(x=long, y=lat, group=group, color=group)) + 
  scale_color_discrete("",labels=c("Center", "3km Radius")) +
  theme(axis.text=element_blank()) + labs(x=NULL,y=NULL) +
  coord_fixed()

Now the circle is.

jlhoward
  • 58,004
  • 7
  • 97
  • 140