Interpolate CSV data and mask with SHP file

20 Views Asked by At

I want to interpolate an index that exist in bottom csv with kriging method, then plot and mask that map with shp file.

lat        lon      B1
25.25      61.25    0.086
25.75      61.25    0.046
25.75      60.75    0.079
25.75      60.25    0.083
.          .         .
.          .         . 
.          .         .
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import geopandas as gpd
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt
File_path="C:/Users/user/Desktop/project/csv/mjo-gis.csv"
df= pd.read_csv(File_path)
df1=df[['LAT' , 'LON' , 'B1']]
lons=np.array(df1['LON']) 
lats=np.array(df1['LAT']) 
zdata=np.array(df1['B1'])
File_path2="C:/Users/user/Desktop/project/shp/Ostan/Ostan.shp"
Iran=gpd.read_file(File_path2)
xmin, ymin, xmax, ymax = Iran.total_bounds
xmin = xmin-0.01
xmax = xmax+0.01
ymin = ymin-0.01
ymax = ymax+0.01
grid_lon = np.linspace(xmin, xmax, 100)
grid_lat = np.linspace(ymin, ymax, 100)
OK = OrdinaryKriging(lons, lats, zdata, variogram_model='gaussian', verbose=True,                                  enable_plotting=False,nlags=20)
z1, ss1 = OK.execute('grid', grid_lon, grid_lat)
xintrp, yintrp = np.meshgrid(grid_lon, grid_lat) 
xnorm = (xintrp - xintrp.min()) / (xintrp.max() - xintrp.min())
ynorm = (yintrp - yintrp.min()) / (yintrp.max() - yintrp.min())
v = np.cos((xnorm * 2 - 1) * np.pi) + np.sin((ynorm * 2 - 1) * np.pi)
fig, ax = plt.subplots(figsize=(30,30))
Irangeom = Iran.geometry
contour = plt.contourf(xintrp, yintrp, z1,len(z1),cmap='RdBu',alpha = 0.8) 
plt.colorbar(contour)
Iran.plot(ax=ax,color='white' , alpha = 0.2, linewidth=5.5, edgecolor='black', zorder = 5)
plt.xticks(fontsize = 30, rotation=60)
plt.yticks(fontsize = 30)
plt.title('B1' ,fontsize = 40)
plt.show()

After plotting I want to mask outside shp file.

0

There are 0 best solutions below