spatial join points closest to line segments returns incorrect count

01-19-2017 11:40 AM
New Contributor

I have a point file with observation data, and a line file with segmented transect lines. I'm trying to come up with a count of observations on each line segment. The attached map shows that some line segments have more than one observation, while others have no observations (lines_points_map.JPG). Using the Spatial Join Tool from the Analysis/Overlay Toolbox, I specified the line layer as my target input, observation point layer as the join input and choose a "one to one" join operation with a match option of "closest". Because the default count field kept returning a value of 1 for every record, I added a Value field to my observation point layer and assigned a value of 1 to each record in the observation layer. I tried running the Spatial Join tool again, this time using the field map to add a new field called ObsCount and specified a merge rule to sum the Value field from the observation layer (spatialjoindialogue.JPG). So each line segment should have a count of all observations in the ObsCount field. Or so I thought. Each record had a value of 1 (spatialjoinoutput.JPG).  

When I use the joins & relate option available by right clicking on my line segment layer in the table of contents (toc_join.JPG) and select the "Join data from another layer based on spatial location" option, specifying my observation layer as the join layer and selecting "closest" as the join option (toc_joindialogue.JPG), my output file gives me the expected results in the Count field (toc_joinoutput.JPG).

I'm trying to add the spatial join into a larger geoprocessing flow, so ideally I'd like to be able to achieve this using a python script. Since I can't get the proper results using the Spatial Join analysis tool manually, I don't think using arcpy.SpatialJoin_analysis in a script would work.  I haven't been able to find python code for the spatial join achieved through the table of contents joins & relates menu. I'm a python novice, so I'm sure there's a way to do it....I'm just kind of stumped right now. If anyone has any suggestions, I would really love to hear them.


0 Kudos
2 Replies
Frequent Contributor

The spatial join tool's help states:

"When the Match Option is set to CLOSEST or CLOSEST_GEODESIC, it is possible that two or more join features are at the same distance from the target feature. When this situation occurs, one of the join features is randomly selected as the matching feature (the join feature's FID does not influence this random selection). If you want to find the 2nd, 3rd, or Nth closest feature, use the Generate Near Table tool."  Effectively it is ignoring your subsequent points for using the sum merge rule.  I have no answer for why this is different when doing this manually in the TOC.

Why not use Intersect for your map option if your points are on the line, it should allow you to get all the points along your line.


Regular Contributor II

Sorry, but if you want to know the count of observer points for each line, you should spatial join for point feature not line.

Right click on points layer in TOC > Join and Relates > Join.. > select Join data from another layer based on spatial location. Then select line layer . then second choice for closest line.

Finally , after you get the output from last step, Summarize the OBJECTID field that related to line feature . and you will get the count table.

0 Kudos