python3.6: gdal.Open('*.tif') succeed, but ReadAsArray() got 'NoneType' error












0















I'm using gdal to read jp2 by lines, here is the code:



def open(self):
if self.ds is None:
self.ds = gdal.Open(self.file_path, gdal.GA_ReadOnly)
self.geo_transform = self.ds.GetGeoTransform()
self.rows = self.ds.RasterYSize
self.cols = self.ds.RasterXSize

def read_strip(self, y_start, read_y_size):
"""
y_start : y in projection coordinate
"""
self.open()
if not self.ds:
raise IOError("Could not open '%s'" % self.file_path)

y_off = int((y_start - self.geo_transform[3]) / self.geo_transform[5])
if y_off < 0 or y_off >= self.rows:
return None
else:
read_y_size_in_data = min(self.rows - y_off, read_y_size)

try:
read_data = self.ds.ReadAsArray(0, y_off, ysize=read_y_size_in_data)
band_data = read_data.astype(float)
self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
return band_data
except Exception:
self.logger.exception('this file is Nonetype, file: {}'.format(self.file_path))

self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
self.close()


Here is the error message:



Traceback (most recent call last): File "scene_reader.py", line 62, in read_strip band_data = read_data.astype(float), 'NoneType' object has no attribute 'astype'


Every time I run the code, I did not get any IOError, which means the jp2 file is opened successfully, while the ReadAsArray() got an 'Nonetype' error. by the way, I have tried to just use gdal.Open() and ReadAsArray() to read the same file by several lines in ipython, everything is ok, so, I'm pretty sure there is nothing wrong with the jp2 file itself.
so, can anyone help me?










