3

I am trying to use R to compare the changes in shape of two multipolygon shapefiles. Specifically, these data are congressional districts from 2010 and 2013 in Wisconsin, so they have the same number of rows, with different shapes.

I am curious how I would compare these to each other to see how they have changed, similar to this question previously asked on here: Identifying differences in polygons across shape files.

So far I have only been able to read both shapefiles into R using the sf and rgdal packages.

Vince
  • 20,017
  • 15
  • 45
  • 64

3 Answers3

2

While you might have your good reasons to use R for the task; I personally would use QGIS or other processing software such as FME.

QGIS is a powerful opensource software (which you can download from here for free) packed with great geoprocessing tools you might have to combine to check out the the difference between the 2 Shapefiles. More details about these tools are available from this link (click me) . I would experiment with:

  1. Clip
  2. Difference
  3. Intersection
  4. Symmetrical Difference

enter image description here

As for FME; it is a commercial software but you can still download and use a fully functional license. They have a "transformer" (tool) called ChangeDetector that does exactly what you are looking for.

enter image description here

You just need to set the original Shapefile and the New Shapefile. You can base your request on Geometry or Fields.

enter image description here

GforGIS
  • 3,126
  • 3
  • 19
  • 38
  • Hey @MyGIS! Thanks for your comment. I am actually using R because this is for a course I'm taking in Spatial Statistics, and I am already familiar with QGIS. Any advice on how to move forward doing this with R? – Drew Heiderscheidt Nov 26 '19 at 01:41
  • @DrewHeiderscheidt, if I had to do this blindly without seeing the data, I would calculate the area of each polygon then join the tables based on the districts names then subtract one area column from the other (assuming that the tables have been joined) and the ones with a result different from zero "0" would be the one that has changed. After a quick search, I have found that there are some Spatial tools such as "spatial.tools" from https://rdrr.io/rforge/spatial.tools/ or RSpatial: https://www.rspatial.org/ but I wouldn't be able to comment much on them as I have never used them. Lyes. – GforGIS Nov 26 '19 at 02:09
2

There are functions in the sf package for doing geometric operations on polygons. For example st_difference(a,b) returns a-b, the parts of a that don't overlap b. If that has a substantial area (compute with st_area) or perimeter (st_length) then you know something has changed.

Spacedman
  • 63,755
  • 5
  • 81
  • 115
1

I know this is an old thread, but it was not actually answered as far as I can see. I would certainly prefer R over any desktop GIS package for any operations involving attribute tables.The last approach recommended (but not demonstrated) by @MyGIS is one I use frequently - here's how to do it in R:

To begin, I have two shape files containing polygons, and I want to know if they are different (they should not be, but I fear they may be). I import both shape files into R, using the sf package:

library(sf)
old_SE <- st_read("siose05_REC_COMP.shp")
new_SE <- st_read("siose05_REC_COMP_MADRID_SE.shp")
head(old_SE)  #check the file and show the first 6 records in the attribute table
head(new_SE) #check the file and show the first 6 records in the attribute table
#first, check that they are not actually identical
identical(new_SE,old_SE)
[1] FALSE
#ok, fine, we go on...

old_SE$area <- st_area(st_geometry(old_SE)) #calculate the area of the polgons and add a new attribute table called "area" new_SE$area <- st_area(st_geometry(new_SE)) #calculate the area of the polgons and add a new attribute table called "area" areascf <- as.data.frame(cbind(old_SE$area,new_SE$area)) #bind the two columns together into a new dataframe. This will only work if both the original files have the same number of rows. if we are not certain that this is the case, we can check with length(st_geometry(new_SE)) for each file. #and inspect the new data frame we created:

> head(areascf) V1 V2 1 3329963.76 3329963.76 2 9019401.17 9019401.17 3 11946003.36 11946003.36 4 88149.58 88149.58 5 362726410.62 362726410.61 6 182194640.92 182194640.92

#rename the columns something more useful colnames(areascf) <- c("old","new") #create a new column showing the difference in area between the two shape files #I rounded up to 3 dps to avoid very small differences. areascf$diff <- round(areascf$old-areascf$new,3) areascf old new diff 1 3329963.76 3329963.76 0.000 2 9019401.17 9019401.17 0.000 3 11946003.36 11946003.36 0.000 4 88149.58 88149.58 0.000 5 362726410.62 362726410.61 0.006 6 182194640.92 182194640.92 0.000 7 996525.61 996525.61 0.000 8 570390.89 570390.89 0.000 9 1988238.58 1988238.58 0.002 10 7800656.42 7800656.42 0.000 11 20168058.82 20168058.82 0.000 12 1081882.23 1081882.23 0.000 13 12461270.34 12461270.33 0.001 14 17473784.76 17473784.77 -0.010 15 73942146.44 73942146.44 0.000 16 623481292.98 623481293.00 -0.015 17 154669526.15 154669526.15 0.002 18 1878343.04 1878343.04 0.000 19 7532929.98 7532929.98 0.000 20 41260279.37 41260279.37 -0.002 21 854978.09 854978.09 0.000 22 1299636.02 1299636.02 0.000 23 264717.59 264717.59 0.000 24 3478200.96 3478200.96 0.000 25 39736401.64 39736401.64 0.000 26 1951992.11 1951992.11 0.000 27 27268888.54 27268888.54 0.000 28 1512236.56 1512236.56 0.000 29 273531.01 273531.01 0.000 >

Though many polygon areas disagree slightly, the differences are not large, the largest difference is feature 16, in this case the polygon in the new file is around 15cm2 larger than the old one.

richjh
  • 11
  • 1