コード例 #1
0
    def testValidUnsuccessfulCommand(self):

        scmd = SystemCommand('ls abc.txt')

        self.assertEqual(scmd.msg, 
                   b"ls: cannot access 'abc.txt': No such file or directory\n")

        scmd = SystemCommand('gdalinfo abc.txt', logging.getLogger())
        self.assertTrue(b'ERROR 4: abc.txt: No such file or' in scmd.msg)

        with self.assertRaisesRegex(RuntimeError, 'A system command error'):

            scmd = SystemCommand('gdalinfo abc.txt',
                                 logging.getLogger(),
                                 True)
コード例 #2
0
    def getBand(self, outputDir, bandName):

        gdalBandIndex = int(self.bandNameList.index(bandName)) + 1

        baseName = os.path.basename(self.fileName().replace(
            self.extension, '_b{}.tif'.format(gdalBandIndex)))

        tempBandFile = os.path.join(outputDir, baseName)

        if not os.path.exists(tempBandFile):

            cmd = 'gdal_translate' + \
                  ' -b {}'.format(gdalBandIndex) + \
                  ' -a_nodata 0' + \
                  ' -strict' + \
                  ' -mo "bandName={}"'.format(bandName) + \
                  ' ' + self.fileName() + \
                  ' ' + tempBandFile

            sCmd = SystemCommand(cmd, self.logger)

            if sCmd.returnCode:
                tempBandFile = None

        # Copy scene .xml to accompany the extracted .tif for dg_mosaic
        shutil.copy(self.xmlFileName, tempBandFile.replace('.tif', '.xml'))

        return tempBandFile
コード例 #3
0
    def resample(self, xScale, yScale):

        cmd = self._getBaseCmd() + ' -tr ' + str(xScale) + ' ' + \
            str(yScale)

        # Finish the command.
        outFile = tempfile.mkstemp(suffix='.nc')[1]
        cmd += ' ' + self._filePath + ' ' + outFile
        SystemCommand(cmd, None, True)
        shutil.move(outFile, self._filePath)

        # Update the dataset.
        self.getDataset()
コード例 #4
0
    def clipReproject(self, envelope=None, outputSRS=None, dataset=None):

        dataset = dataset or self._filePath

        # At least one operation must be configured.
        if not envelope and not outputSRS:

            raise RuntimeError('Clip envelope or output SRS must be ' +
                               'specified.')

        cmd = self._getBaseCmd()

        # Clip?
        if envelope:

            if not isinstance(envelope, Envelope):
                raise TypeError('The first parameter must be an Envelope.')

            if not self.envelope().Intersection(envelope):

                raise RuntimeError('The clip envelope does not intersect ' +
                                   'the image.')

            cmd += (' -te' + ' ' + str(envelope.ulx()) + ' ' +
                    str(envelope.lry()) + ' ' + str(envelope.lrx()) + ' ' +
                    str(envelope.uly()) + ' -te_srs' + ' "' +
                    envelope.GetSpatialReference().ExportToProj4() + '"')

        # Reproject?
        if outputSRS and not self._dataset.GetSpatialRef().IsSame(outputSRS):

            cmd += ' -t_srs "' + outputSRS.ExportToProj4() + '"'
            self._srs = outputSRS

        # Finish the command.
        outFile = tempfile.mkstemp()[1]
        cmd += ' ' + dataset + ' ' + outFile
        SystemCommand(cmd, self.logger, True)

        shutil.move(outFile, self._filePath)

        # ---
        # Update the dataset.  It would be nice to use the SRS inside the
        # reprojected file; however, if it is EPSG:4326, the axis order could
        # be incorrect.  Passing the requested SRS keeps the axis order
        # consistent.
        # ---
        self.__init__(self._filePath, spatialReference=outputSRS)
コード例 #5
0
    def runMaxEntJar(self, jarFile=MAX_ENT_JAR):

        print ('Running MaxEnt.')

        # Invoke maxent.jar.
        baseCmd = 'java -Xmx1024m -jar ' + \
                  jarFile + \
                  ' visible=false autorun -P -J writeplotdata ' + \
                  '"applythresholdrule=Equal training sensitivity and ' + \
                  'specificity" removeduplicates=false '

        cmd = baseCmd + \
            '-s "' + self._maxEntSpeciesFile + '" ' + \
            '-e "' + self._ascDir + '" ' + \
            '-o "' + self._outputDirectory + '"'

        SystemCommand(cmd, None, True)
コード例 #6
0
    def __exit__(self, type, value, traceback):

        try:
            print('In ILProcessController.__exit__() {}'.format(os.getpid()))

            processToKill = '\"{} worker -Q {}\"'.format(
                ILProcessController.celeryConfig, os.getpid())

            # Shutdown the Celery workers
            shutdownWorkers = "/usr/bin/pkill -9 -f {}".format(processToKill)
            SystemCommand(shutdownWorkers, None, True)

        except exceptions.ConnectionError as inst:
            print("Connection Error ignore: {}".format(inst))
        except OSError as e:
            print("Execution failed:", e, file=sys.stderr)
        except Exception as inst:
            print(type(inst))  # the exception instance
            print(inst.args)  # arguments stored in .args
            print(inst)  # __str__ allows args to be printed directly,
コード例 #7
0
    def readFile(self, dayFile, band):

        outBaseName = os.path.splitext(os.path.basename(dayFile))[0]
        bandName = band.split(':')[-1]

        outName = os.path.join(self._outDir,
                               outBaseName + '-' + bandName + '.tif')

        # Only bother reading, if the file does not exist.
        if not os.path.exists(outName):

            # 'HDF4_EOS:EOS_GRID:"' +
            cmd = 'gdal_translate -tr 231.656358 231.656358 ' + \
                  'HDF4_EOS:EOS_GRID:"' + \
                  dayFile + '"' + \
                  band + ' ' + \
                  outName + ' '\
                  '-co "COMPRESS=LZW"'

            SystemCommand(cmd, self._logger, True)

        return outName
コード例 #8
0
    def prepareImage(image, srs, envelope, ascDir):

        # ---
        # First, to preserve the original files, copy the input file to the
        # output directory.
        # ---
        baseName = os.path.basename(image.fileName())
        nameNoExtension = os.path.splitext(baseName)[0]
        ascImagePath = os.path.join(ascDir, nameNoExtension + '.asc')

        if not os.path.exists(ascImagePath):

            copyPath = os.path.join(ascDir, baseName)
            print ('Processing ' + copyPath)
            shutil.copy(image.fileName(), copyPath)
            imageCopy = GeospatialImageFile(copyPath, srs)
            imageCopy.clipReproject(envelope)

            squareScale = imageCopy.getSquareScale()
            imageCopy.resample(squareScale, squareScale)

            # Convert to ASCII Grid.
            cmd = 'gdal_translate -ot Float32 -of AAIGrid -a_nodata -9999.0' +\
                  ' "' + imageCopy.fileName() + '"' + \
                  ' "' + ascImagePath + '"'

            SystemCommand(cmd, None, True)

            # Fix NaNs.
            for line in fileinput.FileInput(ascImagePath, inplace=1):

                line = line.replace('nan', '-9999')
                sys.stdout.write(line)

        else:
            print(baseName, 'was previously prepared.')

        return ascImagePath
コード例 #9
0
    def testValidSuccessfulCommand(self):

        SystemCommand('ls')
        SystemCommand('ls', logging.getLogger())
        SystemCommand('ls', logging.getLogger(), True)
コード例 #10
0
    def testInvalidCommand(self):

        with self.assertRaisesRegex(RuntimeError, 'A system command error'):

            SystemCommand('notA_Cmd', None, True)