Ejemplo n.º 1
0
    def run_transform_pointwise(self, outSRS, transform_func, inpath=None, inlayername=None,
                                outpath=None, outlayername=None, outformat=None):
        if inpath is None: inpath = self.in_path
        if inlayername is None: inlayername = self.in_layername
        if outpath is None: outpath = self.out_path
        if outlayername is None: outlayername = self.out_layername
        if outSRS is None: return None
        if outformat is None: outformat = self.out_format
        if transform_func is None: return None

        res = False
        self.in_wks.openFromFile(inpath)
        in_layer = self.in_wks.openLayer(inlayername)

        # out_DS = out_wks.openFromFile(self.out_path)
        out_wks = workspaceFactory().get_factory(outformat)
        out_path, out_layername = out_wks.cloneLayer(in_layer, outpath,
                                                          outlayername, outSRS, outformat)

        if out_path is not None:
            outDS = out_wks.openFromFile(out_path)
            out_layer = outDS.GetLayer(out_layername)
            res = self.transform_pointwise(in_layer, out_layer, transform_func)

        if res:
            return out_path, out_layername
        else:
            return None
Ejemplo n.º 2
0
def coordTransform(inpath, inlayer, insrs, outpath, outlayer, outsrs):
    if inpath[-1] == os.sep:
        inpath = inpath[:-1]
    if outpath[-1] == os.sep:
        outpath = outpath[:-1]

    in_format = get_suffix(inpath)

    if in_format == DataType.cad_dwg:
        checked_insrs = insrs
        checked_outsrs = outsrs
    else:
        in_wks = workspaceFactory().get_factory(in_format)

        if in_wks is None:
            return False

        if in_wks.openFromFile(inpath) is None:
            log.error("无法读取输入文件!可能原因:数据引擎未加载或者数据格式不正确.")
            return False
        in_layer = in_wks.openLayer(inlayer)

        if in_layer is None:
            log.error("输入图层不存在!")
            return False

        srs_ref = in_layer.GetSpatialRef()
        # in_DS.Release()
        in_layer = None

        checked_insrs = check_srs(insrs, srs_ref)
        if checked_insrs == -2435:
            log.error("输入的空间参考在ESPG中不存在!")
            return False
        elif checked_insrs == -4547:
            log.error("不支持输入空间数据的坐标转换!")
            return False

        checked_outsrs = check_srs(outsrs, outlayer)
        if checked_outsrs == -2435:
            log.error("输出的空间参考在ESPG中不存在!")
            return False
        elif checked_outsrs == -4547:
            log.error("不支持输出空间数据的坐标转换!")
            return False

    out_format = get_suffix(outpath)

    try:
        tfer = Transformer(in_format, out_format, inpath, inlayer, outpath, outlayer)
        tfer.transform(checked_insrs, checked_outsrs)
        return True
    except:
        log.error(traceback.format_exc())
        return False
Ejemplo n.º 3
0
    def __init__(self, in_format, out_format, inpath, inlayername, outpath, outlayername):
        self.out_format = out_format
        self.in_format = in_format
        self.in_layername = inlayername
        self.out_layername = outlayername
        self.in_path = inpath
        self.out_path = outpath

        self.lco = []

        if out_format == DataType.shapefile:
            self.lco = ["ENCODING=GBK"]
            self.out = outpath
        elif out_format == DataType.fileGDB:
            self.lco = ["FID=FID"]
            self.out = os.path.join(outpath, outlayername)

        self.in_wks = workspaceFactory().get_factory(self.in_format)
