rasterio “invalid dtype: 'bool'”Python, GDAL, VRT: help wth code efficiency and processing timeGDAL (or other open-source) equivalent of Arc's Point To Raster ToolCalculating NDVI with RasterioReading raster files by block with rasterio?First time rasterio errorUpdate geotiff metadata with rasterioRasterio write_band output has no CRSRasterizing by attribute in rasterioCannot upscale raster with rasterioStoring image with rasterio as gtiff modifies image histogram

What actually is "unallocated space"?

First author doesn't want a co-author to read the whole paper

Labeling lines that are not within polygons using field calculator

Russian Caesar cipher

Company indirectly discriminating against introverts, specifically INTJ

Make mapping that accepts count independent of line count

Who discovered the covering homomorphism between SU(2) and SO(3)?

What is this "very, very powerful article" that Trump is referring to vis à vis the Kurds?

Did the US push the Kurds to lower their defences against Turkey in the months preceding the latest Turkish military operation against them?

I run daily 5kms but I cant seem to improve stamina when playing soccer

Car as a good investment

Why were germanium diodes so fast and germanium transisters so slow?

How can the mourner remarry within one month, if he has to wait two regalim?

Is it allowed to let the engine of an aircraft idle without a pilot in the plane. (For both helicopters and aeroplanes)

How can I make a smooth transition from being a Black-Box Tester to an expert Automation Engineer?

How much does freezing grapes longer sweeten them more?

Why is Mars cold?

Did I Traumatize My Puppy?

Is it unusual that English uses possessive for past tense?

Did it take 3 minutes to reload a musket when the second amendment to the US constitution was ratified?

If we should encrypt the message rather than the method of transfer, why do we care about wifi security? Is this just security theatre?

Digit Date Range

If you revoke a certificate authority's certificate, do all of the certificates it issued become invalid as well?

Can elves trance in armor without any downsides?



rasterio “invalid dtype: 'bool'”


Python, GDAL, VRT: help wth code efficiency and processing timeGDAL (or other open-source) equivalent of Arc's Point To Raster ToolCalculating NDVI with RasterioReading raster files by block with rasterio?First time rasterio errorUpdate geotiff metadata with rasterioRasterio write_band output has no CRSRasterizing by attribute in rasterioCannot upscale raster with rasterioStoring image with rasterio as gtiff modifies image histogram






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









1

















I want to write a binary raster to disk. I have the numpy array whose dtype is:



dtype('bool')


I try to open a GTiff for writing:



img_output = rasterio.open(
"../../binary_output.tif",
'w',
driver='GTiff',
nodata=nd,
height=self.raster.height,
width = self.raster.width,
count=1,
dtype = binary_raster.dtype,
crs=self.raster.crs,
transform=self.raster.transform,)


But I end up with:



TypeError: invalid dtype: 'bool'


I have also tried things like:



dtype = bool
dtype = 'bool'
dtype = "bool"


What is the problem with this? According to the docs, dtype can be any numpy dtype. How do I write a binary raster?










