ArcGIS for Android 100.3.0(16):RasterLayer(栅格图层)

栅格图层(RasterLayer)

用以加载展示移动端本地文件、移动镶嵌数据集、影像服务。

api地址

https://developers.arcgis.com/android/latest/api-reference/reference/com/esri/arcgisruntime/layers/RasterLayer.html

1.影像服务
   mMapView = (MapView) findViewById(R.id.mapView);
        ArcGISMap map = new ArcGISMap(Basemap.createDarkGrayCanvasVector());
        mMapView.setMap(map);
        // create image service raster as raster layer
        final ImageServiceRaster imageServiceRaster = new ImageServiceRaster(image_service_url);
        final RasterLayer rasterLayer = new RasterLayer(imageServiceRaster);
        // add raster layer as map operational layer
        map.getOperationalLayers().add(rasterLayer);
        // zoom to the extent of the raster service
        rasterLayer.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                if (rasterLayer.getLoadStatus() == LoadStatus.LOADED) {
                    // get the center point
                    Point centerPnt = imageServiceRaster.getServiceInfo().getFullExtent().getCenter();
                    mMapView.setViewpointCenterAsync(centerPnt, 55000000);
                }
            }
        });
    }
2.栅格文件
 Raster raster = new Raster(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xhk.tif");
        // create a raster layer
        final RasterLayer rasterLayer = new RasterLayer(raster);
        ArcGISMap map = new ArcGISMap(Basemap.createImagery());
        mMapView.setMap(map);
        map.getOperationalLayers().add(rasterLayer);
        // set viewpoint on the raster
        rasterLayer.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                mMapView.setViewpointGeometryAsync(rasterLayer.getFullExtent(), 50);
            }
        });
