def run(self):
		# Get inputs
		outname = str(self.ui.lineOutfile.text())
		eqstring = str(self.ui.textEqEdit.toPlainText())
		# Basic user validation
		if (len(eqstring) < 1):
			sys.stderr.write('Error: No equation to process.\n')
		elif(self.ui.listWidget_Layers.count() < 1):
			sys.stderr.write('Error: No input files.\n')
		# Process to new file	
		else:	
			try:
				# Test if output box is checked
				if self.ui.checkBoxGenerateOutput.isChecked() == False:
					if (len(outname) < 1):
						sys.stderr.write('Error: No output filename specified.\n')
					else:
						newband = eval(eqstring)
						newband = ma.masked_values(newband, 9999.0)
						epsg = rasterIO.wkt2epsg(proj)
						# setup python dictionary of rgdal formats and drivers
						formats = {'GeoTiff (.tif)':'.tif','Erdas Imagine (.img)':'.img'}
						drivers = {'GeoTiff (.tif)':'GTiff','Erdas Imagine (.img)':'HFA'}
						out_ext = formats[str(self.ui.comboFormats.currentText())]
						driver = drivers[str(self.ui.comboFormats.currentText())]
						outfile = outname + out_ext
						#driver = 'GTiff'
						rasterIO.writerasterband(newband, outfile, driver, XSize, YSize, geotrans, epsg)
						sys.stdout.write('Process complete, created newfile ')
						sys.stdout.write(str(outfile))
						sys.stdout.write('\n')
						if self.ui.checkBoxQGIS.isEnabled() == True:
							qgis.utils.iface.addRasterLayer(outfile)
						self.ui.textPyout.insertPlainText('# create a new matrix from equation\n')
						self.ui.textPyout.insertPlainText('newband = %s\n' %(eqstring))
						self.ui.textPyout.insertPlainText('# get the epsg code from the projection\n')
						self.ui.textPyout.insertPlainText('epsg = rasterIO.wkt2epsg(proj)\n')
						self.ui.textPyout.insertPlainText('# set the gdal driver / output file type\n')
						self.ui.textPyout.insertPlainText('driver = "%s"\n' %(driver))
						self.ui.textPyout.insertPlainText('# specify the new output file\n')
						self.ui.textPyout.insertPlainText('outfile = "%s"\n' %(outfile))
						self.ui.textPyout.insertPlainText('# write the new matrix to the new file\n')
						self.ui.textPyout.insertPlainText('rasterIO.writerasterband(newband, outfile, driver, XSize, YSize, geotrans, epsg)\n\n')
						self.ui.textPyout.insertPlainText('# add the new file to qgis\n')
						self.ui.textPyout.insertPlainText('qgis.utils.iface.addRasterLayer(outfile)\n\n')
				else:
					outputstring = (str(eval(str(self.ui.textEqEdit.toPlainText())))) +'\n'
					self.ui.textInformation.setTextColor(QtGui.QColor(0,0,255))
					self.ui.textInformation.insertPlainText(outputstring)
					self.ui.textInformation.moveCursor(QtGui.QTextCursor.End)
					self.ui.textPyout.insertPlainText('# run without output file\n')
					self.ui.textPyout.insertPlainText('print %s\n\n' %(eqstring))
			except ValueError:
				sys.stderr.write('Error: Could not perform calculation. Are input rasters same shape and size? Is the output a matrix?\n')
			except TypeError:
				sys.stderr.write('Error: Could not perform calculation. Are input rasters loaded?\n')
			except SyntaxError:
				sys.stderr.write('Error: Could not perform calculation. Is the equation correct?\n')
			except AttributeError:
				sys.stderr.write('Error: Could not perform calculation. Is the output raster correct?\n')
