Skip to main content
Imroved formatting
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
  • first thing, osgeo.gdal is for pure Python scripting:

     from osgeo import gdal  
     raster  = gdal.Open('your.tif')   
     raster.GetProjection()
    

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

 

PS: in in the last version of the processing module, it is now:

processing.getObjects()

PS2:

 

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thing, osgeo.gdal is for pure Python scripting:

     from osgeo import gdal  
     raster  = gdal.Open('your.tif')   
     raster.GetProjection()
    

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PS: in the last version of the processing module, it is now:

processing.getObjects()

PS2:

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thing, osgeo.gdal is for pure Python scripting:

     from osgeo import gdal  
     raster  = gdal.Open('your.tif')   
     raster.GetProjection()
    

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

 

in the last version of the processing module, it is now:

processing.getObjects()
 

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thingfirst thing, osgeo.gdal is for pure Python scripting:

    from osgeo import gdal
    raster = gdal.Open('your.tif')
    raster.GetProjection()

     from osgeo import gdal  
     raster  = gdal.Open('your.tif')   
     raster.GetProjection()
    

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thingsecond thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PSPS: in the last version of the processing module, it is now:

processing.getObjects()

PS2PS2:

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

exampleexample:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thing, osgeo.gdal is for pure Python scripting:

    from osgeo import gdal
    raster = gdal.Open('your.tif')
    raster.GetProjection()

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PS: in the last version of the processing module, it is now:

processing.getObjects()

PS2:

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thing, osgeo.gdal is for pure Python scripting:

     from osgeo import gdal  
     raster  = gdal.Open('your.tif')   
     raster.GetProjection()
    

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PS: in the last version of the processing module, it is now:

processing.getObjects()

PS2:

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

added 718 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191
  • first thing, osgeo.gdal is for pure Python scripting:

    from osgeo import gdal
    raster = gdal.Open('your.tif')
    raster.GetProjection()

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PS: in the last version of the processing module, it is now:

processing.getObjects()

PS2:

For terminate the script in Processing framework, you can embed your script in a fucntionfunction and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thing, osgeo.gdal is for pure Python scripting:

    from osgeo import gdal
    raster = gdal.Open('your.tif')
    raster.GetProjection()

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PS: in the last version of the processing module, it is now:

processing.getObjects()

For terminate script in Processing framework, you can embed your script in a fucntion and use sys.exitfunc()

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

  • first thing, osgeo.gdal is for pure Python scripting:

    from osgeo import gdal
    raster = gdal.Open('your.tif')
    raster.GetProjection()

Everything else is a problem of osgeo, and not of PyQGIS, since you use 2 QGIS existing layers:

 ##raster_1=raster  
 ##raster_2=raster

you don't need it. To process the layers, the correct code is:

raster_1 = processing.getobject(raster_1)   
raster_2 = processing.getobject(raster_2)

an the crs is given by:

new_proj = raster.crs()
  • second thing, you cannot load a raster in QGIS without projection (if not yet existing, it is chosen by QGIS, according to some criteria):

enter image description here

So your first condition if proj is None:, is unnecessary and your script become (you don't need a list with only two layers):

raster_1 = processing.getobject(raster_1)
raster_2 = processing.getobject(raster_2)
if raster_1.crs()==raster_2.crs():
     action
else:
     WrongCRS()

and the script terminate by himself.

PS: in the last version of the processing module, it is now:

processing.getObjects()

PS2:

For terminate the script, you can embed your script in a function and use sys.exitfunc() (I use the Python console to to see the results)

example:

import atexit
.....
def all_done():
    message = '- Rasters must have the same CRS!\n\nExecution cancelled!'
    print message   
atexit.register(all_done)
raster_1 = processing.getObject(raster_1)
raster_2 = processing.getObject(raster_2)
def main():
    if raster_1.crs() != raster_2.crs():
         sys.exitfunc()
    else:
          message = "ok"
          print message
          continue
main()

Result in the Python console:

enter image description here

added 718 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191
Loading
added 103 characters in body
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191
Loading
Source Link
gene
  • 55.4k
  • 3
  • 113
  • 191
Loading