⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 gis,使用c#,arcobject(ae)实现空间分析的列子(geoproceor使用),根据面积淹没,实现灾评信息的统计.
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SpatialAnalystTools;
//using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
//using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.DataSourcesOleDB;
using ESRI.ArcGIS.GeoAnalyst;
using System.Data.SqlClient;

namespace SocietyInforDistill
{
    public partial class FormSID : Form
    {
        public FormSID()
        {
            InitializeComponent();
            axMapControl1.AddShapeFile(@"D:\例子\intersect\FxtTools\乡镇界图层", "乡镇界");
        }

        private void buttonOpenXiangzhenjie_Click(object sender, EventArgs e)
        {
 

            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "shp文件(*.shp)|*.shp";
            if (file.ShowDialog(this) == DialogResult.OK)
            {
                IFeatureClass XZFeatureClass = getIFeatureClass(file.FileName);     //乡镇featureclass

                if (XZFeatureClass.ShapeType.ToString()== "esriGeometryPolygon")
                {
                    this.textBoxXiangzhenjie.Text = file.FileName;
                    

                }
                else
                {
                    MessageBox.Show("乡镇图层必须为面层!");
                    return;
                    
                }
            }

           

        }

        private void buttonHongShuiFenxian_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog file = new FolderBrowserDialog();
            // file.Filter = "shp文件(*.shp)|*.shp";

            if (file.ShowDialog(this) == DialogResult.OK)
            {
                this.textBoxHongShuiFenxian.Text = file.SelectedPath.ToString();
            }
        }

