sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77...

29

Transcript of sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77...

Page 1: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …
Page 2: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …
Page 3: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

library(rgdal)library(sp)iris_Paris_sp<-readOGR("data/ParisIris2017.shp",stringsAsFactors=FALSE)

##OGRdatasourcewithdriver:ESRIShapefile##Source:"/data/user/c/rcura/geoviz2018/data/ParisIris2017.shp",layer:"ParisIris2017"##with992features##Ithas77fields

library(sf)iris_Paris_sf<-st_read("data/ParisIris2017.shp",stringsAsFactors=FALSE)

##Readinglayer`ParisIris2017'fromdatasource`/data/user/c/rcura/geoviz2018/data/ParisIris2017.shp'usingdriver`ESRIShapefile'##Simplefeaturecollectionwith992featuresand77fields##geometrytype:MULTIPOLYGON##dimension:XY##bbox:xmin:643075.6ymin:6857477xmax:661086.2ymax:6867081##epsg(SRID):NA##proj4string:+proj=lcc+lat_1=44+lat_2=49+lat_0=46.5+lon_0=3+x_0=700000+y_0=6600000+ellps=GRS80+units=m+no_defs

str(iris_Paris_sf)

##Classes'sf'and'data.frame':992obs.of78variables:##$INSEE:chr"75119""75117""75112""75110"...##$NOM_C:chr"Paris19eArrondissement""Paris17eArrondissement""Paris12eArrondissement""Paris10eArrondissement"...##$IRIS:chr"7316""6716""4502""3703"...##$CODE_:chr"751197316""751176716""751124502""751103703"...##$NOM_I:chr"Villette16""Batignolles16""BelAir2""Saint-VincentdePaul3"...##$TYP_IRIS_x:chr"H""H""H""H"...##$REG:chr"11""11""11""11"...##$REG20:chr"11""11""11""11"...##$DEP:chr"75""75""75""75"...##$UU201:chr"00851""00851""00851""00851"...##$COM_x:chr"75119""75117""75112""75110"...##$LIBCOM_x:chr"Paris19eArrondissement""Paris17eArrondissement""Paris12eArrondissement""Paris10eArrondissement"...##$TRIRI:chr"752471""752011""750921""750631"...##$GRD_Q:chr"7511973""7511767""7511245""7511037"...##$LIBIRIS_x:chr"Villette16""Batignolles16""BelAir2""Saint-VincentdePaul3"...##$TYP_IRIS_y:chr"H""H""H""H"...##$MODIF:chr"00""00""00""00"...##$LAB_I:chr"1""2""1""1"...##$P12_POP156:num27731608329920731828...##$P12_POP152:num404339473233223...##$P12_POP2:num2028999243915561337...##$P12_POP5:num341270386284268...##$P12_ACTOCC:num18961044244216341269...

spsf

Page 4: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

plot(iris_Paris_sp) plot(iris_Paris_sf)

##Warning:plottingthefirst10outof77attributes;usemax.plot=77to##plotall

Page 5: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

sfdata.frame tibbles

sfsf sp

Page 7: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

sf sfiris_Paris_sf

##Simplefeaturecollectionwith992featuresand77fields##geometrytype:MULTIPOLYGON##dimension:XY##bbox:xmin:643075.6ymin:6857477xmax:661086.2ymax:6867081##epsg(SRID):NA##proj4string:+proj=lcc+lat_1=44+lat_2=49+lat_0=46.5+lon_0=3+x_0=700000+y_0=6600000+ellps=GRS80+units=m+no_defs##First10features:##INSEENOM_CIRISCODE_NOM_I##175119Paris19eArrondissement7316751197316Villette16##275117Paris17eArrondissement6716751176716Batignolles16##375112Paris12eArrondissement4502751124502BelAir2##475110Paris10eArrondissement3703751103703Saint-VincentdePaul3##575118Paris18eArrondissement7104751187104Goutted'Or4##675111Paris11eArrondissement4314751114314Roquette14##775110Paris10eArrondissement3707751103707Saint-VincentdePaul7##875120Paris20eArrondissement7702751207702Belleville2##975113Paris13eArrondissement5004751135004Gare4##1075112Paris12eArrondissement4623751124623Picpus23##TYP_IRIS_xREGREG20DEPUU201COM_xLIBCOM_xTRIRI##1H1111750085175119Paris19eArrondissement752471##2H1111750085175117Paris17eArrondissement752011##3H1111750085175112Paris12eArrondissement750921##4H1111750085175110Paris10eArrondissement750631##5H1111750085175118Paris18eArrondissement752241##6H1111750085175111Paris11eArrondissement750761##7H1111750085175110Paris10eArrondissement750521##8H1111750085175120Paris20eArrondissement752671##9H1111750085175113Paris13eArrondissement751171##10H1111750085175112Paris12eArrondissement750871##GRD_QLIBIRIS_xTYP_IRIS_yMODIFLAB_IP12_POP156##17511973Villette16H0012773.295##27511767Batignolles16H0021607.938##37511245BelAir2H0013298.589##47511037Saint-VincentdePaul3H0012073.318##57511871Goutted'Or4H0011827.740##67511143Roquette14H0012119.054

