How to create irregular raster with gdal using csv points - raster

I am trying to create a irregular shaped .tiff from a csv list of points (xyz data). I am doing this using gdal_grid.
I can seem to generate the .tiff file no problem but I cannot preserve the outline / shape of the original csv points.
Everytime I generate the .tiff file it creates a raster with the size of (xmax-xmin) x (ymax-ymin) and assigns interpolated values to pixels that fall far away from my initial points.
Is it possible to generate a .tiff file of ONLY the points I provide?
For context, I am trying to generate a raster of xyz data for a river, and only want the raster in the river (not the entire bounding box of the river). I am only providing xyz data in the river.
I tried playing with the -nodata flag, and limiting -max_points to the number of points I've provided.
My final code (once everything is imported and declared):
gdal_grid -a invdist:power=2.0:smoothing=1.0:nodata=-999:max_points=2128164 -txe 582387.4 591069.4 -tye 4505028.08 4515344.079999999 -outsize 50 50 -zfield "z" -of GTiff -ot Float64 -l Book2 Book2.vrt Book2.tiff

Welcome to Stack Overflow, Derek!
Maybe there is a creation option inside gdal_grid that would do it, but I think that you will have to achieve desired result with additional calculation:
Run the gdal_grid as you have it.
Create a concave hull from the given points. If this is a one time job, I suggest using QGIS (with grass tools), because there is some tweaking of concave hull parameters required.
Cut the raster with the created shapefile by using gdalwarp.
Let me know if this got you through!

Related

How to resize a NetCDF file so it matches the grid and coordinates of another NetCDFfile in R

I have two .nc files, file1.nc and file2.nc The dimensions of the files are the following:
file1 [lon,lat,t]=[21,9,t1] 0.25x0.25 Grid
file2 [lon,lat,t]=[9,3,t2] 0.5X0.5 Grid
Each netcdf file have different time ranges but I'm only interested in the xy grid.
I want to transform file 1 so it has the grid size and same coordinates as of file 2. I have attached a picture so my explanation is clearer.
see picture
Some remarks:
Some recommend using CDO (Climate Data Operators) but since I'm using my company's computer I don't have the permits to install what's required to run CDO.
Other recommend to use resample() but they apply it to rasters which only visualizes one point in time and I want to resize the entire NetCDF.
-I would like to perform the transformation either by using the netcdf file it self or the multivariate array resulting from extracting one variable form the netcdf file.

How to get raster file from a nested raster list produced by landscapemetrics package in R?

Package landscapemetrics can calculate area of each patch for a given raster file, shape of that patch and so on. I want to have not only tibble-frame with patch metrics calculated, but a new raster where each pixel within specific patch will have a value of the area of that patch, shape indicator and so on. We can do it with function spatialize_lsm() (it produces a Large list nested object with probably RasterObject objects within):
library(landscapemetrics)
plot(podlasie_ccilc) # this raster data is provided with package
podlasie.metrics.area <- spatialize_lsm(podlasie_ccilc, what = 'lsm_p_area') # creates a list
plot(podlasie.metrics.area) # produces an error...
How to get a desirable raster file with patch metrics from that list? I guess it is a question of raster package or something else, since landscapemetrics documentation tells nothing about this step.
I not that this data and new raster do not have resolution of the pixel like in meters (30, 30 for Landsat satellite image, for example). So we cannot plot the new raster produced:
podlasie.metrics.area[[1]]
plot(podlasie.metrics.area[[1]])
So I guess landscapemetrics cannot deal with such rasters, we can even use its function to check a suitability of the prior raster for patch discovering:
check_landscape(podlasie_ccilc)
Upd. I did it for the Landsat dataset with resolution 30, 30 and it produced patch area raster, but again I cannot open/show/save as raster it, because of the same error.
Package maintainer helps to solve a problem (yes, it is just related to the structure of list):
plot(podlasie.metrics.area[[1]]$lsm_p_area)

QGIS gdal_contour not respecting scale_factor/offset for netCDF

