0

I am running a for loop to process a years worth of data. The entire script runs fine for the first run through, but when it runs again (when date is 1/2/2013), I get the following error: 'NoneType object has no attribute 'XMin'.

Before the project tool is run the 1/2/2013 shapefile looks fine. After the project tool is run, the new shp is empty. If I start the script at date = dt(2013, 1, 2) the script runs fine. So there is nothing wrong with the shapefile.

I am not sure if something is stuck in the memory? I added these 4 lines of code at the end of the loop but this doesn't change anything.

    sr = None
    srProj = None
    shpFile = None
    shpFileProj = None

#

date = dt(2013, 1, 1)

for i in range(365):

    print (date)
    fileFolder = date.strftime('%Y') + os.sep + date.strftime('%m') + os.sep + date.strftime('%d')
    fileName = "1day_observed_shape_" + date.strftime('%Y') + date.strftime('%m') + date.strftime('%d') + ".tar.gz"
    filePath = "N:\\zip\\Precip\\" + fileFolder + os.sep + fileName
    print filePath
    date += timedelta(days=1)


    # Copy the tar file to the extracted folder and to the archive folder on the desktop
    shutil.copy(filePath, cfg.exportPath)
    shutil.copy(filePath, "C:\\Users\\alou\\Desktop\\precip_tar\\2013")


    os.chdir(cfg.exportPath)
    #tarFile = tarfile.open(fileString[30:], 'r:gz')
    tarFile = tarfile.open(fileName, 'r:gz')
    tarFile.extractall('.')

    # List the shapefile
    for shpFile in glob.glob("*.shp"):
        print shpFile

    ################## creating grid START ###################
    # Delete prj file
    os.remove(shpFile[:-3] + "prj")

    logger.info("Define the projection")
    # Define projection
    sr = "C:/load_assist_files/precip/HRAP_GCS.prj"
    arcpy.DefineProjection_management(shpFile, sr)

    logger.info("Reproject the data")
    # Reproject the layer
    srProj = "C:/load_assist_files/precip/HRAP_Projected.prj"
    shpFile = "C:/extracted/" + shpFile
    shpFileProj = shpFile[:-4] + "_proj.shp"
    arcpy.Project_management(shpFile, shpFileProj, srProj)

    # Set local variables point to raster
    valField = "Globvalue"
    outRaster = "C:/extracted/outRaster"
    assignmentType = "MAXIMUM"
    priorityField = ""
    cellSize = 1

    # Set environment settings point to raster
    arcpy.env.outputCoordinateSystem = shpFileProj
    arcpy.env.extent = shpFileProj
    print shpFileProj
    print arcpy.env.extent
    XMin = int(arcpy.env.extent.XMin)
    YMin = int(arcpy.env.extent.YMin)
    XMax = int(arcpy.env.extent.XMax)
    YMax = int(arcpy.env.extent.YMax)
  • Untested, but is the extent set for the first shapefile (arcpy.env.extent = shpFileProj) the same as that expected for the second shapefile? If it's not the same, I might expect it to ignore those features outside the previously set extent environment variable from the previous pass through the loop. – phloem Nov 18 '14 at 0:10
2

On the first pass through the loop, the extent environment variable is set to the extent of the projected version of the first shapefile.

On the second pass through the loop, the extent environment variable is still associated with the first shapefile. When Project is called, only the features within that extent are processed (presumably no features in the second shapefile fell within the extent of the first shapefile). Since shpFileProj is now empty, arcpy.env.extent will eventually be set empty, so it does not have an attribute 'XMin', as the error indicates.

As you have discovered, setting the extent environment variable to None at the end of the loop will allow the next Project call to occur without regard to the previous extent.

1

According to the Help for the Project tool it takes a valid spatial reference object as the output coordinate system, it appears you are providing it a string which is a path to a prj file. The help also suggests a prj file can be used so try using a spatial reference object.

0

It seems to be working now. At the very end of my loop I added the following code.

    arcpy.env.outputCoordinateSystem = None
    arcpy.env.extent = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.