Arcmap python script - System Compatibility

1125
4
09-11-2020 03:30 AM
MudassarAli2
Emerging Contributor

I have written a script for automating a task on .img file. The script is running perfect on windows 7 but giving error (snapshot attached) on windows 10. Both the systems have the same version of arcmap. 

# -*- #################
# ---------------------------------------------------------------------------
# 
# 
# 
# 
# ---------------------------------------------------------------------------
# Set the necessary product code
import arcinfo
# Import arcpy module
import arcpy,os,string,glob,sys
# Script arguments
DEM = arcpy.GetParameterAsText(0)
#river splitted shapefiles directory path
rootFolder = arcpy.GetParameterAsText(1)
#output directory
outputDir = arcpy.GetParameterAsText(2)
# Local variables:
home=os.path.expanduser('~')
constant_raster_ = home+"\\Documents\\ArcGIS\\Default2.gdb\\CreateConsta1"
point_feature = home+"\\Documents\\ArcGIS\\Default2.gdb\\c20_FeatureVerticesToPoints"
point_feature_with_elevation = point_feature
min_elevation = home+"\\Documents\\ArcGIS\\Default2.gdb\\c20_FeatureVerticesToPoints_"
Value = min_elevation
rastercalc = home+"\\Documents\\ArcGIS\\Default2.gdb\\rastercalc"
WaterMask = home+"\\Documents\\ArcGIS\\Default2.gdb\\Extract_rast3" # provide a Default2 value if unspecified
#loop for iterations through all shapefiles
for root,dirs,files in os.walk(rootFolder):  
    for name in files:                  
        if name.endswith(".shp"):
            #try:
                shpName = name #os.path.splitext(name)[0]  
                # absolute file path of shapefile river splits 
                absFile = os.path.abspath(os.path.join(rootFolder,shpName))                
                water_body_feature_class = absFile
                # Process: Create Constant Raster
                arcpy.gp.CreateConstantRaster_sa(constant_raster_, "1", "FLOAT", DEM, water_body_feature_class)
                # Process: Feature Vertices To Points
                arcpy.FeatureVerticesToPoints_management(water_body_feature_class, point_feature, "ALL")
                # Process: Extract Multi Values to Points
                arcpy.gp.ExtractMultiValuesToPoints_sa(point_feature, DEM + ' ' +'elevation', 'NONE')
                # Process: Summary Statistics
                arcpy.Statistics_analysis(point_feature_with_elevation, min_elevation, "elevation MIN", "")
                # Process: Get Field Value
                #arcpy.GetFieldValue_mb(min_elevation, "MIN_elevation", "Double", "0")
                SC = arcpy.SearchCursor(min_elevation)
                field_name = 'MIN_elevation'
                for row in SC:                        
                    value=row.getValue(field_name)
                # Process: Raster Calculator
                #arcpy.gp.RasterCalculator_sa("\"%constant_raster_ %\" * float(%value%)", rastercalc)
                #arcpy.gp.RasterCalculator_sa(constant_raster_ * float(value), rastercalc)
                rastercalc=arcpy.sa.Raster(constant_raster_)*float(value)                
                # Process: Extract by Mask (2)            
                WaterMask=arcpy.gp.ExtractByMask_sa(rastercalc, water_body_feature_class)
                arcpy.CopyRaster_management(WaterMask,outputDir+"\\"+name.replace(".shp",'')+".img") 
            #except Exception:
                # e=sys.exc_info()[1]
                # print(e.arg[0])
                #arcpy.AddError("Error in Shapefile "+shpName)
Tags (2)
0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

just a thought, line 41, nothing is going to like a name with a space in it

DEM + ' ' +'elevation'

... sort of retired...
0 Kudos
MichaelVolz
Esteemed Contributor

Can you put in some print statements to see if anything changes with the home variable in the transition from Win7 to Win10 to make it invalid possibly?

0 Kudos
MudassarAli2
Emerging Contributor

You mean I have to print value of home variable after line 18 on windows 7 and 10 both?

0 Kudos
DanPatterson
MVP Esteemed Contributor
# ---- indent the following by 4 spaces !!!!!

for root,dirs,files in os.walk(rootFolder):  
    for name in files:                  
        if name.endswith(".shp"):
            shpName = name #os.path.splitext(name)[0]  
            # absolute file path of shapefile river splits 
            absFile = os.path.abspath(os.path.join(rootFolder,shpName))                
            water_body_feature_class = absFile
            # Process: Create Constant Raster
            arcpy.gp.CreateConstantRaster_sa(
                constant_raster_, "1", "FLOAT", DEM, water_body_feature_class)
            # Process: Feature Vertices To Points
            arcpy.FeatureVerticesToPoints_management(
                water_body_feature_class, point_feature, "ALL")
            # Process: Extract Multi Values to Points
            arcpy.gp.ExtractMultiValuesToPoints_sa(
                point_feature, DEM + ' ' +'elevation', 'NONE')
            # Process: Summary Statistics
            arcpy.Statistics_analysis(
                point_feature_with_elevation, min_elevation, "elevation MIN", "")
            # Process: Get Field Value
            #arcpy.GetFieldValue_mb(min_elevation, "MIN_elevation", "Double", "0")
            SC = arcpy.SearchCursor(min_elevation)
            field_name = 'MIN_elevation'
            for row in SC:                        
                value=row.getValue(field_name)
                # Process: Raster Calculator
                rastercalc=arcpy.sa.Raster(constant_raster_)*float(value)                
                # Process: Extract by Mask (2)            
                WaterMask = arcpy.gp.ExtractByMask_sa(
                                rastercalc, water_body_feature_class)
                arcpy.CopyRaster_management(
                    WaterMask,outputDir+"\\"+name.replace(".shp",'')+".img") 

Print statements.... means throw some in so people can see the output that you are getting.

Also, your rastercalculator would only process one value since lines 29 onward were improperly indented.

I suspect the differences weren't due to Windows differences, but differences in copying and/or formatting from one machine to the other.

Also... note my comment to indent all the lines above by 4 spaces and replace your "for..... " section to the end with the above... after you throw in some print statements to monitor output names etc.


... sort of retired...
0 Kudos