Select to view content in your preferred language

Help creating Arcade Script to extract data from one layer's field to insert into another layer's field based on identical attributes

2090
8
Jump to solution
02-17-2024 04:01 PM
Mowgli_GIS
Occasional Contributor

Hello, I'm trying to extract data from a layer's field and insert into another layer's field based on attributes matching each other from both features, but I'm struggling to create the correct script. 

My scenario is I have a Utility Pole layer and Cable layer, wherein I have a field in both layers for the combined lat/long coordinates, and as I create new features in the Cable Layer I need to provide both the Start Pole ID and End Pole ID on which the cable is attached (Start ID and End ID are fields in Cable Layer, and Pole Layer has an ID field). So my thought, if possible, is to create an Arcade script in the Cable Start/End ID fields that will look at the Pole layer's Lat/Long coordinate field for a feature with the matching coordinates to the Cable's feature, and if there is a match then extract that Pole Feature's ID and insert in either the corresponding Start or End ID field of the Cable feature.  Below is what I have, code is being written within the Start ID and/or End ID fields of the Cable layer feature, however it does not work:

 
var Pole = FeatureSetByName($map, "Pole", ['LatLong'])
var PoleID = FeatureSetByName($map, "Pole", ['ID'])
if($feature.LatLong == Pole) {return PoleID}
 
 
Appreciate any help and insight anyone could offer!  Thank you!
 
0 Kudos
1 Solution

Accepted Solutions
Mowgli_GIS
Occasional Contributor

@JohnEvans6 , I guess I replied a bit too quick, I tried one more thing and it worked!  Below is the code:

var PoleFeatures = FeatureSetByName($map, "Pole", ['PON','ID'])

for (var f in PoleFeatures) {
  if (f.PON == $feature.PON) {
return (f.ID)
  } else {
    return "test"
  }
}
 
I wouldn't have been able to figure this out or even get close without your help.  So again, THANK YOU!!

View solution in original post

0 Kudos
8 Replies
Mowgli_GIS
Occasional Contributor

Adding to/Updating my original post - below is script I've come up with that seems to get me almost there, but not quite - this code block is being written in the "PoleID" field of the Cable layer:

var PoleFeature = FeatureSetByName($map, "Pole", ['LatLong'])
var PoleID = FeatureSetByName($map, "Pole", ['PoleID'])

if (!IsEmpty($feature.LatLong == PoleFeature)) {
  return PoleID
} else {
  return null
}
 
However, the return that I receive is 
featureSet:
OBJECTID ID
1"Pole-1"

And within the actual field it returns [object Object]

I need the Cable's PoleID field for the specific feature to return the "Pole-1" attribute value.

0 Kudos
JohnEvans6
Regular Contributor

Sorry I don't have time to check if this actually works, but I think you want something that looks like this. If it doesn't work it hopefully gets you close to a solution.

// Build SQL to query against Pole Layer. LatLong is in the pole field. feature.LatLong is the cable's LatLong field
var sql = Concatenate("LatLong = ", $feature.LatLong)

// Filter your pole layer get to only return features where the LatLong fields are equal between Cable and Pole
var features = Filter(FeatureSetByName($map, "Pole", ['LatLong','PoleID']), sql)

// If return is not empty. get the first feature and build your return text
if (!IsEmpty(features)) {
  var pole_data = First(features)
  return { 
	  type : 'text', 
	  text : `This cables Start/End Pole is: ${pole_data.PoleID}`
  }
}

 

0 Kudos
Mowgli_GIS
Occasional Contributor

Hi @JohnEvans6 , thank you for the feedback.  I've been working thru what you provided, but am still having trouble/receiving errors, although I think it is likely due to me not replicating correctly.

If/when you have time, I have a few questions.  Below is the code I have written based on the feedback/example you provided - **I realized instead of my fields in both layers called LatLong, they are titled "XYjoin":