I am working with some netCDF files and want to import netCDF parameter's data as a Raster and build a contour layer for it. I am using gdal_contour for this.
When I import the netCDF and choose a parameter (water_temp) in QGIS, the raster is loaded into the map with no problem and displays values in the range of roughly 4 degC to 31.25 degC.
However, when I use gdal_contour to make a contour layer for it, the values are in the range of -15944 to 11250. It certainly doesn't help that among other issues, it takes forever to generate the layer because I'm specifying an interval of 1.0 and the value range is far larger than the expected temperatures for Celsius.
From what I can tell, it looks like perhaps gdal_contour either isn't respecting the raster band's offset and scale_factor or has no knowledge of it. I understand that the netCDF is storing the temperature values as integers instead of floats to optimize file size, but I'm a bit confused by why QGIS can understand the offset when reading the netCDF into a raster layer, but not when generating a contour layer.
Am I missing something, or is there perhaps a caveat to using gdal_contour of which I'm unaware?
The command I am using to generate the conotur layer is:
gdal_contour -b 1 -a water_temp -i 1.0 -snodata -30000.0 -f "ESRI Shapefile" NETCDF:"C:/path/to/input/netcdf/INPUT.nc":water_temp C:/path/to/output/layer/OUTPUT.shp
The scale_factor, offset, and associated metadata for the band are:
add_offset=20
missing_value=-30000
NETCDF_VARNAME=water_temp
scale_factor=0.001
STATISTICS_MAXIMUM=11250
STATISTICS_MEAN=5475.011083141
STATISTICS_MINIMUM=-15944
STATISTICS_STDDEV=5863.9957197805
units=degC
_FillValue=-30000
This question was answered here.
TLDR; Convert the netCDF to a GeoTIFF first using
gdal_translate with the -unscale option to get GDAL to unpack
the values, then perform gdal_contour on the GeoTIFF to get a
contour layer with the correctly unpacked values.
However, one thing that may be important to note is the scaled/unscaled data types, which may have to be explicitly set for gdal_translate (using the -ot option) in order to not lose data precision during unscaling if the scaled data type is a smaller size than the unscaled data type.

Import tiff + tfw + prj files in R

I want to import in R a map that I have downloaded from
http://www.naturalearthdata.com/downloads/10m-raster-data/10m-natural-earth-1/
When I download it I get 3 files with the following extension
.tif
.tfw
.prj
How should I read them? I can read the .tif file with
imported_raster=raster('NE1_HR_LC_SR_W.tif')
but then the colours and the projection are different from the original tif.
Thanks
I was looking for some information on another topic when I came across this one.
It's quite normal that the colours appear different from the original tif. There probably was a color distribution or a colour scheme applied on the original tif which isn't exported to or with the output tif. It's the user that should set a colour scheme or color distribution. (Just like in arcmap for example).
I guess your exported tif has no projection at all when you load it in R? You need to use the information from the .tfw file to give each pixel (row, column) a coordinate.
Read in the .tfw file
Assume that your .tfw (ascii file) is something like this:
10.000000
0.000000000000000
0.000000000000000
-10.000000000000
137184.00000000000
180631.000000000
The last two rows are the X/Y coordinates of the center of the upperleft pixel of your tif.
The first row tells you what your spatial resolution is, in this case 10.
So if you know the coordinate of the center of the upper pixel, than the coordinate of pixel (row=i, column=j) is
137184+i*10, 180631+i*10).

Sort Extracted Data Based On Image Region

I have analysed tree core images through the raster package in an attempt to perform image analysis. In the image:
http://dx.doi.org/10.6084/m9.figshare.1555854
You can see the measured "vessels" (black and numbered) and also annual lines (red) which have been drawn using the locator function and represent each year of growth of the tree core.
By generating a list of the maximum y coordinates of each annual line I have been able to sort the vessels into years for this image. Which is what I am looking for. However, it has occurred to me that in reality things can get a little more difficult as seen in the next image:
http://figshare.com/articles/Complicated/1555855
The approach above will not work on this image as vessels from each year overrun so using the maximum y coordinates will not return the correct result.
So can anyone suggest another approach which may overcome this limitation? I have thought about using spatialpolygons but not sure this will achieve what I am looking for.
If you are creating the lines by clicking on the plot, you can use raster function drawLine or, for polygons, drawPoly. You could rasterize the polygons and mask that with the original image to get the vessels grouped by polygon (year).

Resources