当我们在webgis 想要把某个地块或者多个地块导出dwg或者shp文件的时候怎么办?这个时候最好就是用后台的方式。首先把web gis上的graphic 的polygon提取为坐标的形式(类似于x,y x,y x,y),如果这个图形你知道在某个sde库的,也可以提取它的主键属性信息,方便在后台直接查询,减少坐标传输,地块特别大的很麻烦。后台首先要把坐标存到一个内存的featureclass
1 //创建内存图层 2 private IFeatureClass createMemoryFeatureClass(string coord) 3 { 4 IField oField = new FieldClass(); 5 IFields oFields = new FieldsClass(); 6 IGeometryDef geometryDef = new GeometryDefClass(); 7 8 IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); 9 IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); 10 IName name = (IName)workspaceName; 11 IWorkspace inmemWorkSpace = (IWorkspace)name.Open(); 12 13 IFieldsEdit oFieldsEdit = null; 14 IFieldEdit oFieldEdit = null; 15 16 IFeatureLayer oFeatureLayer = null; 17 IFeatureClass outputFeatureClass = null; 18 IFeatureCursor outputFeatureCursor = null; 19 try 20 { 21 22 oFieldsEdit = oFields as IFieldsEdit; 23 oFieldEdit = oField as IFieldEdit; 24 25 IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; 26 geometryDefEdit.AvgNumPoints_2 = 5; 27 geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; 28 geometryDefEdit.GridCount_2 = 1; 29 geometryDefEdit.HasM_2 = false; 30 geometryDefEdit.HasZ_2 = false; 31 32 //ISpatialReferenceFactory ispfac = new SpatialReferenceEnvironmentClass(); 33 //IGeographicCoordinateSystem igeocoorsys = ispfac.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); 34 //igeocoorsys.SetDomain(76000, 180000, 0, 104000); 35 geometryDefEdit.SpatialReference_2 = getSpatialReference(); 36 37 oFieldEdit.Name_2 = "SHAPE"; 38 oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 39 oFieldEdit.GeometryDef_2 = geometryDef; 40 oFieldEdit.IsNullable_2 = true; 41 oFieldEdit.Required_2 = true; 42 oFieldsEdit.AddField(oField); 43 44 outputFeatureClass = (inmemWorkSpace as IFeatureWorkspace).CreateFeatureClass("selectedPoints", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); 45 outputFeatureCursor = outputFeatureClass.Insert(true); 46 IFeatureBuffer outputFeatureBuffer = outputFeatureClass.CreateFeatureBuffer(); 47 48 IWorkspaceEdit inmemWorkspaceEdit = inmemWorkSpace as IWorkspaceEdit; 49 inmemWorkspaceEdit.StartEditing(false); //Start Editing 50 inmemWorkspaceEdit.StartEditOperation(); 51 if (coord != "") 52 { 53 outputFeatureBuffer.Shape = ArcGISUtil.StringToPolygon(coord); 54 outputFeatureCursor.InsertFeature(outputFeatureBuffer); 55 56 } 57 inmemWorkspaceEdit.StopEditOperation(); 58 inmemWorkspaceEdit.StopEditing(true); 59 60 IGeoDataset outputGeodataset = (IGeoDataset)outputFeatureClass; 61 62 63 64 oFeatureLayer = new FeatureLayerClass(); 65 oFeatureLayer.FeatureClass = outputFeatureClass; 66 oFeatureLayer.Name = "fa"; 67 } 68 catch (Exception e) 69 { 70 System.Diagnostics.Trace.WriteLine(e.StackTrace); 71 } 72 finally 73 { 74 ArcGISUtil.FinalReleaseComObject(outputFeatureCursor); 75 ArcGISUtil.FinalReleaseComObject(oField); 76 ArcGISUtil.FinalReleaseComObject(oFields); 77 ArcGISUtil.FinalReleaseComObject(geometryDef); 78 ArcGISUtil.FinalReleaseComObject(workspaceFactory); 79 ArcGISUtil.FinalReleaseComObject(outputFeatureClass); 80 } 81 return oFeatureLayer.FeatureClass; 82 }
得到的内存图层,如果是要转shp,可以直接转,如果是要转dwg,先要转为gdb,直接转dwg会失败,目前找不到原因。
1 //要素转shp 或者gdb 2 private void feature2GDB(IFeatureClass sourceFeatureClass, IQueryFilter pQueryFilter, string name, string type, string _fileShortName) 3 { 4 try 5 { 6 string fileShortName = string.Empty; 7 if (type == "gdb") 8 { 9 fileShortName = _fileShortName; 10 11 } 12 else 13 { 14 fileShortName = name; 15 } 16 17 string parentDirectory = HttpContext.Current.Server.MapPath(@"Checkservicesgdbpath"); 18 19 IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); 20 IDataset pOutDataset = (IDataset)sourceFeatureClass; 21 sourceFeatureClassName = (IFeatureClassName)pOutDataset.FullName; 22 23 IWorkspaceFactory wsf = null; 24 if (type == "gdb") 25 { 26 wsf = new FileGDBWorkspaceFactoryClass(); 27 } 28 else if (type == "shp") 29 { 30 wsf = new ShapefileWorkspaceFactoryClass(); 31 } 32 33 IWorkspaceName pInWorkspaceName = new WorkspaceNameClass(); 34 pInWorkspaceName = wsf.Create(parentDirectory, name, null, 0); 35 36 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass(); 37 IDatasetName pInDatasetClassName; 38 pInDatasetClassName = (IDatasetName)pInFeatureClassName; 39 pInDatasetClassName.Name = fileShortName; 40 pInDatasetClassName.WorkspaceName = pInWorkspaceName; 41 42 long iCounter; 43 IFields pOutFields, pInFields; 44 IField pGeoField; 45 IEnumFieldError pEnumFieldError = null; 46 pInFields = sourceFeatureClass.Fields; 47 IFieldChecker pFieldChecker = new FieldChecker(); 48 //pFieldChecker.InputWorkspace = pOutDataset.Workspace; 49 50 pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields); 51 pGeoField = null; 52 for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++) 53 { 54 if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry) 55 { 56 pGeoField = pOutFields.get_Field((int)iCounter); 57 break; 58 } 59 } 60 61 IGeometryDef pOutGeometryDef; 62 IGeometryDefEdit pOutGeometryDefEdit; 63 pOutGeometryDef = pGeoField.GeometryDef; 64 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef; 65 66 IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass(); 67 pShpToClsConverter.ConvertFeatureClass(sourceFeatureClassName, pQueryFilter, 68 null, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0); 69 70 ArcGISUtil.FinalReleaseComObject(wsf); 71 ArcGISUtil.FinalReleaseComObject(pInWorkspaceName); 72 ArcGISUtil.FinalReleaseComObject(pInFeatureClassName); 73 ArcGISUtil.FinalReleaseComObject(pFieldChecker); 74 ArcGISUtil.FinalReleaseComObject(pShpToClsConverter); 75 } 76 catch (Exception e) 77 { 78 System.Diagnostics.Trace.WriteLine(e.StackTrace); 79 } 80 } 81 82 private IFeatureClass openFeatureClass(String layerName, String sde) 83 { 84 IFeatureClass fc = null; 85 try 86 { 87 fc = SdeConnectManager.getFeatureClass(layerName, sde); 88 } 89 catch 90 { 91 return null; 92 } 93 return fc; 94 }
要转dwg可以直接调用AO接口
1 private void gdb2Cad(string gdbfilePath, string cadfilePath, int num = 0) 2 { 3 Geoprocessor gp = new Geoprocessor(); 4 try 5 { 6 ExportCAD tool = new ExportCAD(); 7 tool.in_features = gdbfilePath; 8 tool.Output_File = cadfilePath; 9 tool.Output_Type = "DWG_R2004"; 10 gp.Execute(tool, null); 11 } 12 catch (Exception e) 13 { 14 System.Diagnostics.Trace.WriteLine(e.StackTrace); 15 System.Diagnostics.Trace.Write("dwg"); 16 } 17 }
但是如果导出dwg图形有注记的话,AO这个方法就行不通了,貌似arcgis对导出dwg图形带标注支持的不好,后面会有专门的文章讲怎么导出带有注记的dwg。