Ejemplo n.º 2
0
def processfunction(file_list):
	    
	# Create a loop for all files in current directory

	    
	for file in file_list:
	    
		# Check file type (in this case Geotiff)
		if file.endswith('.tif'):
			    
		# Open a pointer to the file
		pointer = rasterIO.opengdalraster(file)
			    
		# Read the raster metadata for the new output file
		driver, XSize, YSize, proj_wkt, geo_t_params = rasterIO.readrastermeta(pointer)
			    
		# Read the first band to a matrix called band_1
		band_1 = rasterIO.readrasterband(pointer, 1)
			    
		# Read the second band to a matrix called band_2

		band_2 =rasterIO.readrasterband(pointer, 2)
			    
		# Perform the NDVI calculation and put the results into a new matrix
		new_ndvi_band = ((band_2 - band_1) / (band_2 + band_1))
			    
		# Get the input file filename without extension and create a new file name
		parts =string.split(file)
		newname = './'+parts[0]+'_ndvi.tif' # ./filename_ndvi.tif

	# Get the EPSG code from well known text projection
	epsg = rasterIO.wkt2epsg(proj_wkt)
	
	# Write the NDVI matrix to a new raster file
	rasterIO.writerasterband(new_ndvi_band, newname, XSize, YSize, geo_t_params, epsg)
    
# loop will now go to next file in input list
# Create a run function to accept jobs from queue
def processRun(q, filename):
	q.put(processfunc(filename))

# Create the main function
def main(arg=sys.argv):
	# Get a list fo files in the current directory (assumed to contain raster data)
	file_list = os.listdir(os.getcwd())
	    
	# Get the length of the file list
	len_flist = len(file_list)
	half_len = len_flist / 2

	# Create a queue object
	q = Queue()
	    
	# Create two processes (add more depending on processor availability)
	p1 = Process(target=processRun, args=(q, flist[:half_len]))
	p2 = Process(target=processRun, args=(q, flist[half_len:]))

	# Start processes
	p1.start()
	p2.start()

# Standard Python script execution/exit handling

if __name__=='__main__':
	sys.exit(main())
Ejemplo n.º 3
0
    if file.endswith('.tif'):

        # Open a pointer to the file
        pointer = rasterIO.opengdalraster(file)

        # Read the raster metadata for the new output file
        driver, XSize, YSize, proj_wkt, geo_t_params = rasterIO.readrastermeta(
            pointer)

        # Read the first band to a matrix called band_1
        band_1 = rasterIO.readrasterband(pointer, 1)

        # Read the second band to a matrix called band_2
        band_2 = rasterIO.readrasterband(pointer, 2)

        # Perform the NDVI calculation and put the results into a new matrix
        new_ndvi_band = ((band_2 - band_1) / (band_2 + band_1))

        # Get the input file filename without extension and create a new file name
        parts = string.split(file)
        newname = './' + parts[0] + '_ndvi.tif'  # ./filename_ndvi.tif

        # Get the EPSG code from well known text projection
        epsg = rasterIO.wkt2epsg(proj_wkt)

        # Write the NDVI matrix to a new raster file
        rasterIO.writerasterband(new_ndvi_band, newname, XSize, YSize,
                                 geo_t_params, epsg)

# loop will now go to next file in input list
Ejemplo n.º 4
0
# Create a loop for all files in current directory

for file in flist:
	# Check file type (in this case Geotiff)    
	if file.endswith('.tif'):
        
	# Open a pointer to the file
	pointer = rasterIO.opengdalraster(file)
        
	# Read the raster metadata for the new output file
        driver, XSize, YSize, proj_wkt, geo_t_params = rasterIO.readrastermeta(pointer)
        
	# Read the first band to a matrix called band_1
        band_1 =rasterIO.readrasterband(pointer, 1)
        
	# Read the second band to a matrix called band_2
        band_2 = rasterIO.readrasterband(pointer, 2)
        
	# Perform the NDVI calculation and put the results into a new matrix
        new_ndvi_band = ((band_2 - band_1) / (band_2 + band_1))
	# Get the input file filename without extension and create a new file name
        parts = string.split(file)
        newname ='./'+parts[0]+'_ndvi.tif' # ./filename_ndvi.tif
	# Get the EPSG code from well known text projection
	epsg =rasterIO.wkt2epsg(proj_wkt)
        
	# Write the NDVI matrix to a new raster file
	rasterIO.writerasterband(new_ndvi_band, newname, XSize, YSize, geo_t_params, epsg)
    
