Arcgis engnie 创建SHP文件
2013-03-16 10:38
232 查看
以创建点图层为例。
首先要得到保存文件的地址。
SaveFileDialogsaveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Shape文件(*.shp)|*.shp";
saveFileDialog.Title = "新建点形shp文件";
saveFileDialog.CheckFileExists = false;
DialogResult dialogResult = saveFileDialog.ShowDialog();
IWorkspaceFactory pWorkspaceFactory = newShapefileWorkspaceFactory();
intindex;
stringfileName;
stringfilePath;
if(dialogResult == DialogResult.OK)
{
fileFullPath =saveFileDialog.FileName;
index =fileFullPath.LastIndexOf("\");
fileName =fileFullPath.Substring(index + 1);
filePath =fileFullPath.Substring(0, index);
if(System.IO.File.Exists(saveFileDialog.FileName))//检查文件是否存在
{
if (MessageBox.Show("该文件夹下已经有同名文件,替换原文件?", "询问",MessageBoxButtons.OKCancel, MessageBoxIcon.Question) ==DialogResult.OK)
{
IFeatureWorkspace FWS = pWorkspaceFactory.OpenFromFile(filePath, 0)as IFeatureWorkspace;
IFeatureClass pFeatureClass = FWS.OpenFeatureClass(fileName);
IDatasetpDataset = pFeatureClass as IDataset;
pDataset.Delete();
}
//System.IO.File.Delete(saveFileDialog.FileName);
else
return;
}
}
else
{
fileFullPath = null;
return;
}
然后,要为该SHP建立新的字段:
IFieldspFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IFieldpField = new FieldClass();
IFieldEditpFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "Shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef asIGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 =esriGeometryType.esriGeometryPoint;//点、线、面什么的
pGeometryDefEdit.SpatialReference_2 =axMapControl1.SpatialReference;
ISpatialReferenceFactory2 ipSpaRefFa = newSpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem ipGeoCorSys = newGeographicCoordinateSystemClass();
ipGeoCorSys =ipSpaRefFa.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
ISpatialReference ipSpaRef = ipGeoCorSys;
//IControlPrecision2 contrPrecision = ipSpaRef asIControlPrecision2;
pGeometryDefEdit.SpatialReference_2 = ipSpaRef;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
pField =new FieldClass();//新建字段
pFieldEdit= pField as IFieldEdit;
pFieldEdit.Length_2 = 100;
pFieldEdit.Name_2 = "editorName";
pFieldEdit.AliasName_2 = "editorName";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
//继续增加其它字段
最后创建图层
IFeatureWorkspace pFeatureWorkspace =pWorkspaceFactory.OpenFromFile(filePath, 0) asIFeatureWorkspace;
int i =fileName.IndexOf(".shp");
if(i == -1)
pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields,null, null, esriFeatureType.esriFTSimple, "Shape", "");
else
pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null,esriFeatureType.esriFTSimple, "Shape", "");
axMapControl1.AddShapeFile(filePath, fileName + ".shp");
首先要得到保存文件的地址。
SaveFileDialogsaveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Shape文件(*.shp)|*.shp";
saveFileDialog.Title = "新建点形shp文件";
saveFileDialog.CheckFileExists = false;
DialogResult dialogResult = saveFileDialog.ShowDialog();
IWorkspaceFactory pWorkspaceFactory = newShapefileWorkspaceFactory();
intindex;
stringfileName;
stringfilePath;
if(dialogResult == DialogResult.OK)
{
fileFullPath =saveFileDialog.FileName;
index =fileFullPath.LastIndexOf("\");
fileName =fileFullPath.Substring(index + 1);
filePath =fileFullPath.Substring(0, index);
if(System.IO.File.Exists(saveFileDialog.FileName))//检查文件是否存在
{
if (MessageBox.Show("该文件夹下已经有同名文件,替换原文件?", "询问",MessageBoxButtons.OKCancel, MessageBoxIcon.Question) ==DialogResult.OK)
{
IFeatureWorkspace FWS = pWorkspaceFactory.OpenFromFile(filePath, 0)as IFeatureWorkspace;
IFeatureClass pFeatureClass = FWS.OpenFeatureClass(fileName);
IDatasetpDataset = pFeatureClass as IDataset;
pDataset.Delete();
}
//System.IO.File.Delete(saveFileDialog.FileName);
else
return;
}
}
else
{
fileFullPath = null;
return;
}
然后,要为该SHP建立新的字段:
IFieldspFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IFieldpField = new FieldClass();
IFieldEditpFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "Shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef asIGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 =esriGeometryType.esriGeometryPoint;//点、线、面什么的
pGeometryDefEdit.SpatialReference_2 =axMapControl1.SpatialReference;
ISpatialReferenceFactory2 ipSpaRefFa = newSpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem ipGeoCorSys = newGeographicCoordinateSystemClass();
ipGeoCorSys =ipSpaRefFa.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
ISpatialReference ipSpaRef = ipGeoCorSys;
//IControlPrecision2 contrPrecision = ipSpaRef asIControlPrecision2;
pGeometryDefEdit.SpatialReference_2 = ipSpaRef;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
pField =new FieldClass();//新建字段
pFieldEdit= pField as IFieldEdit;
pFieldEdit.Length_2 = 100;
pFieldEdit.Name_2 = "editorName";
pFieldEdit.AliasName_2 = "editorName";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
//继续增加其它字段
最后创建图层
IFeatureWorkspace pFeatureWorkspace =pWorkspaceFactory.OpenFromFile(filePath, 0) asIFeatureWorkspace;
int i =fileName.IndexOf(".shp");
if(i == -1)
pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields,null, null, esriFeatureType.esriFTSimple, "Shape", "");
else
pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null,esriFeatureType.esriFTSimple, "Shape", "");
axMapControl1.AddShapeFile(filePath, fileName + ".shp");
相关文章推荐
- 在ArcGIS中因字符问题无法创建shp文件
- c#+ArcGIS Engine-创建shp数据文件结构
- ArcGIS 网络分析[1.1] 创建用于网络分析用的线类型shp文件[这个太基础了吧!]
- ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径
- 结合C++和GDAL实现shapefile(shp)文件的创建和写入
- arcgis10.0以后版本,已知坐标,生成shp文件 ---经验证,此博客记录步骤较繁琐,ArcGIS Desktop有更简单的方法
- arcgis 中利用txt坐标文件创建要素的办法,含txt文件详细格式~
- (转载)ArcGIS Flex API配合LibertyGISI加载SHP文件
- ArcGIS中KML转为shp文件
- ArcGIS ModelBuilder批量导出SHP文件中的要素
- -05 08:57 ARCGIS地统计学计算文件后缀名为.shp文件制作
- 利用arcgis由txt文件创建图层
- arcGIS编辑报错:无法创建日志文件系统表。用户可能缺少创建表的权限或资源
- 用matlab写Arcgis的shp文件
- arcgis10.2转shp文件中文乱码问题解决方案
- 利用ArcGIS对shp文件进行投影转换
- 2.删除ArcGIS中创建的自定义地理(坐标)变换文件
- 有关ARCGIS中批量导入SHP文件的问题
- arcgis 10.2 创建style图标文件
- ArcGIS问题:dbf shp shx sbn sbx mdb adf等类型的文件的解释