Ejemplo n.º 4
0
    def check_paras(self):
        rows = range(0, self.tbl_address.model().rowCount(QModelIndex()))
        for row in rows:
            url_index, service_index, url, service = self.return_url_and_level(
                row)
            layername_index = self.tbl_address.model().index(
                row, 2, QModelIndex())
            output_index = self.tbl_address.model().index(
                row, 3, QModelIndex())
            layername = str(self.tbl_address.model().data(
                layername_index, Qt.DisplayRole)).strip()
            output = str(self.tbl_address.model().data(
                output_index, Qt.DisplayRole)).strip()

            if url == "":
                log.error('第{}行缺失必要参数"地址",请补全!'.format(row), dialog=True)
                return False
            if service == "":
                log.error('第{}行缺失必要参数"服务号",请补全!'.format(row), dialog=True)
                return False

            # in_format = get_suffix(in_path)
            # if in_format is None:
            #     log.error('第{}行的输入数据格式不支持!目前只支持csv, excel和dbf'.format(row), dialog=True)
            #     return False
            # else:
            #     in_wks = workspaceFactory().get_factory(in_format)
            #     if in_wks.driver is None:
            #         log.error("缺失图形文件引擎{}!".format(in_wks.driverName))
            #         return False

            out_format = get_suffix(output)
            out_wks = workspaceFactory().get_factory(DataType.fileGDB)

            if out_wks.driver is None:
                log.error("缺失图形文件引擎{}!".format(out_wks.driverName))
                return False

            if service != "*":
                key = url + "_" + str(service)
                url_lst = url.split(r'/')
                service_name = url_lst[-2]
                # filepath, filename = os.path.split(output)

                if layername == "":
                    layername = self.paras[key]['old_layername']
                    log.warning('第{}行缺失非必要参数"输出图层名",将使用默认值"{}".'.format(
                        row + 1, layername))

                if out_format != DataType.fileGDB:
                    # if os.path.splitext(filename)[1] != '.gdb':
                    gdb_name = service_name + ".gdb"
                    if output == "":
                        if not os.path.exists("res"):
                            os.makedirs("res")
                        output = os.path.join(os.path.abspath("res"), gdb_name)
                        self.paras[key]['output'] = output
                        log.warning('第{}行缺失非必要参数"输出路径",将使用默认值"{}".'.format(
                            row + 1, output))
                    else:
                        output = os.path.join(output, gdb_name)
                        self.paras[key]['output'] = output
                        log.warning(
                            '第{}行"输出路径"参数缺失输出gdb数据库名,将使用默认值"{}".'.format(
                                row + 1, gdb_name))
            else:
                filepath, filename = os.path.split(output)
                key_all = url + "_*"
                # if os.path.splitext(filename)[1] != '.gdb':
                if out_format != DataType.fileGDB:
                    gdb_name = urlEncodeToFileName(url) + ".gdb"
                    output = os.path.join(output, gdb_name)
                    self.paras[key_all]['output'] = output
                    log.warning('第{}行缺失非必要参数"输出路径",将使用默认值"{}".'.format(
                        row + 1, output))
        return True
