Select to view content in your preferred language

The nearest school district calculation

620
2
Jump to solution
08-24-2021 06:51 AM
Robswann
Occasional Contributor

Hello I am working on a calculation rule that inputs the nearest school district from a polygon feature. The problem I am having is that the rule is bringing back the school district that the polygon is within. I would like it to calculate the nearest (if any) school district within 1 mile. Is this possible. I have the following below.... Thanks in advance. 

 

var SCHOOL_DIST = FeatureSetByName($datastore, "SCHOOL_DIST", ["NAME"]);
VAR SEARCHDISTANCE = 1;
VAR SCHOOLINTERSECT = INTERSECTS(SCHOOL_DIST, BUFFER($FEATURE, SEARCHDISTANCE, "MILE"))
VAR FSCHOOL = FIRST(SCHOOLINTERSECT)
IF (FSCHOOL == NULL) RETURN ""
RETURN FSCHOOL.NAME

0 Kudos
1 Solution

Accepted Solutions
Robswann
Occasional Contributor

Yes you got it right. thanks again!

View solution in original post

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

So you create a feature inside a school district (polygon) and want to return the name of the next district within 1 mile that is not the district the feature is in. Did I get that right?

var school_districts = FeatureSetByName($datastore, "SCHOOL_DIST", ["NAME"], true);
var search_radius = Buffer($feature, 1, "Mile")
var districts_within_search_radius = Intersects(school_districts, search_radius)

// no districts around the feature
if(Count(districts_within_search_radius ) == 0) {
  return "feature has no school district within search radius"
}

// this is basically the code from your previous question
var min_distance = Infinity
var nearest_district = ""
for(var district in districts_within_search_radius) {
  var dist = Distance($feature, district)
  // dist > 0: exclude the district that contains the feature
  if(dist > 0 && dist < min_distance) {
    min_distance = dist
    nearest_district = district.NAME
  }
}
// no districts that don't intersect the feature
if(nearest_district == "") {
  return "there are " + Count(districts_within_search_radius) + " districts within the search radius, but they all intersect the feature"
}
return nearest_district

Have a great day!
Johannes
Robswann
Occasional Contributor

Yes you got it right. thanks again!

0 Kudos