# Calculate cell distance between cells

I want to calculate the cell distances between all cells within a feature class grid. Exactly the same as the game Minesweeper. I have included an example below. The original data contains 11000 cells with different labels but I have simplified in the example:

For each cell I want to find how many cells apart they are. Example here is for cell A13. I would then iterate this for every cell in the grid to produce the final csv. I have tried cost distance & allocation in model builder but neither will work as the cells are rectangles and not squares which is returning decimal places in the final result.

• So are you asking to generate 11,000 rasters each with a distance from a specific cell? Feb 5 '19 at 16:21
• Also to my knowledge ESRI software only works with rasters composed of square cells. Feb 5 '19 at 16:22
• I want to know the distance (in cells) from every cell to every cell as a matrix table. The data is in the format of a feature class grid. Feb 5 '19 at 16:26
• Would you mind to use an R code to do so? I can post an example if you like. This kind of mass production could be done may be easily in R. As Hornbydd said, you will need to produce 11,000 csv files. Feb 5 '19 at 16:48
• Could I take up your offer for the R code example please? Feb 6 '19 at 14:44

Here I have an alternative where you can set your own parameters, file names, etc, but using R (not big deal). Here is data for tryout: https://www.dropbox.com/s/a40kk9gl1128qcl/RasterDistance.zip?dl=0

Here is the code:

``````r <- read.csv2("ejemplo") #change ejemplo by your path file
r <- rasterFromXYZ(r[, c('x', 'y', 'value')])

#create a matrix with same dim as r and starting in position 1, 1
r2 <- raster(nrow = r@ncols, ncols = r@nrows, extent(1, r@ncols+1, 1, r@nrows+1), res=1)
r2[] <- seq(r2@nrows*r@ncols)
r2 <- transition(r2, transitionFunction = function(x){1}, directions = 4, symm=F)

#ITERATE OVER EACH CELL AND SAVE CSV
for (x in 1:(ncol(r2))) {
for (y in 1:(nrow(r2))) {
yrev<-(length(1:(nrow(r2)))+2) - y #reverse in order to start in upper left corner
cell <- c(x,yrev)
rcost  <- accCost(r2,cell)
r3 <- r
r3@data@values <- rcost@data@values
name=paste0(y,"_",x)
write.csv2(as.data.frame(rasterToPoints(r3)), file=paste0(getwd(),"/output/",name))
}
}
``````

Then you can check your results, for example something like:

``````#read one of those files
rtry1 <- rasterFromXYZ(rtry1[, c('x', 'y', 'value')])
rtry2 <- rasterFromXYZ(rtry2[, c('x', 'y', 'value')])

#plots
m <- rbind(c(1,2))
layout(m)
#plot 1
plot(rtry1, main="Raster from cell 3_3", xlab = "X coord", ylab = "Y coord")
text(rtry1)
#plot2
plot(rtry2, main="Raster from cell 6_6", xlab = "X coord", ylab = "Y coord")
text(rtry2)
``````

This is what you get: But remember, you will have one file for each of the cells (11.000 files in your case).

Calculate polygon neighbours and use output in this script:

``````import arcpy
import networkx as nx
G=nx.Graph()
with arcpy.da.SearchCursor("neighbours", ["src_FID","nbr_FID"]) as cursor:
for f,t in cursor: 