Ejemplo n.º 5
0
    def check_paras(self):
        rows = range(0, self.tbl_address.model().rowCount(QModelIndex()))

        if self.rbtn_table.isChecked():
            for row in rows:
                in_path_index = self.tbl_address.model().index(
                    row, 0, QModelIndex())
                x_field_index = self.tbl_address.model().index(
                    row, 1, QModelIndex())
                y_field_index = self.tbl_address.model().index(
                    row, 2, QModelIndex())
                in_srs_index = self.tbl_address.model().index(
                    row, 3, QModelIndex())
                out_srs_index = self.tbl_address.model().index(
                    row, 4, QModelIndex())
                out_path_index = self.tbl_address.model().index(
                    row, 5, QModelIndex())

                in_path = str(self.tbl_address.model().data(
                    in_path_index, Qt.DisplayRole)).strip()
                x_field = str(self.tbl_address.model().data(
                    x_field_index, Qt.DisplayRole)).strip()
                y_field = str(self.tbl_address.model().data(
                    y_field_index, Qt.DisplayRole)).strip()
                in_srs = str(self.tbl_address.model().data(
                    in_srs_index, Qt.DisplayRole)).strip()
                out_srs = str(self.tbl_address.model().data(
                    out_srs_index, Qt.DisplayRole)).strip()
                out_path = str(self.tbl_address.model().data(
                    out_path_index, Qt.DisplayRole)).strip()

                if in_path == "":
                    log.error('第{}行缺失必要参数"输入路径",请补全!'.format(row), dialog=True)
                    return False

                in_format = get_suffix(in_path)
                if in_format is None:
                    log.error('第{}行的输入数据格式不支持!目前只支持csv, excel和dbf'.format(row),
                              dialog=True)
                    return False

                if x_field == "":
                    log.error('第{}行缺失必要参数"x坐标",请补全!'.format(row))
                    return False

                if y_field == "":
                    log.error('第{}行缺失必要参数"y坐标",请补全!'.format(row))
                    return False

                if in_srs == "":
                    log.error('第{}行缺失必要参数"输入坐标系",请补全!'.format(row))
                    return False

                if out_srs == "":
                    log.error('第{}行缺失必要参数"输出坐标系",请补全!'.format(row))
                    return False

                if out_path == "":
                    in_filename, in_suffix = os.path.splitext(
                        os.path.basename(in_path))
                    out_file = self.default_outfile(in_path, in_format,
                                                    in_filename, out_srs)
                    if not os.path.exists("res"):
                        os.makedirs("res")

                    out_path = os.path.join(os.path.abspath("res"), out_file)

                if is_already_opened_in_write_mode(out_path):
                    out_path = launderName(out_path)
                self.tbl_address.model().setData(out_path_index, out_path)
                log.warning('第{}行参数"输出路径"缺失数据源,自动补全为默认值{}'.format(
                    row, out_path))
        else:
            for row in rows:
                in_path_index = self.tbl_address.model().index(
                    row, 0, QModelIndex())
                in_layername_index = self.tbl_address.model().index(
                    row, 1, QModelIndex())
                in_srs_index = self.tbl_address.model().index(
                    row, 2, QModelIndex())
                out_srs_index = self.tbl_address.model().index(
                    row, 3, QModelIndex())
                out_path_index = self.tbl_address.model().index(
                    row, 4, QModelIndex())
                out_layername_index = self.tbl_address.model().index(
                    row, 5, QModelIndex())

                in_path = str(self.tbl_address.model().data(
                    in_path_index, Qt.DisplayRole)).strip()
                in_layername = str(self.tbl_address.model().data(
                    in_layername_index, Qt.DisplayRole)).strip()
                in_srs = str(self.tbl_address.model().data(
                    in_srs_index, Qt.DisplayRole)).strip()
                out_srs = str(self.tbl_address.model().data(
                    out_srs_index, Qt.DisplayRole)).strip()
                out_path = str(self.tbl_address.model().data(
                    out_path_index, Qt.DisplayRole)).strip()
                out_layername = str(self.tbl_address.model().data(
                    out_layername_index, Qt.DisplayRole)).strip()

                if in_path == "":
                    log.error('第{}行缺失必要参数"输入路径",请补全!'.format(row), dialog=True)
                    return False

                in_format = get_suffix(in_path)
                if in_format is None:
                    log.error(
                        '第{}行的输入数据格式不支持!目前只支持shapefile, fileGDB, geojson和cad dwg'
                        .format(row),
                        dialog=True)
                    return False
                else:
                    in_wks = workspaceFactory().get_factory(in_format)
                    if in_wks is None:
                        log.error("缺失图形文件引擎!")
                        return False

                    if in_wks.driver is None:
                        log.error("缺失图形文件引擎{}!".format(in_wks.driverName))
                        return False

                if in_layername == "":
                    if in_format == DataType.fileGDB:
                        log.error('第{}行参数缺失必要参数"输入图层"!')
                        return False
                    else:
                        in_layername, suffix = os.path.splitext(
                            os.path.basename(in_path))
                        self.tbl_address.model().setData(
                            in_layername_index, in_layername)
                        log.warning('第{}行参数缺失参数"输入图层",已自动补全为{}'.format(
                            row, in_layername))

                if in_srs == "":
                    log.error('第{}行缺失必要参数"输入坐标系",请补全!'.format(row),
                              dialog=True)
                    return False

                if out_srs == "":
                    log.error('第{}行缺失必要参数"输出坐标系",请补全!'.format(row),
                              dialog=True)
                    return False

                if out_layername == "":
                    out_layername = in_layername + "_" + str(out_srs)
                    self.tbl_address.model().setData(out_layername_index,
                                                     out_layername)
                    log.warning('第{}行参数缺失参数"输出图层",自动补全为默认值{}'.format(
                        row, out_layername))

                self.autofill_outpath(row, in_path, out_path, in_layername,
                                      out_srs, in_format, out_path_index)

        return True