3.GeoPackage
 GeoPackage geoPackage = new GeoPackage(Environment.getExternalStorageDirectory() + "/ArcGIS/Samples/GeoPackage/AuroraCO.gpkg";
 geoPackage.loadAsync();
 geoPackage.addDoneLoadingListener(() -> {
            if (geoPackage.getLoadStatus() == LoadStatus.LOADED) {
                if (!geoPackage.getGeoPackageRasters().isEmpty()) {
                    // read raster images and get the first one
                    Raster geoPackageRaster = geoPackage.getGeoPackageRasters().get(0);

                    // create a layer to show the raster
                    RasterLayer geoPackageRasterLayer = new RasterLayer(geoPackageRaster);

                    // add the image as a raster layer to the map (with default symbology)
                    mMapView.getMap().getOperationalLayers().add(geoPackageRasterLayer);

                } else {
                    String emptyMessage = "No rasters found in this GeoPackage!";
                    Toast.makeText(RasterLayerGeopackageActivity.this, emptyMessage, Toast.LENGTH_LONG).show();
                    Log.e(TAG, emptyMessage);
                }
            } else {
                String error = "GeoPackage failed to load!";
                Toast.makeText(RasterLayerGeopackageActivity.this, error, Toast.LENGTH_LONG).show();
                Log.e(TAG, error);
            }
        });
4.移动镶嵌数据集(MosaicDatasetRaster)
String rasterFilePath = "/path/to/mosaic.sqlite";
mainMapView = (MapView) findViewById(R.id.mainMapView);
 ArcGISMap mainArcGISMap = new ArcGISMap(Basemap.createTopographic());
mainMapView.setMap(mainArcGISMap);
List<String> mainMosaicDatasetRasterNames = MosaicDatasetRaster.getNames(rasterFilePath);
                for (String name: mainMosaicDatasetRasterNames
                     ) {
                    MosaicDatasetRaster mainMosaicDatasetRaster = new MosaicDatasetRaster(urlOrPath,name);
                    RasterLayer mainMDRasterLayer = new RasterLayer(mainMosaicDatasetRaster);
                    mainArcGISMap.getOperationalLayers().add(mainMDRasterLayer);
                }
5.RenderingRuleInfo(渲染规则)

用以定义如何对请求的影像进行渲染和处理。

它可以从服务中定义的名称创建,也可以使用获得getRenderingRuleInfos()。它可以用来构建一个RenderingRule。

通过ImageServiceRaster,获取RenderingRuleInfo,进一步得到Rendering Rule。

下面给出官方案例:

public class RasterRenderingRuleActivity extends AppCompatActivity {

    private MapView mMapView;

    private ArcGISMap map;

    private String image_service_url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/CharlotteLAS/ImageServer";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_raster_rendering_rule);

        mMapView = findViewById(R.id.mapView);

        map = new ArcGISMap(Basemap.createStreets());
        mMapView.setMap(map);

        // create image service raster as raster layer and add to map
        final ImageServiceRaster imageServiceRaster = new ImageServiceRaster(image_service_url);
        final RasterLayer imageRasterLayer = new RasterLayer(imageServiceRaster);
        map.getOperationalLayers().add(imageRasterLayer);

        Spinner spinner = findViewById(R.id.spinner);
        final List renderRulesList = new ArrayList<>();
        final ArrayAdapter spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, renderRulesList);
        spinner.setAdapter(spinnerAdapter);
        // zoom to the extent of the raster service
        imageRasterLayer.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                if (imageRasterLayer.getLoadStatus() == LoadStatus.LOADED) {
                    // zoom to extent of raster
                    mMapView.setViewpointGeometryAsync(imageServiceRaster.getServiceInfo().getFullExtent());
                    // get the predefined rendering rules and add to spinner
                    List renderingRuleInfos = imageServiceRaster.getServiceInfo().getRenderingRuleInfos();
                    for (RenderingRuleInfo renderRuleInfo : renderingRuleInfos) {
                        String renderingRuleName = renderRuleInfo.getName();
                        renderRulesList.add(renderingRuleName);
                        // update array adapter with list update
                        spinnerAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

        // listen to the spinner
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int position, long id) {
                applyRenderingRule(imageServiceRaster, position);
            }

            @Override
            public void onNothingSelected(AdapterView adapterView) {
                Log.d("MainActivity", "Spinner nothing selected");
            }
        });

    }

    /**
     * Apply a rendering rule on a Raster and add it to the map
     *
     * @param imageServiceRaster image service raster to apply rendering on
     * @param index              spinner selected position representing the rule to apply
     */
    private void applyRenderingRule(ImageServiceRaster imageServiceRaster, int index) {
        // clear all rasters
        map.getOperationalLayers().clear();
        // get the rendering rule info at the selected index
        RenderingRuleInfo renderRuleInfo = imageServiceRaster.getServiceInfo().getRenderingRuleInfos().get(index);
        // create a rendering rule object using the rendering rule info
        RenderingRule renderingRule = new RenderingRule(renderRuleInfo);
        // create a new image service raster
        ImageServiceRaster appliedImageServiceRaster = new ImageServiceRaster(image_service_url);
        // apply the rendering rule
        appliedImageServiceRaster.setRenderingRule(renderingRule);
        // create a raster layer using the image service raster
        RasterLayer rasterLayer = new RasterLayer(appliedImageServiceRaster);
        // add the raster layer to the map
        map.getOperationalLayers().add(rasterLayer);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.dispose();
    }
}

通过Json设置渲染规则:

