3

I am posting this question after three days searching the net but no success. Hope can get the answer here. Please do NOT delete the post as I did not find an answer for it here also. Thanks.

I have 2 files:

  1. A raster image file (i.e., Air temperature 2020-01-01.tif)
  2. World countries boundary shapefile ((i.e., World_Countries_base_map.shp)

Goal: I want to plot the shapefile on top of raster file, and then save the plot in a Jpeg file format to get something like this eventually:

enter image description here

I am quite new in Python, and used Spyder to prepare this simple code to do so:

# Import needed packages
import os
import rasterio
import matplotlib.pyplot as plt
import geopandas as gpd
import earthpy as et
from matplotlib import pyplot

## list all raster images in tiff format in the folder:
list_files = [f for f in 
       os.listdir('C:/Users/Desktop/Question/Raster_Air_temp') 
       if '.tif' in f]
print(list_files[1])  # checking the 1st file in the list

## reading the first tiff file:    
raster_image = rasterio.open(list_files[1])

## plot it
draft_output = pyplot.imshow(raster_image.read(1), cmap='jet')

## importing world shapefile
World_map = gpd.read_file('C:/Users/Desktop/Question/World_shapefile/World_Countries_base_map.shp')

# plot World shapefile
fig, ax = plt.subplots(figsize = (30,30))  # image size and quality can be controled by figsize
ax.set_title('The Glob Map', fontsize=50); 
World_map.plot(ax=ax, color='white', edgecolor='black')     # colors note at  https://matplotlib.org/tutorials/colors/colormaps.html
plt.show()

## Plot both World shapefile and raster image in one graph:

????  

enter image description here

However, this code just produces 2 separated plots in the console for me as can be seen above.

Question: How can I type a proper code in ???? section of the code to get to my Goal (mentioned above)? Thanks to all comments and helps.

Here, I share the two files in order to make it easier for those who want help. Download the files from my Dropbox

.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Canada2015
  • 187
  • 1
  • 12

1 Answers1

3

since i have no access to your data I am showing the principle with some sample data from geopandas and a random numpy ndarray as a tiff surrogate.

the key point is to show the tiff with rasterios rasterplot and don't forget to set the extent of your DEM!

enter image description here

import rasterio
import numpy as np
from rasterio import plot as rasterplot
import geopandas as gpd
from matplotlib import pyplot as plt


# this is how you'd open the raster dataset if you have one
#tiff = rasterio.open('example.tif')
#tiff_extent = [tiff.bounds[0], tiff.bounds[2], tiff.bounds[1], tiff.bounds[3]]

# i am making this array up
tiff_band_1 = np.random.randint(0, 10, size=(65, 64))
tiff_extent = [4159200.0, 4808100.0, 2828000.0, 3482600.0]

shapefile = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
shapefile = shapefile.to_crs('epsg:3035')
shapefile = shapefile[shapefile.name == 'Germany']

f, ax = plt.subplots()

# plot DEM
rasterplot.show(
    tiff_band_1,  # use tiff.read(1) with your data
    extent=tiff_extent,
    ax=ax,

)
# plot shapefiles
shapefile.plot(ax=ax, facecolor='w', edgecolor='k')
plt.savefig('test.jpg')
plt.show()
AlexWach
  • 592
  • 4
  • 16
  • 1
    It worked perfectly fine for me But it doesn't actually save the image which I think can be done using some matplotlib function that can save the plot as an image. Thanks – Talha Khan Jul 29 '22 at 13:52