I have a raster data of 1992 and a 'shapefile' of Los Angeles county. I want to reclassify the pixel value of the raster data set to 'no data' or zero in the region where the raster and the 'shapefile' overlaps. I'm using ArcGIS 10.1. I tried using spatial analyst tool> 'reclass'. But I'm not getting the desired result.
[GIS] How to change the pixel value of a raster dataset which overlaps a polygon featured shapefile in ArcGIS
arcgis-10.0rastershapefilespatial-analyst
Related Solutions
Agreeing with relima, but just adding a little more detail to the first step. You need to convert your polygons into rasters (make sure that you set the raster extents to be sufficiently large that they include all of the polygons). When converting poygons to rasters, you can choose what the raster value is. One easy way to keep track of the various intersections could be to set the raster value to 1, 2, 4 (which is 2^0, 2^1, 2^2; Whuber pointed out in a comment that using powers of 2 is necessary, as my previous example of using 1,2,3 could have had an ambiguous solution). Then when you add raster 1 and 4 together (using spatial data) the intersection will have a value of 5 which is only due to combining raster 1 and raster 4.
Something to watch out for in this process is the 'NoData' values. By default ArcGIS will make any area that do not contain data to the value of 'NoData'. 'NoData' when added to '0' results in 'NoData'. You may need to reclassify your values of 'NoData' to a value of '0'.
Following @Dango's idea I created and tested (on small rasters with the same extent and cell size) the following code:
import arcpy, numpy
inRaster = r"C:\tmp\RastersArray.gdb\InRaster"
inRaster2 = r"C:\tmp\RastersArray.gdb\InRaster2"
##Get properties of the input raster
inRasterDesc = arcpy.Describe(inRaster)
#coordinates of the lower left corner
rasXmin = inRasterDesc.Extent.Xmin
rasYmin = inRasterDesc.Extent.Ymin
# Cell size, raster size
rasMeanCellHeight = inRasterDesc.MeanCellHeight
rasMeanCellWidth = inRasterDesc.MeanCellWidth
rasHeight = inRasterDesc.Height
rasWidth = inRasterDesc.Width
##Calculate coordinates basing on raster properties
#create numpy array of coordinates of cell centroids
def rasCentrX(rasHeight, rasWidth):
coordX = rasXmin + (0.5*rasMeanCellWidth + rasWidth)
return coordX
inRasterCoordX = numpy.fromfunction(rasCentrX, (rasHeight,rasWidth)) #numpy array of X coord
def rasCentrY(rasHeight, rasWidth):
coordY = rasYmin + (0.5*rasMeanCellHeight + rasHeight)
return coordY
inRasterCoordY = numpy.fromfunction(rasCentrY, (rasHeight,rasWidth)) #numpy array of Y coord
#combine arrays of coordinates (although array for Y is before X, dstack produces [X, Y] pairs)
inRasterCoordinates = numpy.dstack((inRasterCoordY,inRasterCoordX))
##Raster conversion to NumPy Array
#create NumPy array from input rasters
inRasterArrayTopLeft = arcpy.RasterToNumPyArray(inRaster)
inRasterArrayTopLeft2 = arcpy.RasterToNumPyArray(inRaster2)
#flip array upside down - then lower left corner cells has the same index as cells in coordinates array
inRasterArray = numpy.flipud(inRasterArrayTopLeft)
inRasterArray2 = numpy.flipud(inRasterArrayTopLeft2)
# combine coordinates and value
inRasterFullArray = numpy.dstack((inRasterCoordinates, inRasterArray.T))
#add values from second raster
rasterValuesArray = numpy.dstack((inRasterFullArray, inRasterArray2.T))
Based on @hmfly code, you can have access to desired values:
(height, width, dim )=rasterValuesArray.shape
for row in range(0,height):
for col in range(0,width):
#now you have access to single array of values for one cell location
Unfortunately there's one 'but' - the code is right for NumPy arrays which can be handled by system memory. For my system (8GB), the largest array was about 9000,9000.
As my experience doesn't let me provide more help, you can consider some suggestions about dealing wiht large arrays: https://stackoverflow.com/questions/1053928/python-numpy-very-large-matrices
arcpy.RasterToNumPyArray
method allows to specify the subset of raster converted to NumPy array (ArcGIS10 help page) what can be useful when chunking large dataset into submatrices.
Best Answer
I'd try the following methodology to get your result:
Create a new field (NO_DATA) in your vector file ("No Data Layer") for your no data values records, for example "-9999".
Convert your polygon ("No Data Layer") to raster, selecting the same pixel size of the raster data to overlap and the pixel value NO_DATA.
Link
Mosaic both raster dataset to a new raster, putting your "No Data Layer" on the top of the list and select the option "mosaic_colormap_mode" to FIRST: The pixel value from the first raster dataset in the list will be applied to the output raster mosaic.
Link
In your new raster, reclasiffy your No data value (for instance, -9999) to "No data"
Link