share|improve this question





























    0















    I'm using gdal to read jp2 by lines, here is the code:



    def open(self):
    if self.ds is None:
    self.ds = gdal.Open(self.file_path, gdal.GA_ReadOnly)
    self.geo_transform = self.ds.GetGeoTransform()
    self.rows = self.ds.RasterYSize
    self.cols = self.ds.RasterXSize

    def read_strip(self, y_start, read_y_size):
    """
    y_start : y in projection coordinate
    """
    self.open()
    if not self.ds:
    raise IOError("Could not open '%s'" % self.file_path)

    y_off = int((y_start - self.geo_transform[3]) / self.geo_transform[5])
    if y_off < 0 or y_off >= self.rows:
    return None
    else:
    read_y_size_in_data = min(self.rows - y_off, read_y_size)

    try:
    read_data = self.ds.ReadAsArray(0, y_off, ysize=read_y_size_in_data)
    band_data = read_data.astype(float)
    self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
    return band_data
    except Exception:
    self.logger.exception('this file is Nonetype, file: {}'.format(self.file_path))

    self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
    self.close()


    Here is the error message:



    Traceback (most recent call last): File "scene_reader.py", line 62, in read_strip band_data = read_data.astype(float), 'NoneType' object has no attribute 'astype'


    Every time I run the code, I did not get any IOError, which means the jp2 file is opened successfully, while the ReadAsArray() got an 'Nonetype' error. by the way, I have tried to just use gdal.Open() and ReadAsArray() to read the same file by several lines in ipython, everything is ok, so, I'm pretty sure there is nothing wrong with the jp2 file itself.
    so, can anyone help me?










    share|improve this question



























      0












      0








      0


      1






      I'm using gdal to read jp2 by lines, here is the code:



      def open(self):
      if self.ds is None:
      self.ds = gdal.Open(self.file_path, gdal.GA_ReadOnly)
      self.geo_transform = self.ds.GetGeoTransform()
      self.rows = self.ds.RasterYSize
      self.cols = self.ds.RasterXSize

      def read_strip(self, y_start, read_y_size):
      """
      y_start : y in projection coordinate
      """
      self.open()
      if not self.ds:
      raise IOError("Could not open '%s'" % self.file_path)

      y_off = int((y_start - self.geo_transform[3]) / self.geo_transform[5])
      if y_off < 0 or y_off >= self.rows:
      return None
      else:
      read_y_size_in_data = min(self.rows - y_off, read_y_size)

      try:
      read_data = self.ds.ReadAsArray(0, y_off, ysize=read_y_size_in_data)
      band_data = read_data.astype(float)
      self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
      return band_data
      except Exception:
      self.logger.exception('this file is Nonetype, file: {}'.format(self.file_path))

      self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
      self.close()


      Here is the error message:



      Traceback (most recent call last): File "scene_reader.py", line 62, in read_strip band_data = read_data.astype(float), 'NoneType' object has no attribute 'astype'


      Every time I run the code, I did not get any IOError, which means the jp2 file is opened successfully, while the ReadAsArray() got an 'Nonetype' error. by the way, I have tried to just use gdal.Open() and ReadAsArray() to read the same file by several lines in ipython, everything is ok, so, I'm pretty sure there is nothing wrong with the jp2 file itself.
      so, can anyone help me?










      share|improve this question
















      I'm using gdal to read jp2 by lines, here is the code:



      def open(self):
      if self.ds is None:
      self.ds = gdal.Open(self.file_path, gdal.GA_ReadOnly)
      self.geo_transform = self.ds.GetGeoTransform()
      self.rows = self.ds.RasterYSize
      self.cols = self.ds.RasterXSize

      def read_strip(self, y_start, read_y_size):
      """
      y_start : y in projection coordinate
      """
      self.open()
      if not self.ds:
      raise IOError("Could not open '%s'" % self.file_path)

      y_off = int((y_start - self.geo_transform[3]) / self.geo_transform[5])
      if y_off < 0 or y_off >= self.rows:
      return None
      else:
      read_y_size_in_data = min(self.rows - y_off, read_y_size)

      try:
      read_data = self.ds.ReadAsArray(0, y_off, ysize=read_y_size_in_data)
      band_data = read_data.astype(float)
      self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
      return band_data
      except Exception:
      self.logger.exception('this file is Nonetype, file: {}'.format(self.file_path))

      self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
      self.close()


      Here is the error message:



      Traceback (most recent call last): File "scene_reader.py", line 62, in read_strip band_data = read_data.astype(float), 'NoneType' object has no attribute 'astype'


      Every time I run the code, I did not get any IOError, which means the jp2 file is opened successfully, while the ReadAsArray() got an 'Nonetype' error. by the way, I have tried to just use gdal.Open() and ReadAsArray() to read the same file by several lines in ipython, everything is ok, so, I'm pretty sure there is nothing wrong with the jp2 file itself.
      so, can anyone help me?







      gdal






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 14:16









      swatchai

      3,67221627




      3,67221627










      asked Nov 26 '18 at 8:18









      AaayueAaayue

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I think you have to pick a band, even if it is a single band data set. ds.GetRasterBand(1).ReadAsArray() like that. I always refer to this GDAL API Tutorial which has simple examples.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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/3.0/"u003ecc by-sa 3.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%2fstackoverflow.com%2fquestions%2f53477048%2fpython3-6-gdal-open-tif-succeed-but-readasarray-got-nonetype-error%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I think you have to pick a band, even if it is a single band data set. ds.GetRasterBand(1).ReadAsArray() like that. I always refer to this GDAL API Tutorial which has simple examples.






            share|improve this answer




























              0














              I think you have to pick a band, even if it is a single band data set. ds.GetRasterBand(1).ReadAsArray() like that. I always refer to this GDAL API Tutorial which has simple examples.






              share|improve this answer


























                0












                0








                0







                I think you have to pick a band, even if it is a single band data set. ds.GetRasterBand(1).ReadAsArray() like that. I always refer to this GDAL API Tutorial which has simple examples.






                share|improve this answer













                I think you have to pick a band, even if it is a single band data set. ds.GetRasterBand(1).ReadAsArray() like that. I always refer to this GDAL API Tutorial which has simple examples.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 15:20









                yosukesabaiyosukesabai

                4,38611934




                4,38611934
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f53477048%2fpython3-6-gdal-open-tif-succeed-but-readasarray-got-nonetype-error%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

                    Wiesbaden

                    Marschland

                    Dieringhausen