I am trying to spatially join a polygon shapefile to a point shapefile such that every point is matched to the characteristics of the polygon it falls inside. I have succeeded to do this spatial join within the ArcMap following the steps below and in the picture:
- Right-click on point shapefile --> Joins and Relates --> Join
- What I have selected in the picture
Now I am trying to replicate the result with ArcPy which is more convenient for me because it will allow me to replicate the results in the future. This is the code I tried:
### Define input, output etc.
target_features = "data/raw/population_weights/nhgis0009_shape/CA_block_2010.shp"
join_features = "data/raw/voting_precincts/shapefile/voting_precincts.shp"
out_feature_class = "data/raw/population_weights/nhgis0008_shape/block_points_merged_2.shp"
### Do spatial join
arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)
However, I then end up with a polygon shapefile (the points must be somehow joined to the polygons and not vice versa). I have tried to switch target feature and join feature but the result is the same.
Is SpatialJoin_analysis the correct tool for what I am trying to do? How would the correct python code look that will replicate the steps I described above?
Edit (see comments below): Results window for the spatial join I want to perform
-
3Use tool spatial join and play with inputs. When you get what you want, go to results window, copy as Python, insert in your scriptFelixIP– FelixIP2017年02月09日 08:39:22 +00:00Commented Feb 9, 2017 at 8:39
-
Thank you. From your statement I understand that you can perform any action performed in the ArcMap interface, look at the results and get the equivalent code for python. Is that correct? That would indeed be awesome. However, I tried to follow the advice, spatially joined layers manually and then looked at the results window but it remains empty. Why? Or have I misunderstood you?eigenvector– eigenvector2017年02月09日 09:00:43 +00:00Commented Feb 9, 2017 at 9:00
-
1Not any action, the ones where you use TOOLs. There is one called spatial join.FelixIP– FelixIP2017年02月09日 17:17:08 +00:00Commented Feb 9, 2017 at 17:17
-
Thanks. Now I understand what you mean. I have used the spatial join (analysis) tool and updated the question with a screenshot of how the results window looks like. Could you point out to me where I can find the python code in the results window?eigenvector– eigenvector2017年02月09日 18:04:39 +00:00Commented Feb 9, 2017 at 18:04
-
1Right click on itFelixIP– FelixIP2017年02月09日 19:54:23 +00:00Commented Feb 9, 2017 at 19:54
1 Answer 1
[Answering for completeness and in case it helps future generations. Credits to FelixIP for the suggestion with checking the results window.]
If you want to join polygons to points such that each point in the point-data set will also have information of the polygon it lies in, then SpatialJoin_analysis
is indeed the right tool and the correct code is:
### Join polygons to points
target_features = "points.shp"
join_features = "polygons.shp"
out_feature_class = "polygons_merged_to_points.shp"
arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)
Explore related questions
See similar questions with these tags.