1

I'm trying to use arcpy.ExtractValuesToPoints() to get raster values to a points feature class for several raster files (8760). The goal is to get a time-slice view of how the values at certain locations change over time.

I'm fairly well versed in Python as well as the built-in datetime module. The trouble that I'm having is saving the date-time values to a table in ArcGIS.

(I've gotten a similar approach to work using arcpy.da.InsertCursor() but this approach is very process-intensive and I've concluded that it isn't a viable approach for what I'm trying to do. Plus I'd rather not re-invent the wheel and use the ArcGIS function: ExtractValuesToPoints() instead.)

To give a little background, I'm using dates that require datetime precision to the hour so I've elected to use a File Geodatabase to accommodate that feature (since shapefiles can only handle the date portion).

I'm trying to solve for a single case as I can create a solution for iterating over the 8760 files that I'm working with.

So far this is what I have:

import arcpy
import os

GRID_Table = "E:\HRRR.gdb\GRID_Table"               # GRID_Table is an ArcGIS table
SPP_LMP_Points = "E:\HRRR.gdb\SPP\SPP_LMP_Points"   # that contains datetime and                  
out_path = "C:\scratch.gdb"                         # raster_path pairs.


row1 = []                                           # Get date_time and raster_path      
for row in arcpy.SearchCursor(GRID_Table):          # from every entry in GRID_Table
    row1.append((row.DateTime, row.RasterPath))     #

date_time = row1[0][0]                              # Set date_time to the first 
raster_path = row1[0][1]                            # entry 

out_path_shp = os.path.join(out_path, os.path.basename(raster_path))  # Generate 
                                                                      # output path.

arcpy.sa.ExtractValuesToPoints(SPP_LMP_Points, raster_path, out_path_shp)

arcpy.AddField_management(out_path_shp, "DateTime", "DATE")

arcpy.CalculateField_management(out_path_shp, "DateTime", 
                                "!DateTime! = {0}".format(date_time), 
                                "PYTHON_9.3")

This all works fine and doesn't report any exceptions or errors. The trouble that I'm having is that the call to arcpy.CalculateField_management() doesn't populate the DateTime field. Instead I get a NULL value in the table.

Any thoughts?

5
  • Have you considered using an InsertCursor instead of CalculateField_management?
    – nmpeterson
    Dec 23, 2014 at 16:39
  • What do you get when you do a print date_time prior to your field calculate? Dec 23, 2014 at 16:53
  • 1
    Possibly a datatype mismatch? Your field DateTime is a DATE field, but are passing string to in CalculateField maybe? Dec 23, 2014 at 16:54
  • nmpeterson - I have tried the InsertCursor() and I've gotten it to work. The trouble is that I'm working with a fairly large set of data and accessing each feature line-by-line isn't feasible. Dec 23, 2014 at 17:00
  • Emil Brundage - print date_time gives me the standard __str__() output from a datetime object (ie 2014-12-20 11:00:00). If I type print(type(date_time)) I get <type 'datetime.datetime'>. Dec 23, 2014 at 17:02

2 Answers 2

3

You just don't quite have the quotes right in your expression.You need double quotes around your datetime. Also, you shouldn't have !DateTime! = as part of it.

Try:

arcpy.CalculateField_management(out_path_shp, "DateTime", 
                                '"' + date_time + '"', 
                                "PYTHON_9.3")
1
  • Emil - Great suggestion. This wasn't exactly the answer but I was able to use this snippet to get done what I was intending. I'll give this a +1 but check my answer for the solution. Dec 29, 2014 at 16:07
1

Based on Emil's response below I was able to find a sufficient answer.

I used Emil's code, verbatim, and was able to get the following error: "TypeError: cannot concatenate 'str' and 'datetime.datetime' objects"

The final solution is as follows (note the str() call):

arcpy.CalculateField_management(out_path_shp, "DateTime", 
                                '"' + str(date_time) + '"', 
                                "PYTHON_9.3")

To clarify, the above code enters the datetime information into an ArcGIS "date" featureclass field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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