Page 8: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

sf PostGISst_OPERATION

irisParis<-st_read(dsn="data/ParisIris2017.shp",stringsAsFactors=FALSE)#Commepourlalecturedesdata.frame

##Readinglayer`ParisIris2017'fromdatasource`/data/user/c/rcura/geoviz2018/data/ParisIris2017.shp'usingdriver`ESRIShapefile'##Simplefeaturecollectionwith992featuresand77fields##geometrytype:MULTIPOLYGON##dimension:XY##bbox:xmin:643075.6ymin:6857477xmax:661086.2ymax:6867081##epsg(SRID):NA##proj4string:+proj=lcc+lat_1=44+lat_2=49+lat_0=46.5+lon_0=3+x_0=700000+y_0=6600000+ellps=GRS80+units=m+no_defs

Page 9: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

st_crs

st_crs(irisParis)

##CoordinateReferenceSystem:##NoEPSGcode##proj4string:"+proj=lcc+lat_1=44+lat_2=49+lat_0=46.5+lon_0=3+x_0=700000+y_0=6600000+ellps=GRS80+units=m+no_defs"

irisParis<-irisParis%>%st_set_crs(2154)#SRID/EPSGduLambert93

##Warning:st_crs<-:replacingcrsdoesnotreprojectdata;usest_transform##forthat

head(irisParis)

##Simplefeaturecollectionwith6featuresand77fields##geometrytype:MULTIPOLYGON##dimension:XY##bbox:xmin:648979.6ymin:6859518xmax:656589.8ymax:6866159##epsg(SRID):2154##proj4string:+proj=lcc+lat_1=49+lat_2=44+lat_0=46.5+lon_0=3+x_0=700000+y_0=6600000+ellps=GRS80+towgs84=0,0,0,0,0,0,0+units=m+no_defs##INSEENOM_CIRISCODE_NOM_I##175119Paris19eArrondissement7316751197316Villette16##275117Paris17eArrondissement6716751176716Batignolles16##375112Paris12eArrondissement4502751124502BelAir2##475110Paris10eArrondissement3703751103703Saint-VincentdePaul3##575118Paris18eArrondissement7104751187104Goutted'Or4##675111Paris11eArrondissement4314751114314Roquette14

Page 10: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

irisParis%>%select(geometry)%>%#Lambert93plot()

GallPeters<-"+proj=cea+lon_0=0+lat_ts=45\+x_0=0+y_0=0+ellps=WGS84+units=m+no_defs"irisParis%>%select(geometry)%>%st_transform(crs=GallPeters)%>%plot()

sf st_transform(SRID/CRS)

Page 11: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

plot()plot()

plot(irisParis)

##Warning:plottingthefirst9outof77attributes;usemax.plot=77to##plotall

Page 12: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

plot()

plot(irisParis["NOM_C"])

Page 13: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

mapviewmapview

library(mapview)#install.packages("mapview")mapview(irisParis)

+−

Leaflet

Page 14: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

mapviewmapview

zcol

mapview(irisParis,zcol="NOM_C")

+−

Leaflet

Page 15: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

sf data.framefilter select

rename mutate

