1

I am using the DistanceToPoints tool to make a raster where cell values contain the distance to the nearest point. enter image description here

r <- raster('./Raster.tif')

s <- readOGR('./Shapefile.shp')

dfp <- distanceFromPoints(r, s)

It is giving me an error: Error in .pointsToMatrix(xy) : A points matrix should have 2 columns

Raster and Point layer are in the same projection.

> summary(s)
Object of class SpatialPointsDataFrame
Coordinates:
              min         max
coords.x1 4352987 5334474.056
coords.x2 2180706 2887232.448
coords.x3       0    1100.361
Is projected: TRUE 
proj4string :
[+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs]
Number of points: 792668
Data attributes:
    OBJECTID            id        
 595    :416281   Min.   :   0    
 18     : 92323   1st Qu.:1486    
 261    : 19468   Median :2972    
 463    : 17326   Mean   :2972    
 472    : 12768   3rd Qu.:4457    
 596    : 11914   Max.   :5943    
 (Other):222588   NA's   :786724 

As you can see in the picture, the raster is not one whole 'thing' but patchy. Do you think that could be the problem? The tool was working fine with another whole,'normal' raster.

1 Answer 1

2

Your points are three-dimensions - the clue is here in the summary:

Coordinates:
              min         max
coords.x1 4352987 5334474.056
coords.x2 2180706 2887232.448
coords.x3       0    1100.361

They must be XYZ or XYM geometry. There should be a way to drop the third dimension but failing that the function coordinates(s) should get the coordinates out and if that includes the third dimension then take the first two. Feed the distance function that matrix. Something like:

dfp <- distanceFromPoints(r, coordinates(s)[,1:2])

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.