// create rendering rule
RenderingRule renderingRule = new RenderingRule("{\"rasterFunction\" : \"Hillshade\",\"rasterFunctionArguments\" : {\"Azimuth\" : 215.0,\"Altitude\" : 75.0,\"ZFactor\" 未完
// get a property of rendering rule
renderingRule.getRenderingRuleJson();
// set on image service raster
serviceRaster.setRenderingRule(renderingRule);
// create a raster layer
RasterLayer layer = new RasterLayer(serviceRaster);
6.RasterFunction(定义加载)

RasterFunction是针对Raster结合展现的方法进而呈现不同渲染的影像,本质上不改变源数据。

案例:

通过RasterFunction定义山体阴影,得到新的RasterLayer

/**
 * RasterFunction定义加载
 * RasterFunction是针对Raster结合展现的方法进而呈现不同渲染的影像,本质上不改变源数据。
 */
public class RasterFunctionServiceActivity extends AppCompatActivity {


    private final String TAG = MainActivity.class.getSimpleName();

    private MapView mMapView;

    private Button mRasterFunctionButton;

    private String image_service_raster_url="http://sampleserver6.arcgisonline.com/arcgis/rest/services/NLCDLandCover2001/ImageServer";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_raster_function_service);

        mMapView = findViewById(R.id.mapView);
        mRasterFunctionButton = findViewById(R.id.rasterButton);
        mRasterFunctionButton.setEnabled(false);

        // create a map with the BasemapType topographic
        ArcGISMap map = new ArcGISMap(Basemap.createDarkGrayCanvasVector());
        final ImageServiceRaster imageServiceRaster = new ImageServiceRaster(image_service_raster_url);
        final RasterLayer imageRasterLayer = new RasterLayer(imageServiceRaster);
        map.getOperationalLayers().add(imageRasterLayer);

        // zoom to the extent of the raster service
        imageRasterLayer.addDoneLoadingListener(() -> {
            if (imageRasterLayer.getLoadStatus() == LoadStatus.LOADED) {
                // get the center point
                Point centerPnt = imageServiceRaster.getServiceInfo().getFullExtent().getCenter();
                mMapView.setViewpointCenterAsync(centerPnt, 55000000);
                mRasterFunctionButton.setEnabled(true);
            } else {
                String error = "Error loading image raster layer: " + imageRasterLayer.getLoadError();
                Log.e(TAG, error);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
            }
        });

        // 点击按钮:通过RasterFunction定义山体阴影,得到新的RasterLayer
        mRasterFunctionButton.setOnClickListener(v -> applyRasterFunction(imageServiceRaster));

        // set the map to be displayed in this view
        mMapView.setMap(map);
    }

    private void applyRasterFunction(Raster raster) {
        // 通过Json设置渲染规则
        RasterFunction rasterFuntionFromJson = RasterFunction.fromJson(getString(R.string.hillshade_simplified));
        // get parameter name value pairs used by hillside
        RasterFunctionArguments rasterFunctionArguments = rasterFuntionFromJson.getArguments();
        // get a list of raster names associated with the raster function
        List rasterNames = rasterFunctionArguments.getRasterNames();
        rasterFunctionArguments.setRaster(rasterNames.get(0), raster);
        // create raster as raster layer
        raster = new Raster(rasterFuntionFromJson);
        RasterLayer hillshadeLayer = new RasterLayer(raster);
        mMapView.getMap().getOperationalLayers().add(hillshadeLayer);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.dispose();
    }
}

json字符串:


        {
        \"raster_function_arguments\":
        {
        \"z_factor\":{\"double\":25.0,\"type\":\"Raster_function_variable\"},
        \"slope_type\":{\"raster_slope_type\":\"none\",\"type\":\"Raster_function_variable\"},
        \"azimuth\":{\"double\":315,\"type\":\"Raster_function_variable\"},
        \"altitude\":{\"double\":45,\"type\":\"Raster_function_variable\"},
        \"type\":\"Raster_function_arguments\",
        \"raster\":{\"name\":\"raster\",\"is_raster\":true,\"type\":\"Raster_function_variable\"},
        \"nbits\":{\"int\":8,\"type\":\"Raster_function_variable\"}
        },
        \"raster_function\":{\"type\":\"Hillshade_function\"},
        \"type\":\"Raster_function_template\"
        }
    

栅格渲染器(RasterRenderer)

RasterRenderer能够对栅格图层( RasterLayer )进行展示和可视化。目前包括以下内容:

  1. HillshadeRenderer:山体阴影渲染
  2. BlendRenderer:混合渲染
  3. ColormapRenderer :色彩映射表渲染
  4. StretchRenderer:拉伸渲染
  5. RGBRenderer:RGB 渲染

这些渲染器官方例子中都有,有兴趣的可以自己去下载,下面就来看看官方案例.

HillshadeRenderer(山体阴影渲染)

主要针对数字高程模型、单波段栅格数据,通过设置太阳的方位角和高出地平线的高度(角度或坡度),创建一个灰度级的3D高程表面。

构造方法1:

altitude:太阳高度角,默认45°
azimuth:太阳方位角,默认315°
zFactor:垂直方向的拉伸夸张程度

HillshadeRenderer(double altitude, double azimuth, double zFactor)

构造方法2:

HillshadeRenderer(double altitude, double azimuth, double zFactor, SlopeType slopeType, double pixelSizeFactor, double pixelSizePower, int outputBitDepth) 

使用:

