2

I have a set of polygon features in a GeoJSON file. They are neigbourhoods in Chicago. I would like to produce an adjacency matrix A, in CSV format or any other form, where the i,j element of A is 1 if Pi is adjacent to Pj (i.e. they are touching).

Is there any tool that can easily read the file and do this?

  • 1
    Are you prepared to use Postgis? You could import the GeoJSON to geometries, run a spatial self-join using ST_Touches and then export to csv using Postgres's Copy command. There are undoubtedly other tools, but that is how I would do it. – John Powell Apr 15 '15 at 21:50
3

You can do this in R using gTouches in the rgeos package.
Use rgdal package to open your geojson:

library(rgeos)
library(rgdal)

polys <- readOGR("polys.json", "OGRGeoJSON")
gTouches(polys, byid=TRUE)

With success, will produce an adjacency matrix in the form of:

##       0     1     2     3   
## 0 FALSE  TRUE  TRUE FALSE 
## 1 FALSE FALSE FALSE  TRUE 
## 2  TRUE FALSE  TRUE  TRUE
  • Do you know something to do the same in Python, @toms? – pceccon Feb 22 '16 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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