|
POST
|
As you asked about using a dicitonary as part of the solution, here is an approach similar to the one suggested by Nicholas Klein-Baer. I used the collections dictionary which simplifies things when the values are lists. One issue that may be contributing to the problem is that when 2 features using the same field names are joined, the joined feature's fiields will be renamed, usually by appending "_1". You should check the join to verify that you are using the proper field names. Here is my test code: import collections
d = collections.defaultdict(list)
polygons = 'buildoutPolygons1' # outer polygons that border groups of parcels
parcels = 'buildoutParcels1'
joined = 'in_memory\\test_join' # this can be temporary
# join blocks to parcels
arcpy.SpatialJoin_analysis(
target_features = parcels,
join_features = polygons,
out_feature_class = joined,
join_operation = "JOIN_ONE_TO_ONE",
join_type = "KEEP_COMMON",
match_option = "HAVE_THEIR_CENTER_IN")
# NOTE: both polygons and parcels contain the fields: OBJECTID and PARCEL
# the join has appended "_1" to these fields from polygons
# *** verify this before continuing ***
fields2dict = ['PARCEL_1', 'PARCEL' ] # key is from polygons and value is from parcels
# an alternative is to use OBJECTID_1 in place of PARCEL_1
# search cursor processes joined feature into dictionary
with arcpy.da.SearchCursor(joined, fields2dict) as cursor:
for key, value in cursor:
d[key].append(value)
# print d
# dictionary will be in format { 'BLOCK_ID' : [ 'PIN_ID_1', 'PIN_ID_2', ... ], ... }
# examining the dictionary will help determine if length of
for k in d.keys():
print k, "parcels = {} : field length = {}".format(len(d ),len(d )*12)
# the PARCEL field in polygons will be the key, PARCELS will be updated
updateFields = ['PARCEL','PARCELS'] # BLOCK matches dictionary key
# NOTE this will fail as field length needs to be about 800 characters wide
# shapefiles have a text field limit of 250 characters
# update cursor uses dictionary d to update PINS field
with arcpy.da.UpdateCursor(parcels, updateFields) as cursor:
for row in cursor:
row[1] = ','.join(d[row[0]]) # row[0] is key for dictionary, row[1] will be updated with list
cursor.updateRow(row)
arcpy.Delete_management(joined) # delete in_memory object
# refresh may be required to see changes As I mentioned in my previous post, some of the parcel id numbers concatenate into a string longer than the width of the field. Lines 31 an 32 will print an estimate of the required field width to store the concatenated string. R3590700000 parcels = 66 : field length = 792
R3601400000 parcels = 7 : field length = 84
R3733600000 parcels = 3 : field length = 36
R3612900000 parcels = 4 : field length = 48
R3681301000 parcels = 4 : field length = 48
R3681100000 parcels = 2 : field length = 24
R3676401100 parcels = 5 : field length = 60
R3682300000 parcels = 1 : field length = 12
R3602300000 parcels = 2 : field length = 24
R3701400000 parcels = 1 : field length = 12
R3690600000 parcels = 2 : field length = 24
R36103010A0 parcels = 8 : field length = 96
R3675500000 parcels = 12 : field length = 144
R3610302000 parcels = 6 : field length = 72
R3691500000 parcels = 1 : field length = 12
R3613301000 parcels = 5 : field length = 60
R3608900000 parcels = 10 : field length = 120
R3608601000 parcels = 1 : field length = 12
R3602601000 parcels = 16 : field length = 192
R3610100000 parcels = 3 : field length = 36
R3663801000 parcels = 54 : field length = 648
R3729700000 parcels = 6 : field length = 72
R3675400000 parcels = 2 : field length = 24
R3613500000 parcels = 1 : field length = 12
R3710800000 parcels = 1 : field length = 12
R3613600000 parcels = 2 : field length = 24
R3701500000 parcels = 2 : field length = 24
R3666600000 parcels = 2 : field length = 24
R3612901100 parcels = 8 : field length = 96
R3703500000 parcels = 3 : field length = 36
R3678200000 parcels = 4 : field length = 48
R3702500000 parcels = 2 : field length = 24
R3672200000 parcels = 1 : field length = 12
R3677000000 parcels = 6 : field length = 72
R3671100000 parcels = 2 : field length = 24
R3703200000 parcels = 1 : field length = 12
R3676200000 parcels = 2 : field length = 24
R3672300000 parcels = 4 : field length = 48
R3682600000 parcels = 14 : field length = 168
R3610301000 parcels = 2 : field length = 24
R3608600000 parcels = 2 : field length = 24
R3600000000 parcels = 1 : field length = 12
R3710501200 parcels = 2 : field length = 24
R3665700000 parcels = 2 : field length = 24
R3592200000 parcels = 17 : field length = 204
R3612300000 parcels = 2 : field length = 24
R3679300000 parcels = 1 : field length = 12
R3677601000 parcels = 6 : field length = 72
R3733400000 parcels = 1 : field length = 12
R3602401100 parcels = 1 : field length = 12
R3667800000 parcels = 2 : field length = 24
R3678700000 parcels = 4 : field length = 48
R3663700000 parcels = 3 : field length = 36
R3612400000 parcels = 3 : field length = 36
R3679501000 parcels = 8 : field length = 96 Two of the "buildoutPolygon" features have more parcel id's than will fit in a field width of 250 characters. Hope this helps.
... View more
08-02-2020
09:02 PM
|
2
|
1
|
2008
|
|
POST
|
Thanks for sharing some sample data. I used nested cursors for a quick test and noticed that several of the buildoutPolygons contained a large number of parcels. If you concatenated the parcel id numbers, you would end up with a string much longer than the field length of 250 characters allowed with a shapefile. In addition, some of the polygons appear complex (multipart and non-contiguous) , which might indicate some other issues. Here is screenshot showing one selected polygon feature that has 66 parcels located in it. Here is the test script: polygons = 'buildoutPolygons1'
parcels = 'buildoutParcels1'
# clear all selected features
arcpy.SelectLayerByAttribute_management(polygons, "CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management(parcels, "CLEAR_SELECTION")
with arcpy.da.UpdateCursor(polygons, ['SHAPE@', 'OID@', 'PAR_COUNT', 'PARCELS']) as cursor:
for row in cursor:
arcpy.management.SelectLayerByLocation(parcels, "HAVE_THEIR_CENTER_IN", row[0], "", "NEW_SELECTION")
par_count = int(arcpy.GetCount_management(pars)[0]) # parcel count
# get list of parcel id numbers
pins = []
with arcpy.da.SearchCursor(parcels, ['PARCEL']) as parcelsCursor:
for parcelRow in parcelsCursor:
pins.append(parcelRow[0]) # save PIN in pins list
# print for testing
print row[1], par_count # OID@ and count
print ','.join(pins) # parcel ids in polygon
print
# update row
row[2] = par_count
# row[3] = ','.join(pins) # not updated as some results longer than field width
cursor.updateRow(row) The single polygon from the photo, FID = 34, gave the following results: 34 66
R3590700000,R3590701000,R3590801000,R3590900000,R3591800000,R3594801100,R3595100000,R3595500000,R3595500000,R35957010A0,
R3595901000,R3596001000,R3596001100,R3596800000,R3596801000,R3597200000,R3597301000,R3597500000,R3597700000,R3597701000,
R3598101100,R3598200000,R3598500000,R3598901100,R3599600000,R3599900000,R3600101000,R3600101100,R3600500000,R3601200000,
R3601300000,R3601500000,R3601900000,R3602100000,R3602401000,R3602600000,R3602701200,R3602901000,R3603500000,R3603600000,
R3603700000,R3604000000,R3604301000,R3604900000,R3605100000,R3605200000,R3605201000,R3606400000,R3606400000,R3606401000,
R3606401000,R3606500000,R3606500000,R3606600000,R3606800000,R3607600000,R3607800000,R3607900000,R3608000000,R3608100000,
R3608301000,R3609601000,R3609801300,R3610000000,R3610101000,R3610200000 Hope this helps.
... View more
08-02-2020
07:45 PM
|
0
|
4
|
6143
|
|
POST
|
Although the old InsertCursor still works with 10.8, you may wish to convert to the faster da.InsertCursor.
... View more
08-02-2020
01:09 PM
|
1
|
0
|
2647
|
|
POST
|
Can you share the field layout of the two features that are being joined with, perhaps, some sample data? It would be helpful in understanding what needs to be done.
... View more
07-29-2020
11:14 AM
|
0
|
1
|
6143
|
|
POST
|
In my experience (only with AGOL), I only update sections. In your case since you want to add additional domains, the section would be the fields section, and it would only contain the fields using the domain being changed. This way, the things that cannot be changed, such as "allowGeometryUpdates", are not included. I also do not include the "drawingInfo" section. Since you are working with domains, the "types" section may need editing depending on the set up. For example, to add the color Blue to a domain list, start with the comma on line 24 and add through line 28. Then check it with something like jsonlint for valid json before updating. Also, experiment on a backup of the service first. {
"fields": [{
"name": "Color",
"type": "esriFieldTypeString",
"alias": "Point Color",
"sqlType": "sqlTypeOther",
"length": 1,
"nullable": false,
"editable": true,
"domain": {
"type": "codedValue",
"name": "Colors",
"codedValues": [{
"name": "Red",
"code": "1"
},
{
"name": "Yellow",
"code": "2"
},
{
"name": "Green",
"code": "3"
},
{
"name": "Blue",
"code": "4"
}
]
},
"defaultValue": null
}]
}
... View more
07-27-2020
07:40 PM
|
1
|
1
|
2288
|
|
POST
|
I took another look at your code and noticed the SpatialJoin is making a call to the Layers1 function for the field mapping. It is being passed the Par1 variable, but the function is not using it. This may be part of the problem. While I don't completely understand field mapping, I did some testing and have the following test code that may be of help. I created a block layer with an ID field called BLOCK. I created a parcels layer with a text ID field called PIN. (I don't like working with shape files, so I used a file geodatabase. The joined layer, once created, could be dumped to a shape file.) The features and the join table created look like the following. Here's my test code. Hope it helps. import arcpy
blocks = r"C:\Test\Test.gdb\test_blocks"
# only field used from blocks was the id field "BLOCK"
parcels = r"C:\Test\Test.gdb\test_parcels"
# only field used from parcels was the id field "PIN"
joined = r"C:\Test\Test.gdb\test_sj"
# the joined feature will also contain fields created during the spatial join
# use {0} for blocks and {1} for parcels in field mapping string
# for PIN field, parcels uses a text field of 12 characters, this was lengthened in the fms to 200
# so the joined values would fit
fms = """
BLOCK "BLOCK" true true false 12 Text 0 0 ,First,#,{0},BLOCK,-1,-1;
PIN "PIN" true true false 200 Text 0 0 ,Join,",",{1},PIN,-1,-1
""".format(blocks, parcels)
arcpy.SpatialJoin_analysis(
target_features = blocks,
join_features = parcels,
out_feature_class = joined,
join_operation = "JOIN_ONE_TO_ONE",
join_type = "KEEP_ALL",
field_mapping = fms,
match_option="INTERSECT",
search_radius="",
distance_field_name="")
... View more
07-27-2020
06:58 PM
|
2
|
1
|
3552
|
|
POST
|
From your picture, it looks like you are getting the feature's definition information. The "drawingInfo" and "imageData" is indicating the default symbology to use when drawing your feature. My guess is that you missed the word "/query" at the end of the URL string. The link you followed shows this example: https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0/query Trailheads is the name of the feature service, and the first layer (zero) is being queried.
... View more
07-24-2020
08:29 PM
|
0
|
0
|
1662
|
|
POST
|
It looks as if the {0} should be replaced with something via .format. For example: fc = r"C:\Path\To\File.gdb\feature"
FieldMapString = """PIN "PIN" true true false 13 Text 0 0 ,First,#, {0}, PIN,-1,-1;""".format(fc)
Print FieldMapString
# PIN "PIN" true true false 13 Text 0 0 ,First,#, C:\Path\To\File.gdb\feature, PIN,-1,-1;
... View more
07-22-2020
01:42 PM
|
0
|
0
|
3552
|
|
POST
|
If I understand your question correctly, you want to change the status field based on the most recent date in 3 date fields. You can use an UpdateCursor or the FieldCalculator and a modified version of: from datetime import datetime # to make dates for testing
def status(date1, date2, date3):
stat = ['Status 1', 'Status 2', 'Status 3']
dates = [date1, date2, date3]
return stat[dates.index(max(dates))]
print(status(datetime(2020, 7, 12, 10, 10), # 3 fields with dates
datetime(2018, 6, 12, 10, 10),
datetime(2017, 8, 12, 10, 10))) If it is possible that two date fields could have the same maximum date, the first date in the list that matches would be returned. So the order of the status and date fields would need to be in order of most to least important.
... View more
07-20-2020
01:39 PM
|
0
|
0
|
920
|
|
POST
|
Based on some of your past questions, I assume you are using a file geodatabase with Desktop. You could do a daily export to a network file that the engineers, etc. could access/query via some program. But, I imagine that it would also be helpful for your engineers to see the data located on a map. This could be allowing your users to query an online map such as Portal or ArcGIS Online. Apps like Collector and Explorer would then be easy to use options to query the data. Desktop should come with an AGOL account, so you should be able to do some experimenting.
... View more
07-17-2020
02:46 PM
|
0
|
0
|
2812
|
|
POST
|
Some of the problems I've come across (with iPhones and Collector most frequently) involves auto-correct where an apostrophe or quote will be replaced with the "intelligent" version which can take you to unicode errors.
... View more
07-16-2020
04:33 PM
|
1
|
0
|
1624
|
|
POST
|
My first thoughts would be to examine lines 77-85. I think the backslashes are causing your problem; they are used to escape certain characters. I think the specific problem is occurring with the '\2020'. Put a small r in front of your variables. # Instead of:
# Local variables:
Output = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Model_Buffer.shp"
SpatialJoin_shp = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Joined.shp"
# Try:
# Local variables:
Output = r"C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\Model_Buffer.shp"
SpatialJoin_shp = r"C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\Joined.shp"
The other variables in line 88 are from user input parameters in lines 9-10 and lines 77-85. You may need to check the values the user entered if you are still having issues.
... View more
07-16-2020
03:41 PM
|
1
|
2
|
1624
|
|
POST
|
A simple search cursor example: fc = 'feature or table to search'
fields = ['TWACS_NUMBER', 'XFMR_BANK_ID']
where = "TWACS_NUMBER = 'some value'" # if string wrap value in single quotes; if number then no single quotes
with arcpy.da.SearchCursor(fc, fields, where) as cursor:
for row in cursor:
print('TWACS_NUMBER: {0} XFMR_BANK_ID: {1}'.format(row[0], row[1]))
... View more
07-16-2020
03:20 PM
|
1
|
0
|
790
|
|
POST
|
I made several attempts to generate an error similar to yours, but the tool seemed to handle a variety of situations quite well. For example, I tried the "POINT_X_Y_Z_M" option with polygon and line features, and the tool responded with appropriate error messages. Since the error message is coming from inside the AddGeometryAttributes tool, I would suspect the issue is with your wHydrantValveGPS feature layer - perhaps bad geometry, or some other corruption. Does the feature load properly in Desktop? Have you tried the actual tool in Desktop? The tool's dialog box may provide some additional error messages as the tool validator script will be invoked as the parameters are checked.
... View more
06-26-2020
04:46 PM
|
0
|
1
|
3592
|
| 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
|