Ejemplo n.º 6
0
    def btn_addRow_clicked(self):
        try:
            save_srs_list = list(srs_dict.values())

            if self.rbtn_file.isChecked():
                fileNames, types = QFileDialog.getOpenFileNames(
                    self, "选择需要转换的图形文件", os.getcwd(),
                    "图形文件(*.shp *.geojson *.dwg);;ESRI Shapefile(*.shp);;GeoJson(*.geojson);;CAD drawing(*.dwg)"
                )
                if len(fileNames) == 0:
                    return

                for fileName in fileNames:
                    fileType = get_suffix(fileName)
                    if fileType == DataType.shapefile:
                        wks = workspaceFactory().get_factory(
                            DataType.shapefile)
                    elif fileType == DataType.geojson:
                        wks = workspaceFactory().get_factory(DataType.geojson)
                    elif fileType == DataType.cad_dwg:
                        wks = workspaceFactory().get_factory(DataType.cad_dwg)
                    else:
                        log.error("不识别的图形文件格式!", dialog=True)
                        return None

                    datasource = wks.openFromFile(fileName)

                    if datasource is not None:
                        layer_name = wks.getLayerNames()[0]
                        in_layer = datasource.GetLayer()
                        row = self.add_layer_to_row(in_layer, fileName,
                                                    layer_name)
                        # self.add_delegate_to_row(row, fileName, [layer_name], save_srs_list)
                        levelData = {
                            'layer_names': [layer_name],
                            'srs_list': save_srs_list
                        }
                        self.model.setLevelData(fileName, levelData)

                        datasource.Release()
                        datasource = None
                        in_layer = None
                    else:
                        layer_name, suffix = os.path.splitext(
                            os.path.basename(fileName))
                        row = self.add_layer_to_row(None, fileName, layer_name)
                        levelData = {
                            'layer_names': [layer_name],
                            'srs_list': save_srs_list
                        }
                        self.model.setLevelData(fileName, levelData)
                        # self.add_delegate_to_row(row, fileName, [layer_name], save_srs_list)

            elif self.rbtn_filedb.isChecked():
                fileName = QtWidgets.QFileDialog.getExistingDirectory(
                    self, "选择需要转换的GDB数据库", os.getcwd(),
                    QFileDialog.ShowDirsOnly)
                wks = workspaceFactory().get_factory(DataType.fileGDB)
                datasource = wks.openFromFile(fileName)

                if datasource is not None:
                    lst_names = wks.getLayerNames()
                    selected_names = None
                    if len(lst_names) > 1:
                        selected_names = nameListDialog().openListDialog(
                            "请选择要转换的图层", lst_names)
                    elif len(lst_names) == 1:
                        selected_names = [lst_names[0]]

                    rows = []
                    if selected_names is not None:
                        for selected_name in selected_names:
                            layer = wks.openLayer(selected_name)
                            row = self.add_layer_to_row(
                                layer, fileName, selected_name)
                            rows.append(row)

                        for row in rows:
                            levelData = {
                                'layer_names': lst_names,
                                'srs_list': save_srs_list
                            }
                            self.model.setLevelData(fileName, levelData)
                            # self.add_delegate_to_row(row, fileName, lst_names, save_srs_list)

            elif self.rbtn_table.isChecked():
                fileNames, types = QFileDialog.getOpenFileNames(
                    self, "选择需要转换的表格文件", os.getcwd(),
                    "表格文件(*.csv *.xlsx *.dbf);;csv文件(*.csv);;excel文件(*.xlsx);;dbf文件(*.dbf)"
                )
                if len(fileNames) == 0:
                    return
                for fileName in fileNames:
                    fileType = get_suffix(fileName)
                    row = self.add_table_to_row(fileName)

                    if fileType == DataType.xlsx:
                        # selected_sheet = []
                        # wb = load_workbook(fileName, read_only=True)
                        # wb.close()
                        # lst_names = wb.sheetnames
                        # if len(lst_names) > 1:
                        #     selected_sheet = nameListDialog().openListDialog(
                        #         "请选择工作表(sheet)", lst_names, QAbstractItemView.SingleSelection)
                        # elif len(lst_names) == 1:
                        #     selected_sheet = lst_names[0]
                        header, bheader = read_table_header(
                            fileName, fileType, None)
                        levelData = {
                            'is_header': bheader,
                            # 'sheet': selected_sheet[0],
                            'field_list': header,
                            'srs_list': save_srs_list
                        }
                    elif fileType == DataType.csv:
                        header, encoding, bheader = read_table_header(
                            fileName, fileType)
                        levelData = {
                            'is_header': bheader,
                            'encoding': encoding,
                            'field_list': header,
                            'srs_list': save_srs_list
                        }
                    elif fileType == DataType.dbf:
                        header = read_table_header(fileName, fileType)
                        levelData = {
                            'is_header': True,
                            'field_list': header,
                            'srs_list': save_srs_list
                        }
                    else:
                        log.error("不识别的图形文件格式!", dialog=True)
                        return None

                    field_delegate = xyfieldDelegate(self, [
                        None, {
                            'type': 'xy'
                        }, {
                            'type': 'xy'
                        }, {
                            'type': 'srs'
                        }, {
                            'type': 'srs'
                        }, {
                            'type': 'f',
                            'text': '请选择需要保存的文件'
                        }
                    ])
                    self.tbl_address.setItemDelegateForRow(row, field_delegate)
                    self.model.setLevelData(fileName, levelData)
        except:
            log.error(traceback.format_exc())