# loop will now go to next file in input list
 def run(self):
     # Get inputs
     outname = str(self.ui.lineOutfile.text())
     eqstring = str(self.ui.textEqEdit.toPlainText())
     # Basic user validation
     if (len(eqstring) < 1):
         sys.stderr.write('Error: No equation to process.\n')
     elif (self.ui.listWidget_Layers.count() < 1):
         sys.stderr.write('Error: No input files.\n')
     # Process to new file
     else:
         try:
             # Test if output box is checked
             if self.ui.checkBoxGenerateOutput.isChecked() == False:
                 if (len(outname) < 1):
                     sys.stderr.write(
                         'Error: No output filename specified.\n')
                 else:
                     newband = eval(eqstring)
                     newband = ma.masked_values(newband, 9999.0)
                     epsg = rasterIO.wkt2epsg(proj)
                     # setup python dictionary of rgdal formats and drivers
                     formats = {
                         'GeoTiff (.tif)': '.tif',
                         'Erdas Imagine (.img)': '.img'
                     }
                     drivers = {
                         'GeoTiff (.tif)': 'GTiff',
                         'Erdas Imagine (.img)': 'HFA'
                     }
                     out_ext = formats[str(
                         self.ui.comboFormats.currentText())]
                     driver = drivers[str(
                         self.ui.comboFormats.currentText())]
                     outfile = outname + out_ext
                     #driver = 'GTiff'
                     rasterIO.writerasterband(newband, outfile, driver,
                                              XSize, YSize, geotrans, epsg)
                     sys.stdout.write('Process complete, created newfile ')
                     sys.stdout.write(str(outfile))
                     sys.stdout.write('\n')
                     if self.ui.checkBoxQGIS.isEnabled() == True:
                         qgis.utils.iface.addRasterLayer(outfile)
                     self.ui.textPyout.insertPlainText(
                         '# create a new matrix from equation\n')
                     self.ui.textPyout.insertPlainText('newband = %s\n' %
                                                       (eqstring))
                     self.ui.textPyout.insertPlainText(
                         '# get the epsg code from the projection\n')
                     self.ui.textPyout.insertPlainText(
                         'epsg = rasterIO.wkt2epsg(proj)\n')
                     self.ui.textPyout.insertPlainText(
                         '# set the gdal driver / output file type\n')
                     self.ui.textPyout.insertPlainText('driver = "%s"\n' %
                                                       (driver))
                     self.ui.textPyout.insertPlainText(
                         '# specify the new output file\n')
                     self.ui.textPyout.insertPlainText('outfile = "%s"\n' %
                                                       (outfile))
                     self.ui.textPyout.insertPlainText(
                         '# write the new matrix to the new file\n')
                     self.ui.textPyout.insertPlainText(
                         'rasterIO.writerasterband(newband, outfile, driver, XSize, YSize, geotrans, epsg)\n\n'
                     )
                     self.ui.textPyout.insertPlainText(
                         '# add the new file to qgis\n')
                     self.ui.textPyout.insertPlainText(
                         'qgis.utils.iface.addRasterLayer(outfile)\n\n')
             else:
                 outputstring = (str(
                     eval(str(self.ui.textEqEdit.toPlainText())))) + '\n'
                 self.ui.textInformation.setTextColor(
                     QtGui.QColor(0, 0, 255))
                 self.ui.textInformation.insertPlainText(outputstring)
                 self.ui.textInformation.moveCursor(QtGui.QTextCursor.End)
                 self.ui.textPyout.insertPlainText(
                     '# run without output file\n')
                 self.ui.textPyout.insertPlainText('print %s\n\n' %
                                                   (eqstring))
         except ValueError:
             sys.stderr.write(
                 'Error: Could not perform calculation. Are input rasters same shape and size? Is the output a matrix?\n'
             )
         except TypeError:
             sys.stderr.write(
                 'Error: Could not perform calculation. Are input rasters loaded?\n'
             )
         except SyntaxError:
             sys.stderr.write(
                 'Error: Could not perform calculation. Is the equation correct?\n'
             )
         except AttributeError:
             sys.stderr.write(
                 'Error: Could not perform calculation. Is the output raster correct?\n'
             )