Spatial Join in Model Builder (Field Map)

6263
7
12-07-2011 12:30 AM
MartinScheiber
Occasional Contributor
Hey everybody,

I have a problem, I need some help with.
I want to create an automated process in Model Builder that, beside other things, does a spatial join from a point to a polygon dataset. Once it´s finished, I want it to run with different datasets specified by the user.

In the Spatial Join tool I want to pre-specify a field map with join features. I want to calculate the MEAN and the STANDARD DEVIATION of point data fields. The process works fine, as long as I have input datasets specified.

The problem is that once I create Variables and Model Parameters and remove the original input datasets, the field map in the Spatial Join tool is getting deleted. Is there any possbility to keep the pre-defined field map?

Please refer to the attached screenshots for further information.  

Regards,
Martin
0 Kudos
7 Replies
by Anonymous User
Not applicable
Original User: DanEvans83

I've had the same problem and asked about it several times here without a reply 😞

I think it can only be done in a python script. Bit crap really.
MartinScheiber
Occasional Contributor
Hey Dan,

that doesn´t sound very promising. 😞
Did you solve your problem with a python script? If yes, could you please provide a sample?
Unforntunately I´m just a beginner in python programming.

Martin
0 Kudos
by Anonymous User
Not applicable
Original User: DanEvans83

Hi Martin,

I never did get it working in Python. All the documentation and tutorials for ArcGIS scripting seem to assume you're already a master programmer unfortunately. I'm not completely inexperienced in programming (started with BASIC when I was about 7, done bits and pieces of C++ on and off over the years, and worked as a web developer using PHP for over a year) but I just haven't found a good way in to this Python scripting stuff.

I think the idea is roughly that you'd create and populate a fieldmappings object programmatically and pass that as a parameter to the spatial join command... But I never quite got my head around how to set things up right (like environment settings) so that it'll work with any appropriate input, how to check for invalid inputs, how to correctly program the paths to the intermediate data so it'll work properly wherever the data is located....

If I'd had more time I might have got it, but the process in question is only done once or twice a year. I think in the end I split the process into three parts and ran a model with specified inputs for each part, which saved a bit of time compared to doing it all manually.

Very frustrating.

Dan
0 Kudos
MartinScheiber
Occasional Contributor
Dan: Thank you very much for your answer.

Could someone from ESRI please help clarifying if there is an easier solution, or maybe provide a script example in case there isn´t?

Any help would be highly appreciated.

Regards,
Martin
0 Kudos
by Anonymous User
Not applicable
Original User: Clockwise

I think the script tool attached below will allow you to do this. If not I hope it will help you figure out how to get what you are looking for.
(I've also put inserted the code for the script below)

From your description I have assumed that the preset field names are not going to be changing for your inputs and that they don't need to be changed in the model. However, you can change them in the script itself.
The script is also written for ArcMap 10. If you are using an older version of ArcMap the code will probably have to be changed.
If you download the script most of it is already setup. The script and toolbox just need to be extracted.
Or you can copy the code and setup the tool yourself.

In the script, prior to use, you will have to change the outputFields list to tell the script which field names you want in your output.

outputFields = ("fieldname1","fieldname2")

When I tested the script I noticed the output seemed to add some general join information to the attribute table too. I didn't set the code up to remove the fields because I wasn't sure if you wanted that information or not.

Once you set up your list you should be able to add the tool to your model and use it like any other tool.



Hope this helps. : )



import arcpy

# Manually specify the desired fieldnames here
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

outputFields = ("fieldname1","fieldname2")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Get input parameters from user/model 
targetFeature = arcpy.GetParameterAsText(0)
joinFeature = arcpy.GetParameterAsText(1)
outputFeature = arcpy.GetParameterAsText(2)

#Create Fieldmappings and add tables from input
tempMappings = arcpy.FieldMappings()
tempMappings.addTable(targetFeature)
tempMappings.addTable(joinFeature)

#Create new fieldmappings using the listed fields, from the current(temp) fieldmappings.  
fieldMappings = arcpy.FieldMappings()
for field in outputFields:
    fieldMappings.addFieldMap(tempMappings.getFieldMap(tempMappings.findFieldMapIndex(field)))

#Process: Spatial Join
#(note: you can change how the join works on this line)
arcpy.SpatialJoin_analysis(targetFeature, joinFeature, outputFeature, "JOIN_ONE_TO_ONE", "KEEP_ALL", fieldMappings, "INTERSECT")
0 Kudos
MartinScheiber
Occasional Contributor
Hey Benjamin,

that sounds promising. Thanks a lot for the Python Script. I´ll give it a try.

Regards,
Martin
0 Kudos
by Anonymous User
Not applicable
Original User: jmork045

This script works well, but is there any way that you can extract field names as params and then use them for FieldMappings? I have tried to set outputFields to these params, but it will not work with the tool. I am not sure exactly how the list of fields is converted into text, so it is difficult to work with them. Ideally, if i could use all of the fields that the user selects as params and put them into the outputFields in the same format as in the script that would be great. Any help would be appreciated.

Thanks
0 Kudos