private void btnPolygonDataset_Click(object sender, EventArgs e) { try { SaveFileDialog fbd = new SaveFileDialog(); fbd.Title = "新建File Geodatabase"; if (fbd.ShowDialog() == DialogResult.OK) { if (System.IO.Directory.Exists(fbd.FileName + ".gdb") == true) { MessageBox.Show("GDB已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } //IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass(); //IWorkspaceName workspaceName = workspaceFactory.Create(System.IO.Path.GetDirectoryName(fbd.FileName), System.IO.Path.GetFileName(fbd.FileName), null, 0); ClsGDBDataCommon com = new ClsGDBDataCommon(); com.Create_FileGDB(System.IO.Path.GetDirectoryName(fbd.FileName), System.IO.Path.GetFileName(fbd.FileName) + ".gdb"); IWorkspace pws = com.OpenFromFileGDB(fbd.FileName + ".gdb"); pFeatureDataset = creatdataset(pws, System.IO.Path.GetFileName(fbd.FileName), pMapControl.SpatialReference); creatfeatureclass(System.IO.Path.GetFileName(fbd.FileName)); txtPolygonDataset.Text = fbd.FileName + ".gdb"; } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } }
private bool ExportLayers(IMapDocument mapDoc, string strGdbName, bool bLayerSpatial) { //打开GDB工作空间 ClsGDBDataCommon cls = new ClsGDBDataCommon(); //IWorkspace wks = (IWorkspace)cls.OpenFromFileGDB(strGdbName); IWorkspace2 wksFeature = (IWorkspace2)cls.OpenFromFileGDB(strGdbName); if (wksFeature == null) { return(false); } IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); IWorkspace wksRaster = pWSF.OpenFromFile(System.IO.Path.GetDirectoryName(mapDoc.DocumentFilename), 0); if (wksRaster == null) { return(false); } for (int i = 0; i < mapDoc.MapCount; i++) { IMap map = mapDoc.get_Map(i); if (map == null) { continue; } ILayer layer = null; IEnumLayer pEnumLayer = map.get_Layers(null, true); pEnumLayer.Reset(); while ((layer = pEnumLayer.Next()) != null) { //只导出可见图层 if (chkExportVisible.Checked == true) { if (layer.Visible == false) { map.DeleteLayer(layer); continue; } } this.labelExport.Text = "导出:" + layer.Name; this.labelExport.Refresh(); if (layer is IFeatureLayer) { IFeatureClass featureClassNew = CopyFeatureClassToWorkspace(map, layer, wksFeature, bLayerSpatial); if (featureClassNew != null) { //拷贝完成重设数据源 cls.SetdataSourceOfFeatureLayer(layer, featureClassNew); } else { continue; } } else if (layer is IRasterLayer) { IRasterDataset rasterDatasetNew = CopyRasterToWorkspace(map, layer, wksRaster, bLayerSpatial); if (rasterDatasetNew != null) { //拷贝完成重设数据源 IRasterLayer rasterLayer = layer as IRasterLayer; rasterLayer.CreateFromDataset(rasterDatasetNew); } else { continue; } } } } this.labelExport.Text = "导出完成!"; return(true); }
private void btnOK_Click(object sender, EventArgs e) { //首先进行坐标系x,y变换 //string fileExpTran;//进行x,y做表转换后输出的tiff文件存储路径,用这一文件在进行后期的Z转换 //fileExpTran = System.IO.Path.GetDirectoryName(LayerExpName) +"\\"+ System.IO.Path.GetFileNameWithoutExtension(LayerExpName)+"XY.tif"; //try //{ // if (NorthEastToEastNorth(pRasterLayer, LayerExpName)) // { // RasterLayerClass rasterlayer = new RasterLayerClass(); // rasterlayer.CreateFromFilePath(LayerExpName); // IRaster2 pRaster2 = rasterlayer.Raster as IRaster2; // IRasterDataset2 pRasterDataset = pRaster2.RasterDataset as IRasterDataset2; // ChangeRasterValue(pRasterDataset, -1, 0); // pMapControl.AddLayer(rasterlayer as ILayer); // this.Close(); // } //} //catch (System.Exception ex) //{ // MessageBox.Show(ex.Message); //} try { IFeatureClass pFC = pFeatureLayer.FeatureClass; IDataset pDS = pFC as IDataset; IWorkspace pWS = pDS.Workspace; string filedir = pWS.PathName; string fdestname = System.IO.Path.GetFileNameWithoutExtension(txtFeatureName.Text); ClsGDBDataCommon CGD = new ClsGDBDataCommon(); IWorkspace pTargetWS = CGD.OpenFromShapefile(txtLayerExp.Text); IWorkspace2 workspace2 = pTargetWS as IWorkspace2; if (workspace2.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, fdestname)) { MessageBox.Show("目标文件已存在!"); return; } //shape文件直接拷贝后再修改 if (pDS.CanCopy() == true) { pDS.Copy(txtFeatureName.Text, pTargetWS); IFeatureWorkspace pFW = pTargetWS as IFeatureWorkspace; pTargetFeatureClass = pFW.OpenFeatureClass(txtFeatureName.Text); TransCoordiante(pFW as IWorkspace, pTargetFeatureClass); } //表示为gdb的feature,只能往gdb拷贝 else { // Create workspace name objects. IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass(); IWorkspaceName targetWorkspaceName = new WorkspaceNameClass(); IName targetName = (IName)targetWorkspaceName; // Set the workspace name properties. sourceWorkspaceName.PathName = pWS.PathName; sourceWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"; // targetWorkspaceName.PathName = @"PartialMontgomery.gdb"; targetWorkspaceName.PathName = txtLayerExp.Text; targetWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"; //if (txtLayerExp.Text.Length>4 && txtLayerExp.Text.Substring(txtLayerExp.Text.Length-4,4) == ".gdb") //{ // targetWorkspaceName.WorkspaceFactoryProgID = // "esriDataSourcesGDB.FileGDBWorkspaceFactory"; //} //else //{ // targetWorkspaceName.WorkspaceFactoryProgID = // "esriDataSourcesFile.ShapefileWorkspaceFactory"; //} // Create a name object for the source feature class. IFeatureClassName featureClassName = new FeatureClassNameClass(); // Set the featureClassName properties. IDatasetName sourceDatasetName = (IDatasetName)featureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = pDS.BrowseName; IName sourceName = (IName)sourceDatasetName; // Create an enumerator for source datasets. IEnumName sourceEnumName = new NamesEnumeratorClass(); IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit)sourceEnumName; // Add the name object for the source class to the enumerator. sourceEnumNameEdit.Add(sourceName); // Create a GeoDBDataTransfer object and a null name mapping enumerator. IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass(); IEnumNameMapping enumNameMapping = null; // Use the data transfer object to create a name mapping enumerator. Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName, targetName, out enumNameMapping); enumNameMapping.Reset(); //修改拷贝的文件名 INameMapping nameMapping = enumNameMapping.Next(); if ((nameMapping != null)) { nameMapping.TargetName = txtFeatureName.Text; } // Check for conflicts. //if (conflictsFound) //{ // // Iterate through each name mapping. // INameMapping nameMapping = null; // while ((nameMapping = enumNameMapping.Next()) != null) // { // // Resolve the mapping's conflict (if there is one). // if (nameMapping.NameConflicts) // { // nameMapping.TargetName = nameMapping.GetSuggestedName(targetName); // } // // See if the mapping's children have conflicts. // IEnumNameMapping childEnumNameMapping = nameMapping.Children; // if (childEnumNameMapping != null) // { // childEnumNameMapping.Reset(); // // Iterate through each child mapping. // INameMapping childNameMapping = null; // while ((childNameMapping = childEnumNameMapping.Next()) != null) // { // if (childNameMapping.NameConflicts) // { // childNameMapping.TargetName = childNameMapping.GetSuggestedName // (targetName); // } // } // } // } //} // Start the transfer. geoDBDataTransfer.Transfer(enumNameMapping, targetName); IFeatureWorkspace pFW = CGD.OpenFromFileGDB(txtLayerExp.Text) as IFeatureWorkspace; pTargetFeatureClass = pFW.OpenFeatureClass(txtFeatureName.Text); TransCoordiante(pFW as IWorkspace, pTargetFeatureClass); } if (pTargetFeatureClass != null) { //添加到图层中 IFeatureLayer featureLayer = new FeatureLayerClass(); featureLayer.FeatureClass = pTargetFeatureClass; featureLayer.Name = fdestname; pMapControl.AddLayer(featureLayer as ILayer); pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } this.DialogResult = DialogResult.OK; } catch (System.Exception ex) { MessageBox.Show(ex.Message); } }
//public IFeatureDataset CreateFeatureDataset_Example(IWorkspace workspace, string fdsName, ISpatialReference fdsSR) //{ // IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; // return featureWorkspace.CreateFeatureDataset(fdsName, fdsSR); //} public void ConvertLabelsToGDBAnnotationSingleLayer(IMap pMap, bool featureLinked) //(IMap pMap, int layerIndex, bool featureLinked) { try { IConvertLabelsToAnnotation pConvertLabelsToAnnotation = new ConvertLabelsToAnnotationClass(); ITrackCancel pTrackCancel = new CancelTrackerClass(); //Change global level options for the conversion by sending in different parameters to the next line. pConvertLabelsToAnnotation.Initialize(pMap, esriAnnotationStorageType.esriDatabaseAnnotation, esriLabelWhichFeatures.esriAllFeatures, true, pTrackCancel, null); //ILayer pLayer = pMap.get_Layer(layerIndex); IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer; if (pGeoFeatureLayer != null) { IFeatureClass pFeatureClass = pGeoFeatureLayer.FeatureClass; IDataset pDataset = pFeatureClass as IDataset; IFeatureWorkspace pFeatureWorkspace = pDataset.Workspace as IFeatureWorkspace; //Add the layer information to the converter object. Specify the parameters of the output annotation feature class here as well. //if (pFeatureClass.FeatureDataset != null) //{ // pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer, // pGeoFeatureLayer.Name + "_Anno", pFeatureWorkspace, // pFeatureClass.FeatureDataset, featureLinked, false, false, true, true, ""); //} //else //{ //必须指定路径,默认存放到原有GDB中会造成无法第二次创建的问题 if (MessageBox.Show("请指定一个位置生成GeoDatabase存放注记图层", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.DefaultExt = "gdb"; saveFileDlg.Filter = "(文件型数据库)*.gdb|*.gdb"; if (saveFileDlg.ShowDialog() == DialogResult.OK) { string strFileGDB = saveFileDlg.FileName; string strPath = System.IO.Path.GetDirectoryName(strFileGDB); string strName = System.IO.Path.GetFileName(strFileGDB); ClsGDBDataCommon com = new ClsGDBDataCommon(); com.Create_FileGDB(strPath, strName); string strfile = strPath + "\\" + strName; IWorkspace pws = com.OpenFromFileGDB(strfile); IFeatureDataset pdataset = creatdataset(pws, pLayer.Name + "Anno", pMap.SpatialReference); pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer, pGeoFeatureLayer.Name + "_Anno", (IFeatureWorkspace)pws, pdataset, featureLinked, false, false, true, true, ""); } } //} //Do the conversion. pConvertLabelsToAnnotation.ConvertLabels(); IEnumLayer pEnumLayer = pConvertLabelsToAnnotation.AnnoLayers; //Turn off labeling for the layer converted. pGeoFeatureLayer.DisplayAnnotation = false; //Add the result annotation layer to the map. pMap.AddLayers(pEnumLayer, true); //Refresh the map to update the display. IActiveView pActiveView = pMap as IActiveView; pActiveView.Refresh(); } } catch (System.Exception ex) { MessageBox.Show("该图层已存在注记图层", "提示", MessageBoxButtons.OK); } }