convert a png with known bounds from plate carree to mercator with gdal - projection

I have a png image that shows the region defined by the geo bounds UpperLat, LeftLon, BottomLat, RightLon (in degrees & -ve for West Lon and South Lat) in Plate Carree EPSG:4326. I want to convert it to Mercator EPSG:3857 using GDAL.
When I tried doing it this way:
Step1) gdal_translate -of Gtiff -a_srs EPSG:4326 pc_map_piece.png pc_map_piece.tiff
Step2) gdalwarp -s_srs EPSG:4326 -t_srs EPSG:3857 pc_map_piece.tiff mer_map_piece.tiff
I got this error:
ERROR 1: Unable to compute a transformation between pixel/line and georeferenced coordinates for pc_map_piece.tiff. There is no affine transformation and no GCPs.
Then I tried doing it this way:
Step1) gdal_translate -of Gtiff -a_ullr UpperLat LeftLon BottomLat RightLon -a_srs EPSG:4326 pc_map_piece.png pc_map_piece.tiff
Step2) gdalwarp -s_srs EPSG:4326 -t_srs EPSG:3857 pc_map_piece.tiff mer_map_piece.tiff
I didn't get the above error but the output image is not converted to mercator projection, I simply got the same image upside down as output.
Any ideas to get around this problem and convert the image to Mercator? Thanks.

Problem solved. Changing the order to LeftLon UpperLat RightLon BottomLat in my second approach gdal_translate did it for me!!

Related

Shapefiles do not overlay raster layer in R

I have hundreds of shapefiles without a coordinate reference system. My goal is the overlay the spatial polygons over the WorldClim raster layer. I used this approach before without any problems. However, this time the coordinates from my shapefiles are strange for me. Each coordinate for bounding box and coords within polygons is composed of 8 digit numbers without comma or dot to separate de decimals.
This is the bounding box from one of the shapes:
SHP bbox: xmin:-17367529, xmax:17367529, ymin:-5997367 and ymax:7052489
which are clearly different from the bounding box of the WorldClim raster layer.
WorldClim bbox: xmin=-180,xmax=180,ymin=-60 and ymax=90
When I tried to overlay the shapefile over the raster layer using plot command nothing happens.
plot(shapefile, add=T)
I understood that this is a projection problem. Then I tried to assign the same coordinate system of the WorldClim raster layer in the shapefile using the CRS function. However, the result remains the same (i.e. the shapefiles do not over the raster). In the sequence, I tried to use the spTransform function from the rgdal package to reproject the shapefile coordinates. However, because shapefile does not have any reference system the function does not work and I do not know how to reproject the shapefile in order to match with the raster layer. I've been researching for a few days about how to deal with this problem and I believe that the absence of a reference system is a key point to the problem. However, I'm failing to overcome this problem and I would like to know if someone could help how to deal with this situation.
You need to define the projection of shape files first using proj4string(meuse) or crs(shapefile)<-crs string then you can use spTransform:
library(rgdal)
data(meuse)
coordinates(meuse) <- c("x", "y")
Here you have the spatial data with x and y but you do not have the crs yet! So if you use spTransform it will fail.
summary(meuse) #proj4string : [NA] so below line fails!
meuse.utm <- spTransform(meuse, CRS("+proj=utm +zone=32 +datum=WGS84"))
# Error in spTransform(xSP, CRSobj, ...) :
# No transformation possible from NA reference system
To get around this, as mentioned above, you first need to define the projection as below:
proj4string(meuse) <- CRS(paste("+init=epsg:28992",
"+towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812"))
summary(meuse) #proj4string : epsg:28992... and then you may use spTransform
and then:
meuse.utm <- spTransform(meuse, CRS("+proj=utm +zone=32 +datum=WGS84"))

How to convert EPSG:4326 coordinates to EPSG: 3857 in r using rgdal?

