Excel列表坐标点转成带Z值的shape点文件和线文件.docx
《Excel列表坐标点转成带Z值的shape点文件和线文件.docx》由会员分享,可在线阅读,更多相关《Excel列表坐标点转成带Z值的shape点文件和线文件.docx(19页珍藏版)》请在冰豆网上搜索。
Excel列表坐标点转成带Z值的shape点文件和线文件
Excel列表坐标点转成带Z值的shape点文件和线文件
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingESRI.ArcGIS.Controls;
usingESRI.ArcGIS.Geodatabase;
usingESRI.ArcGIS.DataSourcesFile;
usingESRI.ArcGIS.Geometry;
namespaceNetWorkDatamanger
{
publicpartialclassCreateShpFileBaseOnExcel:
Form
{
privateDataGridViewexcelDataGridViewX;
privateAxMapControlaxMapControl;
privatestringfileName;
privatestringfilePath;
publicCreateShpFileBaseOnExcel(AxMapControl_axMapControl,DataGridView_DataView)
{
axMapControl=_axMapControl;
excelDataGridViewX=_DataView;
InitializeComponent();
}
privatevoiddelFieldButtonX_Click(objectsender,EventArgse)
{
if(addFieldListBox.SelectedItem!
=null)
{
fieldListBox.Items.Add(addFieldListBox.SelectedItem);
addFieldListBox.Items.Remove(addFieldListBox.SelectedItem);
}
}
privatevoidaddFieldButtonX_Click(objectsender,EventArgse)
{
if(fieldListBox.SelectedItem!
=null)
{
addFieldListBox.Items.Add(fieldListBox.SelectedItem);
fieldListBox.Items.Remove(fieldListBox.SelectedItem);
}
}
privatevoidCreateShpFileBaseOnExcel_Load(objectsender,EventArgse)
{
for(inti=0;i{
stringHeaderString=excelDataGridViewX.Columns[i].HeaderText;
xComboBoxEx.Items.Add(HeaderString);
yComboBoxEx.Items.Add(HeaderString);
zComboBoxEx.Items.Add(HeaderString);
fieldListBox.Items.Add(HeaderString);
}
}
privatevoidCreate_Click(objectsender,EventArgse)
{
switch(shpTypeComboBox.Text)
{
case"Point":
CreatPointShp();
break;
case"Polyline":
CreatePolylineShp();
break;
case"PolylineZM":
CreatePolylineZMShp();
break;
default:
MessageBox.Show("请选择创建的shp类型!
");
break;
}
}
publicvoidCreatPointShp()
{
try
{
IWorkspaceFactorypShpWksFact=newShapefileWorkspaceFactory();
IFeatureWorkspacepFeatWks;
pFeatWks=(IFeatureWorkspace)pShpWksFact.OpenFromFile(filePath,0);
conststringstrShapeFieldName="Shape";
//定义属性字段
IFieldspFields=newFields();
IFieldsEditpFieldsEdit;
pFieldsEdit=pFieldsasIFieldsEdit;
IFieldpField=newField();
IFieldEditpFieldEdit=newField()asIFieldEdit;
pFieldEdit.Name_2=strShapeFieldName;
pFieldEdit.Type_2=esriFieldType.esriFieldTypeGeometry;
pField=pFieldEditasIField;
//定义几何属性
IGeometryDefpGeomDef=newGeometryDef();
IGeometryDefEditpGeomDefEdit=newGeometryDef()asIGeometryDefEdit;
pGeomDefEdit=pGeomDefasIGeometryDefEdit;
pGeomDefEdit.HasZ_2=true;//图层是有高程值的
switch(shpTypeComboBox.Text)
{
case"Point":
pGeomDefEdit.GeometryType_2=esriGeometryType.esriGeometryPoint;
break;
case"Polyline":
pGeomDefEdit.GeometryType_2=esriGeometryType.esriGeometryPolyline;
break;
case"Polygon":
pGeomDefEdit.GeometryType_2=esriGeometryType.esriGeometryPolygon;
break;
}
pGeomDefEdit.GeometryType_2=esriGeometryType.esriGeometryPoint;
pGeomDefEdit.SpatialReference_2=newUnknownCoordinateSystem()asISpatialReference;
pFieldEdit.GeometryDef_2=pGeomDef;
pFieldsEdit.AddField(pField);
pFields=pFieldsEditasIFields;
IFeatureClasspFeatureClass;
pFeatureClass=pFeatWks.CreateFeatureClass(fileName,pFields,null,null,
esriFeatureType.esriFTSimple,strShapeFieldName,"");
//添加属性字段
for(inti=0;i{
IFieldpfield=newField();
IFieldEditpfieldEdit=newField()asIFieldEdit;
pfieldEdit.Name_2=addFieldListBox.Items[i].ToString();
pfieldEdit.Type_2=esriFieldType.esriFieldTypeString;
pfield=pfieldEditasIField;
pFeatureClass.AddField(pfield);
}
//绘制点
for(inti=0;i{
DataGridViewRowdataRow=excelDataGridViewX.Rows[i];
doublepointX,pointY,pointZ;
pointX=double.Parse(dataRow.Cells[xComboBoxEx.Text].Value.ToString());
pointY=double.Parse(dataRow.Cells[yComboBoxEx.Text].Value.ToString());
pointZ=double.Parse(dataRow.Cells[zComboBoxEx.Text].Value.ToString());
IPointpPoint=newESRI.ArcGIS.Geometry.Point()asIPoint;
//pPoint.PutCoords(pointX,pointY);
pPoint.X=pointX;
pPoint.Y=pointY;
pPoint.Z=pointZ;
IZAwarefromZAware=pPointasIZAware;
fromZAware.ZAware=true;
IFeaturepFeature=pFeatureClass.CreateFeature();
pFeature.Shape=pPoint;
//为该点添加属性值
for(intj=0;j{
stringfieldName=addFieldListBox.Items[j].ToString();
pFeature.set_Value(pFeature.Fields.FindField(fieldName),
dataRow.Cells[fieldName].Value.ToString());
}
pFeature.Store();
}
//添加新建的数据至Map中
axMapControl.AddShapeFile(filePath,fileName);
this.Hide();
MessageBox.Show("Excel转换shp完毕!
");
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
publicvoidCreatePolylineShp()
{
try
{
IWorkspaceFactorypShpWksFact=newShapefileWorkspaceFactory();
IFeatureWorkspacepFeatWks;
pFeatWks=(IFeatureWorkspace)pShpWksFact.OpenFromFile(filePath,0);
IFeatureClasspFeatureClass;
pFeatureClass=CreateFeatureClassFromFactory(pFeatWks,fileName,false);
ISegment[]segmentArray=newISegment[excelDataGridViewX.Rows.Count-1];
stringstrname="";//每条线的名称
intnStartI=0;//每条线的起始位置
//绘制线
for(inti=0;i{
DataGridViewRowdataRow=excelDataGridViewX.Rows[i];
if(i==0)
strname=dataRow.Cells["name"].Value.ToString();
doublepointX,pointY,pointZ;
pointX=double.Parse(dataRow.Cells[xComboBoxEx.Text].Value.ToString());
pointY=double.Parse(dataRow.Cells[yComboBoxEx.Text].Value.ToString());
pointZ=double.Parse(dataRow.Cells[zComboBoxEx.Text].Value.ToString());
DataGridViewRowdataRow2=excelDataGridViewX.Rows[i+1];
if(dataRow2.Cells["name"].Value.ToString().Length!
=0&&i>0)
{
IGeometryCollectionseColletion=newPolylineClass();
ISpatialReferencepsRef=newUnknownCoordinateSystemClass();
ISegmentCollectionpathCollection=newPolylineClass();
IGeometrypGeometry=seColletionasPolylineClass;
pGeometry.SpatialReference=psRef;
ILineline=segmentArray[0]asILine;
objecto1=Type.Missing;
objecto2=Type.Missing;
for(intm=nStartI;m{
//IZAwareiPolylineAware=(IZAware)(segmentArray[m]asIPolyline);
//iPolylineAware.ZAware=true;
pathCollection.AddSegment(segmentArray[m],refo1,refo2);
}
pGeometry=pathCollectionasIGeometry;
seColletion.AddGeometryCollection(pGeometryasIGeometryCollection);
IFeaturepFeature=pFeatureClass.CreateFeature();
pFeature.Shape=(seColletionasIGeometry);
pFeature.set_Value(pFeature.Fields.FindField("name"),strname);
pFeature.Store();
strname=dataRow2.Cells["name"].Value.ToString();
nStartI=i+1;
continue;
}
doublepointX2,pointY2,pointZ2;
pointX2=double.Parse(dataRow2.Cells[xComboBoxEx.Text].Value.ToString());
pointY2=double.Parse(dataRow2.Cells[yComboBoxEx.Text].Value.ToString());
pointZ2=double.Parse(dataRow2.Cells[zComboBoxEx.Text].Value.ToString());
IPointfromPoint=newPointClass();
IPointtoPoint=newPointClass();
IPathpPathA=newPathClass();
ILinepLine=newLineClass();
//IPolylinepLine=newPolylineClass();
fromPoint.X=pointX;
fromPoint.Y=pointY;
fromPoint.Z=pointZ;
IZAwarefromZAware=fromPointasIZAware;
fromZAware.ZAware=true;
toPoint.X=pointX2;
toPoint.Y=pointY2;
toPoint.Z=pointZ2;
IZAwaretoZAware=toPointasIZAware;
toZAware.ZAware=true;
pPathA.FromPoint=fromPoint;
pPathA.ToPoint=toPoint;
pLine.PutCoords(fromPoint,toPoint);
//pLine.FromPoint=fromPoint;
//pLine.ToPoint=toPoint;
segmentArray[i]=pLineasISegment;
//处理最后一段线
if(dataRow2.Cells["name"].Value.ToString().Length==0&&i==excelDataGridViewX.Rows.Count-2)
{
IGeometryCollectionseColletion=newPolylineClass();
ISpatialReferencepsRef=newUnknownCoordinateSystemClass();
ISegmentCollectionpathCollection=newPolylineClass();
IGeometrypGeometry=seColletionasPolylineClass;
pGeometry.SpatialReference=psRef;
ILineline=segmentArray[0]asILine;
objecto1=Type.Missing;
objecto2=Type.Missing;
for(intm=nStartI;m{
//IZAwareiPolylineAware=(IZAware)(segmentArray[m]asIPolyline);
//iPolylineAware.ZAware=true;
pathCollection.AddSegment(segmentArray[m],refo1,refo2);
}
pGeometry=pathCollectionasIGeometry;
seColletion.AddGeometryCollection(pGeometryasIGeometryCollection);
IFeaturepFeature=pFeatureClass.CreateFeature();
pFeature.Shape=(seColletionasIGeometry);
pFeature.set_Value(pFeature.Fields.FindField("name"),strname);
pFeature.Store();
continue;
}
}
//添加新建的数据至Map中
axMapControl.AddShapeFile(filePath,fileName);
this.Hide();
MessageBox.Show("Excel转换shp完毕!
");
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
publicvoidCreatePolylineZMShp()
{
try
{
IWorkspaceFactorypShpWksFact=newShapefileWorkspaceFactory();
IFeatureWorkspacepFeatWks;
pFeatWks=(IFeatureWorkspace)pShpWksFact.OpenFromFile(filePath,0);
IFeatureClasspFeatureClass;
pFeatureClass=CreateFeatureClassFromFactory(pFeatWks,fileName,true);
ISegment[]segmentArray=newISegment[excelDataGridViewX.Rows.Count-1];
stringstrname="";//每条线的名称
intnStartI=0;//每条线的起始位置
//绘制线
for(inti=0;i{
DataGridViewRowdataRow=excelDataGridViewX.Rows[i];
if(i==0)
strname=dataRow.Cells["name"].Value.ToString();
doublepointX,pointY,pointZ;
pointX=double.Parse(dataRow.Cells[xComboBoxEx.Text].Value.ToString());
pointY=double.Parse