SDE与shapefile之间的数据导入与导出

时间:2022-03-18 21:57:17

一、SDE要素导出到shapefile中。

1、创建一个新的shapefile文件。

 private bool CreateShapefile(string filepath, string name)
{
bool isSuccssed = false;
try
{
IFeatureWorkspace pFWS = null;
IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
pFWS = pWSF.OpenFromFile(filepath, ) 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、从SDE中读取要素,得到Geometry图形。

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();
//sde数据库连接的各个参数
pPropertySet.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, );
}
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("192.168.121.110", "", "server110_SDE", "sde", "sde2014", "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、将sde中获得的要素,加到shapefile中。

 private void AddFeature2Shapefile(IGeometry geometry, string filepath, string name)
{
IFeatureWorkspace pFWS = null;
IFeatureClass pFeaCls = null;
IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
pFWS = pWSF.OpenFromFile(filepath, ) 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);
}

二、将一个shapefile中的要素加到另一个shapefile中方法与上面类似。只是获得的要素源不同。

 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, ) as IFeatureWorkspace;
pFeaCls = pFWS.OpenFeatureClass(name);
IFeatureCursor featureCursor = pFeaCls.Search(null, false);
while ((pFeature = featureCursor.NextFeature()) != null)
{
pGeometry = pFeature.ShapeCopy;
}
return pGeometry;
}