def detect_python_path(): if (IS_WINDOWS and PYTHON_EXEC_CONFIGURED_BY_USER) or not IS_WINDOWS: return [] suggested = [] dirs = [] try: drives = [ QDir.toNativeSeparators(d.absolutePath()) for d in QDir.drives() ] for drive in drives: info = QFileInfo(drive) if info.isReadable(): dirs += [ os.path.join(drive, folder) for folder in os.listdir(drive) ] for folder in dirs: file_path = os.path.join(folder, "python.exe") if ("python" in folder.lower()) and os.path.exists(file_path): suggested.append(file_path) except: print("Detection couldnt be executed") finally: return suggested
def xls_2_qgs_vector_layer(xls_path, headers_first_line=True): """ Reads the Excel file from the given file path and converts it to a geometry-less QGIS vector layer. The column type (in the QGIS vector layer) will be determined automatically based on the type of the type of the column in the first row that will be parsed in the Excel file. If the workbook contains multiple sheets, then only the first sheet will be loaded. :param xls_path: File path to Excel file. :type xls_path: str :param headers_first_line: True if the first line contains the header names else the headers will be automatically generated. :return: Returns a QGIS memory layer containing the Excel data. :rtype: QgsVectorLayer """ if not XLRD_AVAIL: msg = '\'xlrd library\' for parsing Excel files is missing.' raise XLSException(msg) # Check permissions xfileinfo = QFileInfo(xls_path) if not xfileinfo.isReadable(): msg = 'The current user does not have read permissions ' \ 'on \'{0}\''.format(xls_path) raise XLSException(msg) book = open_workbook(xls_path) if not book: raise XLSException('The workbook could not be loaded.') # Get first sheet xl_sheet = book.sheet_by_index(0) sheet_name = xl_sheet.name if not sheet_name: sheet_name = DEFAULT_SHEET_NAME xls_vl = QgsVectorLayer('None', sheet_name, 'memory') dp = xls_vl.dataProvider() # Get QgsField items col_idx_mapping, fields = _create_qgs_fields(xl_sheet, headers_first_line) # Add fields dp.addAttributes(fields) xls_vl.updateFields() fields = xls_vl.pendingFields() # Create qgis features items, add them to data provider features = _cells_2_features(xl_sheet, headers_first_line, col_idx_mapping, fields) dp.addFeatures(features) return xls_vl
def fill_media_location(self): caption = _('Select media location') selected = unicode(QFileDialog.getExistingDirectory(self, caption)) if not selected: return info = QFileInfo(selected) if not info.isReadable(): self.main_widget().layout().addWidget( self.not_accessible_media_path_label, 13, 1, 1, 4) return if not info.isWritable(): self.main_widget().layout().addWidget( self.not_writable_media_path_label, 13, 1, 1, 4) return self.media_location_editor.setText(selected)
def detect_python_path(): if (IS_WINDOWS and PYTHON_EXEC_CONFIGURED_BY_USER) or not IS_WINDOWS: return [] suggested = [] try: drives = [QDir.toNativeSeparators(d.absolutePath()) for d in QDir.drives()] dirs = [] for drive in drives: info = QFileInfo(drive) if info.isReadable(): dirs += [os.path.join(drive, folder) for folder in os.listdir(drive)] for folder in dirs: file_path = os.path.join(folder, "python.exe") if ("Python" in folder) and os.path.exists(file_path): suggested.append(file_path) except: print("Detection couldnt be executed") return suggested
def load_workbook(self, path): """ Loads the workbook contained in the specified file to the view. :param path: Path to fil containing workbook data. :type path: str """ xfile = QFile(path) if not xfile.exists(): QMessageBox.critical( self, self.tr('Invalid path'), u'\'{0}\' {1}'.format(path, self.tr('does not exist.'))) return # Check permissions xfileinfo = QFileInfo(xfile) if not xfileinfo.isReadable(): QMessageBox.critical( self, self.tr('Unreadable file'), u'{0} {1}'.format(path, self.tr('is not readable.'))) return # Clear view self.clear_view() # Show progress bar self._pg_par.setVisible(True) pg_val = 0 # Add sheets wb = open_workbook(path) self._pg_par.setRange(0, wb.nsheets) for s in wb.sheets(): self.add_xlrd_sheet(s) # Update progress bar pg_val += 1 self._pg_par.setValue(pg_val) self._pg_par.setVisible(False)
def load_holders_file(self, path): """ Loads the holders data contained in the specified file to the view. :param path: Path to file containing holders data. :type path: str """ holders_file = QFile(path) if not holders_file.exists(): QMessageBox.critical( self, self.tr('Invalid path'), u'\'{0}\' {1}'.format(path, self.tr('does not exist.'))) return # Check permissions holders_fileinfo = QFileInfo(holders_file) if not holders_fileinfo.isReadable(): QMessageBox.critical( self, self.tr('Unreadable file'), u'{0} {1}'.format(path, self.tr('is not readable.'))) return # Get file extension ext = holders_fileinfo.suffix() # Get reader based on suffix if ext not in holder_readers: msg = 'No reader defined for \'{0}\' file extension'.format(ext) QMessageBox.critical(self, self.tr('Invalid Extension'), msg) return reader = holder_readers[ext] vl = None try: # Get vector layer vl = reader(path) except Exception as ex: QMessageBox.critical(self, self.tr('Error Loading Data Source.'), str(ex)) return if not vl: QMessageBox.critical( self.parentWidget(), self.tr('Null Data Source'), self.tr('Data source object is None, cannot be loaded.')) return if not vl.isValid(): err = vl.error() if not err.isEmpty(): err_msg = err.summary() else: err_msg = 'The holders data source is invalid.' QMessageBox.critical(self.parentWidget(), self.tr('Invalid Data Source'), err_msg) return # Clear view self.clear_view() # Show progress bar self._pg_par.setVisible(True) pg_val = 0 # Add vector layer to the view self._pg_par.setRange(0, 1) self.add_vector_layer(vl) self._pg_par.setValue(1) self._pg_par.setVisible(False)