|
POST
|
My apologizes for number of responses. So field "Year" has just years, 2015,1997, 2022, etc and is a "text" field. It doesn't have month or days When I replace the "%Y" with "%m/%d/%Y" d1 = datetime.strptime(row[0], "%m/%d/%Y").year I get; ERROR 000539: Traceback (most recent call last): File "<expression>", line 1, in <module> File "<string>", line 11, in calcYear TypeError: strptime() argument 1 must be str, not int Failed to execute If i use the following from datetime import datetime
from datetime import time
import datetime
def calcYear():
fc = "Lyr"
#testdate = '04/25/2015'
with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
d1 = datetime.datetime.strptime("{}".format(row[0]),"%Y").year
return d1
#formattedTime = datetime.datetime.strptime("{}".format(!Year!), "%Y")
#row[1] = d1
#cursor.updateRow(row) I get Value = 1997 the Field "Year1" doesn't get updated I tested this in python window import arcpy
from datetime import datetime
from datetime import time
import datetime
#def calcYear():
fc = "Lyr"
#testdate = '04/25/2015'
with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
d1 = datetime.datetime.strptime("{}".format(row[0]),"%Y").year
print (d1)
#formattedTime = datetime.datetime.strptime("{}".format(!Year!), "%Y")
row[1] = d1
cursor.updateRow(row) I get the year printed 2022 2022 2022 2022 2022 2022 2022 2007 But the field "Year1" doesn't get populated/updated.
... View more
09-20-2022
03:26 PM
|
0
|
3
|
2886
|
|
POST
|
Thanks fro the replay. Tried the following but nothing happens, no print, no error. def calcYear():
fc = "Lyr"
#testdate = '04/25/2015'
with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
d1 = datetime.strptime(row[0],"%Y").year
print(d1) #"{}".formate(d1)
row[1] = d1
cursor.updateRow(row)
... View more
09-20-2022
12:49 PM
|
0
|
5
|
2916
|
|
POST
|
I need to convert a text field "Year1" into a date filed (Year1) with just the year. Field Year is a text field, with just a year date, "2015" and field Year1 is a date field. from datetime import datetime
from datetime import time
import datetime as dt
from time import gmtime, strftime
def calcYear():
fc = "Lyr"
#testdate = '04/25/2015'
with arcpy.da.UpdateCursor(fc,['Year','Year1']) as cursor:
for row in cursor:
if row[0] not in (""," ",None):
d1 = datetime.strptime(datetime.strftime(row[0], "%Y"), "%Y")
row[1] = d1.year
cursor.updateRow(row) I get the following error. I was thinking that line 11 was converting the text into a day(year). ERROR 000539: Traceback (most recent call last): File "<expression>", line 1, in <module> File "<string>", line 11, in calcYear TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'int' Failed to execute (Calculate Field
... View more
09-20-2022
11:44 AM
|
0
|
7
|
2932
|
|
POST
|
I was trying to so this in the layout view, THANKS FOR THE REPLY.
... View more
09-07-2022
11:37 AM
|
0
|
0
|
1906
|
|
POST
|
Using Pro I've converted a layers labels to graphics, can they converted graphics be edited, if so how? I know they can be edited if labels are converted to Annotation. I have tried to to edit them in the layout and have tried activating the map frame. This was easily done in Arcmap. Thanks.
... View more
09-07-2022
09:54 AM
|
0
|
2
|
1936
|
|
POST
|
Sorry for the late response. I wasn't sure if you meat separate the two into two separate calculate values, how I was suppose to call the first calculate value to the second calculate value. So I figured why the model didn't stop, FLD_ZONE had some empty attributes. I tried the following it runs but nothing is merged, no errors. fc = r"C:\Temp\FEMA.gdb\FEMA_TempLyrs
def f(fc):
case_fields = ["FLD_ZONE",'ZONE_SUBTY'] # field(s) to group records for merging
sort_field, sort_order = "OBJECTID", "ASC"
shape_field = "SHAPE@"
fields = case_fields + [sort_field, shape_field]
sql_orderby = "ORDER BY {}, {} {}".format(", ".join(case_fields), sort_field, sort_order)
with da.UpdateCursor(fc, fields, sql_clause=(None, sql_orderby)) as cur:
for row in cur:
print (row)
if row[0] == None:
case_func = itemgetter(*range(len(case_fields)))
merged_polys = {
key:reduce(arcpy.Geometry.union, (row[-1] for row in group))
for key, group
in groupby(cur, case_func)
}
cur.reset()
for key, group in groupby(cur, case_func):
row = next(group)
cur.updateRow(row[:-1] + [merged_polys[key]])
for row in group:
cur.deleteRow()
del cur
return fc I also tried separating the operations like you stated but nothing gets merged, with error. Error: ERROR 000539: Traceback (most recent call last): File "<expression>", line 1, in <module> File "<string>", line 38, in fc1 TypeError: 'NoneType' object is not subscriptable import sys, arcpy, os
from arcpy import env
from arcpy import da
from itertools import groupby
from operator import itemgetter
from functools import reduce
fc = r"C:\Temp\FEMA.gdb\FEMA_TempLyrs"
def f(fc):
case_fields = ["FLD_ZONE",'ZONE_SUBTY'] # field(s) to group records for merging
sort_field, sort_order = "OBJECTID", "ASC"
shape_field = "SHAPE@"
fields = case_fields + [sort_field, shape_field]
sql_orderby = "ORDER BY {}, {} {}".format(", ".join(case_fields), sort_field, sort_order)
merged_polys = None
with da.SearchCursor(fc, fields, sql_clause=(None, sql_orderby)) as cur:
case_func = itemgetter(*range(len(case_fields)))
for row in cur:
print (row)
if row[0] == None:
merged_polys = {
key:reduce(arcpy.Geometry.union, (row[-1] for row in group))
for key, group
in groupby(cur, case_func)
}
else:
pass
del cur
with da.UpdateCursor(fc, fields, sql_clause=(None, sql_orderby)) as uCur:
for key, group in groupby(uCur, case_func):
row = next(group)
uCur.updateRow(row[:-1] + [merged_polys[key]])
for row in group:
uCur.deleteRow()
return fc
... View more
09-02-2022
03:11 PM
|
0
|
0
|
1248
|
|
POST
|
Thanks for the reply, I guess I am not understand how to put the code/model together to work. Is there another way that this can be accomplished?
... View more
08-31-2022
08:46 AM
|
0
|
2
|
1262
|
|
POST
|
I know I can do it with Dissolve, but the issue with Dissolve is that it creates a separate layer and I know I can do this a script but I need to in Model Builder. I need the function to merge feature class based on two attributes. Can this be down with Calculate value in model builder? I have tried the following, but it never seems to finish the process. The model function does seem to work, I check the feature class and the feature class features are merged. I have to manually hit stop. Expression: f(fc) Code block: import sys, arcpy, os
from arcpy import env
from arcpy import da
from itertools import groupby
from operator import itemgetter
from functools import reduce
fc = r"C:/TEMP/FEMA.gdb/FEMA_TempLyrs"
def f(fc):
case_fields = ["FLD_ZONE",'ZONE_SUBTY'] # field(s) to group records for merging
sort_field, sort_order = "OBJECTID", "ASC"
shape_field = "SHAPE@"
fields = case_fields + [sort_field, shape_field]
sql_orderby = "ORDER BY {}, {} {}".format(", ".join(case_fields), sort_field, sort_order)
with da.UpdateCursor(fc, fields, sql_clause=(None, sql_orderby)) as cur:
case_func = itemgetter(*range(len(case_fields)))
merged_polys = {
key:reduce(arcpy.Geometry.union, (row[-1] for row in group))
for key, group
in groupby(cur, case_func)
}
cur.reset()
for key, group in groupby(cur, case_func):
row = next(group)
cur.updateRow(row[:-1] + [merged_polys[key]])
for row in group:
cur.deleteRow()
del cur
return fc
... View more
08-29-2022
12:15 PM
|
0
|
4
|
1324
|
|
POST
|
man, totally over looked that one. Got it to work with. Thanks for all the help! for row in cursor:
print (f'checking: {row[1]}')
row[2] = 'PLAT' if row[1] in plats_list else assignName.get(row[1], row[3])
cursor.updateRow(row)
... View more
08-23-2022
04:04 PM
|
0
|
0
|
1936
|
|
POST
|
Jeff, Sorry for the delay. I did remove the "*", after I posted my response but still line 14 won't update the row with the plats_list. Both fields are text fields. I've been trying to figure it out but I can't seem to. Logically line 14 makes sense. Also looking at the data there seems to be some blanks in row 1 "Type" field and if that is the case I would like to update row[2] "Type2" with with field1. Sorry I didn't realize this until I was looking into the Type field to see why line 14 was not being updated. If I can get pasted line 14 not passing the plat_list I can figure out how to update "Type" if "Type" field is empty. ObjectID Type field1 Type2 1 BP Permit BuildingPermit 2 FEMA FEMA_LOMARs 3 PLAT - Preliminary Sub PLAT 4 Sub PLAT 5 Floodway FEMA_Floodway import arcpy
fc = 'C:/Temp/permits.gdb/test'
assignName = {"BP" : "BuildingPermit", "FMEA" : "FEMA_LOMAR", 'PLAT': "Sub"}
#fields = ["OBJECTID", "Type", "Type2"]
#print(assignName)
plats_list = ['PLAT - Preliminary' ,'PLAT - Final', 'PLAT - Short']
cursor = arcpy.da.UpdateCursor(fc,["OBJECTID", "Type", "Type2"])
for row in cursor:
if row[1] in assignName:
print (assignName[row[1]])
row[2] = assignName['PLAT'] if row[1] in plats_list else assignName.get(row[1])
cursor.updateRow(row)
... View more
08-23-2022
12:51 PM
|
0
|
2
|
1949
|
|
POST
|
Thanks for the reply, really appreciate it. That explains why I didn't find anything about wildcards in Dictionaries. I have added your suggestion but field "Type2" doesn't get populated with "Plat". import arcpy
fc = 'C:/Temp/permits.gdb/test'
assignName = {"BP" : "BuildingPermit", "FMEA" : "FEMA_LOMAR", 'PLAT*': "Sub"}
#fields = ["OBJECTID", "Type", "Type2"]
#print(assignName)
plats_list = ['PLAT - Preliminary' ,'PLAT - Final', 'PLAT - Short']
cursor = arcpy.da.UpdateCursor(fc,["OBJECTID", "Type", "Type2"])
for row in cursor:
if row[1] in assignName:
print (assignName[row[1]])
row[2] = assignName['PLAT'] if row[1] in plats_list else assignName.get(row[1])
cursor.updateRow(row)
... View more
08-19-2022
02:18 PM
|
0
|
5
|
2016
|
|
POST
|
I need to update the third row with the second row, the issue is that the entries are not consistent. Field three has entities like so; PLAT - final PLAT -short PLAT -prelim PLAT -Reserved PLAT -asdf PLAT -lkjhh etc. I would like to update row 3 with just Plat. I don't want to write a pair for each one. So can I do a wild card? I tried 'PLAT*' on line 5 but it didn't work. import arcpy
fc = 'C:/Temp/permits.gdb/test'
assignName = {"BP" : "BuildingPermit", "FMEA" : "FEMA_LOMAR", 'PLAT*': "Sub"}
#fields = ["OBJECTID", "Type", "Type2"]
print(assignName)
cursor = arcpy.da.UpdateCursor(fc,["OBJECTID", "Type", "Type2"])
for row in cursor:
if row[1] in assignName:
print (assignName[row[1]])
row[2] = assignName[row[1]]
cursor.updateRow(row)
... View more
08-19-2022
10:40 AM
|
0
|
7
|
2050
|
|
POST
|
I was able to get what I need with the following. My only other question is how do I get the counter to on top of the OBJECTID rows? fields = ["OBJECTID", "PN1"]
where = "PN1 = ' ' OR PN1 IS NULL"
counter = 0
with arcpy.da.SearchCursor(fc, fields, where) as cursor:
for row in cursor:
if row[1]in ["", " ", None]:
counter += 1
print ("OBJECTID {0}".format(row[0]) + "has a NULL value in field")
print ("{} {} records have blank/empty or NULL records".format(row[0],counter)) Results; OBJECTID 44has a NULL value in field OBJECTID 47has a NULL value in field OBJECTID 88has a NULL value in field OBJECTID 106has a NULL value in field OBJECTID 108has a NULL value in field OBJECTID 121has a NULL value in field OBJECTID 130has a NULL value in field OBJECTID 182has a NULL value in field OBJECTID 210has a NULL value in field 210 9 records have blank/empty or NULL records How do i put the "210 9 records have blank/empty or NULL records" on top like so 210 9 records have blank/empty or NULL records. OBJECTID 44has a NULL value in field OBJECTID 47has a NULL value in field OBJECTID 88has a NULL value in field OBJECTID 106has a NULL value in field OBJECTID 108has a NULL value in field OBJECTID 121has a NULL value in field OBJECTID 130has a NULL value in field OBJECTID 182has a NULL value in field OBJECTID 210has a NULL value in field
... View more
08-18-2022
10:51 AM
|
0
|
0
|
3051
|
|
POST
|
I was able to get something printed with the suggestion but it repeats. fields = ["OBJECTID", "PN1"]
for field in fields:
where = "PN1 = ' ' OR PlN1 IS NULL"
try:
with arcpy.da.SearchCursor(fc, fields, where) as cursor:
for row in cursor:
#print("Null value in row {0}".format(row[0], row[1]))
print ("OBJECTID {0}".format(row[0]) + "has a NULL value in field")
## result = arcpy.GetCount_management(fc).getOutput(0)
## print ('{} has {} records'.format(fc, result[0]))
except RuntimeError:
pass
del cursor OBJECTID 44has a NULL value in field
OBJECTID 47has a NULL value in field
OBJECTID 88has a NULL value in field
OBJECTID 106has a NULL value in field
OBJECTID 108has a NULL value in field
OBJECTID 121has a NULL value in field
OBJECTID 130has a NULL value in field
OBJECTID 182has a NULL value in field
OBJECTID 210has a NULL value in field
OBJECTID 44has a NULL value in field
OBJECTID 47has a NULL value in field
OBJECTID 88has a NULL value in field
OBJECTID 106has a NULL value in field
OBJECTID 108has a NULL value in field
OBJECTID 121has a NULL value in field
OBJECTID 130has a NULL value in field
OBJECTID 182has a NULL value in field
OBJECTID 210has a NULL value in field
... View more
08-18-2022
09:59 AM
|
0
|
0
|
3054
|
|
POST
|
I am trying to print empty/blank or Null records with the following but when nothing is printed and there is empty/blank and Null records in the layer. I would also like to print the number of empty/blank or Null records. fc = "C:/Temp/Test.gdb/feature Class"
fields = ["OBJECTID", "PN1"]
for field in fields:
where = field + " IS " " OR NULL"
try:
with arcpy.da.SearchCursor(fc, fields, where) as cursor:
for row in cursor:
print("OBJECTID {0}").format(row[0]) + " has a Empty/ Blank or NULL value in field " + field
except RuntimeError:
pass
del cursor
counter = 0
with arcpy.da.SearchCursor(fc,["OBJECTID","PN1"]) as cursor:
for row in cursor:
if row[1] in ["", " ", None]:
counter += 1
print('{} {} is null {} times'.format(row[0],row[1],counter))
else:
pass
... View more
08-18-2022
08:26 AM
|
0
|
4
|
3072
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 08-01-2024 07:19 AM | |
| 1 | 07-26-2024 09:38 AM | |
| 1 | 01-08-2024 09:44 AM | |
| 1 | 03-07-2023 11:46 AM | |
| 1 | 11-02-2020 08:24 AM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
12:36 PM
|