Skip to main content
adding more information
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

I fixed some minor issues and assumed some value parameters to test your code. I found out that this extrange issue was due to the absence of the minus sign in the last parameter of 'SetGeoTransform' method (-cell_size).

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           -cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           -cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

I fixed some minor issues and assumed some value parameters to test your code. I found out that this extrange issue was due to the absence of the minus sign in the last parameter of 'SetGeoTransform' method (-cell_size).

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           -cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

added 1 character in body
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           -cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           -cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

added 3 characters in body
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 300.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 30
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

I fixed some minor issues and assumed some value parameters to test your code.

from osgeo import gdal, osr
import numpy as np

raster_classified = 'c:/Users/Usuario/pyqgis_data/raster_classified.tif'

col = 20 
row = 20

cell_size = 0.003
origin_x = 10000
origin_y = 10000

driver = gdal.GetDriverByName('GTiff')

outRaster = driver.Create(raster_classified, col, row, 1, gdal.GDT_Int16)

outband = outRaster.GetRasterBand(1)

outRaster.SetGeoTransform((origin_x, 
                           cell_size, 
                           0, 
                           origin_y  - (cell_size * row), 
                           0, 
                           cell_size))

values = [ [] for i in range(row) ]

for i in range(row):
    for j in range(col):
            values[i].append(1)

outras_array = np.array(values)

outband.WriteArray(outras_array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster = None

After running it at the Python Console of QGIS, I got a 20x20 *.tif raster (all pixels equal 1); as it can be seen at next image:

enter image description here

It worked.

Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82
Loading