1

Using the the shapefie "roi.shp" provided. Extract values from the Landsat image and plot the spectral profiles of the different materials i.e Water,Urban or built-up areas, roads, and vegetation.

I have the folowing piece of code:

##Load shape files
samp <- readOGR(paste0(layer="roi.shp")) ##roi.shp is a shapefile for a region of interest

##Generate point samples
ptsamp <- spsample(samp, 300, type='regular')

##Add land cover class
ptsamp$class <- over(ptsamp, samp)$class

# extract values with points
df <- extract(s, ptsamp)

# To see some of the reflectance values
head(df)

The following error is given on the 'ptsamp$class <- over(ptsamp, samp)$class' line

Error in validObject(.Object) : invalid class “SpatialPointsDataFrame” object: number of rows in data.frame and SpatialPoints don't match
1

Are you sure there's a class column in the shapefile, and hence in samp? I get your error if I try and assign a non-existent value from samp:

> ptsamp$class = over(ptsamp, samp)$class
Error in validObject(.Object) : 
  invalid class “SpatialPointsDataFrame” object: number of rows in data.frame and SpatialPoints don't match

but with one that does, it works:

> ptsamp$AFF = over(ptsamp, samp)$AFF
> names(samp)
 [1] "SP_ID"  "NAME"   "ID_x"   "COUNT"  "SMR"    "LONG"   "LAT"    "PY"    
 [9] "EXP_"   "AFF"    "X_COOR" "Y_COOR" "ID_y"  

Check class is in names(samp). You've not given us your shapefile so we can't test.

I've used the scot_BNG data from example(readOGR).

1
  • I changed the code to : ptsamp$CLASS_NAME <- over(ptsamp, samp)$CLASS_NAME which solved my issue thank you! – Jillian Wittstock Aug 28 '20 at 14:33

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.