将日期字段转换为一年中的某一天,然后将其添加到空字段arcpy python arcgis中

2024-05-23 16:45:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在ArcGIS中的一个项目中工作,其中有一组要素类都包含一个日期字段,该字段表示要素类中采样的日期。在

我需要将这些特性类中的每个日期(字段类型是“date”而不是字符串或整数)转换为一年中的某一天,然后将一年中的某一天添加到一个名为DOY的空字段中。在

日期采用mm/dd/yyyy格式,但没有前导零(7/4/15,而不是07/04/15)。每个要素类中的示例日期字段分别命名为DateSample_Dat或{},但每个要素类只包含其中一个字段。我所附的代码是我目前所拥有的。在

注意:在这3种情况下,arcpy.calculateField_management应该与上面的rows.updateRows对齐,但是当我把代码复制到这个网站上时,存在格式问题,我无法修复。在

我什么都不想编辑这一天的代码。另外,它不会给出一个红色的X或绿色的复选框,而是会给出一个黄色的三角形来发出警告,这是因为大约80%的要素类已经有了一个“DOY”字段,但我在开始时包含了addfield_管理部分,以占到没有DOY字段的20%。我认为通过添加elif语句,它不会给出警告信号,而是循环,直到到达相关语句为止。在

import arcpy      
import datetime    
fcList = arcpy.ListFeatureClasses()  
for fc in fcList:    
    if "DOY" not in arcpy.ListFields(fc):    
        arcpy.AddField_management(fc,"DOY","SHORT")    
    elif "DOY" in arcpy.ListFields(fc) and "Date" in arcpy.ListFields(fc):    
        with arcpy.da.UpdateCursor(fc, "Date") as rows:    
            for row in rows:    
                rows.updateRow([datetime.datetime.strptime(row, '%m/%d/%Y')])    
            arcpy.CalculateField_management(fc,"DOY",row.timetuple().tm_yday)    
    elif "DOY" in arcpy.ListFields(fc) and "Sample_Dat" in arcpy.ListFields(fc):  
        with arcpy.da.UpdateCursor(fc,"Sample_Dat") as rows:    
            for row in rows  :  
                rows.updateRow([datetime.datetime.strptime(row, '%m/%d/%Y')])    
            arcpy.CalculateField_management(fc,"DOY",row.timetuple().tm_yday)    
    elif "DOY" in arcpy.ListFields(fc) and "T0_Date" in arcpy.ListFields(fc):     
        with arcpy.da.UpdateCursor(fc,"T0_Date") as rows:    
            for row in rows:    
                rows.updateRow([datetime.datetime.strptime(row, '%m/%d,%Y')])                              
            arcpy.CalculateField_management(fc,"DOY",row.timetuple().tm_yday)    
    else:    
        pass    

Tags: sampleinfordatetimedatemanagementdatrows
1条回答
网友
1楼 · 发布于 2024-05-23 16:45:29

根据你的评论,我认为问题是你在检查DOY是否存在于每一个案例中,而不是在需要的地方创建它并在每个案例中更新它。尝试以下操作:

编辑:我检查了文档,看起来像arcpy.ListFields()返回字段对象,而不是字符串。我已经更新了我的代码。在

import arcpy
import datetime

for fc in arcpy.ListFeatureClasses():
    # get fields ONCE for each FC, and only get the fields we care about
    fields = [f.name for f in arcpy.ListFields(fc) if f.name in ("DOY", "Date", "Sample_Dat", "T0_Date")]

    # add desired field if it does not exist
    if "DOY" not in fields:
        arcpy.AddField_management(fc,"DOY","SHORT")
    # otherwise, remove "DOY" from the list of fields.
    else:
        fields.pop("DOY")

    if len(fields) == 0:
        print "No DATE field for FC", fc
    else:
        # the remaining item in fields should be the other date column.
        # NOTE: this MAY HAVE UNEXPRECTED RESULTS if you can have more than one of the listed date columns in a FC.
        dtm_field = fields[0]

        # now that we've ensured that the desired "DOY" field exists, we can update it from the other date field.
        with arcpy.da.UpdateCursor(fc, dtm_field) as rows:
            count = 0
            for row in rows:
                # debugging printout so you can see progress being made
                if count % 1000 == 0:
                    print count, "rows updated"
                count += 1

                ## this assumes that all date fields have the same date pattern
                #rows.updateRow([datetime.datetime.strptime(row, '%m/%d/%Y')])
                #arcpy.CalculateField_management(fc, "DOY", row.timetuple().tm_yday)

                # do you actually need to update the datetime field? If you only need to update DOY, you can do the following:
                dtm = datetime.datetime.strptime(row, '%m/%d/%Y')
                arcpy.CalculateField_management(fc, "DOY", dtm.timetuple().tm_yday)

相关问题 更多 >