Routing with added/scaled cost barriers

272
1
Jump to solution
11-21-2023 05:47 PM
Yik-ChingTsui
New Contributor III

[Repost of https://community.esri.com/t5/arcgis-experience-builder-questions/routing-with-added-scaled-cost-bar... from the Experience Builder community]

 

Hello All,

I'm trying to get routing working using the JS REST API. It will be used in a custom widget in experience builder. I got point barriers working with PointBarrier, but I need it to be added cost and it ignores everything I try. The ultimate goal is actually to use polyline barriers with scaled cost, but point barriers are simpler and neither worked.

Here is the code I have:

 

 

 

// variables from elsewhere
const vars = {
    // start and end point
    start, end,
    // JimuMapView
    jmv
}

const url = 'https://my-portal.com/server/rest/services/NetworkAnalysis/NAServer/Route'

const stops = new Collection([
  new Stop({
    geometry: vars.start
  }),
  new Stop({
    geometry: vars.end
  }),
])

const point = new Point({
  x: 114.168562,
  y: 22.322603
});

const start = [
  114.16853662192821, 22.323295447135724
]
const end = [
  114.16890644904946, 22.32143923097987
]
const path = [start, end]
const polyline = new Polyline({
  paths: [path]
})

const params1 = new RouteParameters({
  stops,
  pointBarriers: new Collection([
    new PointBarrier({
      geometry: point
    })
  ]),
//   pointBarriers: new Collection([
//     new NetworkFeatureSet({
//       features: [
//         new Graphic({
//           geometry: point,
//         })
//       ]
//     })
//   ]),
//   polylineBarriers: new Collection([
//     new NetworkFeatureSet({
//       features: [
//         new Graphic({
//           geometry: polyline,
//         })
//       ],
//     })
//  ])
})

route.solve(url, params1).then(r => {
  const res = r.routeResults[0].route
  console.log('params1')
  console.log(r)

  const layer = new FeatureLayer({
    title: 'Route',
    source: [res],
    objectIdField: 'ObjectID',
    fields: [
      {
        name: 'ObjectID',
        type: 'oid'
      }
    ],
    renderer: {
      type: 'simple',
      symbol: {
        type: 'simple-line',
        color: 'green',
        width: '2px'
      },
    }
  })

  vars.jmv.view.map.add(layer)
})

 

 

 

That code has a working point barrier, but if the commented-out point barrier is used instead, it ignores the point barrier.   

I use NetworkFeatureSet instead of PointBarrier as the former can carry attribute data, which should allow me to add cost to the barriers. (Or is there an alternative way to specify the cost for barrier?). I didn't specify attributes yet for the above code, because the simpler case of a completely restricted barrier isn't even working.

The second commented out code is the polyline barrier attempt using NetworkFeatureSet, which doesn't work at all. The RouteSolveResult has null for the polylineBarrier and pointBarrier when using NetworkFeatureSet, so it outright ignores it.

I don't need to use NetworkFeatureSet, I'll accept anything that can make routing work with added/scaled costs barriers. Thank you very much. Please let me know if I can provide additional context

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Yik-ChingTsui
New Contributor III

I got it to work in ArcGIS Pro, and then applied it to JS. For polyline barriers, I just used a FeatureSet containing an array of graphics. Each graph has the attributes BarrierType=1, Attr_Minutes, Attr_Length. Minutes and Length are the names of the costs

View solution in original post

0 Kudos
1 Reply
Yik-ChingTsui
New Contributor III

I got it to work in ArcGIS Pro, and then applied it to JS. For polyline barriers, I just used a FeatureSet containing an array of graphics. Each graph has the attributes BarrierType=1, Attr_Minutes, Attr_Length. Minutes and Length are the names of the costs

0 Kudos