0

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:

  1. Right-click on point shapefile --> Joins and Relates --> Join
  2. What I have selected in the picture

enter image description here

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

enter image description here

  • 3
    Use tool spatial join and play with inputs. When you get what you want, go to results window, copy as Python, insert in your script – FelixIP Feb 9 '17 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 Feb 9 '17 at 9:00
  • 1
    Not any action, the ones where you use TOOLs. There is one called spatial join. – FelixIP Feb 9 '17 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 Feb 9 '17 at 18:04
  • 1
    Right click on it – FelixIP Feb 9 '17 at 19:54
2

[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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.