Need: clip/select/cut/subset (or just show) the values of a USA raster including the Contiguous US, Alaska...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
GIS people, I need to clip/select/cut/subset (or just show) the values of a USA raster including the mainland, Alaska and Hawaii. It is confusing to see the big map including some Islands or territories very far away. So, I have been trying to select/cut the raster to only include USA mainland, Alaska and Hawaii and then do the visualization. The code I have developed is as follow:
library(rgdal)
library(raster)
state <- getData("GADM", country="USA", level=1)
projection(state) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
nestates <- c("Alabama","Arizona", "Arkansas", "California", "Colorado", # Contiguous/Continental United States
"Connecticut", "Delaware", "Florida", "Georgia", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New
Jersey", "New Mexico", "New York", "North Carolina",
"North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island","South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
"Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming",
"Alaska", "Hawaii") # I tried excluding Hawaii too
# I believe the issue is with insular territories
state.sub <- state[as.character(state@data$STATE_NAME) %in% nestates, ]
elevation <- raster("USA_1.tif")
elevation.sub <- crop(elevation, extent(state.sub))
elevation.sub <- mask(elevation.sub, state.sub) # Error in x@polygons[[i]] : subscript out of bounds
plot(elevation.sub)
plot(state.sub, add = TRUE)
The output, so far:
The reproducible example:
Need something like this:
I already tried this, this and this ones.
Any help is very much appreciated.
r raster r-raster sp rgdal
add a comment |
GIS people, I need to clip/select/cut/subset (or just show) the values of a USA raster including the mainland, Alaska and Hawaii. It is confusing to see the big map including some Islands or territories very far away. So, I have been trying to select/cut the raster to only include USA mainland, Alaska and Hawaii and then do the visualization. The code I have developed is as follow:
library(rgdal)
library(raster)
state <- getData("GADM", country="USA", level=1)
projection(state) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
nestates <- c("Alabama","Arizona", "Arkansas", "California", "Colorado", # Contiguous/Continental United States
"Connecticut", "Delaware", "Florida", "Georgia", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New
Jersey", "New Mexico", "New York", "North Carolina",
"North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island","South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
"Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming",
"Alaska", "Hawaii") # I tried excluding Hawaii too
# I believe the issue is with insular territories
state.sub <- state[as.character(state@data$STATE_NAME) %in% nestates, ]
elevation <- raster("USA_1.tif")
elevation.sub <- crop(elevation, extent(state.sub))
elevation.sub <- mask(elevation.sub, state.sub) # Error in x@polygons[[i]] : subscript out of bounds
plot(elevation.sub)
plot(state.sub, add = TRUE)
The output, so far:
The reproducible example:
Need something like this:
I already tried this, this and this ones.
Any help is very much appreciated.
r raster r-raster sp rgdal
add a comment |
GIS people, I need to clip/select/cut/subset (or just show) the values of a USA raster including the mainland, Alaska and Hawaii. It is confusing to see the big map including some Islands or territories very far away. So, I have been trying to select/cut the raster to only include USA mainland, Alaska and Hawaii and then do the visualization. The code I have developed is as follow:
library(rgdal)
library(raster)
state <- getData("GADM", country="USA", level=1)
projection(state) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
nestates <- c("Alabama","Arizona", "Arkansas", "California", "Colorado", # Contiguous/Continental United States
"Connecticut", "Delaware", "Florida", "Georgia", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New
Jersey", "New Mexico", "New York", "North Carolina",
"North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island","South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
"Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming",
"Alaska", "Hawaii") # I tried excluding Hawaii too
# I believe the issue is with insular territories
state.sub <- state[as.character(state@data$STATE_NAME) %in% nestates, ]
elevation <- raster("USA_1.tif")
elevation.sub <- crop(elevation, extent(state.sub))
elevation.sub <- mask(elevation.sub, state.sub) # Error in x@polygons[[i]] : subscript out of bounds
plot(elevation.sub)
plot(state.sub, add = TRUE)
The output, so far:
The reproducible example:
Need something like this:
I already tried this, this and this ones.
Any help is very much appreciated.
r raster r-raster sp rgdal
GIS people, I need to clip/select/cut/subset (or just show) the values of a USA raster including the mainland, Alaska and Hawaii. It is confusing to see the big map including some Islands or territories very far away. So, I have been trying to select/cut the raster to only include USA mainland, Alaska and Hawaii and then do the visualization. The code I have developed is as follow:
library(rgdal)
library(raster)
state <- getData("GADM", country="USA", level=1)
projection(state) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
nestates <- c("Alabama","Arizona", "Arkansas", "California", "Colorado", # Contiguous/Continental United States
"Connecticut", "Delaware", "Florida", "Georgia", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New
Jersey", "New Mexico", "New York", "North Carolina",
"North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island","South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
"Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming",
"Alaska", "Hawaii") # I tried excluding Hawaii too
# I believe the issue is with insular territories
state.sub <- state[as.character(state@data$STATE_NAME) %in% nestates, ]
elevation <- raster("USA_1.tif")
elevation.sub <- crop(elevation, extent(state.sub))
elevation.sub <- mask(elevation.sub, state.sub) # Error in x@polygons[[i]] : subscript out of bounds
plot(elevation.sub)
plot(state.sub, add = TRUE)
The output, so far:
The reproducible example:
Need something like this:
I already tried this, this and this ones.
Any help is very much appreciated.
r raster r-raster sp rgdal
r raster r-raster sp rgdal
asked Jan 3 at 16:58
Diego MoyaDiego Moya
327
327
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
As an alternative, consider that package spData provides polygon boundaries for the
US already splitted for contigouous US, Alaska, Hawaii and other islands that
you can use directly. For example, using an elevation raster downloaded with
raster::getData
:
library(spData)
library(sf)
library(raster)
elev <- raster::getData("alt", country="USA", level=1)
#> returning a list of RasterLayer objects
usa <- spData::us_states %>% sf::st_transform(4326)
hawaii <- spData::hawaii %>% sf::st_transform(4326)
alaska <- spData::alaska %>% sf::st_transform(4326)
# crop raster to area of interest and plot (Note that the data downloaded with `raster::getData`
# is split in four subdatasets, so in this case you need to select the correct one.)
usa_elev <- crop(elev[[1]], usa)
plot(usa_elev)
plot(sf::st_geometry(usa), add = TRUE)
alaska_elev <- crop(elev[[2]], alaska)
plot(alaska_elev)
plot(st_geometry(alaska), add = TRUE)
hawaii_elev <- crop(elev[[4]], hawaii)
plot(hawaii_elev)
plot(st_geometry(hawaii), add = TRUE)
Created on 2019-01-04 by the reprex package (v0.2.1)
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
add a comment |
I have solved it in this way:
us<-getData('GADM', country='USA', level=1) #Get the County Shapefile for the US
nestates <- c("Alabama","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia",
"Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming")
#"Alaska" polygons include the far away Islands #"Hawaii"
# I followed these tutorials/Q&A
#http://data-analytics.net/wp-content/uploads/2014/09/geo2.html
#https://gis.stackexchange.com/questions/61243/clipping-a-raster-in-r/61278
ne = us[match(toupper(nestates),toupper(us$NAME_1)),]
raster_c <- crop(raster_1, extent(ne))
I got this:
Hope it might help others
add a comment |
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%2f54026612%2fneed-clip-select-cut-subset-or-just-show-the-values-of-a-usa-raster-including%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As an alternative, consider that package spData provides polygon boundaries for the
US already splitted for contigouous US, Alaska, Hawaii and other islands that
you can use directly. For example, using an elevation raster downloaded with
raster::getData
:
library(spData)
library(sf)
library(raster)
elev <- raster::getData("alt", country="USA", level=1)
#> returning a list of RasterLayer objects
usa <- spData::us_states %>% sf::st_transform(4326)
hawaii <- spData::hawaii %>% sf::st_transform(4326)
alaska <- spData::alaska %>% sf::st_transform(4326)
# crop raster to area of interest and plot (Note that the data downloaded with `raster::getData`
# is split in four subdatasets, so in this case you need to select the correct one.)
usa_elev <- crop(elev[[1]], usa)
plot(usa_elev)
plot(sf::st_geometry(usa), add = TRUE)
alaska_elev <- crop(elev[[2]], alaska)
plot(alaska_elev)
plot(st_geometry(alaska), add = TRUE)
hawaii_elev <- crop(elev[[4]], hawaii)
plot(hawaii_elev)
plot(st_geometry(hawaii), add = TRUE)
Created on 2019-01-04 by the reprex package (v0.2.1)
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
add a comment |
As an alternative, consider that package spData provides polygon boundaries for the
US already splitted for contigouous US, Alaska, Hawaii and other islands that
you can use directly. For example, using an elevation raster downloaded with
raster::getData
:
library(spData)
library(sf)
library(raster)
elev <- raster::getData("alt", country="USA", level=1)
#> returning a list of RasterLayer objects
usa <- spData::us_states %>% sf::st_transform(4326)
hawaii <- spData::hawaii %>% sf::st_transform(4326)
alaska <- spData::alaska %>% sf::st_transform(4326)
# crop raster to area of interest and plot (Note that the data downloaded with `raster::getData`
# is split in four subdatasets, so in this case you need to select the correct one.)
usa_elev <- crop(elev[[1]], usa)
plot(usa_elev)
plot(sf::st_geometry(usa), add = TRUE)
alaska_elev <- crop(elev[[2]], alaska)
plot(alaska_elev)
plot(st_geometry(alaska), add = TRUE)
hawaii_elev <- crop(elev[[4]], hawaii)
plot(hawaii_elev)
plot(st_geometry(hawaii), add = TRUE)
Created on 2019-01-04 by the reprex package (v0.2.1)
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
add a comment |
As an alternative, consider that package spData provides polygon boundaries for the
US already splitted for contigouous US, Alaska, Hawaii and other islands that
you can use directly. For example, using an elevation raster downloaded with
raster::getData
:
library(spData)
library(sf)
library(raster)
elev <- raster::getData("alt", country="USA", level=1)
#> returning a list of RasterLayer objects
usa <- spData::us_states %>% sf::st_transform(4326)
hawaii <- spData::hawaii %>% sf::st_transform(4326)
alaska <- spData::alaska %>% sf::st_transform(4326)
# crop raster to area of interest and plot (Note that the data downloaded with `raster::getData`
# is split in four subdatasets, so in this case you need to select the correct one.)
usa_elev <- crop(elev[[1]], usa)
plot(usa_elev)
plot(sf::st_geometry(usa), add = TRUE)
alaska_elev <- crop(elev[[2]], alaska)
plot(alaska_elev)
plot(st_geometry(alaska), add = TRUE)
hawaii_elev <- crop(elev[[4]], hawaii)
plot(hawaii_elev)
plot(st_geometry(hawaii), add = TRUE)
Created on 2019-01-04 by the reprex package (v0.2.1)
As an alternative, consider that package spData provides polygon boundaries for the
US already splitted for contigouous US, Alaska, Hawaii and other islands that
you can use directly. For example, using an elevation raster downloaded with
raster::getData
:
library(spData)
library(sf)
library(raster)
elev <- raster::getData("alt", country="USA", level=1)
#> returning a list of RasterLayer objects
usa <- spData::us_states %>% sf::st_transform(4326)
hawaii <- spData::hawaii %>% sf::st_transform(4326)
alaska <- spData::alaska %>% sf::st_transform(4326)
# crop raster to area of interest and plot (Note that the data downloaded with `raster::getData`
# is split in four subdatasets, so in this case you need to select the correct one.)
usa_elev <- crop(elev[[1]], usa)
plot(usa_elev)
plot(sf::st_geometry(usa), add = TRUE)
alaska_elev <- crop(elev[[2]], alaska)
plot(alaska_elev)
plot(st_geometry(alaska), add = TRUE)
hawaii_elev <- crop(elev[[4]], hawaii)
plot(hawaii_elev)
plot(st_geometry(hawaii), add = TRUE)
Created on 2019-01-04 by the reprex package (v0.2.1)
answered Jan 4 at 21:19
lbusettlbusett
3,66921434
3,66921434
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
add a comment |
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
Great solution! sorry for the late comment.
– Diego Moya
Jan 29 at 23:18
add a comment |
I have solved it in this way:
us<-getData('GADM', country='USA', level=1) #Get the County Shapefile for the US
nestates <- c("Alabama","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia",
"Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming")
#"Alaska" polygons include the far away Islands #"Hawaii"
# I followed these tutorials/Q&A
#http://data-analytics.net/wp-content/uploads/2014/09/geo2.html
#https://gis.stackexchange.com/questions/61243/clipping-a-raster-in-r/61278
ne = us[match(toupper(nestates),toupper(us$NAME_1)),]
raster_c <- crop(raster_1, extent(ne))
I got this:
Hope it might help others
add a comment |
I have solved it in this way:
us<-getData('GADM', country='USA', level=1) #Get the County Shapefile for the US
nestates <- c("Alabama","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia",
"Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming")
#"Alaska" polygons include the far away Islands #"Hawaii"
# I followed these tutorials/Q&A
#http://data-analytics.net/wp-content/uploads/2014/09/geo2.html
#https://gis.stackexchange.com/questions/61243/clipping-a-raster-in-r/61278
ne = us[match(toupper(nestates),toupper(us$NAME_1)),]
raster_c <- crop(raster_1, extent(ne))
I got this:
Hope it might help others
add a comment |
I have solved it in this way:
us<-getData('GADM', country='USA', level=1) #Get the County Shapefile for the US
nestates <- c("Alabama","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia",
"Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming")
#"Alaska" polygons include the far away Islands #"Hawaii"
# I followed these tutorials/Q&A
#http://data-analytics.net/wp-content/uploads/2014/09/geo2.html
#https://gis.stackexchange.com/questions/61243/clipping-a-raster-in-r/61278
ne = us[match(toupper(nestates),toupper(us$NAME_1)),]
raster_c <- crop(raster_1, extent(ne))
I got this:
Hope it might help others
I have solved it in this way:
us<-getData('GADM', country='USA', level=1) #Get the County Shapefile for the US
nestates <- c("Alabama","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia",
"Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming")
#"Alaska" polygons include the far away Islands #"Hawaii"
# I followed these tutorials/Q&A
#http://data-analytics.net/wp-content/uploads/2014/09/geo2.html
#https://gis.stackexchange.com/questions/61243/clipping-a-raster-in-r/61278
ne = us[match(toupper(nestates),toupper(us$NAME_1)),]
raster_c <- crop(raster_1, extent(ne))
I got this:
Hope it might help others
answered Jan 3 at 21:15
Diego MoyaDiego Moya
327
327
add a comment |
add a comment |
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%2f54026612%2fneed-clip-select-cut-subset-or-just-show-the-values-of-a-usa-raster-including%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