2

I´m very new at these geospatial data analysis and manipulation in R. Right now I have a shapefile that you can find here: http://sina.conagua.gob.mx/sina/tema.php?tema=cuencas&ver=mapa&o=0&n=nacional if you click in "opciones" and then "Descargar capa 2020". The name of the file is 'MexicoCuencas_disponibilidad_2020.shp'.

enter image description here

I don't need all of the polygons, just the ones in the south of Mexico (I already filtered my data set).

What I want to do is to create an adjacent matrix with this polygons. I've seen that maybe I could do it with the rgeos or rgdal packages, but I don't fully understand what they are doing or if the data I have is compatible with these libraries.

1 Answer 1

2

Check the following code:

# Load packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(mapview)
library(spdep)
#> Loading required package: sp
#> Loading required package: spData
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
    
# data
cuencas <- st_read("Cuencas_Disponibilidad_2020/Cuencas_Disponibilidad_2020.shp")
#> Reading layer `Cuencas_Disponibilidad_2020' from data source 
#>   `C:\Users\Utente\Desktop\Cuencas_Disponibilidad_2020\Cuencas_Disponibilidad_2020.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 757 features and 11 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1071153 ymin: 319366.3 xmax: 4074870 ymax: 2349614
#> Projected CRS: CCL_ITRF_1992
    
# display data
mapview(cuencas)

# estimate first order adjacency matrix
cuencas_mat <- nb2mat(poly2nb(cuencas), style = "B")
cuencas_mat[1:10, 1:10]
#>    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> 1     0    1    0    1    1    1    0    0    0     0
#> 2     1    0    1    0    1    0    0    0    0     0
#> 3     0    1    0    0    1    0    0    0    0     0
#> 4     1    0    0    0    0    1    0    0    0     0
#> 5     1    1    1    0    0    1    0    1    1     0
#> 6     1    0    0    1    1    0    1    1    0     0
#> 7     0    0    0    0    0    1    0    1    0     0
#> 8     0    0    0    0    1    1    1    0    1     1
#> 9     0    0    0    0    1    0    0    1    0     1
#> 10    0    0    0    0    0    0    0    1    1     0

Created on 2021-07-07 by the reprex package (v2.0.0)

I think you may want to read the following book to understand more about geographic data in R: https://geocompr.robinlovelace.net/ Check also the help pages of ?nb2mat and ?poly2nb for more details on creating the first order adjacency matrix. Moreover, if you have already created your own filter to select the desired polygon, just apply that filter before running the last operation.

1
  • That totally worked, thank you so much!!! Commented Jul 8, 2021 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.