How to crop an image using polygon shapefile in R?
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.
r crop raster shapefile
|
show 7 more comments
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.
r crop raster shapefile
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 8: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 1: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 1:28
I was asking for a reproducible example. Granted, it can be a bit tricky with spatial data. Let's do this: just execute the tow objectsstackImage
andshp
, and post the console output into your question (not the comment section)
– Val
Nov 22 '18 at 8:14
@Val I have posted the exact codes I hope this is what you have asked for.
– amit haldar
Nov 22 '18 at 9:37
|
show 7 more comments
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.
r crop raster shapefile
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.
r crop raster shapefile
r crop raster shapefile
edited Nov 22 '18 at 10:02
amit haldar
asked Nov 20 '18 at 8:21
amit haldaramit haldar
66
66
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 8: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 1: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 1:28
I was asking for a reproducible example. Granted, it can be a bit tricky with spatial data. Let's do this: just execute the tow objectsstackImage
andshp
, and post the console output into your question (not the comment section)
– Val
Nov 22 '18 at 8:14
@Val I have posted the exact codes I hope this is what you have asked for.
– amit haldar
Nov 22 '18 at 9:37
|
show 7 more comments
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 8: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 1: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 1:28
I was asking for a reproducible example. Granted, it can be a bit tricky with spatial data. Let's do this: just execute the tow objectsstackImage
andshp
, and post the console output into your question (not the comment section)
– Val
Nov 22 '18 at 8:14
@Val I have posted the exact codes I hope this is what you have asked for.
– amit haldar
Nov 22 '18 at 9:37
3
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 8:38
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 8: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 1:19
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 1: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 1:28
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 1:28
I was asking for a 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 8:14
I was asking for a 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 8:14
@Val I have posted the exact codes I hope this is what you have asked for.
– amit haldar
Nov 22 '18 at 9:37
@Val I have posted the exact codes I hope this is what you have asked for.
– amit haldar
Nov 22 '18 at 9:37
|
show 7 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388826%2fhow-to-crop-an-image-using-polygon-shapefile-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388826%2fhow-to-crop-an-image-using-polygon-shapefile-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 8: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 1: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 1:28
I was asking for a reproducible example. Granted, it can be a bit tricky with spatial data. Let's do this: just execute the tow objects
stackImage
andshp
, and post the console output into your question (not the comment section)– Val
Nov 22 '18 at 8:14
@Val I have posted the exact codes I hope this is what you have asked for.
– amit haldar
Nov 22 '18 at 9:37