Пример #1
0
    def populate_from_fname(self, fname):
        shortname = os.path.split(fname)[1]

        ext = os.path.splitext(shortname)[1]
        if ext.lower() == '.csv':
            self.ui.fgdc_enttypl.setText(shortname)
            self.ui.fgdc_enttypd.setPlainText('Comma Separate Value (CSV) file containing data.')

            df = data_io.read_data(fname)
            self.attributes.load_df(df)
        elif ext.lower() == '.shp':
            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)
        elif ext.lower() in ['.xlsm', '.xlsx', '.xls']:
            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.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)
        else:
            msg = "Can only read '.csv', '.shp', and Excel files here"
            QMessageBox.warning(self, "Unsupported file format", msg)
Пример #2
0
    def populate_from_fname(self, fname):
        shortname = os.path.split(fname)[1]

        ext = os.path.splitext(shortname)[1]
        if ext.lower() == '.csv':
            try:
                self.clear_widget()
                self.ui.fgdc_enttypl.setText(shortname)
                self.ui.fgdc_enttypd.setPlainText('Comma Separate Value (CSV) file containing data.')

                df = data_io.read_data(fname)
                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)

        elif ext.lower() in ['.xlsm', '.xlsx', '.xls']:
            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() == ".p":
            p = pickle.load(open(fname, "rb"), encoding='bytes')

            self.ui.fgdc_enttypl.setText('{}'.format(shortname[:-2]))
            self.ui.fgdc_enttypd.setPlainText('Geospatial Dataset')

            self.attributes.load_pickle(p)
        else:
            msg = "Can only read '.csv', '.shp', and Excel files here"
            QMessageBox.warning(self, "Unsupported file format", msg)
Пример #3
0
    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)
Пример #4
0
    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)