I managed to convert my set of coordinates projected in EPSG:4326 to EPSG:3857, however, the result does not match with the ones I get at
https://epsg.io/transform#s_srs=4326&t_srs=3857&x=14.5172200&y=46.0658300 or directly in ArcGIS, although the specifics of the EPSG systems provided in r (see the script below) match up with the description in the GIS software.
I have tried it with the following code:
library("rgdal", lib.loc="~/Library/R/3.4/library")
orig_coords <- data.frame(lat=c(46.065830, 46.042211, 46.094612), lon=c(14.517220, 14.487756, 14.597046))
coordinates(orig_coords) <- c('lat', 'lon')
#Determine the projection of the lat-long coordinates, by default it is EPSG:4326
proj4string(orig_coords) <- CRS("+init=epsg:4326")
print(summary(orig_coords))
#Convert the coordinates to the used metric system (EPSG:3857)
Metric_coords<-spTransform(orig_coords,CRS("+init=epsg:3857"))
print(summary(Metric_coords))
The correct coordinates would be
5790904.807 1616049.538;
5787116.145 1612769.621;
5795523.844 1624935.728;
Instead, I get:
lat - lon;
[1,] 5128025 1633624;
[2,] 5125395 1630236;
[3,] 5131229 1642804;
I cannot find where the problem is...
I believe that the CRS("+init=epsg:3857") must by capital CRS("+init=EPSG:3857")

Gdalwarp results in raster of zero's

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

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"

Assigning spatial coordinates to an array in R

I have downloaded a text file of data from the following link: http://radon.unibas.ch/files/us_rn_50km.zip
After unzipping I use the following lines of code to plot up the data:
# load libraries
library(fields)
# function to rotate a matrix (and transpose)
rotate <- function(x) t(apply(x, 2, rev))
# read data
data <- as.matrix(read.table("~/Downloads/us_rn_50km.txt", skip=6))
data[data<=0] <- NA
# rotate data
data <- rotate(data)
# plot data
mean.rn <- mean(data, na.rm=T)
image.plot(data, main=paste("Mean Rn emissions =", sprintf("%.3f", mean.rn)) )
This all looks OK, but I want to be able to plot the data on a lat-long grid. I think I need to convert this array into an sp class object but I don't know how. I know the following (from the web site): "The projection used to project the latitude and longitude coordinates is that used for the Decade of North American Geology (DNAG) maps. The projection type is Spherical Transverse Mercator with a base latitude of zero degrees and a reference longitude of 100 degrees W. The scale factor used is 0.926 with no false easting or northing. The longitude-latitude datum is NAD27 and the units of the xy-coordinates are in meters. The ellipsoid used is Clarke 1866. The resolution of the map is 50x50km". But don't know what to do with this data. I tried:
proj4string(data)=CRS("+init=epsg:4267")
data.sp <- SpatialPoints(data, CRS("+proj=longlat+ellps=clrk66+datum=NAD27") )
But had various problems (with NA's) and fundamentally I think that the data isn't in the right format.. I think that the SpatialPoints function wants a data on location (in 2-D) and a third array of values associated with those locations (x,y,z data - I guess my problem is working out the x and the y's from my data!)
Any help greatly appreciated!
Thanks,
Alex
The file in question is an ASCII raster grid. Coordinates are implicit in this format; a header describes the position of the (usually) lower left corner, as well as the grid dimensions and resolution. After this header section, values separated by white space describe how the variable varies across the grid, with values given in row-major order. Open it in a text editor if you're interested.
You can import such files to R with the fantastic raster package, as follows:
download.file('http://radon.unibas.ch/files/us_rn_50km.zip',
destfile={f <- tempfile()})
unzip(f, exdir=tempdir())
r <- raster(file.path(tempdir(), 'us_rn_50km.txt'))
You can plot it immediately, without assigning the projection:
If you didn't want to transform it to another CRS, you wouldn't necessarily need to assign the current coordinate system. But since you do want to transform it to WGS84 (geographic), you need to first assign the CRS:
proj4string(r) <- '+proj=tmerc +lon_0=-100 +lat_0=0 +k_0=0.926 +datum=NAD27 +ellps=clrk66 +a=6378137 +b=6378137 +units=m +no_defs'
Unfortunately I'm not entirely sure whether this proj4string correctly reflects the info given at the website that provided the data (it would be great if they actually provided the definition in a standard format).
After assigning the CRS, you can project the raster with projectRaster:
r.wgs84 <- projectRaster(r, crs='+init=epsg:4326')
And if you want, write it out to a raster format of your choice, e.g.:
writeRaster(r.wgs84, filename='whatever.tif')

Resources