|
POST
|
When creating a new script tool or add-in, I will start experimenting in the Python window with the various arcpy functions that look applicable. From your description, you are wanting something to identify polygon layers, select one of those layers, make an initial selection of polygons from the current map extent, and get a list of attributes to allow the user to make a further choices. I hope this is a reasonable summary of your project. With that in mind, here's a test script that I've been experimenting with in the Python window using desktop 10.5. You may need to check the arcpy functions if you're using Pro or other version. mxd = arcpy.mapping.MapDocument("CURRENT")
polyLayers = [] # empty list
# ListLayers (map_document_or_layer, {wildcard}, {data_frame})
lyrs = arcpy.mapping.ListLayers(mxd)
for lyr in lyrs:
# Describe (object)
desc = arcpy.Describe(lyr)
if desc.ShapeType == 'Polygon':
polyLayers.append(lyr.name)
selected_layer = polyLayers[0] # for demo, select first layer
print selected_layer
fields = [] # empty list for field selection
# ListFields (dataset, {wild_card}, {field_type})
flds = arcpy.ListFields(selected_layer, field_type='String')
for f in flds:
# print f.name
fields.append(f.name)
print fields
selected_field = fields[0] # select first field tor testing
print selected_field
# get extent and select polygons in selected layer
# ListDataFrames (map_document, {wildcard})
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
ext = df.extent
array = arcpy.Array([arcpy.Point(ext.XMin, ext.YMin),
arcpy.Point(ext.XMin, ext.YMax),
arcpy.Point(ext.XMax, ext.YMax),
arcpy.Point(ext.XMax, ext.YMin)
])
polygon = arcpy.Polygon(array) # use this polygon for making selection
# SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})
arcpy.SelectLayerByLocation_management(selected_layer, "INTERSECT", polygon, "", "NEW_SELECTION", "NOT_INVERT")
selected = [] # empty list for selected polygons' attributes
# SearchCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
with arcpy.da.SearchCursor(selected_layer,selected_field) as cursor:
for row in cursor:
selected.append(row[0])
print selected # user would choose from this list
# SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
arcpy.SelectLayerByAttribute_management(selected_layer, "CLEAR_SELECTION") # clear selected items Once the code is tested and does what is desired, then you can work it into a script tool or add-in. Hope this helps.
... View more
05-01-2020
12:07 PM
|
0
|
1
|
1523
|
|
POST
|
You could wrap your AddField with a try/except block. You could then print an error message if there is a problem creating the field. for fc in arcpy.ListFeatureClasses():
try:
arcpy.AddField_management(fc, "sourceOFData", "TEXT", field_length="75")
except:
print "Error creating {} field".format('sourceOFData')
try:
arcpy.AddField_management(fc, "SourceLastEditDate", "DATE")
except:
print "Error creating {} field".format('SourceLastEditDate')
# etc. See Python Try Except for more information. Another option would be to use ListFields to find out what fields are in your feature and then add those that are not already in the list. Hope this helps.
... View more
04-30-2020
02:11 PM
|
1
|
2
|
7404
|
|
POST
|
Since you are using the defaults for nullable and required, you could just omit those parameters. Provide the first three required parameters in order, and add field_length=00 (some number) for your text fields.
... View more
04-30-2020
12:32 PM
|
0
|
4
|
5796
|
|
POST
|
It also looks like you were missing the field alias. #AddField(in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain})
# arcpy.AddField_management(fc, "Source Last Edit Date", "DATE", "", "", "75", "True", "NON_REQUIRED", "")
arcpy.AddField_management( fc, "SourceLastEditDate", "DATE", "#", "#", "#", "FieldAlias", "NULLABLE", "NON_REQUIRED", "#")
# or
arcpy.AddField_management(fc, "SourceLastEditDate", "DATE",
field_is_nullable="NULLABLE",
field_is_required="NON_REQUIRED")
... View more
04-30-2020
12:20 PM
|
1
|
1
|
5796
|
|
POST
|
Also use NULLABLE or NON_NULLABLE, not "True" in your DATE lines.
... View more
04-30-2020
11:56 AM
|
0
|
0
|
5796
|
|
POST
|
Looks like you have spaces (not allowed) in the field name: "Source Last Edit Date". Since it is a date field, I also suggest leaving the length parameter blank.
... View more
04-30-2020
11:51 AM
|
1
|
3
|
5796
|
|
POST
|
Try this format: """ "Installa_1" >= DATE '2010-01-01' """ What type of geodatabase are you working with? The query syntax can vary between various systems. See: SQL reference for query expressions used in ArcGIS EDIT: I did some additional tests with a file geodatabase. It seems that how the field is quoted (single, double or none) also has an effect. # using a file geodatabase with desktop 10.5, the following also worked
""" 'Installa_1' >= '01/01/2010' """
"'Installa_1' >= '01/01/2010'"
"Installa_1 >= DATE '01/01/2010'"
"Installa_1 >= DATE '2010/01/01'" # I generally use this format
# the following failed
""" "Installa_1" >= '01/01/2010' """
"Installa_1 >= '01/01/2010'"
... View more
04-27-2020
12:45 PM
|
1
|
0
|
1324
|
|
POST
|
My initial response would be to use the 'File' data type with a filter of 'xls; xlsx', so the user can select the file. Then the tool validator section can read the file and load the values into a parameter for the user to select from. Alternatively, you could code a relative path to the Excel file in tool validator section. This may have benefits or issues depending on the project.
... View more
04-15-2020
12:40 PM
|
1
|
0
|
1546
|
|
POST
|
Have you tried "in_memory"? Considerations when using the in_memory workspace
... View more
04-10-2020
10:32 AM
|
2
|
2
|
2908
|
|
POST
|
Although they look identical, txt is a file object, and last_update_fra appears to be a list object. >>> last_update_fra = ['Last Updated 4/3/2020, 8:28 p.m.']
>>> txt = open(r"C:\Users\jpilbeam\LastUpdate.txt", "r")
>>> type(txt)
<type 'file'>
>>> t = txt.read()
>>> type(t)
<type 'str'>
>>> t
"['Last Updated 4/3/2020, 8:28 p.m.']"
>>> type(last_update_fra)
<type 'list'>
>>> tt = str(last_update_fra)
>>> tt
"['Last Updated 4/3/2020, 8:28 p.m.']"
>>> if t == tt:
... print "match"
...
match
>>>
... View more
04-06-2020
11:44 AM
|
1
|
4
|
6284
|
|
POST
|
Perhaps if "last update" was done in the last 24 hours (or other time period), then send an email. You may also need to consider the time zone in the time calculations. The basic idea: import re
from dateutil import parser
from datetime import datetime
pattern = re.compile(r"\d[\d\/,:. AaPpMm]{4,24}")
last_update_fra = """<h1>COVID-19 News Updates</h1>
<div><br></div><div>Last Updated 4/3/2020, 12:08 p.m.</div>"""
# assumes datetime will be last element, and a datetime was found
dt = parser.parse(pattern.findall(last_update_fra)[-1])
updated = datetime.now() - dt
if updated.days < 1 : # updated in the past day
print "process -", dt
else:
print "ignore -", dt
# prints: ignore - 2020-04-03 12:08:00
... View more
04-04-2020
08:25 PM
|
2
|
0
|
6284
|
|
POST
|
Seems like you want to extract a county name from a field and assign another field a county number based on the name, you could try something like: # dictionary of counties
counties = { 'ALACHUA' : 1,
'BAKER' : 2,
'BAY' : 3,
'BRADFORD' : 4,
'BREVARD' : 5,
'BROWARD' : 6,
'CALHOUN' : 7,
'CHARLOTTE' : 8,
'CITRUS' : 9,
'CLAY' : 10,
'COLLIER' : 11,
'COLUMBIA' : 12,
'DESOTO' : 13,
'DIXIE' : 14,
'DUVAL' : 15
}
# dictionary keys to list
countyKey = list(counties.keys())
# example of update cursor data : fields = ["CO_NO", "COUNTY_NAME"]
cursor = [
[0, 'baker_2019pin_Dissolve'],
[0, 'calhoun_2019pin_Dissolve'],
[0, 'citrus_2019pin_Dissolve'],
[0, 'duval_2019pin_Dissolve'],
[0, 'alachua_2019pin_Dissolve'],
[0, 'clay_2019pin_Dissolve']
]
for row in cursor:
if row[1][:-17].upper() in countyKey:
row[0] = counties[row[1][:-17].upper()]
print row[0], row[1]
# prints:
2 baker_2019pin_Dissolve
7 calhoun_2019pin_Dissolve
9 citrus_2019pin_Dissolve
15 duval_2019pin_Dissolve
1 alachua_2019pin_Dissolve
10 clay_2019pin_Dissolve This uses a dictionary of county names, uses upper() to compare for a match and then uses the dictionary value for the county number. This process is easier to code than a long if/else. Note that you will need to have dictionary entries that will match your county names if they include spaces, underscores, etc. (St. Lucie, Miami-Dade). It also assumes the the slicing [:-17] is correct. Hope this helps.
... View more
03-25-2020
07:51 PM
|
1
|
4
|
7787
|
|
POST
|
Is there a reason you are using count as text (number inside quotes) and not a number? How is count being set? if count<>"0": # "0" is a text string
# vs
if count <> 0: # >0 assuming negative numbers wouldn't happen
# or
if count:
... View more
03-23-2020
12:04 PM
|
0
|
0
|
3407
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|