Multiple Spatial Joins and field mapping

2284
4
04-20-2012 12:37 AM
ClaireParsons1
New Contributor
Hi

I wonder whether someone can help - I have a town characterisation dataset which I need to 'spatial join' to some ecological designation datatsets - I thought a quick way of doing this might be in Python.

The problem I have is fieldmapping - I don't really get it!! 😞

Ideally, what i'd like to do is create a field and if the designation is within the target layer populate this field with "YES".

Help would be greatly appreciated.  I've posted my starting code below minus field mappings!!

import arcpy

from arcpy import env

env.workspace = r"C:\Users\Documents\GIS\Working\Constraints"

files = arcpy.ListFiles("*.shp")
targetLayer = r"C:\Users\Documents\GIS\Working\LCP\stratford_3d_swedits_1.shp"

for file in files:
    #heres where i need to put the spatial join and field mapping code
Tags (2)
0 Kudos
4 Replies
MarcinGasior
Occasional Contributor III
Have you tried Spatial Join tool?
This tool joins selected fields from one layer to another based on spatial relation.
0 Kudos
ClaireParsons1
New Contributor
I'm trying to automate the spatial join tool as you can only perform I join at a time and i want to join many datasets to 1 target layer.
0 Kudos
DanNarsavage
Occasional Contributor
Field maps contain a ton if information so they're a bit of a handful when you try to make them from scratch in python.  To get your own code, you can perform a spatial join using the tool in ArcCatalog with the field map exactly like you want it, then open the Geoprocessor's Results dialog and copy the field map string from there.  Here's some code that I built with that as a starting point. 

FieldMapString = "" \
                + """AddressNumber "AddressNumber" true true false 4 Long 0 10 ,First,#,""" + AddressFC+ """,AddressNumber,-1,-1;"""\
                + """AddressNumberFraction "AddressNumberFraction" true true false 3 Text 0 0 ,First,#,""" + AddressFC+ """,AddressNumberFraction,-1,-1;"""\
                + """PreDirection "PreDirection" true true false 2 Text 0 0 ,First,#,""" + AddressFC+ """,PreDirection,-1,-1;"""\
                + """PreType "PreType" true true false 20 Text 0 0 ,First,#,""" + AddressFC+ """,PreType,-1,-1;"""\
                + """CoreStreetName "CoreStreetName" true true false 20 Text 0 0 ,First,#,""" + AddressFC+ """,CoreStreetName,-1,-1;"""\
                + """PostType "PostType" true true false 20 Text 0 0 ,First,#,""" + AddressFC+ """,PostType,-1,-1;"""\
                + """PostDirection "PostDirection" true true false 2 Text 0 0 ,First,#,""" + AddressFC+ """,PostDirection,-1,-1;"""\
                + """PostModifier "PostModifier" true true false 10 Text 0 0 ,First,#,""" + AddressFC + """,PostModifier,-1,-1;"""\
                + """PostalCity "PostalCity" true true false 15 Text 0 0 ,First,#,""" + AddressFC+ """,PostalCity,-1,-1;"""\
                + """SubUnitType "SubUnitType" true true false 15 Text 0 0 ,First,#,""" + AddressFC+ """,SubUnitType,-1,-1;"""\
                + """SubUnitIdentifier "SubUnitIdentifier" true true false 10 Text 0 0 ,First,#,""" + AddressFC+ """,SubUnitIdentifier,-1,-1;"""\
                + """AddressPointID "AddressPointID" true true false 4 Long 0 10 ,First,#,""" + AddressFC+ """,AddressPointID,-1,-1;"""\
                + """PointIssue "PointIssue" true true false 2 Short 0 5 ,First,#,""" + AddressFC+ """,PointIssue,-1,-1;"""\
                + """AddressChange "AddressChange" true true false 2 Short 0 5 ,First,#,""" + AddressFC+ """,AddressChange,-1,-1;"""\
                + """ParcelException "ParcelException" true true false 2 Short 0 5 ,First,#,""" + AddressFC+ """,ParcelException,-1,-1;"""\
                + """PARCEL "PARCEL" true true false 50 Text 0 0 ,First,#,""" + ParcelFC+ """,PARCEL,-1,-1;"""\
                + """ADDRESS "ADDRESS" true true false 60 Text 0 0 ,First,#,""" + ParcelFC+ """,ADDRESS,-1,-1;"""\
                + """CITY_STATE "CITY_STATE" true true false 41 Text 0 0 ,First,#,""" + ParcelFC+ """,CITY_STATE,-1,-1;"""\
                + """PROPADDNUM "PROPADDNUM" true true false 10 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPADDNUM,-1,-1;"""\
                + """PROPPREDIR "PROPPREDIR" true true false 2 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPPREDIR,-1,-1;"""\
                + """PROPSTNM "PROPSTNM" true true false 25 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPSTNM,-1,-1;"""\
                + """PROPSTTYPE "PROPSTTYPE" true true false 10 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPSTTYPE,-1,-1;"""\
                + """PROPPOST "PROPPOST" true true false 2 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPPOST,-1,-1;"""\
                + """PROPUNIT "PROPUNIT" true true false 10 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPUNIT,-1,-1;"""\
                + """PROPUNUM "PROPUNUM" true true false 10 Text 0 0 ,First,#,""" + ParcelFC+ """,PROPUNUM,-1,-1;"""
fieldmappings = arcpy.FieldMappings()
fieldmappings.loadFromString(FieldMapString)
arcpy.SpatialJoin_analysis(AddressFC, ParcelFC, SpatialJoinFC, "JOIN_ONE_TO_MANY", "KEEP_COMMON", fieldmappings)
0 Kudos
MathewCoyle
Frequent Contributor
Depending on the level of control you want over your mapped fields, here's an example of what I use. As taken from this thread.
def GetFieldMappings(fc_in, fc_out, dico):
    field_mappings = arcpy.FieldMappings()
    field_mappings.addTable(fc_in)

    for input, output in dico.iteritems():
        field_map = arcpy.FieldMap()
        field_map.addInputField(fc_in, input)
        field = field_map.outputField
        field.name = output
        field_map.outputField = field
        field_mappings.addFieldMap(field_map)
        del field, field_map

    return field_mappings

dico = {
'OBJECTID':         'O_OID',
'BLOCKSTAGE':       'BLOCKSTAGE',
'AREAGIS':          'AREAGIS',
'OPERATIONSTYPE':   'OPTYPE',
'BLOCKTYPE':        'BLOCKTYPE',
'HARVESTSEASON':    'HARVSEASON',
'ROADPERCENTAGE':   'ROADPERCNT',
'SOURCECODE':       'SOURCECODE',
'ZONECODE':         'ZONECODE',
'TOWNSHIP':         'TOWNSHIP',
'RANGE':            'RANGE',
'MERIDIAN':         'MERIDIAN',
'BLOCKSECTION':     'BLOCKSEC',
'GRIDNUMBER':       'GRIDNUMBER',
'PASS':             'PASS'
}

Mapper = GetFieldMappings(fc_in, fc_out, dico)
0 Kudos