// Build SQL to query against Pole Layer. XYjoin is Pole layer coordinates field; feature.XYjoin is the cable's coordinate field
var PoleXY = FeatureSetByName($map, "Pole", ['XYjoin'])
var PoleID = FeatureSetByName($map, "Pole", ['ID'])
var sql = Concatenate("PoleXY = ", $feature.XYjoin)

// Filter your pole layer get to only return features where the coordinate fields are equal between Cable and Pole
var features = Filter(FeatureSetByName($map, "Pole", ['XYjoin', 'ID']), sql)

// If return is not empty. get the first feature and build your return text
if (!IsEmpty(features)) {
  var PoleID = First(features)
  return {
    type : 'text'
    text : `This cables Start/End Pole is: ${PoleID}`
  }
}
 
Appreciate any additional insight you could offer.  Thank you!
0 Kudos
JohnEvans6
Regular Contributor

Knowing me I overcomplicated it. What about something simpler like

 

 

var pole_features = FeatureSetByName($map, "Pole", ['XYjoin','PoleID'])

for (var f in pole_features) {
  If (f.XYjoin == $feature.XYjoin) {
      return { 
           type : 'text', 
           text : `Pole ID is : ${f.PoleID}`
      }
  }
}

 

 

0 Kudos
Mowgli_GIS
Occasional Contributor

Hi @JohnEvans6 , it’s me again, sorry…..

I keep getting errors and I have a tough time understanding the syntax fully.  I’m not sure what I have wrong.  I've tried a bunch of different iterations, but below is the current iteration that I have.  The portions marked with yellow I’m not fully following what they represent and where those pieces are being pulled from.

What does f.XYjoin represent on line 8?

What do lines 10 & 11 represent/where is f.PoleID pulled from/represent?

I also provided the screen shots of the Profile variables

Mowgli_GIS_0-1708552502680.png

 

The string of code is being written within the pole_id field of the Riser Layer

Pole layer fields and info are being pulled from the $map feature service (see below)

Mowgli_GIS_1-1708552502682.png

Mowgli_GIS_2-1708552502682.png

The ID field marked below is the pole layer’s ID field from which I want to pull an attribute and insert into the pole_id field in the Riser layer based on the pole layer’s XYjoin field attribute matching the Riser layer’s XYjoin field attribute

Mowgli_GIS_3-1708552502683.png

Again, apologies for the repeated questions, and I appreciate if/when you’re able to provide any help & feedback.

 

THANK YOU!

0 Kudos
JohnEvans6
Regular Contributor

Get rid of your first 3 variables and just use PoleFeatures.

f is just variable that holds the data from each feature within PoleFeatres

${f.PoleID} should just be ${f.ID}, sorry. Hard to work with data I can't see.

If you put a Console(f) above the if statement, it will print out each feature within PoleFeatures as it iterates. You're sure the XYjoin field will match between the two data sets, yeah?

0 Kudos
Mowgli_GIS
Occasional Contributor

Hi @JohnEvans6 , I think we're close?  I adjusted based on your feedback and below is what came back:

Mowgli_GIS_0-1708632536906.png

I also confirmed that the XY values match in both layers for the specific features I used to test the code.

However, when I created the new Riser feature, below is what populated in the pole_id field for the Riser feature:

Mowgli_GIS_1-1708632686179.png

But what I want to populate is just the HSWG text, which is the pole feature's ID:

Mowgli_GIS_2-1708632964751.png

 

Sorry again for all the questions, and I appreciate all the feedback/help you've given!

 

 

0 Kudos
Mowgli_GIS
Occasional Contributor

@JohnEvans6 , I guess I replied a bit too quick, I tried one more thing and it worked!  Below is the code:

var PoleFeatures = FeatureSetByName($map, "Pole", ['PON','ID'])

for (var f in PoleFeatures) {
  if (f.PON == $feature.PON) {
return (f.ID)
  } else {
    return "test"
  }
}
 
I wouldn't have been able to figure this out or even get close without your help.  So again, THANK YOU!!
0 Kudos