Help with Attribute Rules - Finding Nearest School

793
8
Jump to solution
08-20-2021 06:40 AM
Robswann
New Contributor III

Hello, I am trying to use the attribute rules in my polygon feature to find the nearest school to each polygon. I have the following below, but it seems to be returning a school further away. Can someone please help?

Thanks in advance. 

var CURRENTSCHOOLS = FeatureSetByName($datastore, "ELEMENTARY_SCHOOL", ["School_Nam"]);
VAR SEARCHDISTANCE = 10;
VAR SCHOOLINTERSECT = INTERSECTS(CURRENTSCHOOLS, BUFFER($FEATURE, SEARCHDISTANCE, "MILES"))
VAR MINDISTANCE = INFINITY
VAR FSCHOOL = FIRST(SCHOOLINTERSECT)
IF (FSCHOOL == NULL) RETURN ""
IF(SEARCHDISTANCE < MINDISTANCE)
RETURN FSCHOOL.SCHOOL_NAM

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
var CURRENTSCHOOLS = FeatureSetByName($datastore, "ELEMENTARY_SCHOOL", ["School_Nam"]);
VAR SEARCHDISTANCE = 10;
VAR SCHOOLINTERSECT = INTERSECTS(CURRENTSCHOOLS, BUFFER($FEATURE, SEARCHDISTANCE, "MILES"))
IF(SCHOOLINTERSECT == null || COUNT(SCHOOLINTERSECT) == 0) {
  RETURN ""
}
VAR MINDISTANCE = INFINITY
VAR NEARESTSCHOOL = ""
FOR(VAR SCHOOL IN SCHOOLINTERSECT) {
  VAR DIST = DISTANCE(SCHOOL, $FEATURE)
  IF(DIST < MINDISTANCE) {
    MINDIST = DIST
    NEARESTSCHOOL = SCHOOL.SCHOOL_NAM
  }
}
RETURN NEARESTSCHOOL

Have a great day!
Johannes

View solution in original post

8 Replies
JohannesLindner
MVP Frequent Contributor
var CURRENTSCHOOLS = FeatureSetByName($datastore, "ELEMENTARY_SCHOOL", ["School_Nam"]);
VAR SEARCHDISTANCE = 10;
VAR SCHOOLINTERSECT = INTERSECTS(CURRENTSCHOOLS, BUFFER($FEATURE, SEARCHDISTANCE, "MILES"))
IF(SCHOOLINTERSECT == null || COUNT(SCHOOLINTERSECT) == 0) {
  RETURN ""
}
VAR MINDISTANCE = INFINITY
VAR NEARESTSCHOOL = ""
FOR(VAR SCHOOL IN SCHOOLINTERSECT) {
  VAR DIST = DISTANCE(SCHOOL, $FEATURE)
  IF(DIST < MINDISTANCE) {
    MINDIST = DIST
    NEARESTSCHOOL = SCHOOL.SCHOOL_NAM
  }
}
RETURN NEARESTSCHOOL

Have a great day!
Johannes
Robswann
New Contributor III

Thanks so much Johannes! I had an error but made the change from MINDIST TO MINDISTANCE line 12 and i worked.. 

I hate to ask, but would there be a way to add a rule that returns the distance from the polygon to the nearest school?  Thanks again

0 Kudos
JohannesLindner
MVP Frequent Contributor

Sure. Same as this rule, but return MINDISTANCE instead of NEARESTSCHOOL.


Have a great day!
Johannes
0 Kudos
Robswann
New Contributor III

Thanks again! 

0 Kudos
Robswann
New Contributor III

Would it be possible to find the nearest schools within a certain school district only. The School District that would be referenced would be a entered field within the feature, Thanks 

0 Kudos
JohannesLindner
MVP Frequent Contributor
// change: load your school district field
var CURRENTSCHOOLS = FeatureSetByName($datastore, "ELEMENTARY_SCHOOL", ["School_Nam", "School_District"]);
// change: filter schools by district
var district = $feature.School_District
if(district != null) {
  CURRENTSCHOOLS = Filter(CURRENTSCHOOLS, "School_District = @district")
}
// rest stays the same
VAR SEARCHDISTANCE = 10;
VAR SCHOOLINTERSECT = INTERSECTS(CURRENTSCHOOLS, BUFFER($FEATURE, SEARCHDISTANCE, "MILES"))
IF(SCHOOLINTERSECT == null || COUNT(SCHOOLINTERSECT) == 0) {
  RETURN ""
}
VAR MINDISTANCE = INFINITY
VAR NEARESTSCHOOL = ""
FOR(VAR SCHOOL IN SCHOOLINTERSECT) {
  VAR DIST = DISTANCE(SCHOOL, $FEATURE)
  IF(DIST < MINDISTANCE) {
    MINDISTANCE = DIST
    NEARESTSCHOOL = SCHOOL.SCHOOL_NAM
  }
}
RETURN NEARESTSCHOOL

