Exemple #1
0
        private void CopyDatasource(DatasourceConnectionInfo srcInfo, DatasourceConnectionInfo dstInfo)
        {
            try
            {
                String targetPath = dstInfo.Server;

                this.DeleteDatasource(dstInfo);

                m_datasource = m_workspace.Datasources.Create(dstInfo);
                if (m_datasource == null)
                {
                    throw new Exception("Create datasource failed");
                }

                Datasets datasetsToCopy = m_workspace.Datasources[srcInfo.Alias].Datasets;

                // 逐个拷贝数据集
                foreach (Dataset dataset in datasetsToCopy)
                {
                    m_datasource.CopyDataset(dataset, dataset.Name, dataset.EncodeType);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        private void ImportShpByMemory(string filePath)
        {
            //https://blog.csdn.net/tane_e/article/details/89393493


            //https://www.supermap.com/EN/online/Deskpro%206.0/SDMain/html/R_Dataset_Import.htm
            //https://www.supermap.com/EN/online/Deskpro%206.0/SDTechTheme/ExpressHtml/ImEx_ArcGIS_Shape.htm

            Workspace workspace           = new Workspace();
            DatasourceConnectionInfo info = new DatasourceConnectionInfo();

            //mysql数据源
            //设置数据源连接的引擎类型
            info.EngineType = EngineType.MySQL;
            //设置数据库连接字符串
            info.Server        = server;
            info.Database      = database;
            info.User          = userName;
            info.Password      = password;
            info.Driver        = driver;
            info.IsAutoConnect = true;
            info.Alias         = "MySQL"; //不能为空
                                          // 打开数据库数据源
                                          //超图sdk不能直接连接空数据库,需要使用Create方法新建数据库,才有超图“系统表”
            Datasource datasource = workspace.Datasources.Open(info);
            ////udb数据源
            //DatasourceConnectionInfo udbInfo = new DatasourceConnectionInfo();
            ////设置数据源连接的引擎类型
            //udbInfo.EngineType = EngineType.UDB;
            ////设置文件位置
            //udbInfo.Server = @"D:\MicroDesktop\Temp\test";
            //// 创建/打开数据库数据源
            //Datasource udbDatasource = workspace.Datasources.Create(udbInfo);
            //Datasource udbDatasource = workspace.Datasources.Open(udbInfo);

            //Memory数据源
            DatasourceConnectionInfo memInfo = new DatasourceConnectionInfo();

            //设置数据源连接的引擎类型
            memInfo.EngineType = EngineType.Memory;
            memInfo.Alias      = "fdgdfgd";
            memInfo.Server     = "tyjyutjyu";
            // 创建/打开数据库数据源
            Datasource memDatasource = workspace.Datasources.Create(memInfo);

            //svc-矢量数据
            //DatasourceConnectionInfo scvInfo = new DatasourceConnectionInfo();
            ////设置数据源连接的引擎类型
            //scvInfo.EngineType = EngineType.VectorFile;
            ////设置文件位置
            //scvInfo.Server = @"D:\MicroDesktop\Temp\test2";
            //// 创建/打开数据库数据源
            //Datasource scvDatasource = workspace.Datasources.Create(scvInfo);

            if (datasource != null)
            {
                ImportResult result = ImportShpToMemory(filePath, memDatasource);
                if (result.FailedSettings.Length == 0)
                {
                    Console.WriteLine($"导入{filePath}成功!");
                    //for (int i = 0; i < memDatasource.Datasets.Count; i++)
                    //{
                    //    DatasetVector datasetVector = (DatasetVector)memDatasource.Datasets[i];
                    //    Dataset newDataset = datasource.CopyDataset(datasetVector, datasetVector.Name, EncodeType.None);
                    //}


                    DatasetVector datasetVector = (DatasetVector)memDatasource.Datasets[0];
                    //datasource.Datasets.CreateFromTemplate(datasetVector.Name, datasetVector);
                    //var t1=datasource.Datasets.CreateFromTemplate(datasetVector.Name, memDatasource.Datasets[0]);
                    //var t2= datasource.CopyDataset(datasetVector, datasetVector.Name, EncodeType.None);
                    //datasource.Flush(datasetVector.Name);

                    var re = datasetVector.GetRecordset(false, SuperMap.Data.CursorType.Dynamic);
                    //re.AddNew(

                    var v3 = datasource.Datasets.CreateAndAppendWithSmid(targetTableName, re);
                    var v4 = datasource.Datasets.CreateFromTemplate(targetTableName, memDatasource.Datasets[0]);
                    var v5 = datasource.CopyDataset(datasetVector, targetTableName, datasetVector.EncodeType);
                    //datasource.Datasets.Create(datasetVector);
                    var dataset = datasource.Datasets[targetTableName];
                    var ve      = dataset as DatasetVector;
                    var record  = ve?.GetRecordset(false, SuperMap.Data.CursorType.Dynamic);
                    //record.AddNew(...);
                    //var v2= datasource.RecordsetToDataset(re, targetTableName);

                    datasource.Refresh();
                    //String name = datasource.Datasets.GetAvailableDatasetName(targetTableName);
                    // 设置矢量数据集的信息
                    //DatasetVectorInfo datasetVectorInfo = new DatasetVectorInfo();
                    //datasetVectorInfo.Type = DatasetType.Line;
                    //datasetVectorInfo.IsFileCache = true;
                    //datasetVectorInfo.Name = name;
                    //datasetVectorInfo.Bounds = new Rectangle2D(new Point2D(0, 0), new Point2D(10, 10));
                    //Console.WriteLine("矢量数据集的信息为:" + datasetVectorInfo.ToString());

                    //// 创建矢量数据集
                    //datasource.Datasets.Create(datasetVectorInfo);
                    //datasource.Flush(name);

                    //var d2= datasource.CopyDatasetWithSmID(udbDatasource.Datasets[0], targetTableName, EncodeType.None);
                    //var d = datasource.CopyDataset(udbDatasource.Datasets[0], targetTableName, EncodeType.None);
                }
                else
                {
                    Console.WriteLine($"导入{filePath}失败!");
                }
            }


            // 释放工作空间资源
            info.Dispose();
            workspace.Dispose();
        }