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.

enter image description here

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.

  • 1
    So are you asking to generate 11,000 rasters each with a distance from a specific cell? – Hornbydd Feb 5 at 16:21
  • Also to my knowledge ESRI software only works with rasters composed of square cells. – Hornbydd Feb 5 at 16:22
  • 1
    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. – FRMMO Feb 5 at 16:26
  • 3
    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. – César Arquero Feb 5 at 16:48
  • 1
    Could I take up your offer for the R code example please? – FRMMO Feb 6 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) 

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
        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 <- read.csv2("output/3_3")
rtry1 <- rasterFromXYZ(rtry1[, c('x', 'y', 'value')])
#read one of those files
rtry2 <- read.csv2("output/6_6")
rtry2 <- rasterFromXYZ(rtry2[, c('x', 'y', 'value')])

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

This is what you get: enter image description here

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
with arcpy.da.SearchCursor("neighbours", ["src_FID","nbr_FID"]) as cursor:
    for f,t in cursor:
for node in G.nodes():
    lengths=nx.single_source_dijkstra_path_length(G, node)
    if node!=10: continue
    csvfile = open('c:/scratch/%s.csv'%node, 'w')
    for item in lengths:
        csvfile.write(('%s,%s\n'%(item, lengths[item])))

Picture shows distances to cell 10:

enter image description here

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.