def alter_data(self, observation): observation = copy.deepcopy(observation) if self.staff is None or not self.staff: utils.MessagebarAndLog.critical(log_msg=u'Import error, staff not given') utils.pop_up_info(u'Import error, staff not given') return Cancel() observation[u'staff'] = self.staff return observation
def write_printlist_to_file(printlist): filename = PyQt4.QtGui.QFileDialog.getSaveFileName(None, 'Choose a file name', '', 'csv (*.csv)') if not filename: return try: with open(filename, 'w') as f: f.write(u'\n'.join(printlist).encode('utf-8')) except IOError, e: utils.pop_up_info("Writing of file failed!: " + str(e))
def start_import(self, observations): """ :param observations: :return: """ observations = copy.deepcopy(observations) #Start by saving the parameter settings self.update_stored_settings(self.stored_settings, self.parameter_imports) self.save_stored_settings(self.ms, self.stored_settings, self.stored_settingskey) chosen_methods = [import_method_chooser.import_method for import_method_chooser in self.parameter_imports.values() if import_method_chooser.import_method] if not chosen_methods: utils.pop_up_info("Must choose at least one parameter import method") utils.MessagebarAndLog.critical(bar_msg="No parameter import method chosen") return Cancel() #Update the observations using the general settings, filters and parameter settings observations = self.filter_and_alter_data(observations, self.settings, self.settings_with_own_loop, self.parameter_imports) if isinstance(observations, Cancel): utils.MessagebarAndLog.warning(bar_msg=u"No observations left to import after filtering") return None observations_importmethods = {} for observation in observations: if self.parameter_imports[observation[u'parametername']].import_method: observations_importmethods.setdefault(self.parameter_imports[observation[u'parametername']].import_method, []).append(observation) importer = import_data_to_db.midv_data_importer() data_preparers = {u'w_levels': self.prepare_w_level_data, u'w_flow': self.prepare_w_flow_data, u'w_qual_field': self.prepare_w_qual_field_data, u'comments': self.prepare_comments_data} for import_method, observations in observations_importmethods.iteritems(): if import_method: file_data = data_preparers[import_method](observations) importer.send_file_data_to_importer(file_data, partial(importer.general_csv_import, goal_table=import_method)) importer.SanityCheckVacuumDB() PyQt4.QtGui.QApplication.restoreOverrideCursor()
def add_layers_new_method(self): try:#qgis>=2.4 if self.group_name == 'Midvatten_OBS_DB': position_index = 0 else: position_index = 1 MyGroup = self.root.insertGroup(position_index, self.group_name) except:#qgis < 2.4 MyGroup = self.legend.addGroup (self.group_name,1,-1) uri = QgsDataSourceURI() uri.setDatabase(self.settingsdict['database']) canvas = self.iface.mapCanvas() layer_list = [] map_canvas_layer_list=[] if self.group_name == 'Midvatten_OBS_DB': for tablename in self.default_nonspatlayers: #first load all non-spatial layers uristring= 'dbname="' + self.settingsdict['database'] + '" table="' + tablename + '"' layer = QgsVectorLayer(uristring,tablename, 'spatialite') layer_list.append(layer) for tablename in self.default_layers: # then all the spatial ones uri.setDataSource('',tablename, 'Geometry') layer = QgsVectorLayer(uri.uri(), tablename, 'spatialite') # Adding the layer as 'spatialite' instead of ogr vector layer is preferred layer_list.append(layer) elif self.group_name == 'Midvatten_data_domains': #if self.group_name == 'Midvatten_data_domains': conn_ok, dd_tables = utils.sql_load_fr_db("select name from sqlite_master where name like 'zz_%'") if not conn_ok: return d_domain_tables = [str(dd_table[0]) for dd_table in dd_tables] for tablename in d_domain_tables: uristring= 'dbname="' + self.settingsdict['database'] + '" table="' + tablename + '"' layer = QgsVectorLayer(uristring,tablename, 'spatialite') layer_list.append(layer) #now loop over all the layers and set styles etc for layer in layer_list: if not layer.isValid(): utils.pop_up_info(layer.name() + ' is not valid layer') print(layer.name() + ' is not valid layer') pass else: map_canvas_layer_list.append(QgsMapCanvasLayer(layer)) try:#qgis>=2.4 QgsMapLayerRegistry.instance().addMapLayers([layer],False) MyGroup.insertLayer(0,layer) #MyGroup.addLayer(layer) except:#qgis<2.4 QgsMapLayerRegistry.instance().addMapLayers([layer]) group_index = self.legend.groups().index(self.group_name) self.legend.moveLayer (self.legend.layers()[0],group_index) if self.group_name == 'Midvatten_OBS_DB': layer.setEditorLayout(1)#perhaps this is unnecessary since it gets set from the loaded qml below? #now try to load the style file stylefile_sv = os.path.join(os.sep,os.path.dirname(__file__),"..","definitions",layer.name() + "_sv.qml") stylefile = os.path.join(os.sep,os.path.dirname(__file__),"..","definitions",layer.name() + ".qml") if utils.getcurrentlocale()[0] == 'sv_SE' and os.path.isfile( stylefile_sv ): #swedish forms are loaded only if locale settings indicate sweden try: layer.loadNamedStyle(stylefile_sv) except: try: layer.loadNamedStyle(stylefile) except: pass else: try: layer.loadNamedStyle(stylefile) except: pass if layer.name() == 'obs_points':#zoom to obs_points extent obsp_lyr = layer canvas.setExtent(layer.extent()) elif layer.name() == 'w_lvls_last_geom':#we do not want w_lvls_last_geom to be visible by default self.legend.setLayerVisible(layer,False) else: pass #finally refresh canvas canvas.refresh()
def create_new_db(self, verno, user_select_CRS='y', EPSG_code=u'4326'): #CreateNewDB(self, verno): """Open a new DataBase (create an empty one if file doesn't exists) and set as default DB""" set_locale = self.ask_for_locale() if set_locale == u'cancel': PyQt4.QtGui.QApplication.restoreOverrideCursor() return u'cancel' if user_select_CRS=='y': EPSGID=str(self.ask_for_CRS(set_locale)[0]) else: EPSGID=EPSG_code PyQt4.QtGui.QApplication.setOverrideCursor(PyQt4.QtCore.Qt.WaitCursor) if EPSGID=='0' or not EPSGID: utils.pop_up_info("Cancelling...") else: # If a CRS is selectd, go on and create the database #path and name of new db self.dbpath = PyQt4.QtGui.QFileDialog.getSaveFileName(None, "New DB","midv_obsdb.sqlite","Spatialite (*.sqlite)") if not self.dbpath: PyQt4.QtGui.QApplication.restoreOverrideCursor() return '' #create Spatialite database else: #delete the file if exists if os.path.exists(self.dbpath): try: os.remove(self.dbpath) except OSError, e: utils.MessagebarAndLog.critical("sqlite error, see qgis Log Message Panel", "%s - %s." % (e.filename,e.strerror), duration=10) PyQt4.QtGui.QApplication.restoreOverrideCursor() return '' try: # creating/connecting the test_db self.conn = sqlite.connect(self.dbpath) # creating a Cursor self.cur = self.conn.cursor() self.cur.execute("PRAGMA foreign_keys = ON") #Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled separately for each database connection separately. except: qgis.utils.iface.messageBar().pushMessage("Impossible to connect to selected DataBase", 2,duration=3) #utils.pop_up_info("Impossible to connect to selected DataBase") PyQt4.QtGui.QApplication.restoreOverrideCursor() return '' #First, find spatialite version versionstext = self.cur.execute('select spatialite_version()').fetchall() # load sql syntax to initialise spatial metadata, automatically create GEOMETRY_COLUMNS and SPATIAL_REF_SYS # then the syntax defines a Midvatten project db according to the loaded .sql-file if not int(versionstext[0][0][0]) > 3: # which file to use depends on spatialite version installed utils.pop_up_info("Midvatten plugin needs spatialite4.\nDatabase can not be created") return '' filenamestring = "create_db.sql" SQLFile = os.path.join(os.sep,os.path.dirname(__file__),"..","definitions",filenamestring) qgisverno = QGis.QGIS_VERSION#We want to store info about which qgis-version that created the db with open(SQLFile, 'r') as f: f.readline() # first line is encoding info.... try: for line in f: if not line: continue if line.startswith("#"): continue for replace_word, replace_with in [('CHANGETORELEVANTEPSGID', str(EPSGID)), ('CHANGETOPLUGINVERSION', str(verno)), ('CHANGETOQGISVERSION',qgisverno), ('CHANGETOSPLITEVERSION', str(versionstext[0][0])), ('CHANGETOLOCALE', str(set_locale))]: line = line.replace(replace_word, replace_with) self.cur.execute(line) # use tags to find and replace SRID and versioning info except Exception, e: #utils.pop_up_info('Failed to create DB! sql failed:\n' + line + '\n\nerror msg:\n' + str(e)) utils.MessagebarAndLog.critical("sqlite error, see qgis Log Message Panel", 'Failed to create DB! sql failed: \n%serror msg: %s\n\n'%(line ,str(e)), duration=5) except: qgis.utils.iface.messageBar().pushMessage("Failed to create database", 2,duration=3)
return printlist @staticmethod def write_printlist_to_file(printlist): filename = PyQt4.QtGui.QFileDialog.getSaveFileName(None, 'Choose a file name', '', 'csv (*.csv)') if not filename: return try: with open(filename, 'w') as f: f.write(u'\n'.join(printlist).encode('utf-8')) except IOError, e: utils.pop_up_info("Writing of file failed!: " + str(e)) except UnicodeDecodeError, e: utils.pop_up_info("Error writing " + str(printlist)) class ExportObject(object): def __init__(self, connect): """ This one should contain: Two widgets and two layouts (separate classes. Widget 1 contains the comboboxes and fields for producing the parameter names. part 1: parameter names option 1: less flexible. Choosing a table and a pre-created list of parameters/units will appear using select distinct parameter, unit from ... option 2:
def parse_observations_and_populate_gui(self): splitter = PyQt4.QtGui.QSplitter(PyQt4.QtCore.Qt.Vertical) self.add_row(splitter) self.observations = self.select_file_and_parse_rows(self.parse_rows) if isinstance(self.observations, Cancel): self.status = True return u'cancel' #Filters and general settings settings_widget = PyQt4.QtGui.QWidget() settings_layout = PyQt4.QtGui.QVBoxLayout() settings_widget.setLayout(settings_layout) splitter.addWidget(settings_widget) self.settings = [] self.settings.append(StaffQuestion()) self.settings.append(DateShiftQuestion()) self.settings.append(DateTimeFilter()) #This includes a checkbox for "include only latest for setting in self.settings: if hasattr(setting, u'widget'): settings_layout.addWidget(setting.widget) self.add_line(settings_layout) #Settings with own loop gets self.observations to work on. self.settings_with_own_loop = [ObsidFilter()] #Sublocations sublocations = [observation[u'sublocation'] for observation in self.observations] sublocations_widget = PyQt4.QtGui.QWidget() sublocations_layout = PyQt4.QtGui.QVBoxLayout() sublocations_widget.setLayout(sublocations_layout) sublocations_layout.addWidget(PyQt4.QtGui.QLabel(u'Select sublocations to import:')) splitter.addWidget(sublocations_widget) self.settings.append(SublocationFilter(sublocations)) sublocations_layout.addWidget(self.settings[-1].widget) self.add_line(sublocations_layout) #Parameters parameter_widget = PyQt4.QtGui.QWidget() parameter_layout = PyQt4.QtGui.QVBoxLayout() parameter_widget.setLayout(parameter_layout) splitter.addWidget(parameter_widget) parameter_layout.addWidget(PyQt4.QtGui.QLabel(u'Specify import methods for input fields')) self.parameter_names = list(set([observation[u'parametername'] for observation in self.observations])) self.parameter_imports = OrderedDict() for parametername in self.parameter_names: param_import_obj = ImportMethodChooser(parametername, self.parameter_names, self.connect) self.parameter_imports[parametername] = param_import_obj parameter_layout.addWidget(param_import_obj.widget) #self.main_vertical_layout.addStretch(1) self.stored_settingskey = u'fieldlogger_import_parameter_settings' #General buttons self.save_settings_button = PyQt4.QtGui.QPushButton(u'Save settings') self.gridLayout_buttons.addWidget(self.save_settings_button, 0, 0) self.connect(self.save_settings_button, PyQt4.QtCore.SIGNAL("clicked()"), lambda : map(lambda x: x(), [lambda : self.update_stored_settings(self.stored_settings, self.parameter_imports), lambda : self.save_stored_settings(self.ms, self.stored_settings, self.stored_settingskey)])) self.clear_settings_button = PyQt4.QtGui.QPushButton(u'Clear settings') self.clear_settings_button.setToolTip(u'Clear all parameter settings\nReopen Fieldlogger import gui to have it reset,\nor press "Save settings" to undo.') self.gridLayout_buttons.addWidget(self.clear_settings_button, 1, 0) self.connect(self.clear_settings_button, PyQt4.QtCore.SIGNAL("clicked()"), lambda: map(lambda x: x(), [lambda: self.save_stored_settings(self.ms, [], self.stored_settingskey), lambda: utils.pop_up_info(u'Settings cleared. Restart import Fieldlogger dialog')])) self.start_import_button = PyQt4.QtGui.QPushButton(u'Start import') self.gridLayout_buttons.addWidget(self.start_import_button, 2, 0) self.connect(self.start_import_button, PyQt4.QtCore.SIGNAL("clicked()"), lambda : self.start_import(self.observations)) self.gridLayout_buttons.setRowStretch(3, 1) #Load stored parameter settings self.stored_settings = self.get_stored_settings(self.ms, self.stored_settingskey) self.set_parameters_using_stored_settings(self.stored_settings, self.parameter_imports) self.show()
def about(self): utils.pop_up_info(msg='This feature is not yet implemented',title='Hold on...')
def __init__(self, parent, midv_settings): self.iface = parent self.ms = midv_settings PyQt4.QtGui.QDialog.__init__(self, parent) self.setAttribute(PyQt4.QtCore.Qt.WA_DeleteOnClose) self.setupUi(self) # Required by Qt4 to initialize the UI self.setWindowTitle("Export to FieldLogger file") # Set the title for the dialog self.widget.setMinimumWidth(180) tables_columns = defs.tables_columns() self.parameter_groups = None self.stored_settingskey = 'fieldlogger_export_pgroups' self.stored_settingskey_parameterbrowser = 'fieldlogger_export_pbrowser' for settingskey in [self.stored_settingskey, self.stored_settingskey_parameterbrowser]: if settingskey not in self.ms.settingsdict: utils.MessagebarAndLog.warning(bar_msg=settingskey + " did not exist in settingsdict") self.parameter_groups = self.create_parameter_groups_using_stored_settings(self.get_stored_settings(self.ms, self.stored_settingskey), self.connect) if self.parameter_groups is None or not self.parameter_groups: self.parameter_groups = [ParameterGroup(self.connect)] self.main_vertical_layout.addWidget(PyQt4.QtGui.QLabel(u'Fieldlogger input fields and locations:')) self.main_vertical_layout.addWidget(get_line()) self.splitter = PyQt4.QtGui.QSplitter(PyQt4.QtCore.Qt.Vertical) self.main_vertical_layout.addWidget(self.splitter) #This is about adding a messagebar to the fieldlogger window. But for some reason qgis crashes or closes #when the timer ends for the regular messagebar #self.lbl = MessageBar(self.splitter) #qgis.utils.iface.optional_bar = self.lbl self.widgets_layouts = self.init_splitters_layouts(self.splitter) if self.parameter_groups: for export_object in self.parameter_groups: self.add_parameter_group_to_gui(self.widgets_layouts, export_object) #Buttons #ParameterUnitBrowser self.parameter_browser = ParameterBrowser(tables_columns, self.connect, self.widget) self.parameter_browser_button = PyQt4.QtGui.QPushButton(u'Create Input Fields') self.gridLayout_buttons.addWidget(self.parameter_browser_button, 0, 0) self.connect(self.parameter_browser_button, PyQt4.QtCore.SIGNAL("clicked()"), lambda : self.parameter_browser.show()) self.update_parameter_browser_using_stored_settings(self.get_stored_settings(self.ms, self.stored_settingskey_parameterbrowser), self.parameter_browser) self.add_parameter_group = PyQt4.QtGui.QPushButton(u'More Fields and Locations') self.add_parameter_group.setToolTip(u'Creates an additional empty input field group.') self.gridLayout_buttons.addWidget(self.add_parameter_group, 1, 0) #Lambda and map is used to run several functions for every button click self.connect(self.add_parameter_group, PyQt4.QtCore.SIGNAL("clicked()"), lambda: map(lambda x: x(), [lambda: self.parameter_groups.append(ParameterGroup(self.connect)), lambda: self.add_parameter_group_to_gui(self.widgets_layouts, self.parameter_groups[-1])])) self.gridLayout_buttons.addWidget(get_line(), 2, 0) #Buttons self.save_settings_button = PyQt4.QtGui.QPushButton(u'Save settings') self.save_settings_button.setToolTip(u'Saves the current input fields settings to midvatten settings.') self.gridLayout_buttons.addWidget(self.save_settings_button, 3, 0) self.connect(self.save_settings_button, PyQt4.QtCore.SIGNAL("clicked()"), lambda: map(lambda x: x(), [lambda: self.save_stored_settings(self.ms, self.update_stored_settings(self.parameter_groups), self.stored_settingskey), lambda: self.save_stored_settings(self.ms, self.update_stored_settings([self.parameter_browser]), self.stored_settingskey_parameterbrowser)])) self.clear_settings_button = PyQt4.QtGui.QPushButton(u'Clear settings') self.clear_settings_button.setToolTip(u'Clear input fields and settings.\nReopen Fieldlogger export gui to it reset,\nor press "Save settings" to undo.') self.gridLayout_buttons.addWidget(self.clear_settings_button, 4, 0) self.connect(self.clear_settings_button, PyQt4.QtCore.SIGNAL("clicked()"), lambda: map(lambda x: x(), [lambda: self.save_stored_settings(self.ms, [], self.stored_settingskey), lambda: self.save_stored_settings(self.ms, [], self.stored_settingskey_parameterbrowser), lambda: utils.pop_up_info(u'Settings cleared. Restart Export Fieldlogger dialog')])) self.gridLayout_buttons.addWidget(get_line(), 5, 0) self.export_button = PyQt4.QtGui.QPushButton(u'Export') self.export_button.setToolTip(u'Exports the current combination of locations, sublocations and input fields to a Fieldlogger wells file.') self.gridLayout_buttons.addWidget(self.export_button, 7, 0) # Lambda and map is used to run several functions for every button click self.connect(self.export_button, PyQt4.QtCore.SIGNAL("clicked()"), self.export) self.gridLayout_buttons.setRowStretch(8, 1) self.show()
def showtheplot(self, layer): # PlotTS method that, at the moment, performs all the real work provider = layer.dataProvider() #Something with OGR kolumnindex = provider.fieldNameIndex('obsid') # To find the column named 'obsid' if kolumnindex == -1: kolumnindex = provider.fieldNameIndex('OBSID') # backwards compatibility if(layer): nF = layer.selectedFeatureCount() if (nF > 0): myconnection = utils.dbconnection() if myconnection.connect2db() == True: # skapa en cursor curs = myconnection.conn.cursor() # Load all selected observation points ob = layer.selectedFeatures() # Create a plot window with one single subplot fig = plt.figure() # causes conflict with plugins "statist" and "chartmaker" ax = fig.add_subplot(111) if len(self.y3col): nY = 3 elif len(self.y2col): nY = 2 else: nY = 1 p=[None]*nF*nY # List for plot objects plabel=[None]*nF*nY # List for label strings i=0 j=0 for k in ob: # Loop through all selected objects, a plot is added for each one of the observation points (i.e. selected objects) attributes = ob[i] obsid = attributes[kolumnindex] # Copy value in column obsid in the attribute list # Load all observations (full time series) for the object [i] (i.e. selected observation point no i) sql =r"""SELECT """ sql += unicode(self.xcol) #MacOSX fix1 sql += r""" as 'x'""" if len(self.y1col): sql += r""", """ sql += unicode(self.y1col) #MacOSX fix1 sql += r""" as 'y1'""" if len(self.y2col): sql += r""", """ sql += unicode(self.y2col) #MacOSX fix1 sql += r""" as 'y2'""" if len(self.y3col): sql += r""", """ sql += unicode(self.y3col) #MacOSX fix1 sql += r""" as 'y3'""" sql += """ FROM """ sql += unicode(self.table) #MacOSX fix1 sql += r""" WHERE obsid = '""" sql += obsid sql += """' ORDER BY """ sql += unicode(self.xcol) #MacOSX fix1 rs = curs.execute(sql) #Send SQL-syntax to cursor recs = rs.fetchall() # All data are stored in recs """Transform data to a numpy.recarray""" if len(self.y1col): My_format = [('x', float), ('y1', float)] if len(self.y2col): My_format.append(('y2', float)) if len(self.y3col): My_format.append(('y3', float)) table = np.array(recs, dtype=My_format) #NDARRAY table2=table.view(np.recarray) # RECARRAY Makes the two columns inte callable objects, i.e. write table2.values if self.markers==2: # If the checkbox is checked - markers will be plotted, just a line #MacOSX fix1 p[j], = ax.plot(table2.x, table2.y1, marker = 'o', linestyle = '-', label=obsid) # PLOT!! else: p[j], = ax.plot(table2.x, table2.y1, marker = 'None', linestyle = '-', label=obsid) # PLOT!! plabel[j] = obsid + unicode(self.y1col) #+ str(j)# Label for the plot #MacOSX fix1 if len(self.y2col): j = j + 1 if self.markers==2:# If the checkbox is checked - markers will be plotted, just a line #MacOSX fix1 p[j], = ax.plot(table2.x, table2.y2, marker = 'o', linestyle = '-', label=obsid) # PLOT!! else: p[j], = ax.plot(table2.x, table2.y2, marker = 'None', linestyle = '-', label=obsid) # PLOT!! plabel[j] = obsid + unicode(self.y2col) #+ str(j)# Label for the plot #MacOSX fix1 if len(self.y3col): j = j + 1 if self.markers==2: # If the checkbox is checked - markers will be plotted, just a line #MacOSX fix1 p[j], = ax.plot(table2.x, table2.y3, marker = 'o', linestyle = '-', label=obsid) # PLOT!! else: p[j], = ax.plot(table2.x, table2.y3, marker = 'None', linestyle = '-', label=obsid) # PLOT!! plabel[j] = obsid + unicode(self.y3col) #+ str(j)# Label for the plot #MacOSX fix1 j = j + 1 i = i+1 """ Close SQLite-connections """ rs.close() # First close the table myconnection.closedb()# then close the database """ Finish plot """ ax.grid(True) ax.yaxis.set_major_formatter(tick.ScalarFormatter(useOffset=False, useMathText=False)) ax.set_xlabel(self.xcol) #MacOSX fix1 ylabel = unicode(self.y1col) + ", \n" + unicode(self.y2col) + ", \n" + unicode(self.y3col) #MacOSX fix1 ax.set_ylabel(ylabel) ax.set_title(self.settingsdict['xytable']) #MacOSX fix1 leg = fig.legend(p, plabel, loc=0) leg.draggable(state=True) frame = leg.get_frame() # the matplotlib.patches.Rectangle instance surrounding the legend frame.set_facecolor('0.80') # set the frame face color to light gray frame.set_fill(False) # set the frame face color transparent for t in leg.get_texts(): t.set_fontsize(10) # the legend text fontsize for label in ax.xaxis.get_ticklabels(): label.set_fontsize(10) for label in ax.yaxis.get_ticklabels(): label.set_fontsize(10) plt.show() # causes conflict with plugins "statist" and "chartmaker" else: utils.pop_up_info("Please select at least one point with xy data") else: utils.pop_up_info("Please select a layer containing observations with xy data")
def about(self): utils.pop_up_info(msg='This feature is not yet implemented', title='Hold on...')