Select to view content in your preferred language

Arcade how to copy date field intersection

171
4
a week ago
cat206
by
Occasional Contributor II

Hi, I am following this blog to try and build my smart form to replicate the old Smart Editor Widget in Web App builder: From the Smart Editor to Smart Forms (esri.com)

I have used the below code to copy attribute data from an intersection layer when creating a new feature to replicate the smart editor copy, however I cannot get the code to work to copy across a Date and time field. Does anyone have any ideas how i go about this using Arcade calculated expression form in field maps designer please?

// Get the bus stops layer
// To customize, replace the layer name below
var busStops = FeatureSetByName($map,"RTD Active Bus Stops")
// Buffer the current location and intersect with the bus stops
var bufferedLocation = Buffer($feature, 1000, 'feet')
var candidateStops = Intersects(busStops, bufferedLocation)
// Calculate the distance between the bus stops and the current location
// Store the feature and distance as a dictionary and push it into an array
var featuresWithDistances = []
for (var f in candidateStops) {
    Push(featuresWithDistances, 
        {
            'distance': Distance($feature, f, 'feet'),
            'feature': f
        }
    )
}
// Sort the candidate bus stops by distance using a custom function
function sortByDistance(a, b) {
    return a['distance'] - b['distance']
}
var sorted = Sort(featuresWithDistances, sortByDistance)
// Get the closest bus stop
var closestFeatureWithDistance = First(sorted)
// If there was no bus stop, return null
if (IsEmpty(closestFeatureWithDistance)) { return null }
// Return the bus stop name attribute value
// To customize, replace the field name "STOPNAME" below
return `${closestFeatureWithDistance['feature']['STOPNAME']}`

 

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

I'd expect return closestFeatureWithDistance['some_date'] would work based on how this is written. You might want to check for "no stops" earlier, though. It's possible your sorting and distance calculations are erroring out because there are no features being passed to it. Try checking for nulls prior to calculating distances.

// ...earlier stuff
var candidateStops = Intersects(busStops, bufferedLocation)

// return null if no stops
if (IsEmpty(candidateStops)) { return null }

// calculate distances, etc...

It's possible the issue is specific to your data, perhaps. What does your current expression look like? When you attempt to run it, what does it return? An empty value? An error?

- Josh Carlson
Kendall County GIS
0 Kudos
cat206
by
Occasional Contributor II

Hi Josh, 

Thanks for replying so quickly. I have amended the code slightly to match my feature layers. 

// Get Species sighting layer

var Sighting = FeatureSetByName($map, "Potential Sightings to be reviewed")
// Buffer the current location and intersect with the species
var bufferedLocation = Buffer($feature, 2, 'meter')
var Species = Intersects(Sighting, bufferedLocation)
// Calculate the distance between the species and the current location
// Store the feature and distance as a dictionary and push it into an array
var featuresWithDistances = []
for (var f in Species) {
    Push(featuresWithDistances, 
        {
            'distance': Distance($feature, f, 'meter'),
            'feature': f
        }
    )
}
// Sort the species by distance using a custom function
function sortByDistance(a, b) {
    return a['distance'] - b['distance']
}
var sorted = Sort(featuresWithDistances, sortByDistance)
// Get the closest species sighting
var closestFeatureWithDistance = First(sorted)
// If there was no species, return null
if (IsEmpty(closestFeatureWithDistance)) { return null }
// Return the species name attribute value
// To customize, replace the field name "Date" below
return `${closestFeatureWithDistance['feature']['InspDate']}`

 

The inspection date on the feature is fine:

cat206_0-1718976783695.png

 

However, when i go to add a new feature to the new layer, it looks like this:

cat206_1-1718976853804.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Curious. Most datetimes are just epoch integers representing the number of milliseconds since 1/1/1970, so the fact that you're getting something an hour and two seconds ahead of that date suggests there's something else going on with the time scale of your fields.

Your inspection date on the feature becomes 1700632800000. But that assumes milliseconds. Can you add this right before the end of your expression?

Console(Number(closestFeatureWithDistance['feature']['InspDate']))

What does the Console show?

If your feature is storing timestamps in seconds, but the other layer stores them in, say nanoseconds, that could account for the vast difference in displayed dates.

Assuming this might be the case, the solution is to convert the date to a number and then multiply it by whatever factor you need to get to the appropriate units.

- Josh Carlson
Kendall County GIS
0 Kudos
cat206
by
Occasional Contributor II

Hi Josh,

I've tried adding it in but it doesn't seem to be registering it which is really bizarre:

cat206_0-1718979061561.png

I must be going wrong somewhere but cannot figure it out

 

 

0 Kudos