1. the SDE element is exported to the shapefile.
1. Create a New shapefile.
private bool CreateShapefile(string filepath, string name) { bool isSuccssed = false; try { IFeatureWorkspace pFWS = null; IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); pFWS = pWSF.OpenFromFile(filepath, 0) as IFeatureWorkspace; IFields pFields; IFieldsEdit pFieldsEdit; pFields = new FieldsClass(); pFieldsEdit = pFields as IFieldsEdit; IField pField; IFieldEdit pFieldEdit; pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "Shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeomDef; IGeometryDefEdit pGeomDefEdit; pGeomDef = new GeometryDefClass(); pGeomDefEdit = pGeomDef as IGeometryDefEdit; pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeomDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass(); pFieldEdit.GeometryDef_2 = pGeomDef; pFieldsEdit.AddField(pField); IFeatureClass pFeatureClass; pFeatureClass = pFWS.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); isSuccssed = true; } catch { isSuccssed = false; } return isSuccssed; }
2. Read the elements from SDE to obtain the geometry image.
Private iworkspace getsdeworkspace (string server, string instance, string database, string user, string password, string version) {iworkspace PWS = NULL; iworkspacefactory pwsf = NULL; ipropertyset ppropertyset = new propertyset (); // The ppropertyset parameter of the SDE database connection. setproperty ("server", server); ppropertyset. setproperty ("instance", instance); ppropertyset. setproperty ("Database", database); ppropertyset. setproperty ("user", user); ppropertyset. setproperty ("password", password); ppropertyset. setproperty ("version", version); try {pwsf = new sdeworkspacefactory (); PWS = pwsf. open (ppropertyset, 0);} catch {} finally {system. runtime. interopservices. marshal. releasecomobject (ppropertyset); system. runtime. interopservices. marshal. releasecomobject (pwsf); ppropertyset = NULL; pwsf = NULL;} return PWS ;}
private List<IGeometry> GetGeometryFromSde(string pFeaClassName,string objectid) { List<IGeometry> pGeometryList = new List<IGeometry>(); IWorkspace pWS = null; IFeatureWorkspace pFWS = null; IFeatureCursor pFeatCursor = null; IFeature pFeature = null; IFeatureClass pFeatClss = null; IQueryFilter pQueryFilter =new QueryFilterClass(); pWS = GetSdeWorkspace("172.30.242.185", "5151", "server185_SDE", "sde", "hiway2014", "DEFAULT"); try { pFWS = pWS as IFeatureWorkspace; pFeatClss = pFWS.OpenFeatureClass(pFeaClassName); pQueryFilter.WhereClause = "OBJECTID=" + objectid; IFeatureCursor featureCursor = pFeatClss.Search(pQueryFilter, false); while ((pFeature = featureCursor.NextFeature()) != null) { if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon) { pGeometryList.Add(pFeature.ShapeCopy); } } } catch { } finally { if (pFeatCursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatCursor); pFeatCursor = null; } if (pWS != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(pWS); } } return pGeometryList; }
3. Add the elements obtained from SDE to the shapefile.
private void AddFeature2Shapefile(IGeometry geometry, string filepath, string name) { IFeatureWorkspace pFWS = null; IFeatureClass pFeaCls = null; IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); pFWS = pWSF.OpenFromFile(filepath, 0) as IFeatureWorkspace; pFeaCls = pFWS.OpenFeatureClass(name); IDataset dataset = pFeaCls as IDataset; IWorkspace workspace = dataset.Workspace; IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace; workspaceEdit.StartEditing(true); workspaceEdit.StartEditOperation(); IFeatureBuffer featureBuffer = pFeaCls.CreateFeatureBuffer(); IFeatureCursor featureCursor = pFeaCls.Insert(true); featureBuffer.Shape = geometry; featureCursor.InsertFeature(featureBuffer); featureCursor.Flush(); Marshal.ReleaseComObject(featureCursor); workspaceEdit.StopEditOperation(); workspaceEdit.StopEditing(true); }
2. Adding Elements in one shapefile to another shapefile is similar to the preceding method. Only the source of elements obtained is different.
private IGeometry GetGeometryFromShapefile(string filepath, string name) { IGeometry pGeometry = null; IFeatureWorkspace pFWS = null; IFeatureClass pFeaCls = null; IFeature pFeature = null; IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); pFWS = pWSF.OpenFromFile(filepath, 0) as IFeatureWorkspace; pFeaCls = pFWS.OpenFeatureClass(name); IFeatureCursor featureCursor = pFeaCls.Search(null, false); while ((pFeature = featureCursor.NextFeature()) != null) { pGeometry = pFeature.ShapeCopy; } return pGeometry; }