Display a shapefile with custom symbology.
Use case
Feature layers created from shapefiles do not possess any rendering information, and will be assigned with a default symbology. You can apply custom styling to ensure that the content is visible and usable in the context of a specific map. For example, you could use this to visually differentiate between features originating from two different shapefiles, by applying a blue color to one, and a red color to the other.
How to use the sample
Toggle the switch to apply a new symbology renderer to the feature layer created from the shapefile.
How it works
- Create a
ShapefileFeatureTable
using the shapefile name. - Create a
FeatureLayer
and associate it with theShapefileFeatureTable
. - Create a
SimpleRenderer
to override the default symbology. The simple renderer takes a symbol and applies that to all features in a layer. - Apply the renderer to the
FeatureLayer
by setting the renderer.
Relevant API
- FeatureLayer
- ShapefileFeatureTable
- SimpleFillSymbol
- SimpleLineSymbol
- SimpleRenderer
About the data
This sample displays a shapefile containing subdivisions in Aurora, CO.
Additional information
While shapefiles contain no rendering information, other data sources such as Service Feature Tables or Geodatabase Feature Tables can contain such information. As a result, the rendering properties of the other data sources can be pre-defined by the author.
Tags
package, shape file, shapefile, symbology, visualization
Sample Code
// Copyright 2024 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 ApplySymbologyToShapefile extends StatefulWidget {
const ApplySymbologyToShapefile({super.key});
@override
State<ApplySymbologyToShapefile> createState() =>
_ApplySymbologyToShapefileState();
}
class _ApplySymbologyToShapefileState extends State<ApplySymbologyToShapefile>
with SampleStateSupport {
// Create a controller for the map view.
final _mapViewController = ArcGISMapView.createController();
// A flag for when the map view is ready and controls can be used.
var _ready = false;
// Hold reference to the feature layer so that its renderer can be changed when button is pushed.
late FeatureLayer _shapefileFeatureLayer;
// Hold reference to default renderer to enable switching back.
Renderer? _defaultRenderer;
// Hold reference to alternate renderer to enable switching.
SimpleRenderer? _alternateRenderer;
// Create variable for holding state relating to the renderer.
var _alternate = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: false,
left: false,
right: false,
child: Stack(
children: [
Column(
children: [
Expanded(
// Add a map view to the widget tree and set a controller.
child: ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Text('Alternate renderer'),
Switch(value: _alternate, onChanged: updateRenderer),
],
),
],
),
// Display a progress indicator and prevent interaction until state is ready.
LoadingIndicator(visible: !_ready),
],
),
),
);
}
Future<void> onMapViewReady() async {
// Create a map with the topographic basemap style and an initial viewpoint.
final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
map.initialViewpoint = Viewpoint.fromCenter(
ArcGISPoint(
x: -11662054,
y: 4818336,
spatialReference: SpatialReference(wkid: 3857),
),
scale: 200000,
);
// Download the sample data.
await downloadSampleData(['d98b3e5293834c5f852f13c569930caa']);
// Get the application documents directory.
final appDir = await getApplicationDocumentsDirectory();
// Get the Shapefile from the download resource.
final shapefile = File(
'${appDir.absolute.path}/Aurora_CO_shp/Subdivisions.shp',
);
// Create a feature table from the Shapefile URI.
final shapefileFeatureTable = ShapefileFeatureTable.withFileUri(
shapefile.uri,
);
// Create a feature layer for the Shapefile feature table.
_shapefileFeatureLayer = FeatureLayer.withFeatureTable(
shapefileFeatureTable,
);
// Clear the operational layers and add the feature layer to the map.
map.operationalLayers.clear();
map.operationalLayers.add(_shapefileFeatureLayer);
// Create the symbology for the alternate renderer.
final lineSymbol = SimpleLineSymbol(color: Colors.red);
final fillSymbol = SimpleFillSymbol(
color: Colors.yellow,
outline: lineSymbol,
);
// Create the alternate renderer.
_alternateRenderer = SimpleRenderer(symbol: fillSymbol);
// Wait for the layer to load so that it will be assigned a default renderer.
await _shapefileFeatureLayer.load();
// Hold a reference to the default renderer (to enable switching between the renderers).
_defaultRenderer = _shapefileFeatureLayer.renderer;
_mapViewController.arcGISMap = map;
// Set the ready state variable to true to enable the sample UI.
setState(() => _ready = true);
}
// Set the renderer.
void updateRenderer(bool alternate) {
setState(() => _alternate = alternate);
if (_alternate) {
_shapefileFeatureLayer.renderer = _alternateRenderer;
} else {
_shapefileFeatureLayer.renderer = _defaultRenderer;
}
}
}