mainMapView =(MapView) findViewById(R.id.mapView);
mainMapView.setAttributionTextVisible(false);
ArcGISMap mainArcGISMap = new ArcGISMap(new Basemap());
mainMapView.setMap(mainArcGISMap);
String rasterFilePath = getResources().getString(R.string.raster_file_path);
Raster mainRasterFile = new Raster(rasterFilePath);
mainRasterLayer = new RasterLayer(mainRasterFile);
mAltitude = 45;
mAzimuth = 315;
mZFactor = 0.000016;
mSlopeType = SlopeType.NONE;
mPixelSizeFactor = 1;
mPixelSizePower = 1;
mOutputBitDepth = 8;
HillshadeRenderer hillshadeRenderer = new HillshadeRenderer(mAltitude, mAzimuth,
        mZFactor, mSlopeType, mPixelSizeFactor, mPixelSizePower, mOutputBitDepth);
mainRasterLayer.setRasterRenderer(hillshadeRenderer);
mainArcGISMap.getOperationalLayers().add(mainRasterLayer);

BlendRenderer(混合渲染)

将一张山体阴影的影像(栅格Raster),混合入要渲染的原始栅格,让原始栅格看起来具备地形阴影效果。

mainMapView =(MapView) findViewById(R.id.mapView);
mainMapView.setAttributionTextVisible(false);
ArcGISMap mainArcGISMap = new ArcGISMap(new Basemap());
mainMapView.setMap(mainArcGISMap);
String rasterFilePath = getResources().getString(R.string.raster_file_path);
Raster mainRasterFile = new Raster(rasterFilePath);
String rasterDEMFilePath = getResources().getString(R.string.elevation_file_path);
mainDEMRaster = new Raster(rasterDEMFilePath);
mainRasterLayer = new RasterLayer(mainRasterFile);
mAltitude = 45;
mAzimuth = 315;
mZFactor = 1.5;
mSlopeType = SlopeType.NONE;
//ColorRamp.PresetType不能设置为NONE,不然会报错
mColorRampType = ColorRamp.PresetType.DEM_LIGHT;
mPixelSizeFactor = 1;
mPixelSizePower = 1;
mOutputBitDepth = 8;
ColorRamp colorRamp = new ColorRamp(mColorRampType, 800);
BlendRenderer blendRenderer = new BlendRenderer(
        mainDEMRaster,
        Collections.singletonList(9.0),
        Collections.singletonList(255.0),
        null,
        null,
        null,
        null,
        colorRamp,
        mAltitude,
        mAzimuth,
        mZFactor,
        mSlopeType,
        mPixelSizeFactor,
        mPixelSizePower,
        mOutputBitDepth);
mainRasterLayer.setRasterRenderer(blendRenderer);
mainArcGISMap.getOperationalLayers().add(mainRasterLayer);
mainRasterLayer.loadAsync();
mainRasterLayer.addDoneLoadingListener(new Runnable() {
    @Override
    public void run() {
        mainMapView.setViewpointGeometryAsync(mainRasterLayer.getFullExtent(), 50);
    }
});

ColormapRenderer(色彩映射表渲染)

定义使用颜色映射的渲染器。如果希望栅格图层中的值由指定颜色表示,则可以使用此渲染器。

