Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Service area

Sample Viewer View Sample on GitHub

Find the service area within a network from a given point.

Use case

A service area shows locations that can be reached from a facility based off a certain impedance, such as travel time or distance. Barriers can increase impedance by either adding to the time it takes to pass through the barrier or by altogether preventing passage.

You might calculate the region around a hospital in which ambulances can service in 30 min or less.

How to use sample

  • In order to find any services areas at least one ServiceAreaFaciltiy needs to be added.
  • To add a facility, select "Facility" from the combo-box then click anywhere on the MapView.
  • To add a barrier, select "Barrier" from the combo-box and click multiple locations on the MapView to draw a barrier.
  • To start a new line in a distinct location, click "new barrier"
  • To show service areas around facilities that were added, click the "Solve" button.
  • The "Reset" button, clears all graphics and resets the ServiceAreaTask.

How it works

  1. Create a ServiceAreaTask from an online service.
  2. Create default ServiceAreaParameters from the service area task.
  3. Set the parameters to return polygons (true) to return all service areas.
  4. Add a ServiceAreaFacility to the parameters.
  5. Get the ServiceAreaResult by solving the service area task using the parameters.
  6. Get any ServiceAreaPolygons that were returned using ServiceAreaResult.resultPolygons
  7. Display service areas to MapView by creating graphics for their geometry and adding to a graphics overlay.

Relevant API

  • PolylineBarrier
  • ServiceAreaFacility
  • ServiceAreaParameters
  • ServiceAreaPolygon
  • ServiceAreaResult
  • ServiceAreaTask

Tags

barriers, facilities, impedance, logistics, routing

