I tried to do this over the weekend but had no luck - even after searching this forum. I'm trying to calculate the frontage (length of the property bordering the street) of a number of properties. I've got a shapefile of Hobart, Australia which I've slimmed down for this question as below.
Private Parcel = Houses
Casement = Streets
#Download and unzip the file from https://drive.google.com/open?id=18yyrDSFz2jp0N3R0ehs80leVs9Qyv1wy #libraries library(tmap) library(rgdal) #Import example <- readOGR(dsn="Example GIS",layer="Example") #Visualise the data tm_shape(example) + tm_polygons("CAD_TYPE1") #I want to calculate the total length of each 'Private Parcel' polygon shared border with 'Casement'. I don't know how to do this.
Can anyone tell me how I might be able to do this?
After reading @Spacedman’s comment I noticed that some properties are aggregating.
#Previous Code library(rgeos) library(rgdal) example <- readOGR(#File Location) houses = example[example$CAD_TYPE1 == "Private Parcel",] streets = example[example$CAD_TYPE1 == "Casement",] frontages = gIntersection(houses, streets,byid=c(TRUE,FALSE)) length(frontages) dim(houses) plot(houses[12,]) plot(frontages,add=TRUE,lwd=4,col="red") houses$frontage = gLength(frontages, byid=TRUE) #My test plot(houses) plot(frontages,add=TRUE,lwd=4,col=houses@data$frontage) #As you can see, the frontages are being aggregated between some properties.
#Here's my 2nd attempt library(tidyverse) library(sf) example = read_sf("Example GIS/Example.shp") roads <- example[example$CAD_TYPE1=="Casement",] house <- example[example$CAD_TYPE1=="Private Parcel",] # intersection int = st_intersection(house,roads) # find out about the length of each line segment int$len = st_length(int) #Plot ggplot(data=house) + geom_sf(fill=house$CID,alpha=0.5,col=NA) + geom_sf_label(aes(label=CID),size=2) + geom_sf(data=int,col=int$CID,size=2)
Two properties are still aggregating - 1271016 and 1271014.