library(sp)
library(maps)
library(mapdata)
library(rgdal)
## rgdal: version: 1.1-10, (SVN revision 622)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
## Linking to sp version: 1.2-3
library(maptools)
## Checking rgeos availability: TRUE
library(RColorBrewer)
library(knitr)
library(rmarkdown)
map('worldHires')
map('worldHires','Spain')
points(c(-3.682746,2.1734066),c(40.4893538,41.4850595),pch=19, col="red",cex=0.5)
text(x=c(-3.682746,2.1734066),y=c(40.4893538,41.4850595),labels = c("Madrid", "Barcelona"), pos = 3)
# Descarga de datos geográficos desde la web
download.file(url="http://datos.madrid.es/egob/catalogo/205193-0-semaforos-foto-rojo.zip", "205193-0-semaforos-foto-rojo.zip", "auto")
unzip("205193-0-semaforos-foto-rojo.zip")
file.remove("205193-0-semaforos-foto-rojo.zip")
## [1] TRUE
semaforos_foto<- readOGR(".", "foto-rojo")
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "foto-rojo"
## with 44 features
## It has 16 fields
download.file("http://datos.madrid.es/egob/catalogo/200078-9-distritos-barrios.zip", "200078-9-distritos-barrios.zip", "auto")
unzip("200078-9-distritos-barrios.zip")
file.remove("200078-9-distritos-barrios.zip")
## [1] TRUE
ogrInfo("./SHP_ETRS89", "DISTRITOS")
## Source: "./SHP_ETRS89", layer: "DISTRITOS"
## Driver: ESRI Shapefile; number of rows: 21
## Feature type: wkbPolygon with 2 dimensions
## Extent: (424753.7 4462566) - (456040 4499365)
## CRS: +proj=utm +zone=30 +ellps=GRS80 +units=m +no_defs
## LDID: 89
## Number of fields: 4
## name type length typeName
## 1 NOMBRE 4 50 String
## 2 CODDISTRIT 4 2 String
## 3 SHAPE_area 2 19 Real
## 4 SHAPE_len 2 19 Real
distritos<- readOGR("./SHP_ETRS89", "DISTRITOS")
## OGR data source with driver: ESRI Shapefile
## Source: "./SHP_ETRS89", layer: "DISTRITOS"
## with 21 features
## It has 4 fields
plot(distritos)
points(semaforos_foto)
colour <- sample(1:8, length(distritos), replace=T)
distritos$colour <- colour
palette <- brewer.pal(8,"Purples")
plot(distritos, border=F, col=palette[distritos$colour])
points(semaforos_foto, col="red", pch=18)
Madrid<-SpatialPolygons2map(distritos, namefield = "NOMBRE")
map(Madrid)
points(semaforos_foto, col="green", pch=18)