2

I have one layer with polygons (say, countries) and one layer with x and y coordinates of points (say, cities).

Now, for the city table, I want a new column giving me the id of the country it lies in. I believe the general approach would be to create a new field and then use the field calculator, either of the within or contains function and a loop, like this:

Is City A in Country A? No.

Is City A in Country B? No.

Is City A in Country C? Yes. --> Note down ID.

Is City B in Country A? And so forth.

However, I cannot figure out how to write the code. How do you use these functions if your inputs are from different layers/attribute tables? How to specify the point input since it consists of an x and a y variable? Does anyone have a Python code example for how s/he used the within or contains function?

1
  • 1
    There are many ways to do this. One way is this code (at bottom of page) which should give you a start. desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/… , you want to select by location and then copy one field to another. If you can adapt this code (or any code) and you post it we can help with your design or syntax.
    – ed.hank
    Commented Oct 26, 2016 at 13:41

2 Answers 2

2

You should take a look to Spatial Join analysis. You can perform it using arcpy or you can apply spatial join from toolboxes no code needed.

http://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/spatial-join.htm

0

Using spatial join analysis worked out fine for me.

  • You open both files (polygons and points) in your map.
  • Then you right-click the points data, go to join and select the option "join data based on spatial location.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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