Reclassifying certain values using ArcPy in ArcGIS Pro?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question









New contributor




MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    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?










    share|improve this question









    New contributor




    MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      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?










      share|improve this question









      New contributor




      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 20 mins ago









      PolyGeo♦

      51.9k1777233




      51.9k1777233






      New contributor




      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      MJA

      84




      84




      New contributor




      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      MJA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          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")





          share|improve this answer




















          • 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

















          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)





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "79"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            MJA is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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






























            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")





            share|improve this answer




















            • 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














            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")





            share|improve this answer




















            • 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












            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")





            share|improve this answer












            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")






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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












            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)





            share|improve this answer
























              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)





              share|improve this answer






















                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)





                share|improve this answer












                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)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                kash

                364




                364




















                    MJA is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    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.













                     


                    draft saved


                    draft discarded














                    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













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    What does second last employer means? [closed]

                    One-line joke