How to project GeoTIFF to specified area extent? - python

I am trying to display GeoTIFF file in specified area. What I want to do is project multiband GeoTIFF file to specified area.
My GeoTIFF is satellite image containing 3 bands over area of Europe. I want to project it to area of Central Europe (extent defined by lat/lot). I have been trying to solve this using rasterio but I've been unlucky so far. This is what I get after executing:
import georaster
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.pyplot import figure
#define path to geotiff
file = "/home/lubomir/Desktop/Sentinel3_OLCI/RGB/OLCI_201812140859_Natural_Color.tif"
m = Basemap(epsg=3395,llcrnrlat=45,urcrnrlat=55,\
llcrnrlon=5,urcrnrlon=25,lat_ts=15,resolution='f')
m.drawcoastlines(linewidth=0.5, color='g')
m.fillcontinents(color='beige')
m.drawcountries(linewidth=0.5, color='m')
#load GeoTIFF multiband file
image = georaster.MultiBandRaster(file)
plt.imshow(image.r, extent=image.extent, zorder=10)
plt.savefig('test.tiff')
plt.show()
without_geotiff
As you can see, generated image does not contain my GeoTIF. Any idea how to solve this ?

In order to make use of the Basemap's projection, you must use
m.imshow(image.r, extent=image.extent, zorder=10)
in place of
plt.imshow(image.r, extent=image.extent, zorder=10)
Hope it helps.

Related

Plot a Point on an high resolution Image

I want to automate one process, and I need to place some kind of pointer on my image. I found a great solution which works exactly as I would like, but its disadvantage is that it destroys my picture quality. I want to keep same size of the build in picture.
Bellow I share my code and error, which I receive. I would be grateful for your help :)
from matplotlib import image
from matplotlib import pyplot as plt
from PIL import Image
# to read the image stored in the working directory
# data = image.imread(file_name)
data = Image.open('File_name')
x, y = data.size
# to draw a point on co-ordinate (200,300)
plt.figure(figsize=(x, y))
plt.plot(650, 310, marker='*', color="red")
# plt.axis('off')
plt.imshow(data)
File = "File_name"
plt.savefig(File)
plt.show()
ValueError: Image size of 105480x55224 pixels is too large. It must be less than 2^16 in each direction.

Convert 3D .stl file to JPG image with Python