Have a great day!
Johannes
0 Kudos
Robswann
New Contributor III

Thank you for your response, i have been working on getting what you suggested to work, but still no luck. I Probably didnt provide enough info:

//Note: ELEMENTARY_SCHOOL is the feature that has all the schools

// Note: DISTRICT_1 is the field located in ELEMENTARY_SCHOOL that lists the School District the school is located in, 

//Note: var district = $feature.School_District - Is the field that contains the School District to search within and is the feature class named PROPERTIES (The layer with the Attribute Rule)  

The below returns a null value everytime, not sure what I am doing wrong. Thanks 

// change: load your school district field 
var CURRENTSCHOOLS = FeatureSetByName($datastore, "ELEMENTARY_SCHOOL", ["School_Nam", "DISTRICT_1"]);
// change: filter schools by district
var district = $feature.School_District
if(district != null) {
CURRENTSCHOOLS = Filter(CURRENTSCHOOLS, "DISTRICT_1 = @district")


}
// rest stays the same
VAR SEARCHDISTANCE = 30;
VAR SCHOOLINTERSECT = INTERSECTS(CURRENTSCHOOLS, BUFFER($FEATURE, SEARCHDISTANCE, "MILES"))
IF(SCHOOLINTERSECT == null || COUNT(SCHOOLINTERSECT) == 0) {
RETURN ""
}
VAR MINDISTANCE = INFINITY
VAR NEARESTSCHOOL = ""
FOR(VAR SCHOOL IN SCHOOLINTERSECT) {
VAR DIST = DISTANCE(SCHOOL, $FEATURE)
IF(DIST < MINDISTANCE) {
MINDISTANCE = DIST
NEARESTSCHOOL = SCHOOL.SCHOOL_NAM
}
}
RETURN NEARESTSCHOOL

0 Kudos
JohannesLindner
MVP Frequent Contributor

If the rule returns an empty string, it means it didn't find a school in teh specified search radius. If it worked before adding the filter, then the problem is probably in the filter.

  • make sure that ELEMENTARY_SCHOOL.DISTRICT_1 and PROPERTIES.School_District are of the same data type
  • make sure that School_District is a value that can actually be found in DISTRICT_1
  • include better log messages (at least temporarily)
var log = ""

var CURRENTSCHOOLS = FeatureSetByName($datastore, "ELEMENTARY_SCHOOL", ["School_Nam", "DISTRICT_1"]);
log += Count(CURRENTSCHOOLS) + " schools loaded; "

var district = $feature.School_District
if(!IsEmpty(district)) {
  log += "filtering schools by '" + district + "'; "
  CURRENTSCHOOLS = Filter(CURRENTSCHOOLS, "DISTRICT_1 = @district")
  log += Count(CURRENTSCHOOLS) + " schools found; "
} else {
  log += "School_District is empty; "
}

VAR SEARCHDISTANCE = 30;
VAR SCHOOLINTERSECT = INTERSECTS(CURRENTSCHOOLS, BUFFER($FEATURE, SEARCHDISTANCE, "MILES"))
log += Count(SCHOOLINTERSECT) + " schools found in the search radius; "

IF(SCHOOLINTERSECT == null || COUNT(SCHOOLINTERSECT) == 0) {
  return log
}

VAR MINDISTANCE = INFINITY
VAR NEARESTSCHOOL = ""
FOR(VAR SCHOOL IN SCHOOLINTERSECT) {
  VAR DIST = DISTANCE(SCHOOL, $FEATURE)
  IF(DIST < MINDISTANCE) {
    MINDISTANCE = DIST
    NEARESTSCHOOL = SCHOOL.SCHOOL_NAM
  }
}
RETURN NEARESTSCHOOL

 


Have a great day!
Johannes
0 Kudos