代码进行重分类:
# -*- coding: utf-8 -*-
import os
import sys
import arcpy
from arcpy import env
from arcpy.sa import *
# Reclass_ways:"EQUAL_INTERVAL" "EQUAL_AREA" "NATURAL_BREAKS"
def raster_reclass(inRaster, file_inRaster, Reclass_ways, baseOutputZone, numberZones,Save_path):
# inRaster = "elevation"
# numberZones = 10
# baseOutputZone = 5
# Reclass_ways:"EQUAL_INTERVAL" "EQUAL_AREA" "NATURAL_BREAKS"
# Name: Slice_Ex_02.py
# Description: Slices a range of values of the input cells of a raster by
# zones of equal interval or equal area.
# Requirements: Spatial Analyst Extension.
# Import system modules
# import arcpy
# from arcpy import env
# from arcpy.sa import *
# Set environment settings
env.workspace = file_inRaster
# Set local variables
# inRaster = "quality_c_ex"
# numberZones = 10
# baseOutputZone = 2
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial") # 这个是必须的
# Reclass_ways = "NATURAL_BREAKS"
# Execute Slice
outSlice = Slice(inRaster, numberZones, Reclass_ways, baseOutputZone)
# EQUAL_INTERVAL — Determines the range of the input values and divides the range into the specified number of
# output zones. Each zone on the sliced output raster has the potential of having input cell values that have the
# same range from the extremes.
# EQUAL_AREA — Specifies that the input values will be divided into the specified number of output zones, with each
# zone having a similar number of cells. Each zone will represent a similar amount of area.
# NATURAL_BREAKS — Specifies that the classes will be based on natural groupings inherent in the data. Break points
# are identified by choosing the class breaks that best group similar values and that maximize the differences
# between classes. The cell values are divided into classes whose boundaries are set when there are relatively big
# jumps in the data values.
# 等面积似乎不太对
# Save the output
outSlice.save(Save_path) # 当输入到文件夹为浮点型,但是输入到文件夹则为整形
raster_reclass("quality_c_ex5000_int",r"D:\arcpy_temp\myfgdb.gdb","EQUAL_AREA",1,2,r"D:\arcpy_temp\myfgdb.gdb\quality_c_ex5000_int3")
这里有三种分类方式,即Reclass_ways:“EQUAL_INTERVAL” “EQUAL_AREA” “NATURAL_BREAKS”。我更多用到的是等面积来进行分类。但是进行等面积分类的时候,这里也不太智能。
当数据表是这样的时候,它不会把它一个值分开,本来想分10个值,最后它只分了八个值。我探讨了一下,这里应该是一个值很多个它是不会分开的,这导致了最后分出来的等面积并不是等面积,还是有些差距很大。(我觉得一个好的方法就是把它们相同的值弄成一个值。)
上面是我自己写的代码,下面是我参考的help,大家要是看不懂上面的代码,可以看下面的帮助。
Slice (Spatial Analyst)
ArcGIS 10.2
License Level:BasicStandardAdvanced
Summary
Slices or reclassifies the range of values of the input cells into zones of equal interval, equal area, or by natural breaks.
Usage
- Slice works best on data that is normally distributed. When using input raster data that is skewed the output result may not contain all of the classes that you had expected or specified.
- If an environment [Mask](JavaScript:IDASDOLB.Click()) has been set, those cells that have been masked will receive NoData on the output slice raster.
- When using the EQUAL_AREA method, sometimes not all of the output zones (classes) will have an equal, or even similar, number of cells (i.e., area). This may be an inherent result based on the nature of the input values and the specified number of zones. If the results are deemed undesirable, you can try using a fewer number of zones or applying a statistics transformation (e.g., logarithm, square root, and so on) to the input dataset.
Syntax
Slice (in_raster, number_zones, {slice_type}, {base_output_zone})
Parameter | Explanation | Data Type |
---|---|---|
in_raster | The input raster to be reclassified. | Raster Layer |
number_zones | The number of zones to reclassify the input raster into. When the slice method is EQUAL_AREA, the output raster will have the defined number of zones, with a similar number of cells in each. When EQUAL_INTERVAL is used, the output raster will have the defined number of zones, each containing equal value ranges on the output raster. When NATURAL_BREAKS is used, the output raster will have the defined number of zones, with the number of cells in each determined by the class breaks. | Long |
slice_type (Optional) | The manner in which to slice the values in the input raster. EQUAL_INTERVAL — Determines the range of the input values and divides the range into the specified number of output zones. Each zone on the sliced output raster has the potential of having input cell values that have the same range from the extremes. EQUAL_AREA — Specifies that the input values will be divided into the specified number of output zones, with each zone having a similar number of cells. Each zone will represent a similar amount of area. NATURAL_BREAKS — Specifies that the classes will be based on natural groupings inherent in the data. Break points are identified by choosing the class breaks that best group similar values and that maximize the differences between classes. The cell values are divided into classes whose boundaries are set when there are relatively big jumps in the data values. | String |
base_output_zone (Optional) | Defines the lowest zone value on the output raster dataset. The default value is 1. | Long |
Return Value
Name | Explanation | Data Type |
---|---|---|
out_raster | The output reclassified raster. The output will always be of integer type. | Raster |
Code Sample
Slice example 1 (Python window)
Reclassify the input raster into five classes based on natural groupings inherent in the data.
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outslice = Slice("elevation", 5, "NATURAL_BREAKS")
outslice.save("C:/sapyexamples/output/elev_slice")
Slice example 2 (stand-alone script)
Reclassify the input raster into ten classes based on natural groupings inherent in the data.
# Name: Slice_Ex_02.py
# Description: Slices a range of values of the input cells of a raster by
# zones of equal interval or equal area.
# Requirements: Spatial Analyst Extension
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
# Set environment settings
env.workspace = "C:/sapyexamples/data"
# Set local variables
inRaster = "elevation"
numberZones = 10
baseOutputZone = 5
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute Slice
outSlice = Slice(inRaster, numberZones, "NATURAL_BREAKS", baseOutputZone)
# Save the output
outSlice.save("C:/sapyexamples/output/outslice")