Gdalwarp results in raster of zero's - raster

I'm trying to create a global raster from different 5*5 degree tiles. Herefore, I converting my label + image file to a geotiff with gdal_translate. Afterwards I use gdalwarp to reproject the data. This works fine for all tiles between 0 and 180 degrees longitude but results in a raster with only zeros for all between -180 till 0 degrees longitude.
The data is CRISM summary product data from Mars.
Input Proj4-file:
PROJCS["EQUIRECTANGULAR_MARS",GEOGCS["GCS_MARS",DATUM["D_MARS",SPHEROID["MARS_localRadius",3396000.0,0.0]],PRIMEM["Reference_Meridian",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Equidistant_Cylindrical"],PARAMETER["false_easting",0.0],PARAMETER["false_northing",0.0],PARAMETER["central_meridian",327.5],PARAMETER["standard_parallel_1",42.5],UNIT["Meter",1.0]]
Output Proj4-file:
GEOGCS["GCS_Mars_2000",DATUM["D_Mars_2000",SPHEROID["Mars_2000_IAU_IAG",3396190.0,169.8944472236118]],PRIMEM["Reference_Meridian",0.0],UNIT["Degree",0.0174532925199433],AUTHORITY["Esri",104905]]
Does anyone have an idea why the data in my raster only consists of zero's after the described steps? (see code below)
gdal_translate -a_nodata 65535
/home/kampsom/Documents/CRISM/Raw_data/$filename_lbl
/home/kampsom/Documents/CRISM/Converted/$filename_tif
gdalwarp -overwrite -t_srs
/home/kampsom/Documents/CRISM/Converted/Mars_2000.prj
/home/kampsom/Documents/CRISM/Converted/$filename_tif
/home/kampsom/Documents/CRISM/Converted/$filename_tif_repr

Related

Extracting raster pixels in qGIS

I have a raster file with two bands and a vector shapefile. I used raster extraction by mask and created the desired area. Now I want to calculate the number of pixels for Band 1 and Band 2 of the new clipped mask file. What would be the best way to do that?

Query raster brick layer based on another raster in R

I have a NetCDF file of global oceanographic (OmegaA) data at relatively coarse spatial resolution with 33 depth levels. I also have a global bathymetry raster at much finer resolution. My goal is to use get the seabed OmegaA data from the NetCDF file, using the bathymetry data to determine the desired depth. My code so far;
library(raster)
library(rgdal)
library(ncdf4)
# Aragonite data. Defaults to CRS WGS84
ncin <- nc_open("C:/..../GLODAPv2.2016b.OmegaA.nc")
ncin.depth <- ncvar_get(ncin, "Depth")# 33 depth levels
omegaA.brk <- brick("C:/.../GLODAPv2.2016b.OmegaA.nc")
omegaA.brk <-rotate(omegaA.bkr)# because netCDF is in Lon 0-360.
# depth raster. CRS WGS84
r<-raster("C:/....GEBCO.tif")
# resample the raster brick to the resolution that matches the bathymetry raster
omegaA.brk <-resample(omegaA.brk, r, method="bilinear")
# create blank final raster
omegaA.rast <- raster(ncol = r#ncols, nrow = r#nrows)
extent(omegaA.rast) <- extent(r)
omegaA.rast[] <- NA_real_
# create vector of indices of desired depth values
depth.values<-getValues(r)
depth.values.index<-which(!is.na(depth.values))
# loop to find appropriate raster brick layer, and extract the value at the desired index, and insert into blank raster
for (p in depth.values.index) {
dep.index <-which(abs(ncin.depth+depth.values[p]) == min(abs(ncin.depth+depth.values[p]))) ## this sometimes results in multiple levels being selected
brk.level <-omegaA.brk[[dep.index]] # can be more than on level if multiple layers selected above.
omegaA.rast[p] <-omegaA.brk[[1]][p] ## here I choose the first level if multiple levels have been selected above
print(paste(p, "of", length(depth.values.index))) # counter to look at progress.
}
The problem: The result is a raster with massive gaps (NAs) in it where there should be data. The gaps often take a distinctive shape - eg, follow a contour, or along a long straight line. I've pasted a cropped example.
enter image description here
I think this could be because either 1) for some reason the 'which' statement in the loop is not finding a match or 2) a misalignment of the projections is created which I've read can happen when using 'Rotate'.
I've tried to make sure all the extents, resolutions, number of cells, and CRS's are all the same, which they seem to be.
To speed up the process I've cropped the global brick and bathy raster to my area of interest, again checking that all the spatial resolutions, etc etc match - I've not included those steps here for simplicity.
At a loss. Any help welcome!
Without a reproducible example, this kind of problems is hard to solve. I can't tell where your problem is but I'll present to you the approach I would try. Maybe it's good, maybe it's bad, I don't know but it may inspire you to find a way to go around your problem.
To my understanding, you have a brick of OmegaA (33 layers/depth) and a bathymetry raster. You want to get the OmegaA value at the bottom of the sea. Here is how I would do:
Make OmegaA raster to the same resolution and extent to the bathymetry one
Transforme the bathymetry raster into a raster brick of 33 three layers of 0-1. e.g. If the sea bottom is at 200m for one particular pixel, than this pixel on all depth layer other than 200 is 0 and 1 for the 200. To program this, I would go the long way, something like
:
r_1 <- r
values(r_1) <- values(r)==10 # where 10 is the depth (it could be a range with < or >)
r_2 <- r
values(r_2) <- values(r)==20
...
r_33 <- r
values(r_33) <- values(r)==250
r_brick <- brick(r_1, r_2, ..., r_33)
then you multiple both your raster bricks. They have the same dimension, it should be easy. The output should be a raster brick of 33 layers with 0 everywhere where it isn't the bottom of the sea and the value of OmegaA anywhere else.
Combine all the layer of the brick obtained previously into a simple raster with a sum.
This should work. If you have problem with dealing with raster brick, you could make the data into base R arrays, it could be simpler.
Good luck.

Creating a buffer around a geographic point and then checking whether a list of coordinates is inside that buffer?

I have the following puzzle regarding spatial data in R:
I have a dataset with street segments (with their respective starting and ending coordinates). I want to create a buffer of X meters around these points and then check whether a list of lat/lon points is within that buffer. Is there a way to do this in R?
I'm able to map the points and map the buffers using a combination of various packages: maptools, ggmap, rgdal, sp, and rgeos. But this procedure seems to only map the points and the buffers without allowing me to then check if the other coordinates I have are within the buffers. Ideally, I'd like to produce a vector of 1s and 0s describing whether the list of lat/lon points are within the buffer around the street segments.
Any ideas?
This is the code I've been using, but I get all missing values (and I know this shouldn't be the case). I've also tried using the gContains function from rgeos but it crashed R.
#Load shapefile in R and transform to appropriate CRS
shp <- readOGR(dsn="/Users/Maps/shapes", layer="shp")
shp_transf <- spTransform(shp_transf, CRS( "+init=epsg:21897" ))
#Create buffer around polygons
shp_buff <- gBuffer(shp_transf, width=40, byid=TRUE, quadsegs=10)
#Make my dataframe of lat/lon points into same projection as buffers
points <- SpatialPoints(points,proj4string=CRS(proj4string(shp_buff)))
#Use over function from SP pacakge
result <- as.integer(over(points, shp_buff)$OBJECTID)
Turns out, the over() function wasn't working before because the shapefile with the buffers and the list of GPS coordinates were in a different CRS. I fixed it this way:
shp <- spTransform(shp, CRS("+proj=longlat +datum=WGS84"))
proj4string(points) <- CRS("+proj=longlat +datum=WGS84")
Then, you run the over() function and it'll give you the number of times each point is in each buffer area:
x <- over(points, shp)
table(x$ID)
181 304
118 8
So a particular GPS pulse came from segment ID 181 a total of 118 times and from segment ID 304 a total of 8 times.

R Converting contour lines to elevation plot

I would like to be able to create an elevation plot from contour lines in R. I am very new to using shape files
At the moment I have downloaded data from here
which provides .shp files for all of the UK.
It also provides the contour lines, summarising the topology of the UK.
For the elevation plot I would like a data.frame or data.table of evenly spaced points (100m apart from each other) to produce a data output giving an x, y and z value. Where x and y represent the latitude and longitude (or Eastings and Northings), and z represent the height (in meters above sea-level).
I think there are probably some tools that will automatically carry out the interpolation for you, but am unsure how it would work with geo-spatial data.
This is my basic start...
require(maptools)
xx <- readShapeSpatial("HP40_line.shp")
Choose "ASCII Grid and GML (Grid)" as download format for the "OS Terrain 50" product, and download the file. This will give you a zip file containing many directories of zip files, each of which contains portions of a 50 m elevation grid of the UK (the portion I looked at had 200 x 200 cells, meaning 10 km x 10 km). I went into the directory data/su, unzipped the zip file there, and did
library(raster)
r = raster("SU99.asc")
plot(r)
to aggregate this to a 100 m grid, I did
r100 = aggregate(r) # default is factor 2: 50 -> 100 m
As mentioned above, the advice is to work on the grids as contour lines are derived from grids, working the other way around is a painful and a great loss of information.
Getting grid values in longitude latitude as a data.frame can be done in two ways:
df = as.data.frame(projectRaster(r, crs = CRS("+proj=longlat")), xy = TRUE)
unprojects the grid to a new grid in longitude / latitude. As these grids cannot coincide, it minimally moves points (see ?projectRaster).
The second option is to convert the grid to points, and unproject these to longitude latitude, by
df2 = as.data.frame(spTransform(as(r, "SpatialPointsDataFrame"), CRS("+proj=longlat")))
This does not move points, and as a consequence does not result in a grid.

polygon to raster with raster as cell size

I have a shapefile and a raster file which I want to merge. Normally I would Extract by mask the rasterfile to obtain delineation of the shapefile. But now the shapefile has sub-delineation inside which I want to keep. One way to do this is Polygon to raster where I choose the raster-file inside cell size. However when I do this the right uppper coordiante is off the chart with values of 10^4 for a WGS 1984 decimal degree georeference. The projection of the two files do not seem to make any difference.
ArcGis 10.1

Resources