Package: pyidw
All in one IDW package for pythonThis is an example map created using the pyidw library.FeaturesSimple IDW Interpolation.IDW with external raster (eg, elevation raster) covariable.Accuracy Score.Built-in raster visualisation with coordinate and colour bar.Why pyidw ?Inverse distance weighted interpolation is one of the simplest geospatial interpolation methods available in GIS. Although it is easy to produce an idw raster using conventional desktop GIS software (eg. ArcGIS, QGIS). It was never straightforward to create such a beautiful map image using python. This is why I have created the pyidw library where you can create beautiful idw maps of your desired location using your favourite programming language 🐍pyidw package also incorporates a clever technique to use additional raster data as a covariable using polynomial regression. For example, if you are working with temperature data, it is widely known that temperature is inversely proportional to elevation, the higher the elevation, the lower the temperature is. With pyidw, you can easily add elevation data with traditional idw calculation to obtain a different result.Installationpyidwlibrary can be installed using a simplepip install pyidwcommand. However, if you are facing trouble installingpyidwon your windows machine, please try the commands below on the windows command line.pip install wheel
pip install pipwin
pipwin refresh
pipwin install numpy
pipwin install pandas
pipwin install shapely
pipwin install gdal
pipwin install fiona
pipwin install pyproj
pipwin install six
pipwin install rtree
pipwin install geopandas
pipwin install rasterio
pip install pyidw
pipwin refreshExampleIf you are convinced enough to givepyidwa try, here is a simple tutorial for you. You should first download thepyidw_example.zipfile. This zip file contains four files,pyidw_tutorial.ipynbBangladesh_Temperature.shpBangladesh_Border.shpBangladesh_Elevation.tifThepyidw_tutorial.ipynbfile is a jupyter notebook file of this example tutorial, which you could try to run and then modify with your own data. TheBangladesh_Temperature.shpfile is an ESRI point shapefile that contains maximum and minimum temperature values for 34 weather stations all over Bangladesh. Its attribute table looks something like this.Station_NameStation_IDLatitudeLongitudeMax_TempMin_TempBARISALBGM0004195022.7590.3736.759.60BHOLA4195109999922.6890.6535.6210.19BOGRABGM0004188324.8589.3738.628.29CHANDPUR4194109999923.2790.7035.8711.28CHITTAGONGBGM0004197822.2591.8136.9211.24CHUADANGA4192609999923.6588.8237.848.59COMILLA4193309999923.4391.1835.4110.35COXS BAZARBGM0004199221.4591.9637.1111.51For those who are not familiar with shapefile, every shapefile consists of seven different files with the same name but seven different file extensions. Namely.cpg .dbf .prj .sbn .sbx .shpand.shx. If any of these files are missing then the shapefile system won't work properly. Note thatMax_TempandMin_Tempcolumn inBangladesh_Temperature.shpfiles attribute table, we will use one of these columns later when creating IDW interpolated maps.TheBangladesh_Border.shpis an ESRI polygon shapefile that covers all the areas of the country Bangladesh. We will use this shapefile to define the calculation extent for IDW interpolation. And finally, theBangladesh_Elevation.tiffile which is a raster file containing elevation information in meter, We don't need this file for standard IDW interpolation but with regression_idw, we will use this file as an external covariable. All the files and their spatial dimension is shown below.idw_interpolation()Now the fun part begins. Write these few lines of code from below in any python interpreter while you are onpyidw_exampledirectory.frompyidwimportidwidw.idw_interpolation(input_point_shapefile="Bangladesh_Temperature.shp",extent_shapefile="Bangladesh_Border.shp",column_name="Max_Temp",power=2,search_radious=4,output_resolution=250,)It will take a few seconds to complete, then a map image like below will be shown. And a new file will be created namelyBangladesh_Temperature_idw.tif, this is the saved raster file of the interpolated map. This file is named after input_point_shapefile name with_idw.tifsuffix.idw_interpolation()function take six parameters.The first parameterinput_point_shapefile=take an ESRI point shapefile which should contain the particular data value we are interested to create an interpolation map. Also, there shouldn't be any value outside of our given extent_shapefile area.The second parameterextent_shapefile=take an ESRI polygon shapefile, this shapefile is used for defining the calculation and mapping boundary. The coordinate system of extent_shapefile should be the same as input_point_shapefile.The third parametercolumn_name=take the column name of a particular field as a string. This is the value upon which the IDW map will be created.The fourth parameterpower=is an optional parameter with a default value of 2, this is the power parameter fromidw equation.The fifth parametersearch_radious=is also an optional parameter with a default value of 4, it determines how many nearest points will be used for idw calculation.The sixth parameteroutput_resolution=is also optional with default value of 250. This parameter defines the maximum height or width (which one is higher) of the resulting_idw.tiffile in pixel.Output map  fromidw_interpolation()function.accuracy_standard_idw()If you are interested in accuracy assessment of your interpolation then you could useaccuracy_standard_idw()function frompyidwwhich take 6 parameters same asidw_interpolation()function. But instead of creating a idw interpolated map, theaccuracy_standard_idw()function return tow python list. The first one contains actual data values from the input shapefile and the second list contains the interpolated values for those data points usingLeaveOneOutcross-validation method. Then you could compare them to obtain your desired accuracy score. An example code foraccuracy_standard_idw()function is given below.frompyidwimportidwfromsklearn.metricsimportmean_squared_errororiginal_value,interpolated_value=idw.accuracy_standard_idw(input_point_shapefile="Bangladesh_Temperature.shp",extent_shapefile="Bangladesh_Border.shp",column_name="Max_Temp",power=2,search_radious=6,output_resolution=250,)print("RMSE:",mean_squared_error(original_value,interpolated_value,squared=False))Output:RMSE: 1.401379show_map()We have also implemented a raster visualization function namedshow_map(). This function incorporates easy map visualization with a built-in colour bar and coordinate tick marks. It takes 4 parameters.input_raster=take raster file name as argument.colormap=is an optional parameter which takematplotlib colormapsparameter. By changing this, you can easily alter the looks of your map image.image_size=is also an optional parameter with a default value set to 1.5 which you can change to make your resulting image larger or smaller.The last parameterreturn_figure=is for those people who wish to alter the resulting image to their liking. by default it is set to false andshow_map()function won't return anything other than showing the map on the screen. If set to true, thenshow_map()function will return figure, axes and color_bar to the user. We will see a detailed example of this in the next section.Here is an example code ofshow_map()function.frompyidwimportidwshow_map(input_raster="Bangladesh_Temperature_idw.tif",colormap="nipy_spectral_r",image_size=1.5,return_figure=False,)Below is an example code of settingreturn_figure=toTrueand adding some extra elements to the map 🗺 image.frompyidwimportidwfrommatplotlibimportpyplotaspltfig,ax,cbar=idw.show_map(input_raster="Bangladesh_Temperature_idw.tif",colormap="CMRmap",image_size=1.5,return_figure=True)ax.set_title("Maximum temperature map")ax.set_xlabel("Longitude")ax.set_ylabel("Latitude")cbar.set_label("maximum annual temperature")plt.show()regression_idw_interpolation()This function is quite different than regular IDW interpolation as it incorporates external raster covariable, polynomial regression and r_squared value. This is an experimental method and we don't recommend using it as it doesn't always produce reliable output and accuracy score are also lower than regular idw interpolation. It take same parameters asidw_interpolation()function only with 2 extra parameters namelyinput_raster_file=which is the raster covariable.  input_raster file should be larger thanextent_shapefile. The other parameter ispolynomial_degre=with a default value set to 1 which would define thepolynomial regression equation.  An example code forregression_idw_interpolation()is given below.frompyidwimportidwidw.regression_idw_interpolation(input_point_shapefile="Bangladesh_Temperature.shp",input_raster_file="Bangladesh_Elevation.tif",extent_shapefile="Bangladesh_Border.shp",column_name="Min_Temp",power=2,polynomial_degree=1,search_radious=5,output_resolution=250,)accuracy_regression_idw()This function is similar toaccuracy_standard_idw()function. An example code is given below.frompyidwimportidwfromsklearn.metricsimportmean_squared_errororiginal_value,interpolated_value=idw.accuracy_regression_idw(input_point_shapefile="Bangladesh_Temperature.shp",input_raster_file="Bangladesh_Elevation.tif",extent_shapefile="Bangladesh_Border.shp",column_name="Min_Temp",power=2,polynomial_degree=1,search_radious=5,output_resolution=250,)print("RMSE:",mean_squared_error(original_value,interpolated_value,squared=False))Output:RMSE: 1.086343If you have any questions, problems or suggestion, feel free to contact me at:yahyatamim0@gmail.com