SpatialJoin with two and more fields and mergeRule not working as expected

04-29-2021 08:00 AM
New Contributor III



why is below code giving me very wierd results? I want to do SpatialJoin. Target polygon feature is "zapojporost" with target field "cislo". I want DRUH_POZEMKU and TEXT_KM from input feature to be merged to corresponding cells. 

As a result I always get mixed up results. In some fields, there is no value. In field "cislo" I even get different number and I thought that when it was target feature, it could not be changed by SpatialJoin. Thank you for any tips.


areaVysledek = "area_Vysledek"
zapojporostFeature = "zapojporost"

fms1 = arcpy.FieldMappings()

keepers = ["cislo","KATUZE_KOD","TEXT_KM","DRUH_POZEMKU"] # etc.
# Remove all output fields you don't want.
for field in fms1.fields:
if not in keepers:

parcela_index = fms1.findFieldMapIndex("TEXT_KM")
fieldmap = fms1.getFieldMap(parcela_index)
field = fieldmap.outputField = "parcelaCislo"
field.aliasName = "parcela č."
fieldmap.outputField = field
fieldmap.mergeRule = "Join"
fieldmap.joinDelimiter = ", "
fms1.replaceFieldMap(parcela_index, fieldmap)

drupoz_index = fms1.findFieldMapIndex("DRUH_POZEMKU")
fieldmap1 = fms1.getFieldMap(drupoz_index)
field1 = fieldmap1.outputField = "druhPozemku1"
field1.aliasName = "druh pozemku"
fieldmap1.outputField = field1
fieldmap1.mergeRule = "Join"
fieldmap1.joinDelimiter = ", "
fms1.replaceFieldMap(drupoz_index, fieldmap1)

# SpatailJoin pro plochy
arcpy.analysis.SpatialJoin(zapojporostFeature, "PARCELY_KN_JOIN", areaVysledek, 'JOIN_ONE_TO_ONE',"KEEP_ALL",fms1)

Tags (2)
0 Kudos
2 Replies
MVP Esteemed Contributor

Are you sure INTERSECT (default) is the match option you want?

New Contributor III

Hi, thank you very much for your answer. Yes, I believe INTERSECT is the best option for me. According to this site. There is an issue that can arise (options I and J when selecting polygon with polygon).

This is hardly a case since all target polygons are made by hand. It would be great to have some option INTERSECT WITHOUT TOUCHING :)

As with other options, WITHIN and CONTAINS are too strict. I need the join feature to be matched whenever both join and target inner parts overlap.

As a result, I would like to get target features with lists of values of desired attributes in corresponding cells delimited by comma.


0 Kudos