Scatter plot of two rasters

11157
24
Jump to solution
10-11-2016 12:19 AM
ShouvikJha
Occasional Contributor III

Xander Bakker‌ and Dan Patterson‌ , Kindly your cooperation is highly appreciate. 

I have two raster file, i want to perform Scatter plot of those raster . I found one code that is written by Dan Patterson‌ 

Below code

from matplotlib import pyplot as plt
xs = [1,3,2,5,6,4] # Which fomart of data i have to give here?
ys = [3,2,1,5,3,2]
plt.scatter(xs, ys)
main_title ="My first graph"
plt.title(main_title, loc='center')  #loc is either left, right or center
plt.minorticks_on()
plt.tick_params(which='major', direction='in', length=6, width=2)
plt.tick_params(which='minor', direction='in', length=4, width=2)
plt.show()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Earlier Dan suggest to someone convert those raster to arrays. I followed the same  but could not solve it. How to process tiff to array.

 how do i execute two raster in X and Y axis. I have tiff images.

Branched from Perform Raster Calculation from Multiple Sub-folder Using ArcPy 

0 Kudos
24 Replies
ShouvikJha
Occasional Contributor III

Xander Bakker‌.  Thank you. I am using r001_NPP and r003_NPP, When i mentioned Random_pnts.shp then i am getting error

Input : 1 

ras1 = r'D:\TEST\Reference_RASTER\NPP_2008\r001_NPP.TIF'
    ras2 = r'D:\TEST\Reference_RASTER\NPP_2008\r003_NPP.TIF'
    fc_pnt = r'D:\TEST\Reference_RASTER\Random_Points\Random_pnts.shp'
    num_points = 1000‍‍‍‍

 

Message File Name Line Position 
Traceback 
 <module> <module1> 45 
 main <module1> 34 
RuntimeError: A column was specified that does not exist. ‍‍‍‍‍‍‍‍‍‍

Input: 2 

And when i trying to put another name (Randompnts) of point feature class using same path of exiting point feature class , then its creating the same name of point feature class as mentioned name (Randompnts)

ras1 = r'D:\TEST\Reference_RASTER\NPP_2008\r001_NPP.TIF'
    ras2 = r'D:\TEST\Reference_RASTER\NPP_2008\r003_NPP.TIF'
    fc_pnt = r'D:\TEST\Reference_RASTER\Random_Points\Randompnts'
    num_points = 1000‍‍‍‍

but after creating point feature class its giving error massage 

Traceback (most recent call last):
  File "D:\Arc-GIS-Python-Script\Scatter_Plot.py", line 45, in <module>
    main()
  File "D:\Arc-GIS-Python-Script\Scatter_Plot.py", line 30, in main
    arcpy.sa.ExtractMultiValuesToPoints(fc_pnt, in_rasters, "NONE")
  File "C:\Program Files\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 7180, in ExtractMultiValuesToPoints
    bilinear_interpolate_values)
  File "C:\Program Files\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 7175, in Wrapper
    bilinear_interpolate_values)
  File "C:\Program Files\ArcGIS\Desktop10.3\ArcPy\arcpy\geoprocessing\_base.py", line 504, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input point features: D:\TEST\Reference_RASTER\Random_Points\Randompnts does not exist.
Failed to execute (ExtractMultiValuesToPoints).‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Herewith i have attached raster and vector file which i am using for scatter plotting. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi shouvik jha ,

Let's start with input 1 and the reason why this throws an error. As I mentioned before and as the error indicates there is something wrong with the field name(s). The error points to line 34 where field names are used in a cursor to create a list of values for the graph. Remember that in the code the names of the fields created in the Extract Multi Values to Points tool are basically the names of the rasters (r001_NPP.TIF and r003_NPP.TIF) where the dot is replaced by and underscore yielding r001_NPP_TIF and r003_NPP_TIF. Both the fields have 12 characters which is longer than the 10 character limit for the fields in a Shapefile (that's just a single reason why I prefer to use featureclasses in a File Geodatabase). ArcGIS will correct the field names, however, the code will still try to access the original (invalid names) when it tries to generate the lists of values for the graph.