irisParis%>%select(INSEE,CODE_)%>%#Lasélectiondelacolonne`geometry`estimplicitemutate(ARRONDISSEMENT=as.numeric(INSEE)-100)%>%filter(ARRONDISSEMENT>=75001,ARRONDISSEMENT<=75007)%>%mapview(zcol="ARRONDISSEMENT")

+−

Leaflet

Page 16: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

tibble

arrondissements_Paris<-irisParis%>%group_by(INSEE)%>%summarise()

plot(arrondissements_Paris)

Page 17: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

plot(incidents2015[,"NbIncidents"],main="Incidents2015",breaks=commonBreaks)

plot(incidents2017[,"NbIncidents"],main="Incidents2017",breaks=commonBreaks)

data.frame

df_dmr<-readRDS("dans_ma_rue_clean.RDS")joinData<-df_dmr%>%group_by(CODE_POSTAL,ANNEE_DECLARATION)%>%summarise(NbIncidents=n())%>%ungroup()

donnees_incidents<-arrondissements_Paris%>%mutate(CODGEO=as.character(as.numeric(INSEE)-100))%>%left_join(joinData,by=c("CODGEO"="CODE_POSTAL"))

incidents2015<-filter(donnees_incidents,ANNEE_DECLARATION==2015)incidents2017<-filter(donnees_incidents,ANNEE_DECLARATION==2017)

commonBreaks<-joinData%>%filter(ANNEE_DECLARATION%in%c(2015,2017))%>%pull(NbIncidents)%>%quantile(probs=seq(from=0,to=1,by=0.1))

Page 18: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

sfsf

sf

st_as_sf

dmr_spatial<-df_dmr%>%filter(ANNEE_DECLARATION%in%c(2015,2017))%>%st_as_sf(coords=c("Long","Lat"),crs=4326)%>%st_transform(2154)#Pouruneopérationgéométrique,lesobjetsdoiventavoirlemêmeSRID

Page 19: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

dmr_augmente%>%group_by(INSEE,ANNEE_DECLARATION)%>%summarise(NbIncidents=n())%>%ungroup()%>%head()

##Simplefeaturecollectionwith6featuresand3fields##geometrytype:MULTIPOINT##dimension:XY##bbox:xmin:650249.7ymin:6861822xmax:653644ymax:6863729##epsg(SRID):2154##proj4string:+proj=lcc+lat_1=49+lat_2=44+lat_0=46.5+lon_0=3+x_0=700000+y_0=6600000+ellps=GRS80+towgs84=0,0,0,0,0,0,0+units=m+no_defs###Atibble:6x4##INSEEANNEE_DECLARATIONNbIncidentsgeometry##<chr><int><int><MULTIPOINT[m]>##1751012015653(650305.86862792,650334.46862783…##2751012017931(650249.76862872,650305.96862792…##37510220151028(650926.96863572,650981.86863589…##47510220171492(650871.46863497,650915.66863514…##57510320152316(652345.86862636,652346.36862627…##67510320172466(652343.76862637,652343.96862646…

dmr_augmente<-dmr_spatial%>%st_join(irisParis%>%select(INSEE),join=st_within)

Page 20: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

sfPostGIS

st_agr st_area st_bbox st_bind_cols st_boundary st_buffer st_castst_centroid st_combine st_contains st_contains_properly st_convex_hullst_covered_by st_covers st_crop st_crosses st_difference st_disjointst_distance st_equals st_equals_exact st_graticule st_interpolate_awst_intersection st_intersects st_is_simple st_is_validst_is_within_distance st_jitter st_join st_layers st_lengthst_line_merge st_line_sample st_make_grid st_overlapsst_point_on_surface st_polygonize st_relate st_sample st_segmentizest_simplify st_snap st_sym_difference st_touches st_transformst_triangulate st_union st_viewport st_voronoi st_withinst_wrap_dateline st_write st_write_db st_zm write_sf

sf

Page 21: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

nbIncidentsIris<-dmr_spatial%>%st_join(irisParis%>%select(INSEE),join=st_within)%>%st_set_geometry(NULL)%>%#Onn'aplusbesoinquecesoitunobjetspatialgroup_by(INSEE,ANNEE_DECLARATION)%>%summarise(NbIncidents=n())%>%ungroup()

