1

I have a GeoTIFF pulled from Google Earth Engine. I'm using the following code to see the red values of each pixel:

from osgeo import gdal

gdal.UseExceptions()
ds = gdal.Open(myfile)

band = ds.GetRasterBand(4)
arr = band.ReadAsArray().astype(int)

However, the array contains numerous pixel values below zero. Is there a way to convert these values such that they cannot be less than 0? Or are these pixels just simply error pixels?

Gdalinfo dump:

Driver: GTiff/GeoTIFF
Files: LC08_014030_20170220.tif
Size is 749, 1570
Coordinate System is:
PROJCS["WGS 84 / UTM zone 18N",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",-75],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AUTHORITY["EPSG","32618"]]
Origin = (604200.000000000000000,4855080.000000000000000)
Pixel Size = (30.000000000000000,-30.000000000000000)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  COMPRESSION=LZW
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  604200.000, 4855080.000) ( 73d42'13.61"W, 43d50'29.23"N)
Lower Left  (  604200.000, 4807980.000) ( 73d42'46.30"W, 43d25' 2.88"N)
Upper Right (  626670.000, 4855080.000) ( 73d25'27.68"W, 43d50'16.59"N)
Lower Right (  626670.000, 4807980.000) ( 73d26' 7.41"W, 43d24'50.42"N)
Center      (  615435.000, 4831530.000) ( 73d34' 8.82"W, 43d37'40.10"N)
Band 1 Block=256x256 Type=Float32, ColorInterp=Gray
  Description = B1
Band 2 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B2
Band 3 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B3
Band 4 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B4
Band 5 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B5
Band 6 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B6
Band 7 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B7
Band 8 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B8
Band 9 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B9
Band 10 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B10
Band 11 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = B11
Band 12 Block=256x256 Type=Float32, ColorInterp=Undefined
  Description = BQA
  • arr = band.ReadAsArray().astype(int) is not correct, your data type is Float32 so it should be arr = band.ReadAsArray().astype(numpy.float)... try that and see if negative values persist. – Michael Stimson Nov 28 '18 at 4:49
  • @MichaelStimson I've changed it from int to numpy.float, but the values are still negative. – Pierogi11 Nov 29 '18 at 6:44
  • So how do you want to modify the values? Should values less than 0 become 0 or do you want to add a constant (raster minimum value) to globally offset the values? Should negatives become positives (absolute value)? As you've read the data into an array it would be easier to change the values in the array than to modify the raster. – Michael Stimson Nov 29 '18 at 6:46
  • @MichaelStimson I'm just reading in data from the raster. I'm trying to figure out why these pixels are negative and then determine the best way to make the lowest pixel value 0. – Pierogi11 Nov 29 '18 at 22:22
  • I can't help with why, all the information you have provided is that you downloaded the image from Google. In the previous comment there are 3 methods for dealing with negative numbers but you will need to decide on which applies. The image should have some metadata, possibly where you downloaded it from, read it and see if it explains the valid range and how it was derived... if you don't have metadata then ask a new question about the unexpected values, explaining where and what you have downloaded from Google and link to this post. – Michael Stimson Nov 29 '18 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.