#'Calculate streets height-width ratio
#'@description
#'Calculates the ratio between buildings' height and streets' width (H/W) on two sides of streets with two options available.
#'The ratio is defined as the average height of buildings which intersect with streets to the width of streets.
#'@usage
#'street_hwr (building, street, option = ("average", "weighted"))
#'
#'@param building A Simple Feature containing the building footprints of a city
#'@param street A Simple Feature containing the street network of a city.
#'@param option Defines the method by which the ratio of buildings' height and the streets width is calculated.
#'If option = "average", it returns the ratio of buildings' average height to the width of streets.
#'If option = "weighted", it returns the ratio of buildings' weighted average height (with the length of buildings' intersection with street as weight) to the width of the streets.
#'@details
#'To calculate the height-width ratio of streets, the width of streets should first be calculated.
#'It returns the height-width ratio of every street to the Simple Feature street. If the translation lines of streets ("street") and the buildings ("city") does not have intersection, it will return NA.
#'@examples
#'## Not run:
#'building <-data(building)
#'street<-data(street)
#'street_hwr (building, street, option = "average")
#'building <-data(building)
#'street<-data(street)
#'street_hwr (building, street, option = "weighted")
#'## End(Not run)
road_hwr <- function(city,road,option){
library(rgdal)
library(sf)
library(rgeos)
library(rgdal)
library(sf)
library(dplyr)
#intersect???0的时候返回NA,其他情况算平均???
road2<- st_as_sf(road)
city2 <- st_as_sf(city)
city2$id<-1:nrow(city2)
city2 <- st_transform(city2,3857)
city2 <- st_buffer(city2,0)
road2 <- st_transform(road2,3857)
roadwidthLeft <- road$widthLeft
roadwidthRight <- road$widthRight
roadheight <- character(nrow(road))
i =3
for (i in (1:length(road))){
road_i <- road2[i,]
if ((is.na(roadwidthLeft[i])) | is.na(roadwidthRight[i])) {
roadheight[i] <-NA
} else {
roadbuffer <- st_buffer(road_i,as.numeric(roadwidthLeft[i]),endCapStyle = "FLAT",singleSide=T)
roadbuffer <- st_cast(roadbuffer,"LINESTRING")
intersect_left <- st_intersection(roadbuffer,city2)
roadbuffer <- st_buffer(road_i,as.numeric(roadwidthRight[i]),endCapStyle = "FLAT",singleSide=T)
roadbuffer <- st_cast(roadbuffer,"LINESTRING")
intersect_right <- st_intersection(roadbuffer,city2)
if (option == "average") {
roadheight[i] <- mean(c(intersect_left$floors,intersect_right$floors))
}
if (option == "weighted"){
intersect_left$len <- st_length(intersect_left) %>% as.numeric()
intersect_right$len <- st_length(intersect_right) %>% as.numeric()
roadheight[i] <- weighted.mean(c(intersect_left$floors,intersect_right$floors),c(intersect_left$len,intersect_right$len))
}
}
road_hwratio <- as.numeric(roadheight)/road$width
}
road$hwr <- road_hwratio
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.