share|improve this question
































    1

















    I want to write a binary raster to disk. I have the numpy array whose dtype is:



    dtype('bool')


    I try to open a GTiff for writing:



    img_output = rasterio.open(
    "../../binary_output.tif",
    'w',
    driver='GTiff',
    nodata=nd,
    height=self.raster.height,
    width = self.raster.width,
    count=1,
    dtype = binary_raster.dtype,
    crs=self.raster.crs,
    transform=self.raster.transform,)


    But I end up with:



    TypeError: invalid dtype: 'bool'


    I have also tried things like:



    dtype = bool
    dtype = 'bool'
    dtype = "bool"


    What is the problem with this? According to the docs, dtype can be any numpy dtype. How do I write a binary raster?










    share|improve this question




























      1












      1








      1








      I want to write a binary raster to disk. I have the numpy array whose dtype is:



      dtype('bool')


      I try to open a GTiff for writing:



      img_output = rasterio.open(
      "../../binary_output.tif",
      'w',
      driver='GTiff',
      nodata=nd,
      height=self.raster.height,
      width = self.raster.width,
      count=1,
      dtype = binary_raster.dtype,
      crs=self.raster.crs,
      transform=self.raster.transform,)


      But I end up with:



      TypeError: invalid dtype: 'bool'


      I have also tried things like:



      dtype = bool
      dtype = 'bool'
      dtype = "bool"


      What is the problem with this? According to the docs, dtype can be any numpy dtype. How do I write a binary raster?










      share|improve this question















      I want to write a binary raster to disk. I have the numpy array whose dtype is:



      dtype('bool')


      I try to open a GTiff for writing:



      img_output = rasterio.open(
      "../../binary_output.tif",
      'w',
      driver='GTiff',
      nodata=nd,
      height=self.raster.height,
      width = self.raster.width,
      count=1,
      dtype = binary_raster.dtype,
      crs=self.raster.crs,
      transform=self.raster.transform,)


      But I end up with:



      TypeError: invalid dtype: 'bool'


      I have also tried things like:



      dtype = bool
      dtype = 'bool'
      dtype = "bool"


      What is the problem with this? According to the docs, dtype can be any numpy dtype. How do I write a binary raster?







      python raster gdal rasterio






      share|improve this question














      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      Jan PislJan Pisl

      848 bronze badges




      848 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          3


















          If you call rasterio.dtypes.check_dtype(np.bool_) you'll see that it's not a known dtype, because gdal doesn't support a true 1-bit dtype. GDT_Byte is the smallest. The list that rasterio is checking against is:



          dtype_fwd = 
          0: None, # GDT_Unknown
          1: ubyte, # GDT_Byte
          2: uint16, # GDT_UInt16
          3: int16, # GDT_Int16
          4: uint32, # GDT_UInt32
          5: int32, # GDT_Int32
          6: float32, # GDT_Float32
          7: float64, # GDT_Float64
          8: complex_, # GDT_CInt16
          9: complex_, # GDT_CInt32
          10: complex64, # GDT_CFloat32
          11: complex128 # GDT_CFloat64


          uint8 and int8 are mapped to the ubyte, as seen here:



          https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py#L29-L45






          share|improve this answer


























          • ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

            – Jan Pisl
            7 hours ago











          • i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

            – Jan Pisl
            7 hours ago






          • 2





            I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

            – mikewatt
            6 hours ago







          • 1





            This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

            – user2856
            4 hours ago











          • Oh neat, I never noticed that creation option

            – mikewatt
            3 hours ago


















          2


















          Yes, you can write a one bit raster with rasterio*.



          You need to:



          1. write to a format that supports a 1bit dataset, such as GeoTIFF;

          2. ensure your numpy array is np.uint8/ubyte so rasterio doesnt raise the TypeError: invalid dtype: 'bool' exception; and

          3. pass the NBITS=1 creation option to tell the underlying GDAL GeoTIFF driver to create a one bit file.


          import numpy as np
          import rasterio as rio

          with rio.open('test_byte.tif') as src:
          data = src.read()
          profile = src.profile

          with rio.open('test_bit.tif', 'w', nbits=1, **profile) as dst:
          dst.write(data)
          # If your array is not a byte dtype, you need to cast it as ubyte/uint8
          # dst.write(data.astype(np.uint8))



          $ ls -sh test_bit.tif
          228K test_bit.tif

          $ ls -sh test_byte.tif
          1.8M test_byte.tif

          $ gdalinfo test_bit.tif
          Driver: GTiff/GeoTIFF
          Files: test_bit.tif
          Size is 1588, 1167
          <snip...>
          Band 1 Block=1588x41 Type=Byte, ColorInterp=Palette
          Image Structure Metadata:
          NBITS=1

          $ gdalinfo test_byte.tif
          Driver: GTiff/GeoTIFF
          Files: test_byte.tif
          Size is 1588, 1167
          <snip...>
          Band 1 Block=1588x5 Type=Byte, ColorInterp=Gray


          * whether or not any software other than GDAL based can read it, I don't know...






          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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f338410%2frasterio-invalid-dtype-bool%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown


























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3


















            If you call rasterio.dtypes.check_dtype(np.bool_) you'll see that it's not a known dtype, because gdal doesn't support a true 1-bit dtype. GDT_Byte is the smallest. The list that rasterio is checking against is:



            dtype_fwd = 
            0: None, # GDT_Unknown
            1: ubyte, # GDT_Byte
            2: uint16, # GDT_UInt16
            3: int16, # GDT_Int16
            4: uint32, # GDT_UInt32
            5: int32, # GDT_Int32
            6: float32, # GDT_Float32
            7: float64, # GDT_Float64
            8: complex_, # GDT_CInt16
            9: complex_, # GDT_CInt32
            10: complex64, # GDT_CFloat32
            11: complex128 # GDT_CFloat64


            uint8 and int8 are mapped to the ubyte, as seen here:



            https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py#L29-L45






            share|improve this answer


























            • ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

              – Jan Pisl
              7 hours ago











            • i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

              – Jan Pisl
              7 hours ago






            • 2





              I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

              – mikewatt
              6 hours ago







            • 1





              This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

              – user2856
              4 hours ago











            • Oh neat, I never noticed that creation option

              – mikewatt
              3 hours ago















            3


















            If you call rasterio.dtypes.check_dtype(np.bool_) you'll see that it's not a known dtype, because gdal doesn't support a true 1-bit dtype. GDT_Byte is the smallest. The list that rasterio is checking against is:



            dtype_fwd = 
            0: None, # GDT_Unknown
            1: ubyte, # GDT_Byte
            2: uint16, # GDT_UInt16
            3: int16, # GDT_Int16
            4: uint32, # GDT_UInt32
            5: int32, # GDT_Int32
            6: float32, # GDT_Float32
            7: float64, # GDT_Float64
            8: complex_, # GDT_CInt16
            9: complex_, # GDT_CInt32
            10: complex64, # GDT_CFloat32
            11: complex128 # GDT_CFloat64


            uint8 and int8 are mapped to the ubyte, as seen here:



            https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py#L29-L45






            share|improve this answer


























            • ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

              – Jan Pisl
              7 hours ago











            • i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

              – Jan Pisl
              7 hours ago






            • 2





              I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

              – mikewatt
              6 hours ago







            • 1





              This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

              – user2856
              4 hours ago











            • Oh neat, I never noticed that creation option

              – mikewatt
              3 hours ago













            3














            3










            3









            If you call rasterio.dtypes.check_dtype(np.bool_) you'll see that it's not a known dtype, because gdal doesn't support a true 1-bit dtype. GDT_Byte is the smallest. The list that rasterio is checking against is:



            dtype_fwd = 
            0: None, # GDT_Unknown
            1: ubyte, # GDT_Byte
            2: uint16, # GDT_UInt16
            3: int16, # GDT_Int16
            4: uint32, # GDT_UInt32
            5: int32, # GDT_Int32
            6: float32, # GDT_Float32
            7: float64, # GDT_Float64
            8: complex_, # GDT_CInt16
            9: complex_, # GDT_CInt32
            10: complex64, # GDT_CFloat32
            11: complex128 # GDT_CFloat64


            uint8 and int8 are mapped to the ubyte, as seen here:



            https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py#L29-L45






            share|improve this answer














            If you call rasterio.dtypes.check_dtype(np.bool_) you'll see that it's not a known dtype, because gdal doesn't support a true 1-bit dtype. GDT_Byte is the smallest. The list that rasterio is checking against is:



            dtype_fwd = 
            0: None, # GDT_Unknown
            1: ubyte, # GDT_Byte
            2: uint16, # GDT_UInt16
            3: int16, # GDT_Int16
            4: uint32, # GDT_UInt32
            5: int32, # GDT_Int32
            6: float32, # GDT_Float32
            7: float64, # GDT_Float64
            8: complex_, # GDT_CInt16
            9: complex_, # GDT_CInt32
            10: complex64, # GDT_CFloat32
            11: complex128 # GDT_CFloat64


            uint8 and int8 are mapped to the ubyte, as seen here:



            https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py#L29-L45







            share|improve this answer













            share|improve this answer




            share|improve this answer










            answered 7 hours ago









            mikewattmikewatt

            1,3343 silver badges12 bronze badges




            1,3343 silver badges12 bronze badges















            • ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

              – Jan Pisl
              7 hours ago











            • i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

              – Jan Pisl
              7 hours ago






            • 2





              I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

              – mikewatt
              6 hours ago







            • 1





              This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

              – user2856
              4 hours ago











            • Oh neat, I never noticed that creation option

              – mikewatt
              3 hours ago

















            • ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

              – Jan Pisl
              7 hours ago











            • i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

              – Jan Pisl
              7 hours ago






            • 2





              I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

              – mikewatt
              6 hours ago







            • 1





              This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

              – user2856
              4 hours ago











            • Oh neat, I never noticed that creation option

              – mikewatt
              3 hours ago
















            ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

            – Jan Pisl
            7 hours ago





            ah, okay, thanks. do you perhaps have any suggestion how to store a binary raster then?

            – Jan Pisl
            7 hours ago













            i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

            – Jan Pisl
            7 hours ago





            i might be missing something but it seems very strange to me that bool dtype is not an option. surely, storing a boolean raster must be a common task.

            – Jan Pisl
            7 hours ago




            2




            2





            I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

            – mikewatt
            6 hours ago






            I'd just save it as a byte raster only containing the values 0,1. You can use your_array.astype(np.uint8) before saving it out, and then if you load it later you can do the reverse if necessary, your_array.astype(np.bool_). I'm not sure of the exact reasons behind gdal not having a 1-bit data type, but it potentially it has to do with the fact that a byte is the minimum addressable unit of data on most computer systems, so it's common to use a whole byte to store a boolean value. Doing so is less memory efficient but more computationally efficient.

            – mikewatt
            6 hours ago





            1




            1





            This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

            – user2856
            4 hours ago





            This is generally correct, except that the underlying GDAL GeoTIFF driver can write datasets with nbits < 8, see my answer for details.

            – user2856
            4 hours ago













            Oh neat, I never noticed that creation option

            – mikewatt
            3 hours ago





            Oh neat, I never noticed that creation option

            – mikewatt
            3 hours ago













            2


















            Yes, you can write a one bit raster with rasterio*.



            You need to:



            1. write to a format that supports a 1bit dataset, such as GeoTIFF;

            2. ensure your numpy array is np.uint8/ubyte so rasterio doesnt raise the TypeError: invalid dtype: 'bool' exception; and

            3. pass the NBITS=1 creation option to tell the underlying GDAL GeoTIFF driver to create a one bit file.


            import numpy as np
            import rasterio as rio

            with rio.open('test_byte.tif') as src:
            data = src.read()
            profile = src.profile

            with rio.open('test_bit.tif', 'w', nbits=1, **profile) as dst:
            dst.write(data)
            # If your array is not a byte dtype, you need to cast it as ubyte/uint8
            # dst.write(data.astype(np.uint8))



            $ ls -sh test_bit.tif
            228K test_bit.tif

            $ ls -sh test_byte.tif
            1.8M test_byte.tif

            $ gdalinfo test_bit.tif
            Driver: GTiff/GeoTIFF
            Files: test_bit.tif
            Size is 1588, 1167
            <snip...>
            Band 1 Block=1588x41 Type=Byte, ColorInterp=Palette
            Image Structure Metadata:
            NBITS=1

            $ gdalinfo test_byte.tif
            Driver: GTiff/GeoTIFF
            Files: test_byte.tif
            Size is 1588, 1167
            <snip...>
            Band 1 Block=1588x5 Type=Byte, ColorInterp=Gray


            * whether or not any software other than GDAL based can read it, I don't know...






            share|improve this answer
































              2


















              Yes, you can write a one bit raster with rasterio*.



              You need to:



              1. write to a format that supports a 1bit dataset, such as GeoTIFF;

              2. ensure your numpy array is np.uint8/ubyte so rasterio doesnt raise the TypeError: invalid dtype: 'bool' exception; and

              3. pass the NBITS=1 creation option to tell the underlying GDAL GeoTIFF driver to create a one bit file.


              import numpy as np
              import rasterio as rio

              with rio.open('test_byte.tif') as src:
              data = src.read()
              profile = src.profile

              with rio.open('test_bit.tif', 'w', nbits=1, **profile) as dst:
              dst.write(data)
              # If your array is not a byte dtype, you need to cast it as ubyte/uint8
              # dst.write(data.astype(np.uint8))



              $ ls -sh test_bit.tif
              228K test_bit.tif

              $ ls -sh test_byte.tif
              1.8M test_byte.tif

              $ gdalinfo test_bit.tif
              Driver: GTiff/GeoTIFF
              Files: test_bit.tif
              Size is 1588, 1167
              <snip...>
              Band 1 Block=1588x41 Type=Byte, ColorInterp=Palette
              Image Structure Metadata:
              NBITS=1

              $ gdalinfo test_byte.tif
              Driver: GTiff/GeoTIFF
              Files: test_byte.tif
              Size is 1588, 1167
              <snip...>
              Band 1 Block=1588x5 Type=Byte, ColorInterp=Gray


              * whether or not any software other than GDAL based can read it, I don't know...






              share|improve this answer






























                2














                2










                2









                Yes, you can write a one bit raster with rasterio*.



                You need to:



                1. write to a format that supports a 1bit dataset, such as GeoTIFF;

                2. ensure your numpy array is np.uint8/ubyte so rasterio doesnt raise the TypeError: invalid dtype: 'bool' exception; and

                3. pass the NBITS=1 creation option to tell the underlying GDAL GeoTIFF driver to create a one bit file.


                import numpy as np
                import rasterio as rio

                with rio.open('test_byte.tif') as src:
                data = src.read()
                profile = src.profile

                with rio.open('test_bit.tif', 'w', nbits=1, **profile) as dst:
                dst.write(data)
                # If your array is not a byte dtype, you need to cast it as ubyte/uint8
                # dst.write(data.astype(np.uint8))



                $ ls -sh test_bit.tif
                228K test_bit.tif

                $ ls -sh test_byte.tif
                1.8M test_byte.tif

                $ gdalinfo test_bit.tif
                Driver: GTiff/GeoTIFF
                Files: test_bit.tif
                Size is 1588, 1167
                <snip...>
                Band 1 Block=1588x41 Type=Byte, ColorInterp=Palette
                Image Structure Metadata:
                NBITS=1

                $ gdalinfo test_byte.tif
                Driver: GTiff/GeoTIFF
                Files: test_byte.tif
                Size is 1588, 1167
                <snip...>
                Band 1 Block=1588x5 Type=Byte, ColorInterp=Gray


                * whether or not any software other than GDAL based can read it, I don't know...






                share|improve this answer
















                Yes, you can write a one bit raster with rasterio*.



                You need to:



                1. write to a format that supports a 1bit dataset, such as GeoTIFF;

                2. ensure your numpy array is np.uint8/ubyte so rasterio doesnt raise the TypeError: invalid dtype: 'bool' exception; and

                3. pass the NBITS=1 creation option to tell the underlying GDAL GeoTIFF driver to create a one bit file.


                import numpy as np
                import rasterio as rio

                with rio.open('test_byte.tif') as src:
                data = src.read()
                profile = src.profile

                with rio.open('test_bit.tif', 'w', nbits=1, **profile) as dst:
                dst.write(data)
                # If your array is not a byte dtype, you need to cast it as ubyte/uint8
                # dst.write(data.astype(np.uint8))



                $ ls -sh test_bit.tif
                228K test_bit.tif

                $ ls -sh test_byte.tif
                1.8M test_byte.tif

                $ gdalinfo test_bit.tif
                Driver: GTiff/GeoTIFF
                Files: test_bit.tif
                Size is 1588, 1167
                <snip...>
                Band 1 Block=1588x41 Type=Byte, ColorInterp=Palette
                Image Structure Metadata:
                NBITS=1

                $ gdalinfo test_byte.tif
                Driver: GTiff/GeoTIFF
                Files: test_byte.tif
                Size is 1588, 1167
                <snip...>
                Band 1 Block=1588x5 Type=Byte, ColorInterp=Gray


                * whether or not any software other than GDAL based can read it, I don't know...







                share|improve this answer















                share|improve this answer




                share|improve this answer








                edited 33 mins ago

























                answered 4 hours ago









                user2856user2856

                34.4k2 gold badges65 silver badges114 bronze badges




                34.4k2 gold badges65 silver badges114 bronze badges































                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f338410%2frasterio-invalid-dtype-bool%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown









                    Popular posts from this blog

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單