How do I convert a 3D object in any STL file into a JPG or PNG image.
I tried searching a little bit online but I wasn't been able to arrive at finding any possible solutions.
Can anyone help me with the code that can do this straight forward task with Python? IS there any libraries that can help with that?
EDIT :
Code Sample:
from mpl_toolkits import mplot3d
from matplotlib import pyplot
import pathlib
DIR = str(pathlib.Path(__file__).parent.resolve()).replace('\\', '/')
path = f'{DIR}/any_stl_file.stl'
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file(path)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
pyplot.savefig(f"{DIR}/the_image.jpg")```
Take a look at https://pypi.org/project/numpy-stl/
This code snippet is from the link above:
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
To save a pyplot object as an image:
pyplot.savefig("file_name.jpg")

Changing Astropy WCS projection

I use the below python script to read the world coordinate system (WCS) form a fits file and plot it.
from astropy.io import fits
import matplotlib.pyplot as plt
from astropy.wcs import WCS
hdu = fits.open('file.fits')
header = hdu[0].header
data = hdu[0].data
wcs = WCS(header, naxis=2)
plt.figure().add_subplot(1,1,1, projection=wcs)
plt.imshow(data)
plt.grid()
plt.show()
However, the above script projects the image in RA and DEC. I was wondering if there is a way to make it show the image in Azimuth and Elevation.
Thanks.

Cartopy Image from Grib2 file does not align with Coastlines and Borders in same CRS

This seems like it's going to be something simple that will fix my code but I think I've just looked at the code too much at the moment and need to get some fresh eyes on it. I'm simply trying to bring in a Grib2 file that I've downloaded from NCEP for the HRRR model. According to their information the grid type is Lambert Conformal with the extents of (21.13812, 21.14055, 47.84219, 47.83862) for the latitudes of the corners and (-122.7195, -72.28972, -60.91719, -134.0955) for the longitudes of the corners for the models domain.
Before even trying to zoom into my area of interest I just wanted to simply display an image in the appropriate CRS however when I try to do this for the domain of the model I get the borders and coastlines to fall within that extent but the actual image produced from the Grib2 file is just zoomed into. I've tried to use extent=[my domain extent] but it always seems to crash the notebook I'm testing it in. Here is my code and the associated image that I get from it.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
from mpl_toolkits.basemap import Basemap
from osgeo import gdal
gdal.SetConfigOption('GRIB_NORMALIZE_UNITS', 'NO')
plt.figure()
filename='C:\\Users\\Public\\Documents\\GRIB\\hrrr.t18z.wrfsfcf00.grib2'
grib = gdal.Open(filename, gdal.GA_ReadOnly)
z00 = grib.GetRasterBand(47)
meta00 = z00.GetMetadata()
band_description = z00.GetDescription()
bz00 = z00.ReadAsArray()
latitude_south = 21.13812 #38.5
latitude_north = 47.84219 #50
longitude_west = -134.0955 #-91
longitude_east = -60.91719 #-69
fig = plt.figure(figsize=(20, 20))
title= meta00['GRIB_COMMENT']+' at '+meta00['GRIB_SHORT_NAME']
fig.set_facecolor('white')
ax = plt.axes(projection=ccrs.LambertConformal())
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.coastlines(resolution='110m')
ax.imshow(bz00,origin='upper',transform=ccrs.LambertConformal())
plt.title(title)
plt.show()
Returns Just Grib File
If I change:
ax = plt.axes(projection=ccrs.LambertConformal()
to
ax = plt.axes(projection=ccrs.LambertConformal(central_longitude=-95.5,
central_latitude=38.5,cutoff=21.13)
I get my borders but my actual data is not aligned and it creates what I'm dubbing a Batman plot.
Batman Plot
A similar issue occurs even when I do zoom into the domain and still have my borders present. The underlying data from the Grib file doesn't change to correspond to what I'm trying to get.
So as I've already said this is probably something that is an easy fix that I'm just missing but if not, it would be nice to know what step or what process I'm screwing up that I can learn from so that I don't do it in the future!
Updated 1:
I've added and changed some code and am back to getting only the image to show without the borders and coastlines showing up.
test_extent = [longitude_west,longitude_east,latitude_south,latitude_north]
ax.imshow(bz00,origin='upper',extent=test_extent)
This gives me the following image.
Looks exactly like image 1.
The other thing that I'm noticing which maybe the root cause of all of this is that when I'm printing out the value for plt.gca().get_ylim() and plt.gca().get_xlim() I'm getting hugely different values depending on what is being displayed.
It seems that my problem arises from the fact that the Grib file regardless of whether or not it can be displayed properly in other programs just doesn't play nicely with Matplotlib and Cartopy out of the box. Or at the very least does not with the Grib files that I was using. Which for sake of this perhaps helping others in the future are from the NCEP HRRR model that you can get here or here.
Everything seems to work nicely if you convert the file from Grib2 format to NetCDF format and I was able to get what I wanted with the borders, coastlines, etc. on the map. I've attached the code and the output below to show how it worked. Also I hand picked a single dataset that I wanted to display to test versus my previous code so incase you want to look at the rest of datasets available in the file you'll need to utilize ncdump or something similar to view the information on the datasets.
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
import cartopy.feature as cfeature
from osgeo import gdal
gdal.SetConfigOption('GRIB_NORMALIZE_UNITS', 'NO')
nc_f = 'C:\\Users\\Public\\Documents\\GRIB\\test.nc' # Your filename
nc_fid = Dataset(nc_f, 'r') # Dataset is the class behavior to open the
# file and create an instance of the ncCDF4
# class
# Extract data from NetCDF file
lats = nc_fid.variables['gridlat_0'][:]
lons = nc_fid.variables['gridlon_0'][:]
temp = nc_fid.variables['TMP_P0_L1_GLC0'][:]
fig = plt.figure(figsize=(20, 20))
states_provinces = cfeature.NaturalEarthFeature(category='cultural', \
name='admin_1_states_provinces_lines',scale='50m', facecolor='none')
proj = ccrs.LambertConformal()
ax = plt.axes(projection=proj)
plt.pcolormesh(lons, lats, temp, transform=ccrs.PlateCarree(),
cmap='RdYlBu_r', zorder=1)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':', zorder=2)
ax.add_feature(states_provinces, edgecolor='black')
ax.coastlines()
plt.show()
Final Preview of Map

Display x,y coordinates for loaded image file

I need to load an image file with matplotlib and see the coordinates of points within it, as if it were a simple x,y scatter plot.
I can assume that the x axis extension is [0, 1], and the y axis follows the same scaling. I can load the above image file with
from PIL import Image
im = Image.open("del.png")
im.show()
but this uses ImageMagick (I'm on a Linux system) to display the image, and no coordinates are shown in the bottom left part of the plot window as would for a simple data plot:
Use pyplot for that:
from matplotlib import pyplot as plt
plt.imshow(plt.imread('del.png'))

Categories

Resources