# The nearest school district calculation

426
2
08-24-2021 06:51 AM
by
New Contributor III

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

1 Solution

Accepted Solutions
by
New Contributor III

Yes you got it right. thanks again!

2 Replies
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")

// no districts around the feature
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 = ""
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
by
New Contributor III

Yes you got it right. thanks again!