Attribute Rule: Auto Populate Fields to Nearest Feature

725
2
11-01-2021 11:24 PM
Labels (2)
YongXin
New Contributor

I am working with a large set of Ontario watercourses and wish to automatically populate a field with the nearest road network. In the picture below, the watercourses are represented by the blue lines and the road network is represented by the purple lines. 

YongXin_0-1635834125485.png

I am trying to have a field titled NearestRoad auto-populate with the nearest road network that is 100km from it. Is there a way to go about this? Thank you!

 

 

Tags (1)
0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor
function closest_feature(test_feature, compare_feature_set) {
  var min_distance = 9999999
  var closest_feature = null
  for(var f in compare_feature_set) {
    var d = Distance(test_feature, f)
    if(d < min_distance) {
      min_distance = d
      closest_feature = f
    }
  }
  return closest_feature
}

var roads = FeatureSetByName($datastore, "RoadFeatureclass", ["RoadID"], true)
var waterway_buffer = Buffer($feature, 100, "kilometers")
var close_roads = Intersects(roads, waterway_buffer)
var closest_road = closest_feature($feature, close_roads)
if(closest_road != null) {
  return closest_road.RoadID
}
return null

 


Have a great day!
Johannes
DanPatterson
MVP Esteemed Contributor

Do you want each tributary of a watercourse tested separately or if any tributary is within 100 km of a road?

That will make a big difference on the outcome.  For example, how many different ones are depicted in your image?


... sort of retired...
0 Kudos