NetCDF to Raster File Extent / Cell Size Change - raster

In using the ArcMap tools, as well as an arcpy script, I am getting the same results. When I try to convert a NetCDF (NC) file to a TIF, the file extent increases by a very small amount which also slightly increase the Y cell size.
The only way I can get the images to match is to use the Export Data menu item when I right click the file layer on the Table of Contents.
Here is my workflow:
NetCDF to Raster Layer
Copy to Raster or Project Raster (both tools create the same problem)
Here are the original layer properties in the initial NetCDF to Layer:
Cell Size (X,Y): 0.099990845, 0.099998474
Here are the extent values:
Top: 89.9972572327
Left: -179.999992371
Right: -179.967048645
Bottom: -89.9999961853
Here are the values after I copy to Raster or Project Raster:
Cell Size (X,Y):0.099990845, 0.1000464
Top -90.0973036279
Left -179.999992371
Right- 180.167030334
Bottom -90.0862541199
I have tried to include a NoData value, background value, snap to image, and even include the original extent values of the NC file within the raster tools, but it always ends up increasing the extent which in turn increases the cell size, but a tiny bit - < 0.01 deg, but it's enough that my pixels all shift a tiny bit downward.
I have also tried to change the compression to none and it still does not help.
I have included a transformation and output coordinates in the tools as well. The datums of the files appear to be the same from the source info. Otherwise, using default settings.
It's a single variable file and it is already saved out by a date so it's pretty straight forward. Any ideas why this is happening?

Finally got the image situation figured out. For Arc and ENVI, we just need to specify the cell size because the original extent of the image is just outside the normal extent of a map. Arc goes ahead and adjusts the cell cize to maintain the normal extent (-180,180,90,-90). It's because of the floating point lat and lon coordinates and extra cells on the edge of the file.
Just as in R, once everything is specified, the floating point values are rounded and the image loads in just fine.

This is not a coding question, but you could try a question like this on https://gis.stackexchange.com/.
Here is a way to convert a netcdf file to geotiff using R, that you can use as comparison:
library(raster)
library(ncdf4)
r <- raster("3B-MO.MS.MRG.3IMERG.20140401-S000000-E235959.04.V05B.HDF5.nc")
x <- writeRaster(r, "file.tif")
Now let's look at the dataset:
r
#class : RasterLayer
#dimensions : 3600, 1800, 6480000 (nrow, ncol, ncell)
#resolution : 0.1, 0.1 (x, y)
#extent : -90, 90, -180, 180 (xmin, xmax, ymin, ymax)
#coord. ref. : NA
#data source : 3B-MO.MS.MRG.3IMERG.20140401-S000000-E235959.04.V05B.HDF5.nc
#names : precipitation
#zvar : precipitation
The extent is global and clean: -180 to 180 longitude and -90 to 90 latitude. The resolution is 0.1 degrees in both directions.
That is also what the ncdf metadata say:
print(r)
# ...
# Grid.GridHeader: BinMethod=ARITHMETIC_MEAN;
#Registration=CENTER;
#LatitudeResolution=0.1;
#LongitudeResolution=0.1;
#NorthBoundingCoordinate=90;
#SouthBoundingCoordinate=-90;
#EastBoundingCoordinate=180;
#WestBoundingCoordinate=-180;
#Origin=SOUTHWEST;
Somehow, Arc seems to get it wrong

Related

How do I increase or reduce resolution of MODIS raster in R?

I'm working with MODIS data at 250m resolution. After importing into R, the resolution shows 231.6564. I now want to convert the resolution of MODIS from 250m to 30m resolution or reduce the resolution of Hansen's data from 30m (which displays as 0.0002694946 in R) to 231.6564 as shown in R.
I've tried the aggregate (x, fact=) and disaggregate function in R but the files become too large and I get an error message
Error: cannot allocate vector of size 1.1Gb.
I'm not sure if reprojecting the raster from different sources to have the same coordinate reference system (CRS) will give me a better base to solve the problem of resolution. This will mean all my variables will have the same resolution, CRS and extent.
New_modis_data <- projectRaster(Modis_data, crs = crs(Hansen_data))
Any help with this, please?

How to change the extent of raster file in r from decimal degree to meter?

I have an image data whose reference is in WGS84 UTM ZONE 45N. So the unit extent of this file is in meter. I want to crop a polygon whose reference is in WGS84 UTM ZONE 45N and the extent unit is in decimal degree. I can not crop the polygon because of two different extent units. So how can I change the polygon extent to meters?
What you are saying is not possible. You cannot have a the WGS84 UTM ZONE 45N coordinate reference system (CRS) with degrees as unit. UTM is a planar CRS and the units are meter (sometimes other units are used, but they cannot be degrees). Also, you cannot simply change the extent from one unit to another. If the CRS of two datasets do not match you can transform one to the CRS of the other. In the case of vector and raster data you should normally transform the vector data. You can read about that here and in many other places.
But in your case, there is mistake earlier on that you need to fix.

How to change number of layers of a raster brick

When I load a tiff image in R as raster brick then I have the expected number of pixels (number rows and columns) when I check afterwards the details of the image. But I expect 44 layers. I think I can change the number of layers with the nlayers function. Unfortunately, this does not work. The number of layers remain at 4.
sample_brick = brick("file_example_TIFF_1MB.tiff", nlayers = 44)
Can someone help me how to adapt the number of layers?
Here you can find a example tiff image.
https://file-examples.com/wp-content/uploads/2017/10/file_example_TIFF_1MB.tiff
The brick function creates a RasterBrick that has all the layers that are in your file.
Your expectation is probably wrong.
You cannot add imaginary layers when creating an object from file.

raster to polygon - zero values

I am trying to convert a raster of cell size 50x50 into a polygon without success.
The raster file has some values and most of them are zero values. When I convert the raster using the tool Raster to polygon, the result is a big polygon for all the values equal to zero, instead of a polygon for each cell with zero value.
Someone knows how to create an independent polygon for each zero value?
I attached an image of my resutls.
Read this article ... it may solve your problem:
https://support.esri.com/en/technical-article/000012696
Steps:
1) Use the Raster to Point tool to convert each pixel to a point (found in Conversion Tools > From Raster). These points are later used to label polygons.
2) Navigate to ArcToolbox > Data Management Tools > Feature Class > Create Fishnet. The values entered into this tool are taken from the properties of the raster. To access the properties of the raster, right-click the raster in the Table Of Contents in ArcMap > Properties > Source tab.
3) Navigate to ArcToolbox > Data Management Tools > Features > Feature To Polygon. Run the Feature to Polygon tool on the fishnet output from step 2, with the Raster to Points output specified in the Label Features field. This gives the final polygon which has a field named GRID_CODE with the cell values of the raster.

Determining the size of grid cells in a raster

Is there a good method for determining the size of individual grid cells in a RasterLayer? I can access the resolution but I need the actual size in km^2. I've tried using the area() with my raster but it's not finding the window. I've also tried creating a SpatialGridDataFrame from my raster but have had no luck computing the area of each individual grid cell. Any help would be appreciated.
The cells size is the product of the x and y resolution prod(res(x)) if you have a planar coordinate reference system. Otherwise, if your crs is longitude/latitude, cell size will change with latitude, and you can indeed use the area function to get the size of each cell.
I take it that "it's not finding the window." refers to this error message Error in as.owin.default(w) : Can't interpret W as a window.
This is a name conflict with spatstat. So either do not load spatstat or call the raster function explicitly raster::area(x).

Resources