0
library(raster)
library(rgdal)

Image

RGBimage <- stack("E:/Amit Haldar/Work/1.0 Projects/RGB/D035_Georef.tif")
stackImage <- stack(RGBimage[[1]], RGBimage[[2]], RGBimage[[3]])
plotRGB(stackImage)

Raster (RGB) image

Shapefile

Fiber <- readOGR("E:/Amit Haldar/Work/1.0 Projects/Shp/Landuse2018.shp")
data <- Fiber
data$vb<- paste(data$SECTOR, data$ESTATE, data$COMPNO, sep="")
shp <- subset(data, vb=="D035")
crs(shp) <- "+proj=utm +zone=47 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
shp_simp <- gSimplify(shp, tol = 3, topologyPreserve = TRUE)
plot(shp_simp)

shape boundary

print(shp_simp)
class       : SpatialPolygons 
features    : 1 
extent      : 759548.4, 760468.4, -13233.4, -12185.39  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 

print(stackImage)
class       : RasterStack 
dimensions  : 21660, 20544, 444983040, 3  (nrow, ncol, ncell, nlayers)
resolution  : 0.0562666, 0.0562666  (x, y)
extent      : 759433.3, 760589.2, 9986667, 9987886  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=47 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : TEW_D035_Georef.1, TEW_D035_Georef.2, TEW_D035_Georef.3 
min values  :                 0,                 0,                 0 
max values  :               255,               255,               255 

I am trying to crop the image using the shape file. The shape file extent is not necessarily same as the image. The polygon covers the part of image. I have tried few methods

proj4string(stackImage) <- proj4string(shp_simp)

Then

cropImage <- crop(stackImage, extent(shp_simp), snap="out")

Error in .local(x, y, ...) : extents do not overlap

I tried to transform the projection

compExtent <- spTransform(shp_simp, CRS(proj4string(stackImage)))

cropImage <- crop(stackImage, extent(compExtent), snap="out")

Still same error

Error in .local(x, y, ...) : extents do not overlap

Where I am going wrong? I appreciate your support in advance.

amit haldar
  • 129
  • 1
  • 10
  • 3
    Looks like your extents don't overlap, as the error suggests. But it's hard to help without a reproducible example. Please consider adding one which reflects your data and reproduces the error. – Val Nov 20 '18 at 08:38
  • Hi Val, I didn't quite understand what you are asking. reflecting my data you mean the shapefile and the image I am using? those I put as an example actual is RGBimage <- stack("E:/Amit Haldar/Work/1.0 Projects/Operational Planning/RGB/SlantRange_RGBassessment/TEW_D035_Georef.tif") stackImage <- stack(RGBimage[[1]], RGBimage[[2]], RGBimage[[3]]) crs(stackImage) <- "+proj=utm +zone=47 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" – amit haldar Nov 22 '18 at 01:19
  • I think the major issue is I am not able to fix the projection for both of the files. Though when I check projections it is showing same for both. I am not sure where I am going wrong. – amit haldar Nov 22 '18 at 01:28
  • I was asking for a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Granted, it can be a bit tricky with spatial data. Let's do this: just execute the tow objects `stackImage` and `shp`, and post the console output into your question (not the comment section) – Val Nov 22 '18 at 08:14
  • @Val I have posted the exact codes I hope this is what you have asked for. – amit haldar Nov 22 '18 at 09:37
  • I'm interested in the console output of `stackImage` and `kerinciFiber`. Please add that to your question – Val Nov 22 '18 at 09:41
  • you mean the plots right? I have tried to paste the screenshots of the outputs shape boundary and the stackImage. It might not be appearing in the question due to IT restriction here; however there should be a link to access isn't it? – amit haldar Nov 22 '18 at 09:51
  • No, not the plots. Just run `print(stackImage)` and `print(kerinciFiber)`, and post the outputs – Val Nov 22 '18 at 09:53
  • Yes, please check now – amit haldar Nov 22 '18 at 10:03
  • I need to see `Fiber` or `shp` __before__ you run `crs()` – Val Nov 22 '18 at 10:07
  • @Val Hi hope you are doing great. Did you find any solution yet? – amit haldar Nov 26 '18 at 02:28
  • Please see my previous comment. – Val Nov 26 '18 at 08:09

0 Answers0