Reclassifying certain values using ArcPy in ArcGIS Pro?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I need to reclassify certain values to different values in a raster using Python in ArcGIS Pro 2.2.2. I can't use reclassify because I don't want to change all values and I can't go through each one of them because there is a lot. When I use Raster calculator I get an attribute error.
Is there a way to do that?
arcpy spatial-analyst raster-calculator arcgis-pro reclassify
New contributor
add a comment |Â
up vote
1
down vote
favorite
I need to reclassify certain values to different values in a raster using Python in ArcGIS Pro 2.2.2. I can't use reclassify because I don't want to change all values and I can't go through each one of them because there is a lot. When I use Raster calculator I get an attribute error.
Is there a way to do that?
arcpy spatial-analyst raster-calculator arcgis-pro reclassify
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to reclassify certain values to different values in a raster using Python in ArcGIS Pro 2.2.2. I can't use reclassify because I don't want to change all values and I can't go through each one of them because there is a lot. When I use Raster calculator I get an attribute error.
Is there a way to do that?
arcpy spatial-analyst raster-calculator arcgis-pro reclassify
New contributor
I need to reclassify certain values to different values in a raster using Python in ArcGIS Pro 2.2.2. I can't use reclassify because I don't want to change all values and I can't go through each one of them because there is a lot. When I use Raster calculator I get an attribute error.
Is there a way to do that?
arcpy spatial-analyst raster-calculator arcgis-pro reclassify
arcpy spatial-analyst raster-calculator arcgis-pro reclassify
New contributor
New contributor
edited 20 mins ago
PolyGeoâ¦
51.9k1777233
51.9k1777233
New contributor
asked 2 hours ago
MJA
84
84
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You can use this approach Esri provide for ArcGIS Pro Reclassify
Reclassifies (or changes) the values in a raster.
I would suggest using the code samples they provide as guidance as to how you can build your python code to reclassify the values in your raster
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outReclass1 = Reclassify("landuse", "Value",
RemapValue([[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]]))
outReclass1.save("C:/sapyexamples/output/landuse_rcls")
outReclass2 = Reclassify("slope_grd", "Value",
RemapRange([[0,10,"NODATA"],[10,20,1],[20,30,2],
[30,40,3],[40,50,4],[50,60,5],[60,75,6]]))
outReclass2.save("C:/sapyexamples/output/slope_rcls")
outReclass3 = Reclassify("pop_density", "Value",
RemapRange([[10,10,1],[10,20,2],[20,25,3],
[25,50,4],[50,]]), "NODATA")
outReclass3.save("C:/sapyexamples/output/popden_rcls")
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
add a comment |Â
up vote
0
down vote
Edit: I added this solution before the ArcGIS Pro tag, but here's a GDAL/Numpy solution:
You can could use gdal and numpy to reclassify your raster.
First open the file and read as array:
arr = gdal.Open("/path/to/your/file").ReadAsArray()
Then use numpy to classify certain pixel ranges:
Example:
>>> arr = np.random.randint(100, size = (5,5))
>>> arr
array([[30, 74, 6, 4, 94],
[45, 13, 15, 5, 35],
[26, 48, 17, 30, 24],
[56, 24, 19, 11, 39],
[70, 18, 31, 59, 24]])
>>> arr[arr<50]= 0
>>> arr
array([[ 0, 74, 0, 0, 94],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[56, 0, 0, 0, 0],
[70, 0, 0, 59, 0]])
Then write the array back to file:
def array2raster(rasterfn,newRasterfn,array):
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
proj = raster.GetProjection()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) # Change dtype here
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRaster.SetProjection(proj)
outband.FlushCache()
array2raster("/path/to/raster.tif", "/path/to/newraster.tif", array)
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You can use this approach Esri provide for ArcGIS Pro Reclassify
Reclassifies (or changes) the values in a raster.
I would suggest using the code samples they provide as guidance as to how you can build your python code to reclassify the values in your raster
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outReclass1 = Reclassify("landuse", "Value",
RemapValue([[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]]))
outReclass1.save("C:/sapyexamples/output/landuse_rcls")
outReclass2 = Reclassify("slope_grd", "Value",
RemapRange([[0,10,"NODATA"],[10,20,1],[20,30,2],
[30,40,3],[40,50,4],[50,60,5],[60,75,6]]))
outReclass2.save("C:/sapyexamples/output/slope_rcls")
outReclass3 = Reclassify("pop_density", "Value",
RemapRange([[10,10,1],[10,20,2],[20,25,3],
[25,50,4],[50,]]), "NODATA")
outReclass3.save("C:/sapyexamples/output/popden_rcls")
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
add a comment |Â
up vote
3
down vote
accepted
You can use this approach Esri provide for ArcGIS Pro Reclassify
Reclassifies (or changes) the values in a raster.
I would suggest using the code samples they provide as guidance as to how you can build your python code to reclassify the values in your raster
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outReclass1 = Reclassify("landuse", "Value",
RemapValue([[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]]))
outReclass1.save("C:/sapyexamples/output/landuse_rcls")
outReclass2 = Reclassify("slope_grd", "Value",
RemapRange([[0,10,"NODATA"],[10,20,1],[20,30,2],
[30,40,3],[40,50,4],[50,60,5],[60,75,6]]))
outReclass2.save("C:/sapyexamples/output/slope_rcls")
outReclass3 = Reclassify("pop_density", "Value",
RemapRange([[10,10,1],[10,20,2],[20,25,3],
[25,50,4],[50,]]), "NODATA")
outReclass3.save("C:/sapyexamples/output/popden_rcls")
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You can use this approach Esri provide for ArcGIS Pro Reclassify
Reclassifies (or changes) the values in a raster.
I would suggest using the code samples they provide as guidance as to how you can build your python code to reclassify the values in your raster
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outReclass1 = Reclassify("landuse", "Value",
RemapValue([[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]]))
outReclass1.save("C:/sapyexamples/output/landuse_rcls")
outReclass2 = Reclassify("slope_grd", "Value",
RemapRange([[0,10,"NODATA"],[10,20,1],[20,30,2],
[30,40,3],[40,50,4],[50,60,5],[60,75,6]]))
outReclass2.save("C:/sapyexamples/output/slope_rcls")
outReclass3 = Reclassify("pop_density", "Value",
RemapRange([[10,10,1],[10,20,2],[20,25,3],
[25,50,4],[50,]]), "NODATA")
outReclass3.save("C:/sapyexamples/output/popden_rcls")
You can use this approach Esri provide for ArcGIS Pro Reclassify
Reclassifies (or changes) the values in a raster.
I would suggest using the code samples they provide as guidance as to how you can build your python code to reclassify the values in your raster
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outReclass1 = Reclassify("landuse", "Value",
RemapValue([[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]]))
outReclass1.save("C:/sapyexamples/output/landuse_rcls")
outReclass2 = Reclassify("slope_grd", "Value",
RemapRange([[0,10,"NODATA"],[10,20,1],[20,30,2],
[30,40,3],[40,50,4],[50,60,5],[60,75,6]]))
outReclass2.save("C:/sapyexamples/output/slope_rcls")
outReclass3 = Reclassify("pop_density", "Value",
RemapRange([[10,10,1],[10,20,2],[20,25,3],
[25,50,4],[50,]]), "NODATA")
outReclass3.save("C:/sapyexamples/output/popden_rcls")
answered 1 hour ago
whyzar
9,32292857
9,32292857
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
add a comment |Â
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification?
â MJA
1 hour ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner.
â MJA
47 mins ago
add a comment |Â
up vote
0
down vote
Edit: I added this solution before the ArcGIS Pro tag, but here's a GDAL/Numpy solution:
You can could use gdal and numpy to reclassify your raster.
First open the file and read as array:
arr = gdal.Open("/path/to/your/file").ReadAsArray()
Then use numpy to classify certain pixel ranges:
Example:
>>> arr = np.random.randint(100, size = (5,5))
>>> arr
array([[30, 74, 6, 4, 94],
[45, 13, 15, 5, 35],
[26, 48, 17, 30, 24],
[56, 24, 19, 11, 39],
[70, 18, 31, 59, 24]])
>>> arr[arr<50]= 0
>>> arr
array([[ 0, 74, 0, 0, 94],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[56, 0, 0, 0, 0],
[70, 0, 0, 59, 0]])
Then write the array back to file:
def array2raster(rasterfn,newRasterfn,array):
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
proj = raster.GetProjection()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) # Change dtype here
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRaster.SetProjection(proj)
outband.FlushCache()
array2raster("/path/to/raster.tif", "/path/to/newraster.tif", array)
add a comment |Â
up vote
0
down vote
Edit: I added this solution before the ArcGIS Pro tag, but here's a GDAL/Numpy solution:
You can could use gdal and numpy to reclassify your raster.
First open the file and read as array:
arr = gdal.Open("/path/to/your/file").ReadAsArray()
Then use numpy to classify certain pixel ranges:
Example:
>>> arr = np.random.randint(100, size = (5,5))
>>> arr
array([[30, 74, 6, 4, 94],
[45, 13, 15, 5, 35],
[26, 48, 17, 30, 24],
[56, 24, 19, 11, 39],
[70, 18, 31, 59, 24]])
>>> arr[arr<50]= 0
>>> arr
array([[ 0, 74, 0, 0, 94],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[56, 0, 0, 0, 0],
[70, 0, 0, 59, 0]])
Then write the array back to file:
def array2raster(rasterfn,newRasterfn,array):
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
proj = raster.GetProjection()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) # Change dtype here
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRaster.SetProjection(proj)
outband.FlushCache()
array2raster("/path/to/raster.tif", "/path/to/newraster.tif", array)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Edit: I added this solution before the ArcGIS Pro tag, but here's a GDAL/Numpy solution:
You can could use gdal and numpy to reclassify your raster.
First open the file and read as array:
arr = gdal.Open("/path/to/your/file").ReadAsArray()
Then use numpy to classify certain pixel ranges:
Example:
>>> arr = np.random.randint(100, size = (5,5))
>>> arr
array([[30, 74, 6, 4, 94],
[45, 13, 15, 5, 35],
[26, 48, 17, 30, 24],
[56, 24, 19, 11, 39],
[70, 18, 31, 59, 24]])
>>> arr[arr<50]= 0
>>> arr
array([[ 0, 74, 0, 0, 94],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[56, 0, 0, 0, 0],
[70, 0, 0, 59, 0]])
Then write the array back to file:
def array2raster(rasterfn,newRasterfn,array):
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
proj = raster.GetProjection()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) # Change dtype here
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRaster.SetProjection(proj)
outband.FlushCache()
array2raster("/path/to/raster.tif", "/path/to/newraster.tif", array)
Edit: I added this solution before the ArcGIS Pro tag, but here's a GDAL/Numpy solution:
You can could use gdal and numpy to reclassify your raster.
First open the file and read as array:
arr = gdal.Open("/path/to/your/file").ReadAsArray()
Then use numpy to classify certain pixel ranges:
Example:
>>> arr = np.random.randint(100, size = (5,5))
>>> arr
array([[30, 74, 6, 4, 94],
[45, 13, 15, 5, 35],
[26, 48, 17, 30, 24],
[56, 24, 19, 11, 39],
[70, 18, 31, 59, 24]])
>>> arr[arr<50]= 0
>>> arr
array([[ 0, 74, 0, 0, 94],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[56, 0, 0, 0, 0],
[70, 0, 0, 59, 0]])
Then write the array back to file:
def array2raster(rasterfn,newRasterfn,array):
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
proj = raster.GetProjection()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) # Change dtype here
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(1)
outband.WriteArray(array)
outRaster.SetProjection(proj)
outband.FlushCache()
array2raster("/path/to/raster.tif", "/path/to/newraster.tif", array)
answered 1 hour ago
kash
364
364
add a comment |Â
add a comment |Â
MJA is a new contributor. Be nice, and check out our Code of Conduct.
MJA is a new contributor. Be nice, and check out our Code of Conduct.
MJA is a new contributor. Be nice, and check out our Code of Conduct.
MJA is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f296580%2freclassifying-certain-values-using-arcpy-in-arcgis-pro%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password