def clear_widget(self): """ Clears all content from this widget Returns ------- None """ from pymdwizard.gui import repeating_element widgets = self.findChildren(QWidget, QRegExp(r".*")) for widget in widgets: if isinstance(widget, repeating_element.RepeatingElement): widget.clear_widgets() rep1_widget = widget.get_widgets()[0] if isinstance(rep1_widget, WizardWidget): rep1_widget.clear_widget() elif widget.objectName().startswith("fgdc_"): utils.set_text(widget, "") for widget in widgets: if isinstance(widget, WizardWidget): widget.clear_widget()
def clear_widget(self): """ Clears all content from this widget Returns ------- None """ self.detaileds[0].clear_widget() for i in range(len(self.detaileds), 1, -1): self.ui.fgdc_eainfo.removeTab(i) del self.detaileds[i-1] utils.set_text(self.ui.fgdc_eaover, '') utils.set_text(self.ui.fgdc_eadetcit, '')
def from_xml(self, eainfo): """ parses the xml code into the relevant timeperd elements Parameters ---------- metadata_date - the xml element timeperd and its contents Returns ------- None """ try: self.ui.fgdc_eainfo.setCurrentIndex(0) self.clear_widget() if eainfo.tag == 'eainfo': self.original_xml = eainfo overview = eainfo.xpath('overview') if overview: eaover = eainfo.xpath('overview/eaover') if eaover: utils.set_text(self.ui.fgdc_eaover, eaover[0].text) eadetcit = eainfo.xpath('overview/eadetcit') if eadetcit: utils.set_text(self.ui.fgdc_eadetcit, eadetcit[0].text) self.ui.fgdc_eainfo.setCurrentIndex(2) detailed = eainfo.xpath('detailed') if detailed: self.ui.fgdc_eainfo.setCurrentIndex(1) self.detaileds[0].from_xml(detailed[0]) for i, additional_detailed in enumerate(detailed[1:]): new_detailed = self.add_detailed() self.ui.fgdc_eainfo.setCurrentIndex(i+2) new_detailed.from_xml(additional_detailed) else: print("The tag is not EA") except KeyError: return None return None
def load_pickle(self, contents): self.clear_children() if self.original_xml is not None: self.from_xml(self.original_xml) else: for col_label in contents.keys(): attr_i = mdattr.Attr(parent=self) attr_i.ui.fgdc_attrlabl.setText(col_label) if contents[col_label][b"type"] == "String": s = pd.Series(contents[col_label][b"contents"]) attr_i.set_series(s) attr_i.guess_domain() elif contents[col_label][b"type"] in [ "Integer", "Single", "SmallInteger", "Double", "Date", ]: s = pd.Series(contents[col_label][b"contents"]) attr_i.set_series(s) attr_i.ui.comboBox.setCurrentIndex(1) else: attr_i.populate_domain_content(3) unrep = contents[col_label][b"contents"] utils.set_text(attr_i.ui.fgdc_attrdef, unrep[0].decode("utf-8")) utils.set_text(attr_i.domain.ui.fgdc_udom, unrep[1].decode("utf-8")) utils.set_text(attr_i.ui.fgdc_attrdefs, unrep[2].decode("utf-8")) attr_i.store_current_content() attr_i.supersize_me() attr_i.regularsize_me() self.append_attr(attr_i) try: self.attrs[0].supersize_me() except IndexError: pass
def _from_xml(self, mapproj_node): self.clear_widget() shortname = mapproj_node.tag self.load_projection(shortname) for item in mapproj_node.getchildren(): tag = item.tag item_widget = self.findChild(QLineEdit, "fgdc_" + tag) utils.set_text(item_widget, item.text) stdparll = mapproj_node.xpath('stdparll') try: stdparll_widget = self.findChildren(QLineEdit, "fgdc_stdparll")[0] utils.set_text(stdparll_widget, stdparll[0].text) stdparl_2_widget = self.findChildren(QLineEdit, "fgdc_stdparll_2")[0] utils.set_text(stdparl_2_widget, stdparll[1].text) except: pass
def setText(self, text): utils.set_text(self.added_line, text) cursor = self.added_line.textCursor() cursor.setPosition(0) self.added_line.setTextCursor(cursor)
def from_xml(self, spref_node): self.clear_widget() if spref_node.tag == 'spref': self.original_xml = spref_node self.ui.rbtn_yes.setChecked(True) geograph = xml_utils.search_xpath(spref_node, 'horizsys/geograph') if geograph is not None: self.ui.btn_planar.setChecked(True) self.ui.btn_geographic.setChecked(True) utils.populate_widget_element(self.ui.fgdc_latres, geograph, 'latres') utils.populate_widget_element(self.ui.fgdc_longres, geograph, 'longres') utils.populate_widget_element(self.ui.fgdc_geogunit, geograph, 'geogunit') local = xml_utils.search_xpath(spref_node, 'horizsys/local') if local is not None: self.ui.btn_planar.setChecked(True) self.ui.btn_local.setChecked(True) utils.populate_widget_element(self.ui.fgdc_localdes, local, 'localdes') utils.populate_widget_element(self.ui.fgdc_localgeo, local, 'localgeo') planar = xml_utils.search_xpath(spref_node, 'horizsys/planar') if planar is not None: self.ui.btn_grid.setChecked(True) self.ui.btn_planar.setChecked(True) mapproj = xml_utils.search_xpath(planar, 'mapproj') if mapproj is not None: self.ui.btn_projection.setChecked(True) utils.populate_widget_element(self.ui.fgdc_mapprojn, mapproj, 'mapprojn') mapproj_children = mapproj.getchildren() if len(mapproj_children) > 1: self.mapproj.from_xml(mapproj_children[1]) gridsys = xml_utils.search_xpath(planar, 'gridsys') if gridsys is not None: self.ui.btn_grid.setChecked(True) xml_utils.search_xpath(gridsys, 'gridsysn') utils.populate_widget_element(self.ui.fgdc_gridsysn, gridsys, 'gridsysn') gridsys_children = gridsys.getchildren() if len(gridsys_children) > 1: gridsys_contents = gridsys.getchildren()[1] else: gridsys_contents = [] for item in gridsys_contents.getchildren(): tag = item.tag if spatial_utils.lookup_shortname(tag) is not None: self.grid_mapproj.from_xml(item) elif tag == 'mapproj': mapprojn = xml_utils.search_xpath(item, 'mapprojn') if mapprojn.text in \ spatial_utils.PROJECTION_LOOKUP: self.grid_mapproj.from_xml(item.getchildren()[1]) else: item_widget = self.findChild(QLineEdit, "fgdc_"+tag) utils.set_text(item_widget, item.text) grid_proj = gridsys.xpath('proj') localp = xml_utils.search_xpath(planar, 'localp') if localp: self.ui.btn_localp.setChecked(True) utils.populate_widget_element(self.ui.fgdc_localpd, localp, 'localpd') utils.populate_widget_element(self.ui.fgdc_localpgi, localp, 'localpgi') utils.populate_widget_element(self.ui.fgdc_plance, planar, 'planci/plance') utils.populate_widget_element(self.ui.fgdc_plandu, planar, 'planci/plandu') utils.populate_widget_element(self.ui.fgdc_absres, planar, 'planci/coordrep/absres') utils.populate_widget_element(self.ui.fgdc_ordres, planar, 'planci/coordrep/ordres') self.planar_changed() geodetic = xml_utils.search_xpath(spref_node, 'horizsys/geodetic') if geodetic is not None: utils.populate_widget_element(self.ui.fgdc_horizdn, geodetic, 'horizdn') utils.populate_widget_element(self.ui.fgdc_ellips, geodetic, 'ellips') utils.populate_widget_element(self.ui.fgdc_semiaxis, geodetic, 'semiaxis') utils.populate_widget_element(self.ui.fgdc_denflat, geodetic, 'denflat') vertdef = xml_utils.search_xpath(spref_node, 'vertdef') if vertdef is not None: self.vertdef.from_xml(vertdef)
def populate_from_fname(self, fname): if fname.endswith("$"): fname, sheet_name = os.path.split(fname) sheet_name = sheet_name[:-1] ok = True else: sheet_name = None shortname = os.path.split(fname)[1] ext = os.path.splitext(shortname)[1] self.ui.fgdc_enttypds.setText(default_def_source) if ext.lower() == ".csv": try: self.clear_widget() self.ui.fgdc_enttypl.setText(shortname) self.ui.fgdc_enttypd.setPlainText( "Comma Separated Value (CSV) file containing data.") df = data_io.read_data(fname) max_rows = int(utils.get_setting("maxrows", 1000000)) if df.shape[0] == max_rows: msg = "This CSV file contains more than" " {:,} rows!".format( data_io.MAX_ROWS) msg += ("\n\n Due to speed and memory constraints, " "\ndata from rows past\nthe first {:,} rows" "".format(max_rows)) msg += "\nwere not used " "to populate this section.".format( max_rows) msg += ( "\n\nCheck that the values displayed are " "complete \nand appropriate for the entire record.") QMessageBox.warning(self, "Large File Warning", msg) self.attributes.load_df(df) except BaseException as e: import traceback msg = "Cannot read csv %s:\n%s." % (fname, traceback.format_exc()) QMessageBox.warning(self, "Recent Files", msg) elif ext.lower() == ".shp": self.clear_widget() self.ui.fgdc_enttypl.setText(shortname + " Attribute Table") self.ui.fgdc_enttypd.setPlainText( "Table containing attribute information associated with the data set." ) df = data_io.read_data(fname) self.attributes.load_df(df) fid_attr = self.attributes.get_attr("FID") if fid_attr is not None: fid_attr.populate_domain_content(3) fid_attr.ui.fgdc_attrdef.setPlainText( "Internal feature number.") utils.set_text(fid_attr.ui.fgdc_attrdefs, "ESRI") fid_attr.domain.ui.fgdc_udom.setPlainText( "Sequential unique whole numbers that are automatically generated." ) fid_attr.regularsize_me() fid_attr.supersize_me() shape_attr = self.attributes.get_attr("Shape") if shape_attr is not None: shape_attr.populate_domain_content(3) shape_attr.ui.fgdc_attrdef.setPlainText("Feature geometry.") utils.set_text(shape_attr.ui.fgdc_attrdefs, "ESRI") shape_attr.domain.ui.fgdc_udom.setPlainText( "Coordinates defining the features.") shape_attr.store_current_content() shape_attr.supersize_me() shape_attr.store_current_content() shape_attr.regularsize_me() elif ext.lower() in [".xlsm", ".xlsx", ".xls"]: if sheet_name is None: sheets = data_io.get_sheet_names(fname) sheet_name, ok = QInputDialog.getItem( self, "select sheet dialog", "Pick one of the sheets from this workbook", sheets, 0, False, ) if ok and sheet_name: self.clear_widget() self.ui.fgdc_enttypl.setText("{} ({})".format( shortname, sheet_name)) self.ui.fgdc_enttypd.setPlainText("Excel Worksheet") df = data_io.read_excel(fname, sheet_name) self.attributes.load_df(df) elif ext.lower() in [ ".tif", ".grd", ".png", ".img", ".jpg", ".hdr", ".bmp", ".adf", ]: self.ui.fgdc_enttypl.setText(shortname) num_bands = spatial_utils.get_band_count(fname) if num_bands == 1: self.ui.fgdc_enttypd.setPlainText( "Raster geospatial data file.") else: self.ui.fgdc_enttypd.setPlainText( "{} band raster geospatial data file.".format(num_bands)) df = get_raster_attribute_table(fname) self.attributes.load_df(df) oid_attr = self.attributes.get_attr("OID") if oid_attr is not None: oid_attr.populate_domain_content(3) oid_attr.ui.fgdc_attrdef.setPlainText( "Internal object identifier.") oid_attr.domain.ui.fgdc_udom.setPlainText( "Sequential unique whole numbers that are automatically generated." ) oid_attr.regularsize_me() oid_attr.supersize_me() value_attr = self.attributes.get_attr("Value") if value_attr is not None: value_attr.populate_domain_content(1) value_attr.ui.fgdc_attrdef.setPlainText( "Unique numeric values contained in each raster cell.") count_attr = self.attributes.get_attr("Count") if count_attr is not None: count_attr.populate_domain_content(1) count_attr.ui.fgdc_attrdef.setPlainText( "Number of raster cells with this value.") elif ext.lower() == ".p": p = pickle.load(open(fname, "rb"), encoding="bytes") if self.original_xml is not None: original_content = xml_utils.XMLNode(self.original_xml) self.from_xml(self.original_xml) else: self.ui.fgdc_enttypl.setText("{}".format(shortname[:-2])) self.ui.fgdc_enttypd.setPlainText("Geospatial Dataset") self.attributes.load_pickle(p) elif ext.lower() == ".txt": if sheet_name is None: delimiters = { "comma": ",", "tab": "\t", "pipe": "|", "colon": ":" } delimiter_str, ok = QInputDialog.getItem( self, "Select text delimiter", "Pick the delimiter used in this file", delimiters.keys(), 0, False, ) delimiter = delimiters[delimiter_str] if ok and delimiter: try: self.clear_widget() self.ui.fgdc_enttypl.setText(shortname) self.ui.fgdc_enttypd.setPlainText( "{} delimited text file.".format(delimiter_str)) df = data_io.read_data(fname, delimiter=delimiter) self.attributes.load_df(df) except BaseException as e: import traceback msg = "Cannot read txt file %s:\n%s." % ( fname, traceback.format_exc(), ) QMessageBox.warning(self, "File load problem", msg) else: msg = "Can only read '.csv', '.txt', '.shp', raster files, and Excel files here" QMessageBox.warning(self, "Unsupported file format", msg)
def populate_from_fname(self, fname): if fname.endswith('$'): fname, sheet_name = os.path.split(fname) sheet_name = sheet_name[:-1] ok = True else: sheet_name = None shortname = os.path.split(fname)[1] ext = os.path.splitext(shortname)[1] self.ui.fgdc_enttypds.setText('Producer defined') if ext.lower() == '.csv': try: self.clear_widget() self.ui.fgdc_enttypl.setText(shortname) self.ui.fgdc_enttypd.setPlainText( 'Comma Separated Value (CSV) file containing data.') df = data_io.read_data(fname) if df.shape[0] == data_io.MAX_ROWS: msg = "This CSV file contains more than" \ " {:,} rows!".format(data_io.MAX_ROWS) msg += "\n\n Due to speed and memory constraints, " \ "\ndata from rows past\nthe first {:,} rows" \ "".format(data_io.MAX_ROWS) msg += "\nwere not used " \ "to populate this section.".format(data_io.MAX_ROWS) msg += '\n\nCheck that the values displayed are ' \ 'complete \nand appropriate for the entire record.' QMessageBox.warning(self, "Large File Warning", msg) self.attributes.load_df(df) except BaseException as e: import traceback msg = "Cannot read csv %s:\n%s." % (fname, traceback.format_exc()) QMessageBox.warning(self, "Recent Files", msg) elif ext.lower() == '.shp': self.clear_widget() self.ui.fgdc_enttypl.setText(shortname + ' Attribute Table') self.ui.fgdc_enttypd.setPlainText( 'Table containing attribute information associated with the data set.' ) df = data_io.read_data(fname) self.attributes.load_df(df) fid_attr = self.attributes.get_attr('FID') if fid_attr is not None: fid_attr.populate_domain_content(3) fid_attr.ui.fgdc_attrdef.setPlainText( 'Internal feature number.') utils.set_text(fid_attr.ui.fgdc_attrdefs, 'ESRI') fid_attr.domain.ui.fgdc_udom.setPlainText( "Sequential unique whole numbers that are automatically generated." ) fid_attr.regularsize_me() fid_attr.supersize_me() shape_attr = self.attributes.get_attr('Shape') if shape_attr is not None: shape_attr.populate_domain_content(3) shape_attr.ui.fgdc_attrdef.setPlainText('Feature geometry.') utils.set_text(shape_attr.ui.fgdc_attrdefs, 'ESRI') shape_attr.domain.ui.fgdc_udom.setPlainText( "Coordinates defining the features.") shape_attr.store_current_content() shape_attr.supersize_me() shape_attr.regularsize_me() elif ext.lower() in ['.xlsm', '.xlsx', '.xls']: if sheet_name is None: sheets = data_io.get_sheet_names(fname) sheet_name, ok = QInputDialog.getItem( self, "select sheet dialog", "Pick one of the sheets from this workbook", sheets, 0, False) if ok and sheet_name: self.clear_widget() self.ui.fgdc_enttypl.setText('{} ({})'.format( shortname, sheet_name)) self.ui.fgdc_enttypd.setPlainText('Excel Worksheet') df = data_io.read_excel(fname, sheet_name) self.attributes.load_df(df) elif ext.lower() in [ '.tif', '.grd', '.png', '.img', '.jpg', '.hdr', '.bmp', '.adf' ]: self.ui.fgdc_enttypl.setText(shortname) num_bands = spatial_utils.get_band_count(fname) if num_bands == 1: self.ui.fgdc_enttypd.setPlainText( 'Raster geospatial data file.') else: self.ui.fgdc_enttypd.setPlainText( '{} band raster geospatial data file.'.format(num_bands)) df = get_raster_attribute_table(fname) self.attributes.load_df(df) oid_attr = self.attributes.get_attr('OID') if oid_attr is not None: oid_attr.populate_domain_content(3) oid_attr.ui.fgdc_attrdef.setPlainText( 'Internal object identifier.') oid_attr.domain.ui.fgdc_udom.setPlainText( 'Sequential unique whole numbers that are automatically generated.' ) oid_attr.regularsize_me() oid_attr.supersize_me() value_attr = self.attributes.get_attr('Value') if value_attr is not None: value_attr.populate_domain_content(1) value_attr.ui.fgdc_attrdef.setPlainText( 'Unique numeric values contained in each raster cell.') count_attr = self.attributes.get_attr('Count') if count_attr is not None: count_attr.populate_domain_content(1) count_attr.ui.fgdc_attrdef.setPlainText( 'Number of raster cells with this value.') elif ext.lower() == ".p": p = pickle.load(open(fname, "rb"), encoding='bytes') if self.original_xml is not None: original_content = xml_utils.XMLNode(self.original_xml) self.from_xml(self.original_xml) else: self.ui.fgdc_enttypl.setText('{}'.format(shortname[:-2])) self.ui.fgdc_enttypd.setPlainText('Geospatial Dataset') self.attributes.load_pickle(p) elif ext.lower() == '.txt': if sheet_name is None: delimiters = { 'comma': ',', 'tab': '\t', 'pipe': '|', 'colon': ':' } delimiter_str, ok = QInputDialog.getItem( self, "Select text delimiter", "Pick the delimiter used in this file", delimiters.keys(), 0, False) delimiter = delimiters[delimiter_str] if ok and delimiter: try: self.clear_widget() self.ui.fgdc_enttypl.setText(shortname) self.ui.fgdc_enttypd.setPlainText( '{} delimited text file.'.format(delimiter_str)) df = data_io.read_data(fname, delimiter=delimiter) self.attributes.load_df(df) except BaseException as e: import traceback msg = "Cannot read txt file %s:\n%s." % ( fname, traceback.format_exc()) QMessageBox.warning(self, "File load problem", msg) else: msg = "Can only read '.csv', '.txt', '.shp', raster files, and Excel files here" QMessageBox.warning(self, "Unsupported file format", msg)
def from_xml(self, spref_node): self.clear_widget() if spref_node.tag == "spref": self.original_xml = spref_node self.ui.rbtn_yes.setChecked(True) geograph = xml_utils.search_xpath(spref_node, "horizsys/geograph") if geograph is not None: self.ui.btn_planar.setChecked(True) self.ui.btn_geographic.setChecked(True) utils.populate_widget_element(self.ui.fgdc_latres, geograph, "latres") utils.populate_widget_element(self.ui.fgdc_longres, geograph, "longres") utils.populate_widget_element(self.ui.fgdc_geogunit, geograph, "geogunit") local = xml_utils.search_xpath(spref_node, "horizsys/local") if local is not None: self.ui.btn_planar.setChecked(True) self.ui.btn_local.setChecked(True) utils.populate_widget_element(self.ui.fgdc_localdes, local, "localdes") utils.populate_widget_element(self.ui.fgdc_localgeo, local, "localgeo") planar = xml_utils.search_xpath(spref_node, "horizsys/planar") if planar is not None: self.ui.btn_grid.setChecked(True) self.ui.btn_planar.setChecked(True) mapproj = xml_utils.search_xpath(planar, "mapproj") if mapproj is not None: self.ui.btn_projection.setChecked(True) utils.populate_widget_element(self.ui.fgdc_mapprojn, mapproj, "mapprojn") mapproj_children = mapproj.getchildren() if len(mapproj_children) > 1: self.mapproj.from_xml(mapproj_children[1]) gridsys = xml_utils.search_xpath(planar, "gridsys") if gridsys is not None: self.ui.btn_grid.setChecked(True) xml_utils.search_xpath(gridsys, "gridsysn") utils.populate_widget_element(self.ui.fgdc_gridsysn, gridsys, "gridsysn") gridsys_children = gridsys.getchildren() if len(gridsys_children) > 1: gridsys_contents = gridsys.getchildren()[1] else: gridsys_contents = [] for item in gridsys_contents.getchildren(): tag = item.tag if spatial_utils.lookup_shortname(tag) is not None: self.grid_mapproj.from_xml(item) elif tag == "mapproj": mapprojn = xml_utils.search_xpath(item, "mapprojn") if mapprojn.text in spatial_utils.PROJECTION_LOOKUP: self.grid_mapproj.from_xml( item.getchildren()[1]) else: item_widget = self.findChild( QLineEdit, "fgdc_" + tag) utils.set_text(item_widget, item.text) grid_proj = gridsys.xpath("proj") localp = xml_utils.search_xpath(planar, "localp") if localp: self.ui.btn_localp.setChecked(True) utils.populate_widget_element(self.ui.fgdc_localpd, localp, "localpd") utils.populate_widget_element(self.ui.fgdc_localpgi, localp, "localpgi") utils.populate_widget_element(self.ui.fgdc_plance, planar, "planci/plance") utils.populate_widget_element(self.ui.fgdc_plandu, planar, "planci/plandu") utils.populate_widget_element(self.ui.fgdc_absres, planar, "planci/coordrep/absres") utils.populate_widget_element(self.ui.fgdc_ordres, planar, "planci/coordrep/ordres") self.planar_changed() geodetic = xml_utils.search_xpath(spref_node, "horizsys/geodetic") if geodetic is not None: utils.populate_widget_element(self.ui.fgdc_horizdn, geodetic, "horizdn") utils.populate_widget_element(self.ui.fgdc_ellips, geodetic, "ellips") utils.populate_widget_element(self.ui.fgdc_semiaxis, geodetic, "semiaxis") utils.populate_widget_element(self.ui.fgdc_denflat, geodetic, "denflat") vertdef = xml_utils.search_xpath(spref_node, "vertdef") if vertdef is not None: self.vertdef.from_xml(vertdef)