Summarize length for pipelines segments with the same ID

538
2
Jump to solution
06-23-2022 02:19 AM
Adrian_Negulescu
New Contributor II

For a PipelineLeaks feature, I tried to summarize the length field from Intersected pipeline.

A pipeline can have many segments, but all have the same GIS_PIPE_ID. I tried to: intersect and find the GIS_PIPE_ID from intersected segment, then filter the pipeline feature by this ID and summarize the PIPE_LENGTH field 

This  expression have an error: 

Adrian_Negulescu_0-1655975900102.png

 

 

// This rule will populate the edited features field LENGTH with a sum of PIPE_LENGTH from selected pipelines with same GIS_PIPE_ID
// Value to copy from the intersected feature
var intersecting_field = "GIS_PIPE_ID"
//Field rule is assigned to in the Attribute Rule
var feature_field = "LENGTH"
// Create feature set to the intersecting class
var PipeToIntersect = FeatureSetByName($datastore, "Pipelines")
// Intersect the edited feature with the feature set and retrieve the first feature
var search_feature = buffer($feature,0.5,"meter")
var IntersectedPipe = First(Intersects(PipeToIntersect, search_feature))
//select pipelines by GIS_PIPE_ID
var ID = IntersectedPipe[intersecting_field]
var PipelinesFilteredByID = FeatureSetByName($datastore, "Pipelines",["GIS_PIPE_ID","LENGTH"],true)
var SelectAllSegmentsSameID = filter(PipelinesFilteredByID,ID)
// If this pipeline have many segments, return a sum of PIPE_LENGTH
var PipelineSegmentsNumber = Count(SelectAllSegmentsSameID)
if (PipelineSegmentsNumber > 1)
{
feature_field = Sum(SelectAllSegmentsSameID, "PIPE_LENGTH");
}
feature_field = round(IntersectedPipe["PIPE_LENGTH"],2)
return feature_field

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Reason for your error: The second argument of Filter is a SQL where clause, you only supply a value, not the where clause.

 

Other things:

You don't have to load the pipelines a second time.

Even if PipelineSegmentsNumber is 1, you can still just call Sum on that feature set. That makes the code easier (and your code would not give the correct result for multiple pipeline segments).

You load LENGTH but you want to get the sum of PIPE_LENGTH.

After you intersect the leak feature with the pipelines, you're gonna need a null check to make sure you got an intersecting pipeline.

The variable feature_field isn't used in your script.

The variable intersecting_field is used, but you type out GIS_PIPE_ID multiple times, so you could do it one more time...

 

// This rule will populate the edited features field LENGTH with a sum of PIPE_LENGTH from selected pipelines with same GIS_PIPE_ID

// Create feature set to the intersecting class
var Pipelines = FeatureSetByName($datastore, "Pipelines", ["GIS_PIPE_ID", "PIPE_LENGTH"], true)
// Intersect the edited feature with the feature set and retrieve the first feature
var search_feature = Buffer($feature, 0.5, "meter")
var IntersectedPipe = First(Intersects(Pipelines, search_feature))
// Return null if no pipeline was intersected
if(IntersectedPipe == null) {
    return null
    // or return an error message:
    //return {"errorMessage": "No pipeline was intersected"}
}
//select pipelines by GIS_PIPE_ID
var ID = IntersectedPipe.GIS_PIPE_ID
var SqlWhere = "GIS_PIPE_ID = @ID"
var SelectAllSegmentsSameID = Filter(Pipelines, SqlWhere)
// return the sum of PIPE_LENGTH
return Round(Sum(SelectAllSegmentsSameID, "PIPE_LENGTH"), 2)

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

Reason for your error: The second argument of Filter is a SQL where clause, you only supply a value, not the where clause.

 

Other things:

You don't have to load the pipelines a second time.

Even if PipelineSegmentsNumber is 1, you can still just call Sum on that feature set. That makes the code easier (and your code would not give the correct result for multiple pipeline segments).

You load LENGTH but you want to get the sum of PIPE_LENGTH.

After you intersect the leak feature with the pipelines, you're gonna need a null check to make sure you got an intersecting pipeline.

The variable feature_field isn't used in your script.

The variable intersecting_field is used, but you type out GIS_PIPE_ID multiple times, so you could do it one more time...

 

// This rule will populate the edited features field LENGTH with a sum of PIPE_LENGTH from selected pipelines with same GIS_PIPE_ID

// Create feature set to the intersecting class
var Pipelines = FeatureSetByName($datastore, "Pipelines", ["GIS_PIPE_ID", "PIPE_LENGTH"], true)
// Intersect the edited feature with the feature set and retrieve the first feature
var search_feature = Buffer($feature, 0.5, "meter")
var IntersectedPipe = First(Intersects(Pipelines, search_feature))
// Return null if no pipeline was intersected
if(IntersectedPipe == null) {
    return null
    // or return an error message:
    //return {"errorMessage": "No pipeline was intersected"}
}
//select pipelines by GIS_PIPE_ID
var ID = IntersectedPipe.GIS_PIPE_ID
var SqlWhere = "GIS_PIPE_ID = @ID"
var SelectAllSegmentsSameID = Filter(Pipelines, SqlWhere)
// return the sum of PIPE_LENGTH
return Round(Sum(SelectAllSegmentsSameID, "PIPE_LENGTH"), 2)

 


Have a great day!
Johannes
Adrian_Negulescu
New Contributor II

Thank you Johannes!

Now it works! 

0 Kudos