Select to view content in your preferred language

Arcade Expression works in Map Viewer but not Field Maps

2767
11
03-04-2024 04:05 PM
ChrisJRoss13
Occasional Contributor

I have an arcade expression that works perfectly fine in Map Viewer but when I access the map in Field Maps it always returns "No closest weather station was found within the search distance".

Any help on why this is would be greatly appreciated.

The expression is below:

var searchDistance = 50; // Search distance in kilometers
var weatherStations = FeatureSetByPortalItem(Portal('https://www.arcgis.com'),
  '38406bada3104e258fc8b42a2a96581a',
  0,
  ['Temperature_C'],
  True
);
var proximityWeatherStations = Intersects(weatherStations, Buffer($feature, searchDistance, "kilometers"));
var minDistance = Infinity;
var closestWeatherStationTemp;

for (var f in proximityWeatherStations){
 var weatherDist = Round(Distance(f, $feature, "kilometers"),2);
 if (weatherDist < minDistance) {
     closestWeatherStationTemp = f["Temperature_C"];
     minDistance = weatherDist;
 }
}

// Check if a closest individual weather station was found before accessing its property
if (closestWeatherStationTemp != null) {
    return "Closest Weather Station Temperature is: " + closestWeatherStationTemp + " degrees C and is located approximately " + Round(minDistance,0) + " km away.";
} else {
    return "No closest weather station was found within the search distance.";
}
11 Replies
ChrisJRoss13
Occasional Contributor

I modified the code slightly and checked into a few things.

  • The null check doesn't make any impact.
  • closestWeatherStationTemp grabs the temperature of the specific station.
  • I tested adding the hosted feature layer to the map and comparing FeatureSetByName and FeatureSetByPortalID. The FeatureSetByName option works in Field Map. I've attached the same selected feature in AGOL map viewer and field maps showing reference to ByPortalID and ByName.

My most recent code is:

// Define search parameters
var searchDistanceKm = 50; // Search distance in kilometers

// Access weather stations from the Living Atlas
// Source: Environment Canada dataset
var weatherStationsFS = FeatureSetByPortalItem(
  "38406bada3104e258fc8b42a2a96581a", // Item ID for the dataset
  0, // Layer index
  ["Temperature_C"], // Attributes to include
  true // Spatial filtering flag
);

// Find weather stations within the search distance
var nearbyWeatherStations = Intersects(
  weatherStationsFS,
  Buffer($feature, searchDistanceKm, "kilometers")
);

// Initialize variables to track the closest station
var minDistance = Infinity;
var closestStationTemperature = null;

// Iterate through nearby stations to find the closest one
for (var station in nearbyWeatherStations) {
  var distanceToStation = Round(Distance(station, $feature, "kilometers"), 2);
  if (distanceToStation < minDistance) {
    closestStationTemperature = station["Temperature_C"];
    minDistance = distanceToStation;
  }
}

// Output the result based on whether a close station was found
if (closestStationTemperature != null) {
  return "Closest Weather Station Temperature is " + closestStationTemperature + "°C and is located approximately " + Round(minDistance, 0) + " km away.";
 
} else {
  return "No closest weather station was found within the search distance.";
}
AlexHogan
Esri Contributor

Hi @ChrisJRoss13 

 

To confirm using the FeatureSetByName resolved the issue for you?

I am seeing a similar issue that I am troubleshooting now,

 

Thanks

 

Alex

0 Kudos