Apply function to raster from file

View on GitHub

Apply a raster function to a local raster file and display the output with a raster layer.

Image of apply function to raster from file

Use case

Raster functions allow processing operations that can be applied to one or more rasters on the fly. A land survey agency may apply hillshade and aspect functions to rasters with elevation data in order to better determine the topography of a landscape and to make further planning decisions.

How to use the sample

Load the sample to see a raster function applied to a raster.

How it works

  1. Create a RasterFunction using the path to a local raster function JSON file.
  2. Set the raster function arguments as required by the function used.
  3. Use the raster function to create a new Raster.
  4. Set the raster to a RasterLayer and display it in the Map.

Relevant API

  • Raster
  • RasterFunction
  • RasterLayer

Offline data

This sample uses the Shasta_Elevation.tif raster and the color.json raster function.

About the data

The raster function in the provided JSON file blends a color image with a greyscale image (in this case, a raster image containing elevation data), to add a hillshade effect to the input raster.

Additional information

Learn more about raster functions in the ArcGIS Pro documentation.

Tags

analysis, function, image, layer, processing, raster, raster function, transformation

Sample Code

apply_function_to_raster_from_file.dart
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2025 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import 'dart:io';

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

class ApplyFunctionToRasterFromFile extends StatefulWidget {
  const ApplyFunctionToRasterFromFile({super.key});

  @override
  State<ApplyFunctionToRasterFromFile> createState() =>
      _ApplyFunctionToRasterFromFileState();
}

class _ApplyFunctionToRasterFromFileState
    extends State<ApplyFunctionToRasterFromFile>
    with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();
  // Raster layer to display raster data on the map.
  late RasterLayer _rasterLayer;
  // A flag for when the map view is ready and controls can be used.
  var _ready = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          ArcGISMapView(
            controllerProvider: () => _mapViewController,
            onMapViewReady: onMapViewReady,
          ),
          // Display a progress indicator and prevent interaction until state is ready.
          LoadingIndicator(visible: !_ready),
        ],
      ),
    );
  }

  Future<void> onMapViewReady() async {
    // Create a map with a standard imagery basemap style.
    final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISImageryStandard);
    // Add map to the map view.
    _mapViewController.arcGISMap = map;
    // Load the raster layer.
    await loadRasterLayer();
    // Add the raster layer to the map.
    map.operationalLayers.add(_rasterLayer);
    // Set the viewpoint to the center of the raster layer's full extent.
    final fullExtent = _rasterLayer.fullExtent;
    if (fullExtent != null) {
      final center = fullExtent.center;
      const scale = 80000.0;
      await _mapViewController.setViewpointCenter(center, scale: scale);
    }
    // Set the ready state variable to true to enable the sample UI.
    setState(() => _ready = true);
  }

  Future<void> loadRasterLayer() async {
    // Download the raster file and the color json file.
    await downloadSampleData([
      'b051f5c3e01048f3bf11c59b41507896',
      '5356dbf91788474493467519e268cf87',
    ]);
    // Get the application documents directory.
    final appDir = await getApplicationDocumentsDirectory();
    // Create and load a Raster from the local tif file.
    final shastaElevationRaster = Raster.withFileUri(
      Uri.file('${appDir.absolute.path}/Shasta_Elevation/Shasta_Elevation.tif'),
    );
    await shastaElevationRaster.load();
    // Load the color configuration from the JSON file located in the app's directory.
    final file = File('${appDir.absolute.path}/color.json');
    final rasterColorJson = await file.readAsString();

    // Create a RasterFunction.
    final rasterFunction = RasterFunction.fromJson(rasterColorJson);
    if (rasterFunction != null) {
      final arguments = rasterFunction.arguments;
      if (arguments != null) {
        final rasterNames = arguments.rasterNames;
        // Set the raster function arguments as required by the function used.
        arguments.setRaster(
          name: rasterNames[0],
          raster: shastaElevationRaster,
        );
        arguments.setRaster(
          name: rasterNames[1],
          raster: shastaElevationRaster,
        );
        // Create a Raster from the raster function.
        final raster = Raster.withFunction(rasterFunction);
        // Load the Raster Layer.
        _rasterLayer = RasterLayer.withRaster(raster);
        _rasterLayer.opacity = 0.5;
        await _rasterLayer.load();
      }
    }
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.