        private void OnAnalyse(object sender, EventArgs e)
        {
            Analysis(this.textBoxXiangzhenjie.Text, this.textBoxHongShuiFenxian.Text);
        }
        private void Analysis(string xianzhenjiePath, string hongshuifenxianPath)
        {
            if (!File.Exists(xianzhenjiePath) || !Directory.Exists(hongshuifenxianPath))
            
            {
                MessageBox.Show("文件不存在", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //打开乡镇界图层shp

            /////////////////////////////////////////////////////////////////////////////////add a filed and set value
            IFeatureClass XZFeatureClass = getIFeatureClass(xianzhenjiePath);      //乡镇featureclass
            int Fxfiled = XZFeatureClass.Fields.FindField("xzid");
            if (Fxfiled == -1)
            {
                IField pField = new FieldClass();
                IFieldEdit pfieldEdit = (IFieldEdit)pField;
                pfieldEdit.Name_2 = "xzid";
                pfieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
                pfieldEdit.DefaultValue_2 = 2;
                pfieldEdit.IsNullable_2 = false;
                XZFeatureClass.AddField((IField)pField);



                Fxfiled = XZFeatureClass.Fields.FindField("xzid");
                string dataPath = xianzhenjiePath.Substring(0, xianzhenjiePath.LastIndexOf("\\"));                       //将最后一个字符“/”之前的字符提取出来

                IFeatureWorkspace pFWS = null;
                IWorkspaceFactory pWorkspaceFactory = null;
                pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                pFWS = pWorkspaceFactory.OpenFromFile(dataPath, 0) as IFeatureWorkspace;


                //编辑状态打开
                IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)pFWS;
                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();
                int i=1;

                IFeatureCursor pFeatureCursor = XZFeatureClass.Search(new QueryFilterClass(), false);
                IFeature FZfeature = pFeatureCursor.NextFeature();
                while (FZfeature != null)
                {
                    
                    FZfeature.set_Value(Fxfiled, i);
                    i++;
                    FZfeature.Store();
                    FZfeature = pFeatureCursor.NextFeature();
                }


                //关闭编辑状态
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }



            //.....

            //打开数据库 社会经济数据表  文安洼社会经济数据[1]...xls 导入到数据库的表

            SqlConnection   Conn   =   new SqlConnection( "server=localhost;uid=sa;pwd=sa;database=tongji");
            Conn.Open();
            if (Conn.State != System.Data.ConnectionState.Open)
            {
                MessageBox.Show(" 数据库连接不成功!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            }


            DataTable dt = new DataTable();
            string sql = "select * from  shjj";
            SqlDataAdapter da = new SqlDataAdapter(sql, Conn);
            da.Fill(dt);
            





            //.....



            DataTable ResultesDt = new DataTable();
            //增加列
            ResultesDt.Columns.Add("风险图", typeof(string));
            ResultesDt.Columns.Add("面积", typeof(double));
            ResultesDt.Columns.Add("gdp", typeof(double));
            ResultesDt.Columns.Add("人口", typeof(double));
            ResultesDt.Columns.Add("固定资产", typeof(double));
            ResultesDt.Columns.Add("房屋", typeof(double));
            ResultesDt.Columns.Add("村", typeof(double));

            //查找洪水风险下的面图层列表

            string[] fxtfiles = Directory.GetFiles(hongshuifenxianPath, "*.shp", SearchOption.AllDirectories);
            foreach (string fileName in fxtfiles)
            {
                //MessageBox.Show(fileName);
                calculate(xianzhenjiePath, fileName, dt, ref ResultesDt);
                this.dataGridViewResults.DataSource = ResultesDt.DefaultView;
            }

            // 保存ResultesDt 到数据库 同时界面上绑定显示结果

            
            //this.dataGridViewResults.DataSource = ResultesDt.DefaultView;
            SaveToDatabase(ResultesDt, Conn);
        }

        private void SaveToDatabase(DataTable ResultesDt, SqlConnection Conn)
        {
            //保存到数据库
            ResultesDt.TableName = "result";
            try
            {
                string sql = "select * from result";
                SqlDataAdapter da = new SqlDataAdapter(sql, Conn);

                DataSet dst = new DataSet();
                da.Fill(dst,"result");
                dst.Tables.Clear();                           
                dst.Tables.Add(ResultesDt);
                SqlCommandBuilder  sqlCmdBuilder = new SqlCommandBuilder(da);
                da.Update(dst, "result");

                dst.AcceptChanges();

                //return changedDs;                        //返回更新了的数据库表    
              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "数据库保存失败");
                throw;

            }     

        }
        private void calculate(string XZlayer, string fxtlayer, DataTable dt, ref DataTable pResultesDt)
        {
            //计算一条记录输出到 pResultesDt表
            string XZname;
            double WHArea=0;
            double AftArea=0;
            double Re_people=0;
            double Re_area = 0;
            double Re_village=0;
            double Re_gdp=0;
            double Re_gdzc=0;
            double Re_house=0;

            IFeatureClass XZFeatureClass = getIFeatureClass(XZlayer);      //乡镇featureclass
            IFeatureClass FXFeatureClass = getIFeatureClass(fxtlayer);     //风险图featureclass

            if (FXFeatureClass.ShapeType.ToString() != "esriGeometryPolygon")
               {

               // MessageBox.Show("图层必须为面层!");  判断是否为面层 
                return;
            
                }
                int indexNmfiled = XZFeatureClass.Fields.FindField("name");
                int indexIdfiled = XZFeatureClass.Fields.FindField("xzid");

            ///////////////////////////////////////////////////////////////////////////////细颗粒AO接口,需要转换为raster才能计算统计

                //IZonalOp Zonalop = new RasterZonalOpClass();
        

                //IFeatureClassDescriptor XZFCdcp = new FeatureClassDescriptorClass();
                //XZFCdcp.Create(XZFeatureClass, new QueryFilterClass(), "id");
                ////XZFCdcp.Field = "name";

                //IFeatureClassDescriptor FXFCdcp = new FeatureClassDescriptorClass();
                //FXFCdcp.Create(FXFeatureClass, new QueryFilterClass(), "ID");
                ////FXFCdcp.Field = "id";


                //IGeoDataset XZgeodataset = (IGeoDataset)XZFCdcp;
                //IGeoDataset FXgeodataset = (IGeoDataset)FXFCdcp;
              //  ITable OutTable = Zonalop.TabulateArea(FXgeodataset, XZgeodataset);
            ///////////////////////////////////////////////////////////////////////////////////////////////////toolbox 的Intersect工具
                
                //Geoprocessor GP = new Geoprocessor();
                //GP.OverwriteOutput = true;


                //ESRI.ArcGIS.AnalysisTools.Intersect intersectArea1 = new Intersect();
                //intersectArea1.in_features = XZFeatureClass;
                //intersectArea1.in_features = FXFeatureClass;
                //intersectArea1.out_feature_class = @"C:\temp\test.shp";

                //GP.Execute(intersectArea1,null);
                // end Geoprocessor

            /////////////////////////////////////////////////////////////////////////////////add a filed and set value as 2
                int Fxfiled=FXFeatureClass.Fields.FindField("fxZone");
                if (Fxfiled == -1)
                {
                    IField pField = new FieldClass();
                    IFieldEdit pfieldEdit = (IFieldEdit)pField;
                    pfieldEdit.Name_2 = "fxZone";
                    pfieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
                    pfieldEdit.DefaultValue_2 = 2;
                    pfieldEdit.IsNullable_2 = false;
                    FXFeatureClass.AddField((IField)pField);



                    Fxfiled = FXFeatureClass.Fields.FindField("fxZone");
                    string dataPath = fxtlayer.Substring(0, fxtlayer.LastIndexOf("\\"));                       //将最后一个字符“/”之前的字符提取出来

                    IFeatureWorkspace pFWS = null;
                    IWorkspaceFactory pWorkspaceFactory = null;
                    pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                    pFWS = pWorkspaceFactory.OpenFromFile(dataPath, 0) as IFeatureWorkspace;


                    //编辑状态打开
                    IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)pFWS;
                    workspaceEdit.StartEditing(true);
                    workspaceEdit.StartEditOperation();

                    IFeatureCursor pFeatureCursor = FXFeatureClass.Search(new QueryFilterClass(), false);
                    IFeature FZfeature = pFeatureCursor.NextFeature();
                    while (FZfeature != null)
                    {

                        FZfeature.set_Value(Fxfiled, (int)2);

                        FZfeature.Store();
                        FZfeature = pFeatureCursor.NextFeature();
                    }


                    //关闭编辑状态
                    workspaceEdit.StopEditOperation();
                    workspaceEdit.StopEditing(true);
                }


                ////////////////////////////////////////////////////////////////////////////////读取是esriFieldTypeInteger、esriFieldTypeDouble、esriFieldTypeSmallInteger字段的字段名
                //for (int i = 0; i < FXFeatureClass.Fields.FieldCount; i++) 
                //{

                //    if (FXFeatureClass.Fields.get_Field(i).Type.GetTypeCode().ToString() == esriFieldType.esriFieldTypeInteger.GetTypeCode().ToString() || FXFeatureClass.Fields.get_Field(i).Type.GetTypeCode().ToString() == esriFieldType.esriFieldTypeSmallInteger.GetTypeCode().ToString() || FXFeatureClass.Fields.get_Field(i).Type.GetTypeCode().ToString() == esriFieldType.esriFieldTypeDouble.GetTypeCode().ToString()) 
                //    {
                //        if (FXFeatureClass.Fields.get_Field(i).Name != "FID")
                //        {
                //            zoneFieldNm = FXFeatureClass.Fields.get_Field(i).Name;
                //        }
                //    }
                //    i++;
                //}
                //if (zoneFieldNm == "") 
                //{
                //    MessageBox.Show(FXFeatureClass.AliasName+ "风险图属性表中没有整数型数据列,"+FXFeatureClass.AliasName+"空间统计不能进行!");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -