-
Notifications
You must be signed in to change notification settings - Fork 0
/
1c. Data enrichment.R
77 lines (62 loc) · 3.86 KB
/
1c. Data enrichment.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
easypackages::packages("sf", "sp", "osmdata", "FNN", "tidyverse", "stars", "mapview")
############################################################ AMSTERDAM ###########################################################
############################################################ DATA ###########################################################
funda_data <- st_transform(st_read("data/Houseprices/funda_buy_amsterdam_31-03-2023_full.gpkg"), crs = 28992)
amsterdam_boundaries <- st_read("data/Amsterdam/PC4.json")
public_transport <- st_transform(st_read("data/Amsterdam/public_transport.json"), crs= 28992)
tram <- public_transport %>%
filter(Modaliteit == "Tram")
metro <- public_transport %>%
filter(Modaliteit == "Metro")
train <- st_transform(st_read("data/Amsterdam/Stations_(NS).geojson"), crs= 28992)
############################################################ OSM ###########################################################
school <- opq(st_bbox(amsterdam_boundaries)) %>%
add_osm_feature(key = "amenity", c("school")) %>%
osmdata_sf ()
school_points <- st_as_sf (school$osm_points) %>% dplyr:: select(osm_id, amenity)
daily_shops <- opq(st_bbox(amsterdam_boundaries)) %>%
add_osm_feature(key = "shop", c("convenience", "department_store", "supermarket")) %>%
osmdata_sf ()
daily_shops_points <- st_as_sf (daily_shops$osm_points) %>% dplyr:: select(osm_id, amenity)
############################################################ DISTANCES ######################################################
nn_function <- function(measureFrom,measureTo,k) {
measureFrom_Matrix <- as.matrix(measureFrom)
measureTo_Matrix <- as.matrix(measureTo)
nn <-
get.knnx(measureTo, measureFrom, k)$nn.dist [,k]
return(nn)
}
funda_data <- funda_data %>%
mutate(tram_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(tram$geometry), 1))
funda_data <- funda_data %>%
mutate(metro_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(metro$geometry), 1))
funda_data <- funda_data %>%
mutate(metro_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(metro$geometry), 1))
funda_data <- funda_data %>%
mutate(train_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(train$geometry), 1))
#Re-project the point layer from WGS to local projection to have unit in meter
school_points <- st_transform (school_points, 28992)
daily_shops_points <- st_transform (daily_shops_points, 28992)
funda_data <- funda_data %>%
mutate(school_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(school_points$geometry), 1))
funda_data <- funda_data %>%
mutate(shops_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(daily_shops_points$geometry), 1))
#Distance to city centre and Amsterdam Zuid (Centre of Business)
dam_centre_coords <- st_point(x = c(121391.4, 487329.7), dim="XY")
zuid_coords <- st_point(x = c(119988, 483393), dim="XY")
funda_data <- funda_data %>%
mutate(centre_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(dam_centre_coords), 1))
funda_data <- funda_data %>%
mutate(zuid_dist = nn_function(st_coordinates(funda_data$geom), st_coordinates(zuid_coords), 1))
############################################################ NDVI ######################################################
raster100 <- read_stars("data/Greenness/NDVI_Amsterdam_100m.tif")
raster300 <- read_stars("data/Greenness/NDVI_Amsterdam_300m.tif")
raster500 <- read_stars("data/Greenness/NDVI_Amsterdam_500m.tif")
ndvi100 <- st_extract(raster100, funda_data)
ndvi300 <- st_extract(raster300, funda_data)
ndvi500 <- st_extract(raster500, funda_data)
funda_data$ndvi100 <- ndvi100$NDVI_Amsterdam_100m.tif
funda_data$ndvi300 <- ndvi300$NDVI_Amsterdam_300m.tif
funda_data$ndvi500 <- ndvi500$NDVI_Amsterdam_500m.tif
#write results to updated gpkg
st_write(funda_data, "data/Houseprices/funda_buy_amsterdam_31-03-2023_full_distances.gpkg")