Large .img file processing in R (GIS) - r

I am working with U.S. National Landcover Dataset (NLCD) to classify habitat type at more than 150 sites across the northeast U.S. The dataset is very large (15GB) so I cannot upload it here, but it comes in .img format at 30m resolution.
I have GPS coordinates for the center point of all the sites. I would like to be able to extract the proportion of landcover classes in a 1 square kilometer around the point. My questions are:
1) How do I upload .img files into r?
2) How do I extract the information from around the GPS coordinates as proportions of the different habitat classes?
Has anyone worked with this dataset in r before? If so I could really use the help.
Cheers,
Israel

Use the raster package which can process the files from disk, only reading in chunks at a time.
the raster package has an extract function with a buffer argument. set your buffer to the appropriate value (1000 if your map units are metres and you want a km radius)

Thanks to mnel. I have gotten the basic idea to work (code below). Now if anyone could give me a pointer on how to calculate the proportion of each category for every coordinate. The extract function gives me matrices of values for each set of coordinates. Is there a way to summarize this data?
#load in map and locality data
NLCD<-raster ('NLCD2006/NLCD2006.img')
sites<-read.csv('sites.csv', header=T)
#crop site data to just latitude and longitude
sites<-sites[,4:5]
#convert lat/lon to appropirate projection
str (sites)
coordinates(sites) <- c("Longitude", "Latitude")
proj4string(sites) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
sites_transformed<-spTransform(sites, CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"))
#plot the map
plot (NLCD)
#add the converted x y points
points (sites_transformed, pch=16, col="red", cex=.75)
#extract values to poionts
Landcover<-extract (NLCD, sites_transformed, buffer=1000)

Related

ggplot can find lng and lat in dataset but leaflet cannot, "Need '+proj=longlat +datum=WGS84' "

I have a dataset form the public repo https://github.com/highsource/verbundkarte
Reading the dataset with st_read and plotting it with ggplot yields a beautiful map with correct lng and lat data.
df <- st_read("~/Verkehrsverbunde.shp")
map <- ggplot(df) + geom_sf(aes(fill=SHORTNAME))
I therefore assume, that the lng/lat values are included in the variable df$geometry. However, if I use leaflet, no matter what I try I end up with an error. For instance
df%>% leaflet() %>%
addProviderTiles("CartoDB") %>%
addPolygons(label = htmlEscape(verbunddaten$SHORTNAME)) %>%
setView(lng = 10.3, lat = 51.9, zoom = 5.1)
ends up with
Warning messages:
1: sf layer is not long-lat data
2: sf layer has inconsistent datum (+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +datum=potsdam +units=m +no_defs ).
Need '+proj=longlat +datum=WGS84'
I found this beautiful conversation of which I understood basically nothing. Reading the data with readOGR as suggested here doesn't solve my problem.
How do I force leaflet to assume the same longlat and EPSG as ggplot?
The data in your Verkehrsverbunde.shp shapefile is in a custom traverse mercator projection, where coordinates are expressed in meters. By contrast, r-leaflet expects data to be expressed in degrees of latitude-longitude, which would look like an equirectangular projection if/when plotted.
I'll guess that, contrary to your belief, ggplot is not using latitude and longitude in degrees, but rather northing and easting values in meters. The representation of the data might look similar for small areas.
In GIS terms, your data is using the EPSG:31467 CRS (Coordinate Reference System), as can be inferred by proj=tmerc and datum=postdam; r-leaflet expects data in the EPSG:4326 CRS.
The approach here would be to reproject your data, so that coordinates are in latitude-longitude as expected. There are plenty of ways to do this; running ogr2ogr -t_srs epsg:4326 Verkehrsverbunde-latlng.shp Verkehrsverbunde-latlng.shp on a command line, using R, or using QGIS, amongst other methods.

How read disk image file (.img) in R [duplicate]

I am working with U.S. National Landcover Dataset (NLCD) to classify habitat type at more than 150 sites across the northeast U.S. The dataset is very large (15GB) so I cannot upload it here, but it comes in .img format at 30m resolution.
I have GPS coordinates for the center point of all the sites. I would like to be able to extract the proportion of landcover classes in a 1 square kilometer around the point. My questions are:
1) How do I upload .img files into r?
2) How do I extract the information from around the GPS coordinates as proportions of the different habitat classes?
Has anyone worked with this dataset in r before? If so I could really use the help.
Cheers,
Israel
Use the raster package which can process the files from disk, only reading in chunks at a time.
the raster package has an extract function with a buffer argument. set your buffer to the appropriate value (1000 if your map units are metres and you want a km radius)
Thanks to mnel. I have gotten the basic idea to work (code below). Now if anyone could give me a pointer on how to calculate the proportion of each category for every coordinate. The extract function gives me matrices of values for each set of coordinates. Is there a way to summarize this data?
#load in map and locality data
NLCD<-raster ('NLCD2006/NLCD2006.img')
sites<-read.csv('sites.csv', header=T)
#crop site data to just latitude and longitude
sites<-sites[,4:5]
#convert lat/lon to appropirate projection
str (sites)
coordinates(sites) <- c("Longitude", "Latitude")
proj4string(sites) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
sites_transformed<-spTransform(sites, CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"))
#plot the map
plot (NLCD)
#add the converted x y points
points (sites_transformed, pch=16, col="red", cex=.75)
#extract values to poionts
Landcover<-extract (NLCD, sites_transformed, buffer=1000)

Unit of calculated area for polygons using raster package area function

I am trying to calculate the area (square km or miles) of the intersection of counties and watersheds in R using the raster package and the area function.
My code looks like this so far:
counties <- readOGR('C:\\Shapefiles\\tl_2017_us_county\\tl_2017_us_county.shp')
counties <- spTransform(counties, CRS("+init=epsg:3455"))
huc2_10 <- readOGR('C:\\Shapefiles\\WBD_10_HU2_Shape\\Shape\\WBDHU6.shp')
huc2_10 <- spTransform(huc2_10, CRS("+init=epsg:3455"))
I then intersect the two shapefiles:
pi <- raster::intersect(huc2_10, counties)
The units of this projection are normally in meters (I believe), as it is a NAD83 projection for southern South Dakota, so the area function should calculate area in square meters. I am attempting to calculate the area (in square miles) of each polygon that is formed as a result of this intersection using the area function.
pi$area <- area(pi)/2.589988e6
However, the proj4string looks like this:
+init=epsg:3455 +proj=lcc +lat_1=44.4 +lat_2=42.83333333333334 +lat_0=42.33333333333334 +lon_0=-100.3333333333333 +x_0=600000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0
According to this, the units are "us-ft". So, does the area function output areas for each polygon in square feet because of this? This seems to make sense, but I would like to confirm this, changing my code to:
pi$area <- area(pi)/5280**2
Thank you.
The manual confirms what you expect:
?raster::area
If x is a SpatialPolygons* object: area if each spatial object in squared meters if the CRS is longitude/latitude, or in squared map units (typically meter)
If your map units are feet, the area will be in square feet.

I downloaded shapefile but merging with internal dataset based on geometry fails

Apologies as i'm a little new to GIS features within R, so any help and explanation would be very helpful!
I have downloaded this shape file (Simple Feature Polygon) from source like so:
fire <-tempfile()
download.file("http://frap.fire.ca.gov/webdata/data/statewide/fhszs.sn.zip",destfile = fire)
unzip(fire,exdir = ".")
fire_map<-read_shape("fhszs06_3.shp")
Map has small polygons based on Hazard code (i.e.: 1,2,3)
I also have an internal dataframe that is about 15 variables with 3584 rows, I also have lat/lon for all points (commercial properties in california) that I'm trying to convert to either spatial points DF or simple feature in order to figure out which properties lie within a hazard code.
Example of property file:
ln_bal<- c(500000,200000,6000000,12000,130000)
ln_city <-c('Ventura','Torrance','Buena Park','Concord','Lake View Terrace')
lon <- c(-119.213504,-118.311072,-117.985452,-122.057139,-116.893845)
lat <-c(34.278122,33.844817,33.846594,37.979995,32.844287)
cmbs3 <- data.frame(ln_bal,ln_city,lon,lat)
I think my problem is getting the correct CRS and then matching with the shape file.
The CA Fire map has the following:
epsg (SRID): NA
proj4string: +proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
I've Tried sf_intersect by creating a SF points DF:
fire_map <-st_read("fhszs06_3.shp")%>%
st_transform(4326) #need to set CRS the same as your dataframe below
#transforms coordinates to ellips code and creates matching values with fire_map:
proj4string(cmbs3)<-CRS("+proj=longlat +datum=WGS84")
cmbs3 <- spTransform(cmbs3, CRS("+proj=utm +zone=51 ellps=WGS84"))
#fire_map is a simple feature data frame need to convert our data to this, and then match
cmbs3<-st_as_sf(cmbs3,precision=0)
cmbs3<-st_set_crs(cmbs3,4326)
inters <- st_intersection(cmbs3,fire_map)
Expected (potential) Results:
ln_bal ln_city lon lat HAZ_CODE HAZ_CLASS
12000 Concord -122.057139 37.97 1 Moderate

Using R to change raster projection

I'm trying to change raster projection using R and the raster package. The input raster projection is Lambert Azimuthal; the parameters are here:
Coordinate System:
Lambert_Azimuthal_Equal_Area
False_Easting: 4321000,000000
False_Northing: 3210000,000000
Central_Meridian: 10,000000
Latitude_Of_Origin: 52,000000
GCS_ETRS_1989
Datum: D_ETRS_1989
Prime Meridian: 0
PROJCS
["ETRS_1989_LAEA",
GEOGCS ["GCS_ETRS_1989",
DATUM ["D_ETRS_1989",
SPHEROID ["GRS_1980",6378137.0,298.257222101]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]],
PROJECTION["Lambert_Azimuthal_Equal_Area"],
PARAMETER["False_Easting",4321000.0],
PARAMETER["False_Northing",3210000.0],
PARAMETER["Central_Meridian",10.0],
PARAMETER["Latitude_Of_Origin",52.0],
UNIT["Meter",1.0]]
I need to convert them to simple rasters in ESRI ASCII format, using longitude and latitude coordinates, with a Mercator-style projection, with a cell size of 0.1 degrees (I hope I explain myself well enough, because I don't have enough GIS skills, sorry). What I need are rasters in format .ASC where each value of the raster corresponds to a single cell of size N x N, where N is in degrees (e.g. 0.1 degrees), and the raster coordinates are in longitude/latitude.
I tried to use raster library in R, and follow examples found for the projectRaster function. But after many attempts using many several parameters, I wasn't be able to make it correctly. I think I'm not using the correct parameters for projection, datum, or something like this.
Here's what I tried. I load the raster in R, then I set its projection using:
>crs(r)<-"+proj=laea +lat_1=52 +lon_0=-10 +ellps=GRS80"
Then I define the output projection and I try the conversion and save:
>newproj <- "+proj=lonlat +lat_1=52 +lon_0=-10 +ellps=WGS84"
>pr2 <- projectRaster(r, crs=newproj, res=0.1)
>writeRaster(pr2, "newraster.asc", overwrite=TRUE)
No error messages, but resulting raster is not correctly projected (country borders don’t match, countries are slightly distorted).
Thanks for any help!
Given the description of the projection you provide, this seems wrong:
crs(r) <- "+proj=laea +lat_1=52 +lon_0=-10 +ellps=GRS80"
As you do not include the false northing and easting parameters; and lat_1 should probably be lat_0. This may be better:
crs(r) <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m"
This also looks odd:
newproj <- "+proj=lonlat +lat_1=52 +lon_0=-10 +ellps=WGS84"
How about
newproj <- "+proj=longlat +datum=WGS84"

Resources