As mentioned before your should use ValidateTableName to create valid field names. However, this does not work correctly on shapefiles. It will not take into account the maximum number of characters for the field name in a Shapefile. Take a look at the code below:

def main():
    import arcpy
    import os
    import math
    arcpy.env.overwriteOutput = True
##    from matplotlib import pyplot as plt

    ras1 = r'C:\GeoNet\Scatter_Plot_File\NPP_2008\r001_NPP.TIF'
    ras2 = r'C:\GeoNet\Scatter_Plot_File\NPP_2008\r003_NPP.TIF'
    fc_pnt = r'C:\GeoNet\Scatter_Plot_File\NPP_2008\random_points.shp'
    num_points = 1000

    # minimum distance between points to avoid points in same pixel
    min_dist = math.hypot(arcpy.Describe(ras1).meanCellHeight, arcpy.Describe(ras1).meanCellWidth)

    # Create random points
    ws, fc_name = os.path.split(fc_pnt)
    arcpy.CreateRandomPoints_management(out_path=ws, out_name=fc_name,
                                        constraining_extent=ras1,
                                        number_of_points_or_field=num_points,
                                        minimum_allowed_distance=min_dist)

    # Extract Multi Values to Points
    arcpy.CheckOutExtension("Spatial")
    ws, ras1_name = os.path.split(ras1)
    ws, ras2_name = os.path.split(ras2)
    ws_shp, shp_name = os.path.split(fc_pnt)
    fld_ras1 = arcpy.ValidateTableName(ras1_name, ws_shp)
    fld_ras2 = arcpy.ValidateTableName(ras2_name, ws_shp)
    print ras1_name, fld_ras1
    print ras2_name, fld_ras2
    fld_ras1 = fld_ras1[:10] # take first 10 chars!
    fld_ras2 = fld_ras2[:10] # take first 10 chars!
    print ras1_name, fld_ras1
    print ras2_name, fld_ras2
    in_rasters = [[ras1, fld_ras1], [ras2, fld_ras2]]
    arcpy.sa.ExtractMultiValuesToPoints(fc_pnt, in_rasters, "NONE")
    arcpy.CheckInExtension("Spatial")

    # create lists of values
    print [fld.name for fld in arcpy.ListFields(fc_pnt)]
    xs = [r[0] for r in arcpy.da.SearchCursor(fc_pnt, (fld_ras1)) if not r[0] is None]
    ys = [r[0] for r in arcpy.da.SearchCursor(fc_pnt, (fld_ras2)) if not r[0] is None]

    # create plot
##    plt.scatter(xs, ys)
##    main_title ="Scatter plot of {0} vs {1}".format(ras1_name, ras2_name)
##    plt.title(main_title, loc='center')
##    plt.minorticks_on()
##    plt.show()

if __name__ == '__main__':
    main()

This will print:

r001_NPP.TIF r001_NPP_TIF
r003_NPP.TIF r003_NPP_TIF
r001_NPP.TIF r001_NPP_T
r003_NPP.TIF r003_NPP_T
[u'FID', u'Shape', u'Id', u'r001_NPP_T', u'r003_NPP_T']

On line 1 and 2 the "validated" field names with a length larger than the allowed 10 characters in a Shapefile. Lines 3 and 4 after cutting off the additional characters and on line 5 the actual fieldnames in the shapefile. Choose your output format wisely!

ShouvikJha
Occasional Contributor III

xander_bakker‌. Thank you very much. Now script running perfectly  and I got absolutely correct output from above script.  Once again thank you for your kind cooperation. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome, glad it works!

DanPatterson_Retired
MVP Emeritus

kudos... you can't branch a branch... so an extra +1