irisParis_Incidents<-irisParis%>%mutate(surface=st_area(.))%>%left_join(nbIncidentsIris,by="INSEE")

densite_incidents<-irisParis_Incidents%>%mutate(densiteIncidents=NbIncidents/(surface/1E6))%>%#Pouravoirunedensité/km²select(INSEE,CODE_,ANNEE_DECLARATION,NbIncidents,densiteIncidents)

#Lesdensitéssontexpriméesavecdes"unités",quicomplexifientletraitement.#Onlesenlèvedonc:

densite_incidents<-densite_incidents%>%mutate(densiteIncidents=units::drop_units(densiteIncidents))

Page 22: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

mapviewdensite2015<-filter(densite_incidents,ANNEE_DECLARATION==2015)densite2017<-filter(densite_incidents,ANNEE_DECLARATION==2017)

map2015<-mapview(densite2015,zcol="densiteIncidents",legend=TRUE)map2017<-mapview(densite2017,zcol="densiteIncidents",legend=TRUE)

latticeView(map2013,map2017,ncol=2)

Page 23: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

mapview

+−

Leaflet

+−

Leaflet

Page 24: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

ggplot2ggplot2

sf geom_sf

ggplot(densite_incidents)+geom_sf(data=densite_incidents,aes(fill=densiteIncidents),lwd=0)+#Onestobligéderemettrelacouchesfendatafacet_wrap(~ANNEE_DECLARATION)+scale_fill_viridis_c(name="Incidents/Km²")+guides(fill=guide_colourbar(title.position="top"))+coord_sf()+#Ons'assurequelescoordonnéesdugraphiquerespectentleCRStheme_minimal()+theme(legend.position="bottom",legend.key.width=unit(2,"cm"))

Page 25: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

ggplot2ggsn ggmap

ggplot

tmap cartography

Page 26: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

cartography

library(cartography)#Oncommenceparafficherunfonddecartepar(mar=c(0,0,0,0))plot(st_geometry(arrondissements_Paris),col="#1F4257",border="#B6C1C8",bg="#708694",lwd=2)

#OnajoutedessymbolesproportionnelspourlesincidentspropSymbolsLayer(x=densite_incidents%>%filter(ANNEE_DECLARATION==2017),var="NbIncidents",col="#F97B64",inches=0.1,legend.title.txt="NombrededéclarationsparIRISen2017")

#OnajouteleslabelsdesarrondissementslabelLayer(x=arrondissements_Paris%>%mutate(CP=as.numeric(INSEE)-100),txt="CP",col="#B6C1C8",bg="#1F4257",halo=TRUE,overlap=FALSE,show.lines=FALSE)

Page 27: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

cartography

Page 28: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

leafletmapview

leaflet

library(leaflet)densite_incidents2017<-densite_incidents%>%filter(ANNEE_DECLARATION==2017)%>%st_transform(4326)#LeafletrequiertunecoucheenWGS84

seuils<-quantile(densite_incidents2017$densiteIncidents,probs=seq(from=0,to=1,by=0.2))

colorPalette<-colorBin("YlOrRd",domain=densite_incidents2017$densiteIncidents,bins=seuils)

infosPopup<-sprintf("<strong>%s</strong><br/>%sincidents(%.1f/km²)",densite_incidents2017$CODE_,densite_incidents2017$NbIncidents,densite_incidents2017$densiteIncidents)%>%lapply(htmltools::HTML)

leaflet(data=densite_incidents2017)%>%addProviderTiles(providers$CartoDB.DarkMatterNoLabels)%>%addPolygons(fillColor=~colorPalette(densiteIncidents),fillOpacity=0.7,color="white",weight=.5,opacity=1,dashArray="3",label=infosPopup)%>%addLegend(pal=colorPalette,values=~densiteIncidents,opacity=0.7,title="Densitésd'incidents<br/>[incident/km²]",position="topright")

Page 29: sp - Sciencesconf.org · sf sf iris_Paris_sf ## Simple feature collection with 992 features and 77 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: 643075.6 …

leafletmapview

leaflet

+−

Leaflet