What is the best way to calculate the Euclidean distance between the elements of two point vector layers, without rasterising the layers?

closed as unclear what you're asking by PolyGeo, BradHards, Devdatta Tengshe, Fezter, MappaGnosis Dec 4 '13 at 8:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Using what software package? (edit the question) – Vince Dec 3 '13 at 19:17
  • I am curious how you would do it with rasterizing the layers! – whuber Dec 3 '13 at 19:34
  • 1
    I'm assuming this is how he is doing with rasterizng the layers – GeoGhost Dec 3 '13 at 19:55
  • Welcome to gis.stackexchange! Please note that a good question is expected to include proof of basic research effort and - if applicable - code so far. Questions requesting code or instructions to copy&paste are generally not well received. – underdark Dec 3 '13 at 22:25
  • 1
    I think that this is a misunderstanding based on @Mar's lack of understanding in how the Euclidean Distance tool in ArcGIS works. – Jeffrey Evans Dec 4 '13 at 1:30

Although you provide very little information on this, here is a solution using R.

You can combine the two datasets and use the dist function to calculate a euclidean distance matrix. Here is where some more information would have come in handy. In any method you would end up with a matrix. Is this what you are after or do you want K-nearest neighbor (i.e., your 10 smallest distances for each point observation)? It would also be good to know what you are calculating distance on. Are you wanting euclidean distance on space (coordinates) or covariates? Depending on your actual question you could use spdep or yaImpute as well. If you are using covariates then this is an imputation problem and you want to use yaImpute.

Here is an R function to coerce distance matrices into a data.frame object that, with some additional manipulation, can be joined back to the source data. In the case of using the dist function to return euclidean distance you would just use the dmatrix.df function to coerce to a data.frame. Although, keep in mind that this represents all of the pair-wise distance values and, depending on number of observations, could be a massive matrix. The X1, X2 columns represent the row and column names from the matrix and in this case these are the rownames from the data.frame passed to the dist function. You can figure out how to deal with the pairwise values in the data.frame when you get there.

# Matrix to data.frame function
    dmatrix.df <- function(data) {
      values <- as.vector(data)
      dn <- dimnames(data)
        char <- sapply(dn, is.character)
        dn[char] <- lapply(dn[char], type.convert)
      indices <- do.call(expand.grid, dn)
       names(indices) <- varnames
      data.frame(indices, value=values)

    # Create example data and merge data
    x <- data.frame(ID=seq(1,n), x=runif(n,178605,181390), y=runif(n,329714, 333611))
    y <- data.frame(ID=seq(n+1,n+n), x=runif(n,178605,181390), y=runif(n,329714, 333611))
      xy <- rbind(x,y)
        rownames(xy) <- xy$ID

# Calculate distance matrix using x,y columns
( edist <- dist(xy[,2:3]) )

# Create a data.frame object to flatten matrix. 
( edist.df <- dmatrix.df(as.matrix(edist)) ) 

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