Sample Code

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.ArcGISRuntime 100.9

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 800

    property bool busy: false
    property string message: ""
    property var barrierBuilder: null
    property var facilityParams: null

    // add a mapView component
    MapView {
        id: mapView
        anchors.fill: parent

        Map {
            BasemapStreets {}

            // set the initial viewpoint to San Diego
            initialViewpoint: ViewpointCenter {
                Point {
                    x: -13041154
                    y: 3858170
                    spatialReference: SpatialReference { wkid: 3857 }
                }
                targetScale: 1e5
            }

            onLoadStatusChanged: {
                task.load();
            }
        }

        GraphicsOverlay {
            id: areasOverlay
            opacity: 0.5

            SimpleRenderer {
                SimpleFillSymbol {
                    style: Enums.SimpleFillSymbolStyleSolid
                    color: "green"
                    outline: lineSymbol
                }
            }
        }

        GraphicsOverlay {
            id: facilitiesOverlay

            renderer: SimpleRenderer {
                symbol: PictureMarkerSymbol {
                    url: "https://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png"
                    height: 30
                    width: 30
                }
            }
        }

        GraphicsOverlay {
            id: barriersOverlay

            SimpleRenderer {
                SimpleLineSymbol {
                    id: lineSymbol
                    style: Enums.SimpleLineSymbolStyleSolid
                    color: "black"
                    width: 3.0
                }
            }
        }

        onMouseClicked: {
            if (busy === true)
                return;

            if (modeComboBox.currentText === "Facility") {
                const facilityGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: mouse.mapPoint});
                facilitiesOverlay.graphics.append(facilityGraphic);
            }
            else {
                handleBarrierPoint(mouse.mapPoint);
            }
        }

        Rectangle {
            anchors.centerIn: solveRow
            radius: 8
            height: solveRow.height + (16)
            width: solveRow.width + (16)
            color: "lightgrey"
            border.color: "darkgrey"
            border.width: 2
            opacity: 0.75
        }

        Row {
            id: solveRow
            anchors {
                bottom: mapView.attributionTop
                horizontalCenter: parent.horizontalCenter
                margins: 15
            }

            spacing: 8

            Button {
                id: serviceAreasButton
                text: "Solve"
                enabled: !busy

                onClicked: startSolveTask();
            }

            Button {
                text: "Reset"
                enabled: !busy
                onClicked: {
                    facilitiesOverlay.graphics.clear();
                    barriersOverlay.graphics.clear();
                    areasOverlay.graphics.clear();
                    barrierBuilder = null;
                }
            }
        }
    }

    ServiceAreaTask {
        id: task
        url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ServiceArea"

        onLoadStatusChanged: {
            if (loadStatus !== Enums.LoadStatusLoaded)
                return;

            setupRouting();
        }

        onCreateDefaultParametersStatusChanged: {
            if (createDefaultParametersStatus !== Enums.TaskStatusCompleted)
                return;

            busy = false;
            facilityParams = createDefaultParametersResult;
            facilityParams.outputSpatialReference = Factory.SpatialReference.createWebMercator();
            facilityParams.returnPolygonBarriers = true;
            facilityParams.polygonDetail = Enums.ServiceAreaPolygonDetailHigh;
        }

        onSolveServiceAreaStatusChanged: {
            if (solveServiceAreaStatus !== Enums.TaskStatusCompleted)
                return;

            busy = false;

            if (solveServiceAreaResult === null || solveServiceAreaResult.error)
            {
                message = "No service Areas calculated!";
                return;
            }

            const numFacilities = facilitiesOverlay.graphics.rowCount();
            for (let i = 0; i < numFacilities; i++) {
                const results = solveServiceAreaResult.resultPolygons(i);
                for (let j = 0; j < results.length; j++) {
                    const resultGeometry = results[j].geometry;
                    const resultGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: resultGeometry});
                    areasOverlay.graphics.append(resultGraphic);
                }
            }
        }
    }

    Rectangle {
        anchors.centerIn: editRow
        radius: 8
        height: editRow.height + (16)
        width: editRow.width + (16)
        color: "lightgrey"
        border.color: "darkgrey"
        border.width: 2
        opacity: 0.75
    }

    Row {
        id: editRow
        anchors {
            top: parent.top
            left: parent.left
            margins: 24
        }
        spacing: 8

        ComboBox {
            id: modeComboBox
            property int modelWidth: 0
            width: modelWidth + leftPadding + rightPadding + indicator.width
            model: ["Facility", "Barrier"]

            onCurrentTextChanged: {
                if (currentText !== "Barrier")
                    return;

                if (barrierBuilder === null)
                    createBarrierBuilder();
            }

            Component.onCompleted : {
                for (let i = 0; i < model.length; ++i) {
                    metrics.text = model[i];
                    modelWidth = Math.max(modelWidth, metrics.width);
                }
            }
            TextMetrics {
                id: metrics
                font: modeComboBox.font
            }
        }

        Button {
            id: newBarrierButton
            visible: modeComboBox.currentText === "Barrier"
            text: "new barrier"
            enabled: visible

            onClicked: {
                barrierBuilder = null;
                createBarrierBuilder();
                addBarrierGraphic();
            }
        }
    }

    BusyIndicator {
        anchors.centerIn: parent
        running: busy
    }

    Dialog {
        modal: true
        x: Math.round(parent.width - width) / 2
        y: Math.round(parent.height - height) / 2
        standardButtons: Dialog.Ok
        title: "Route Error"
        visible: text.length > 0
        property alias text : textLabel.text
        Text {
            id: textLabel
            text: message
        }
    }

    function setupRouting() {
        busy = true;
        message = "";
        task.createDefaultParameters();
    }

    function createBarrierBuilder() {
        barrierBuilder = ArcGISRuntimeEnvironment.createObject(
                    "PolylineBuilder", {spatialReference: Factory.SpatialReference.createWebMercator()})
    }

    function handleBarrierPoint(mapPoint) {
        barrierBuilder.addPoint(mapPoint);
        // update the geometry for the current barrier - or create 1 if it does not exist
        const barriersCount = barriersOverlay.graphics.rowCount();
        if (barriersCount > 0)
            barriersOverlay.graphics.get(barriersCount-1).geometry = barrierBuilder.geometry
        else
            addBarrierGraphic();
    }

    function addBarrierGraphic() {
        const barrierGraphic = ArcGISRuntimeEnvironment.createObject("Graphic", {geometry: barrierBuilder.geometry});
        barriersOverlay.graphics.append(barrierGraphic);
    }

    function startSolveTask() {
        facilityParams.clearFacilities();
        facilityParams.clearPolylineBarriers();

        if (facilitiesOverlay.graphics.rowCount() === 0) {
            message = "At least 1 Facility is required.";
            return;
        }

        busy = true;

        const facilities = [];
        facilitiesOverlay.graphics.forEach(graphic => facilities.push(ArcGISRuntimeEnvironment.createObject("ServiceAreaFacility", {
                                                                                                                geometry: graphic.geometry
                                                                                                            })));

        facilityParams.setFacilities(facilities);

        const barriers = [];
        barriersOverlay.graphics.forEach(graphic => barriers.push(ArcGISRuntimeEnvironment.createObject("PolylineBarrier", {
                                                                                                            geometry: graphic.geometry
                                                                                                        })));

        if (barriers.length > 0)
            facilityParams.setPolylineBarriers(barriers);

        task.solveServiceArea(facilityParams);
    }
}