通过提供栅格像素值的离散映射颜色,所有像素匹配指定的值使用映射呈现颜色。这可以用于土地分类等任务。

  private void loadRaster() {
        // create a raster from a local raster file
        Raster raster = new Raster(buildRasterPath());
        // create a raster layer
        final RasterLayer rasterLayer = new RasterLayer(raster);
        // create a Map with imagery basemap
        ArcGISMap map = new ArcGISMap(Basemap.createImagery());
        // add the map to a map view
        mMapView.setMap(map);
        // add the raster as an operational layer
        map.getOperationalLayers().add(rasterLayer);
        // create a color map where values 0-149 are red (Color.RED) and 150-250 are yellow (Color.Yellow)
        List colors = new ArrayList<>();
        for (int i = 0; i <= 250; i++) {
            if (i < 150) {
                colors.add(i, Color.RED);
            } else {
                colors.add(i, Color.YELLOW);
            }
        }
        // create a colormap renderer
        ColormapRenderer colormapRenderer = new ColormapRenderer(colors);
        // set the ColormapRenderer on the RasterLayer
        rasterLayer.setRasterRenderer(colormapRenderer);
        // set Viewpoint on the Raster
        rasterLayer.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                if (rasterLayer.getLoadStatus() == LoadStatus.LOADED) {
                    mMapView.setViewpointGeometryAsync(rasterLayer.getFullExtent(), 50);
                } else {
                    String error = "RasterLayer failed to load: " + rasterLayer.getLoadError().getMessage();
                    Log.e(TAG, error);
                    Toast.makeText(ColormapRendererActivity.this, error, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

  private String buildRasterPath() {
        // get sdcard resource name
        File extStorDir = Environment.getExternalStorageDirectory();
        // get the directory
        String extSDCardDirName = "ArcGIS/samples/raster";
        // get raster filename
        String filename = "ShastaBW";
        // create the full path to the raster file
        return extStorDir.getAbsolutePath()
                + File.separator
                + extSDCardDirName
                + File.separator
                + filename
                + ".tif";
    }

StretchRenderer(拉伸渲染)

用于以平滑渐变的颜色显示连续的栅格像元值。使用“拉伸”渲染器来绘制单波段的连续数据。该方法非常适合于诸如影像、航空像片或高程模型等要显示的像元值位于较大范围的栅格数据。

mainMapView = (MapView) findViewById(R.id.mapView);
mainMapView.setAttributionTextVisible(false);
ArcGISMap mainArcGISMap = new ArcGISMap(new Basemap());
mainMapView.setMap(mainArcGISMap);
String rasterFilePath = getResources().getString(R.string.raster_file_path);
Raster mainRasterFile = new Raster(rasterFilePath);
mainRasterLayer = new RasterLayer(mainRasterFile);
mMin = 0;
mMax = 255;
mPercentClipMin = 0;
mPercentClipMax = 99;
mStdDevFactor = 1;
MinMaxStretchParameters stretchParameters = new MinMaxStretchParameters(
        Collections.singletonList((double) mMin),
        Collections.singletonList((double) mMax));
PercentClipStretchParameters stretcPChParameters = new PercentClipStretchParameters(mPercentClipMin, mPercentClipMax);
StretchRenderer stretchRenderer = new StretchRenderer(stretchParameters, Collections.singletonList((double) 1.5), true, null);
mainRasterLayer.setRasterRenderer(stretchRenderer);
mainArcGISMap.getOperationalLayers().add(mainRasterLayer);
mainRasterLayer.loadAsync();
mainRasterLayer.addDoneLoadingListener(new Runnable() {
    @Override
    public void run() {
        mainMapView.setViewpointGeometryAsync(mainRasterLayer.getFullExtent(), 50);
    }
});

RGBRenderer(RGB 渲染)

RGB 渲染与拉伸渲染使用相同的方法,但前者允许以“红、绿、蓝”合成方式组合多个波段。在使用多波段栅格数据集(如卫星或航空影像)时,可以使用RGB 渲染来显示不同的波段组合。

mainMapView = (MapView) findViewById(R.id.mapView);
mainMapView.setAttributionTextVisible(false);
ArcGISMap mainArcGISMap = new ArcGISMap(new Basemap());
mainMapView.setMap(mainArcGISMap);
String rasterFilePath = getResources().getString(R.string.raster_file_path);
Raster mainRasterFile = new Raster(rasterFilePath);
mainRasterLayer = new RasterLayer(mainRasterFile);
mMinR = 0;
mMaxR = 255;
mMinG = 0;
mMaxG = 255;
mMinB = 0;
mMaxB = 255;
mPercentClipMin = 0;
mPercentClipMax = 99;
mStdDevFactor = 1;
MinMaxStretchParameters stretchParameters = new MinMaxStretchParameters(
        Arrays.asList((double) mMinR, (double) mMinG, (double) mMinB),
        Arrays.asList((double) mMaxR, (double) mMaxG, (double) mMaxB));
RGBRenderer rgbRenderer = new RGBRenderer(stretchParameters, Arrays.asList(0, 1, 2), null, true);
mainRasterLayer.setRasterRenderer(rgbRenderer);
mainArcGISMap.getOperationalLayers().add(mainRasterLayer);
mainRasterLayer.loadAsync();
mainRasterLayer.addDoneLoadingListener(new Runnable() {
    @Override
    public void run() {
        mainMapView.setViewpointGeometryAsync(mainRasterLayer.getFullExtent(), 50);
    }
});

你可能感兴趣的:(ArcGIS,for,Android,100,RasterL,ArcGIS,for,Android,RasterLayer)