ArcEngine对shp文件的操作(代码总结)
目录
新建shp文件
public static void CreatShpFile(out string ErrorMsg, string shpFullFilePath, ISpatialReference spatialReference, esriGeometryType pGeometryType) {
ErrorMsg = "";
try {
string shpFolder = System.IO.Path.GetDirectoryName(shpFullFilePath);
string shpFileName = System.IO.Path.GetFileName(shpFullFilePath);
IWorkspaceFactory pWorkspaceFac = new ShapefileWorkspaceFactoryClass();
IWorkspace pWorkSpace = pWorkspaceFac.OpenFromFile(shpFolder, 0);
IFeatureWorkspace pFeatureWorkSpace = pWorkSpace as IFeatureWorkspace;
//如果文件已存在
if (System.IO.File.Exists(shpFullFilePath)) {
if (MessageBox.Show("文件已存在,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) {
IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(shpFileName);
if (pFCChecker != null) {
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
} else {
return;
}
}
IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
IObjectClassDescription pObjectDescription = (IObjectClassDescription) fcDescription;
IFields fields = pObjectDescription.RequiredFields;
int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);
IField field = fields.get_Field(shapeFieldIndex);
IGeometryDef geometryDef = field.GeometryDef;
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit) geometryDef;
//点
geometryDefEdit.GeometryType_2 = pGeometryType; //geometry类型
ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass(); //坐标系
//IProjectedCoordinateSystem pcsSys = pSpatialRefFac.CreateProjectedCoordinateSystem(pcsType);//投影坐标系
//geometryDefEdit.SpatialReference_2 = pcsSys;
int pcsType = (int) esriSRGeoCSType.esriSRGeoCS_WGS1984;
ISpatialReference spatialReference1 = pSpatialRefFac.CreateGeographicCoordinateSystem(pcsType);
geometryDefEdit.SpatialReference_2 = spatialReference1;
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null; //将传入字段 转成 validatedFields
fieldChecker.ValidateWorkspace = pWorkSpace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
pFeatureWorkSpace.CreateFeatureClass(shpFileName, validatedFields, pObjectDescription.InstanceCLSID, pObjectDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");
} catch(Exception ex) {
ErrorMsg = ex.Message;
}
}
加载shp文件
public static void AddShp2Map(string shpfileFullFilePath, IMap pMap) {
string shpfileName = System.IO.Path.GetFileName(shpfileFullFilePath);
string shpfilePath = System.IO.Path.GetDirectoryName(shpfileFullFilePath);
IWorkspaceFactory workspaceFC = new ShapefileWorkspaceFactory();
IWorkspace workSpace = workspaceFC.OpenFromFile(shpfilePath, 0);
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) workSpace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(shpfileName);
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureClass;
pFeatureLayer.Name = pFeatureClass.AliasName;
pMap.AddLayer(pFeatureLayer);
}
导出shp文件
public static bool SelectedLayerExport(string mLayerName, string shpfileFullFilePath, IMap pMap, bool onlyExportSelected) {
//string layerName = System.IO.Path.GetFileNameWithoutExtension(shpfileFullFilePath);
string fileName = System.IO.Path.GetFileName(shpfileFullFilePath);
string fileNameWithouExtention = System.IO.Path.GetFileNameWithoutExtension(fileName);
string shpFileExportPath = System.IO.Path.GetDirectoryName(shpfileFullFilePath);
ISelectionSet pSelectionSet = null;
if (!System.IO.Directory.Exists(shpFileExportPath)) {
System.IO.Directory.CreateDirectory(shpFileExportPath);
}
try {
string outPath = shpFileExportPath;
ILayer mLayer = GetLayerByName(mLayerName, pMap);
if (mLayer == null) {
return false;
}
IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer;
if (onlyExportSelected) {
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
pSelectionSet = pFeatureSelection.SelectionSet;
}
//export
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IDataset pDataset = pFeatureClass as IDataset;
IDatasetName pDatasetName = (IDatasetName) pDataset.FullName;
IWorkspaceFactory pWsFactory = new ShapefileWorkspaceFactory();
IWorkspace outWorkSpace = pWsFactory.OpenFromFile(outPath, 0);
IDataset outDataSet = (IDataset) outWorkSpace;
IWorkspaceName workspaceName = (IWorkspaceName) outDataSet.FullName;
IFeatureClassName shpFeatureClassName = new FeatureClassNameClass();
IDatasetName shpDatasetName = (IDatasetName) shpFeatureClassName;
shpDatasetName.WorkspaceName = workspaceName;
shpDatasetName.Name = fileNameWithouExtention; //System.IO.Path.GetFileNameWithoutExtension(fileName);
// Use the IFieldChecker interface to make sure all of the field names are valid for the output shapefile.
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields shapefileFields = null;
IEnumFieldError enumFieldError = null;
fieldChecker.InputWorkspace = pDataset.Workspace;
fieldChecker.ValidateWorkspace = outWorkSpace;
fieldChecker.Validate(pFeatureClass.Fields, out enumFieldError, out shapefileFields);
// We also need to retrieve the GeometryDef from the input feature class.
int shapeFieldPosition = pFeatureClass.FindField(pFeatureClass.ShapeFieldName);
IFields inputFields = pFeatureClass.Fields;
IField shapeField = inputFields.get_Field(shapeFieldPosition);
IGeometryDef geometryDef = shapeField.GeometryDef;
// Now we create a feature data converter to "Export" the selected features to a shapefile
IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass();
IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(pDatasetName, null, pSelectionSet, null, shpFeatureClassName, geometryDef, shapefileFields, "", 1000, 0);
//IEnumInvalidObject enumErrors = featureDataConverter2.ConvertFeatureClass(pDatasetName, queryFilter, null,tFeatureClassName, geometryDef, tFields, "", 1000, 0);
return true;
} catch(Exception ex) {
MessageBox.Show(ex.Message);
return false;
}
}
图层操作函数
//根据名称获取图层
public static ILayer GetLayerByName(string layerName, IMap pMap) {
if (string.IsNullOrEmpty(layerName)) {
return null;
}
ILayer layer = null;
for (int i = 0; i < pMap.LayerCount; i++) {
ILayer pLayer = pMap.get_Layer(i);
if (pLayer is IGroupLayer || pLayer is ICompositeLayer) {
layer = GetSubLayer(layerName, pLayer as ICompositeLayer);
if (layer != null) {
break;
}
} else if (pLayer.Name == layerName) {
layer = pLayer;
break;
}
}
return layer;
}
public static ILayer GetSubLayer(string layerName, ICompositeLayer compositeLayer) {
ILayer mLayer = null;
for (int i = 0; i < compositeLayer.Count; i++) {
ILayer pLayer2 = compositeLayer.get_Layer(i);
if (pLayer2 is IGroupLayer || pLayer2 is ICompositeLayer) {
mLayer = GetSubLayer(layerName, pLayer2 as ICompositeLayer);
if (mLayer != null) {
break;
}
} else if (pLayer2.Name.Equals(layerName)) {
mLayer = pLayer2;
break;
}
}
return mLayer;
}
转载自:https://blog.csdn.net/csu2015/article/details/78570131