Skip To Content

Generalized data

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:esri="http://www.esri.com/2008/ags"
               pageTitle="FeatureLayer: Changing MaxAllowableOffset (based on zoom level)">
    <!--
    Description:
    This sample demonstrates how to use the FeatureLayer property maxAllowableOffset to generalize features for display.

    This property name has its roots in the ArcGIS Desktop generalization tools.
    For more technical details, please refer to the Wikipedia page for the Douglas-Peucker algorithm.
    If this is new terminology, refer to the ArcGIS Desktop Help for the Generalize tool.

    Feature Layers come with a property maxAllowableOffset that is used to simplify features on the fly.
    Since the web APIs fire an event when the map's extent/zoom level changes, you can listen for this event
    and use the property maxAllowableOffset to indicate an appropriate value.

    The recommended approach for generalizing features is that a feature's geometry should not display more than one vertex per pixel.
    The reasoning is that a pixel is the smallest unit for displays so displaying more than one vertex per pixel is wasted effort.

    Network Traffic:
    Comparison of the FeatureLayer maxAllowableOffset to load the "Fields" layer
    When this sample loads using the maxAllowableOffset property, the network traffic was approximately: 85.3KB
    When this sample does not use the maxAllowableOffset property, the network traffic was approximately: 1.2MB

    NOTE:
    One caveat: maxAllowableOffset is not applicable on layers that are editable.
    If editing a feature, the true geometry of that feature should be displayed, including all its participating vertices.
    Editing a generalized feature could inadvertently wipe out detailed information that was meticulously created.

    Documentation:
    For more information, see the API documentation.
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/layers/FeatureLayer.html
    https://developers.arcgis.com/en/flex/api-reference/com/esri/ags/layers/FeatureLayer.html#maxAllowableOffset
    -->

    <fx:Script>
        <![CDATA[
            import com.esri.ags.events.MapEvent;
            import com.esri.ags.events.ZoomEvent;

            private function map_loadHandler(event:MapEvent):void
            {
                featureLayer.maxAllowableOffset = getMaxAllowableOffset();
            }

            private function map_zoomEndHandler(event:ZoomEvent):void
            {
                featureLayer.maxAllowableOffset = getMaxAllowableOffset();
            }

            private function getMaxAllowableOffset(pixelTolerance:int = 1):int
            {
                return Math.floor(map.extent.width / map.width) * pixelTolerance;
            }
        ]]>
    </fx:Script>

    <s:controlBarLayout>
        <s:VerticalLayout gap="10"
                          paddingBottom="7"
                          paddingLeft="10"
                          paddingRight="10"
                          paddingTop="7"/>
    </s:controlBarLayout>
    <s:controlBarContent>
        <s:RichText width="100%">
            This sample demonstrates how to use the FeatureLayer property maxAllowableOffset to generalize features for display.
            Run this sample with Firebug's net panel open and notice that the maxAllowableOffset is different based on the zoom level.
        </s:RichText>
        <s:Label text="The current maxAllowableOffset is {featureLayer.maxAllowableOffset}"/>
    </s:controlBarContent>

    <esri:Map id="map"
              load="map_loadHandler(event)"
              zoomEnd="map_zoomEndHandler(event)">
        <esri:extent>
            <esri:WebMercatorExtent minlon="-104" minlat="36" maxlon="-95" maxlat="40"/>
        </esri:extent>
        <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
        <esri:FeatureLayer id="featureLayer" url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/1"/>
    </esri:Map>

</s:Application>