Commit adc00b0b authored by numeroteca's avatar numeroteca

añade barrio y distrito a vut geocodificadas

parent 590087b9
# count points in polygons: https://gis.stackexchange.com/questions/110117/counts-the-number-of-points-in-a-polygon-in-r#110246
# read GeoJSON in R https://stackoverflow.com/questions/24183007/is-it-possible-to-read-geojson-or-topojson-file-in-r-to-draw-a-choropleth-map
# Lodas libraries
library("raster")
library("sp")
# restart R .rs.restartR() if rgdal does not work
library(rgdal)
# check ogr drivers to see if GeoJSON is loaded:
# ogrDrivers()
## read files
# shapes
barrios <- readOGR("data/original/shapes/barrios.valencia.wgs84.geojson")
distritos <- readOGR("data/original/shapes/distritos.valencia.wgs84.geojson")
# class(menores) # checks that is patialPolygonsDataFrame
# plot(menores) # plots the shape map
# proj4string(menores) # check CRS
# points
airbnb <- read_csv("data/output/190302_viviendas-turisticas-comunidad-valenciana_valencia_geocoded.csv")
summary(airbnb)
## Get long and lat from your data.frame. Make sure that the order is in lon/lat.
# source: https://stackoverflow.com/questions/29736577/how-to-convert-data-frame-to-spatial-coordinates#29736844
xy <- airbnb[,c("lon","lat")]
# removes NA values
airbnb <- airbnb[!is.na(airbnb$lon),]
xy <- xy[!is.na(xy$lon),]
airbnbSp <- SpatialPointsDataFrame(coords = xy, data = airbnb, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
# class(airbnbSp)
# proj4string(airbnbSp)
# serves to count points (people in Padrón) in polygon (barrios)
# retrieve the geometry indices of Barrios at the locations of A (people points).
# More in particular, an integer vector of length length(A) is returned, with NA values for locations in
# A not matching with locations in B (e.g. those points outside a set of polygons).
# (https://cran.r-project.org/web/packages/sp/vignettes/over.pdf)
countBarrios <- over(airbnbSp, barrios)
# table(countBarrios$BAR_DS_O)
# same calc for Distritos
countDistritos <- over(airbnbSp, distritos)
# Adds calculated barrios to spatial poligon
airbnbSp$barrio <- countBarrios$nombre
airbnbSp$distrito <- countDistritos$nombre
airbnbSp$coddistbar <- countBarrios$coddistbar
airbnbSp$coddistrit <- countDistritos$coddistrit
# Where are those points without barrio
library(ggmap)
qmplot(longitude, latitude, data = airbnb[is.na(airbnbSp$barrio =="no location"),], maptype = "toner-lite",
color = I("red"),alpha = I(.2)) + labs(title= "Points without barrio" )
qmplot(longitude, latitude, data = airbnb, maptype = "toner-lite",
color = I("red"),alpha = I(.2)) + labs(title= "Points without barrio" )
airbnb <- as.data.frame(airbnbSp) # convert spatial data to regular data frame
# removes duplicated columns with lat and long
drops <- c("lat.1","lon.1")
airbnb <- airbnb[ , !(names(airbnb) %in% drops)]
# Some points will be outside polygons and have Barrio variable fixed
airbnb[is.na(airbnbSp$barrio),]$name
# There are n points that have no Barrio assigned
length(airbnb[is.na(airbnbSp$barrio),]$name)
# library(dplyr)
# airbnb$barrio <- lapply(airbnb$barrio, as.character)
# airbnb$umenores <- lapply(airbnb$umenores, as.character)
# Assign "no location" to points
# airbnb[is.na(airbnbSp$barrio),]$barrio <- "no location"
# airbnb[is.na(airbnbSp$umenores),]$umenores <- "no location"
# airbnb[airbnb$umenores == "Sag�es" ,]$umenores <- "Sagües"
# saves file
# save(airbnb,file="data/output/180423_listings-airbnb-donostia_datahippo_barrio-umenor.Rda")
# write.csv(airbnb, file = "data/output/180423_listings-airbnb-donostia_datahippo_barrio-umenor.csv", row.names = FALSE)
# write.csv(airbnb, file = "data/output/vut-donostia/censo-viviendas-turisticas-donostia-180301_barrio-umenor.csv", row.names = FALSE)
# write.csv(airbnb, file = "data/output/vut-donostia/censo-viviendas-turisticas-donostia-20180914_barrio-umenor.csv", row.names = FALSE)
# write.csv(airbnb, file = "data/output/180604_listings-airbnb-donostia_datahippo_with-last-review-20180912-reviewed_barrio-umenor.csv", row.names = FALSE)
write.csv(airbnb, file = "data/output/190302_viviendas-turisticas-comunidad-valenciana_valencia_geocoded_barrio-distrito.csv", row.names = FALSE)
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment