How to add values from polygon to points which are inside it

301
7
Jump to solution
10-23-2022 06:31 AM
Labels (2)
Carol2
by
New Contributor II

Hello all, I am still learning and now cant find the solution. So, I have points and polygons, polygons have names. I need to select points which are inside polygon and give the same name as polygon. So, should be all points with specific names like polygon in which they are. Spatial  join not suitable for me because it creates output and I need to work with the same feature class, just to edit it.

Any ideas?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor

Arcade, the script language developed by ESRI for the ArcGIS infrastructure, is perfect for that.

Use the Calculate Field tool on the point feature class, change the language to Arcade. Use this expression, edit the polygon feature class's name (first line) and the name field (last line):

// load the polygons
var polygons = FeaturesetByName($datastore, "PolygonFeatureclass")

// get the polygon that intersects the current point
var i_polygon = First(Intersects(polygons, $feature))

// return a default value if no polygon is intersecting
if(i_polygon == null) { return null }

// return the name of the Polygon
return i_polygon.Name

Have a great day!
Johannes

View solution in original post

7 Replies
JohannesLindner
MVP Regular Contributor

Arcade, the script language developed by ESRI for the ArcGIS infrastructure, is perfect for that.

Use the Calculate Field tool on the point feature class, change the language to Arcade. Use this expression, edit the polygon feature class's name (first line) and the name field (last line):

// load the polygons
var polygons = FeaturesetByName($datastore, "PolygonFeatureclass")

// get the polygon that intersects the current point
var i_polygon = First(Intersects(polygons, $feature))

// return a default value if no polygon is intersecting
if(i_polygon == null) { return null }

// return the name of the Polygon
return i_polygon.Name

Have a great day!
Johannes
Carol2
by
New Contributor II

Thank you, thats worked! I will try to combine it with iterator and model builder because have 2 polygon layers and more than 10 point layers

Cheers!

0 Kudos
JohannesLindner
MVP Regular Contributor

If you modify the rule, you can make it so that if it doesn't find an intersecting polygon in the first FC, it tries the second Polygon FC:

// load the polygons
var polygons = FeaturesetByName($datastore, "PolygonFeatureclass")

// get the polygon that intersects the current point
var i_polygon = First(Intersects(polygons, $feature))

// if no polygon is intersecting, try the second polygon fc
if(i_polygon == null) { 
    var polygons = FeaturesetByName($datastore, "PolygonFeatureclass_2")
    var i_polygon = First(Intersects(polygons, $feature))
    // if there is no0 intersecting polygon here, too, return null
    if(i_polygon == null) { return null }
}

// return the name of the Polygon
return i_polygon.Name

 

Is that what you're trying to do?

 

Also, if this is a task you have to do often, you might want to consider making this expression into an Attribute Rule for each of the point fcs. Attribute Rules can be executed when a feature is inserted or edited. This way, the name would be updated automatically every time you edit a point.


Have a great day!
Johannes
0 Kudos
Carol2
by
New Contributor II

I will check that might use it in the future. For now it's one time job because points do not changes often. I got 2 new fields in point layers in which I will put 2 polygon layers names for all point layers is the same.

Thanks again!

0 Kudos
JohannesLindner
MVP Regular Contributor

Great, glad I could help. Please mark my answer as solution, so that this question is shown as solved.


Have a great day!
Johannes
Carol2
by
New Contributor II

One more question, if I have point intersecting 2 polygons I want to return both (or more) values in one field. How should i do? Something like merge rule Join with delimiter

0 Kudos
JohannesLindner
MVP Regular Contributor
// load the polygons
var polygons = FeaturesetByName($datastore, "PolygonFeatureclass")

// get all intersecting polygons (instead of the first one)
polygons = Intersects(polygons, $feature)

// loop through the intersecting polygons and extract the names into an array
var names = []
for(var poly in polygons) {
    Push(names, poly.Name)
}

// concatenate and return
return Concatenate(names, ", ")

Have a great day!
Johannes