0

Can someone help me understand why I have issue getting my dataframe (point coordinates into the same coordinate with my Raster data?

My dataframe with point files (lets call it trainData_crs)

trainData_crs<- read.csv(file.csv)  

class(trainData_crs)
[1] "data.frame"

The data frame had no coordinate system

crs(trainData_crs)
[1] NA




summary(trainData_crs)
 PlotType      Class                      Latitude       Longitude    
 closedforest:22   Min.   :1.000   Min.   :17.81   Min.   :24.72  
 grass       :34   1st Qu.:2.000   1st Qu.:17.86   1st Qu.:24.98  
 lowforest   :24   Median :3.000   Median :17.92   Median :25.02  
 mediumforest:26   Mean   :2.904   Mean   :17.95   Mean   :25.02  
 shrubs      : 4   3rd Qu.:4.000   3rd Qu.:18.01   3rd Qu.:25.10  
 unknown     : 3   Max.   :7.000   Max.   :18.46   Max.   :25.16  
 water       : 1 

Create a TIFF raster and convert it to Rasterbricks

all_landsat_bands<-list.files ("file.tif")

img <- brick(all_landsat_bands)

img <- brick(LandsatAprJune_2019_st)
 class(img)
[1] "RasterBrick"
attr(,"package")
[1] "raster"
 names(img)
[1] "B1"  "B2"  "B3"  "B4"  "B5"  "B6"  "B7"  "B10" "B11"

First convert data frame to Latitude and Longitude coordinate numbers

coordinates(trainData_crs)<-~Latitude +Longitude

proj4string(trainData_crs) = CRS("+init=epsg:4326")

Convert to same projection

trainData_crs_sp <- spTransform(trainData_crs, crs(img))

Confirm if points and image are in same coordinates

crs(trainData_crs_sp)
CRS arguments:
 +proj=utm +zone=34 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-138,-105,-289,0,0,0,0 +units=m
+no_defs 

 crs(img)
CRS arguments:
 +proj=utm +zone=34 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-138,-105,-289,0,0,0,0 +units=m
+no_defs 

Plot them to check if the points overlap with rasterBrick

plotRGB(img,
    r = 4, g = 3, b = 2,
    stretch = "lin",
    scale=1000,
    axes = TRUE,
    main = "RGB composite image\n Landsat Bands 7, 3, 2"); plot(trainData_crs_sp, add=T)

But the difficulty that I have is that, even after converting the dataframe of points to rasterbrick coordinates, they still do not fall in the same area or overlap.

1
  • Can you please show us the results of sp::bbox(trainData_crs_sp) and raster::extent(img)? Commented Aug 23, 2022 at 3:50

1 Answer 1

0

A data.frame will not have a CRS unless it is turned into a spatial object.

Your workflow should look like this, I am using the sf library here instead of sp since it's more up to date. The next step is figuring out what your CRS should be for the trainData, which may come from your metadata or whoever collected the data. In your example above, it looks like you are incorrectly assigning the CRS using the proj4string instead of CRS in sp.

trainData_crs<- read.csv(file.csv) 

trainData_sf<-st_as_sf(trainData_crs, coords=c("Latitude","Longitude"), crs="+init=epsg:4326")

Then to project use st_transform based on the projection that your raster is in.

1
  • The op is turning the data.frame into a spatial object, just using the sp library rather than sf. All you are doing here is showing an identical workflow in sf. Commented Aug 23, 2022 at 3:48

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.