Esempio n. 1
0
 private bool PolygonToRaster(ILayer selectedLayer, string inputType, string valueField, string priorityField)
 {
     try
     {
         ESRI.ArcGIS.Geoprocessor.Geoprocessor       gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
         ESRI.ArcGIS.ConversionTools.PolygonToRaster polygonToRaster = new ESRI.ArcGIS.ConversionTools.PolygonToRaster();
         polygonToRaster.in_features       = selectedLayer;
         polygonToRaster.value_field       = valueField;//"distance";
         polygonToRaster.out_rasterdataset = AppSingleton.Instance().WorkspacePath + "\\Poly_Raster_" + selectedLayer.Name;
         polygonToRaster.cell_assignment   = "MAXIMUM_AREA";
         polygonToRaster.cellsize          = AppSingleton.Instance().CellSize;
         if (priorityField != "")
         {
             polygonToRaster.priority_field = priorityField; //"priority";
         }
         IFeatureLayer fLayer = AppSingleton.Instance().SinirLayer as IFeatureLayer;
         IEnvelope     env    = fLayer.AreaOfInterest.Envelope;
         gp.SetEnvironmentValue("Extent", env.XMin.ToString() + " " + env.YMin.ToString() + " " + env.XMax.ToString() + " " + env.YMax.ToString());
         gp.AddOutputsToMap = AppSingleton.Instance().AralariEkle;
         gp.OverwriteOutput = true;
         gp.Execute(polygonToRaster, null);
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
        private void button1_Click(object sender, EventArgs e)
        {
            string myListBoxContent;
            double myCellSize;

            myCellSize = System.Convert.ToDouble(textBox4.Text);
            //try
            {
                myListBoxContent = "正在转换所选ShpFile到栅格数据...";
                textBox3.Text   += myListBoxContent;
                ILayer        selectedLayer        = mainForm.axMapControl1.get_Layer(comboBox1.SelectedIndex);
                IFeatureLayer selectedFeatureLayer = selectedLayer as IFeatureLayer;
                IFeatureClass selectedFeatureClass = selectedFeatureLayer.FeatureClass;
                Geoprocessor  pGeoprocessor        = new Geoprocessor();
                pGeoprocessor.OverwriteOutput = true;

                if (selectedFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    ESRI.ArcGIS.ConversionTools.PointToRaster pPointToRaster =
                        new ESRI.ArcGIS.ConversionTools.PointToRaster();
                    pGeoprocessor.SetEnvironmentValue("workspace", myFilePath);
                    pPointToRaster.cellsize          = myCellSize;
                    pPointToRaster.in_features       = selectedFeatureLayer;
                    pPointToRaster.value_field       = selectedFeatureClass.Fields.get_Field(comboBox2.SelectedIndex);
                    pPointToRaster.out_rasterdataset = textBox2.Text + ".tif";
                    pGeoprocessor.Execute(pPointToRaster, null);
                    myListBoxContent = "转换成功!";
                    textBox3.Text   += Environment.NewLine + Environment.NewLine + myListBoxContent;
                }
                else if (selectedFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                {
                    ESRI.ArcGIS.ConversionTools.PolylineToRaster pPolylineToRaster =
                        new ESRI.ArcGIS.ConversionTools.PolylineToRaster();
                    pGeoprocessor.SetEnvironmentValue("workspace", myFilePath);
                    pPolylineToRaster.cellsize          = myCellSize;
                    pPolylineToRaster.in_features       = selectedFeatureLayer;
                    pPolylineToRaster.value_field       = selectedFeatureClass.Fields.get_Field(comboBox2.SelectedIndex);
                    pPolylineToRaster.out_rasterdataset = textBox2.Text + ".tif";
                    pGeoprocessor.Execute(pPolylineToRaster, null);
                    myListBoxContent = "转换成功!";
                    textBox3.Text   += Environment.NewLine + Environment.NewLine + myListBoxContent;
                }
                else if (selectedFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                {
                    ESRI.ArcGIS.ConversionTools.PolygonToRaster pPolygonToRaster =
                        new ESRI.ArcGIS.ConversionTools.PolygonToRaster();
                    pGeoprocessor.SetEnvironmentValue("workspace", myFilePath);
                    pPolygonToRaster.cellsize          = myCellSize;
                    pPolygonToRaster.in_features       = selectedFeatureLayer;
                    pPolygonToRaster.value_field       = selectedFeatureClass.Fields.get_Field(comboBox2.SelectedIndex);
                    pPolygonToRaster.out_rasterdataset = textBox2.Text + ".tif";
                    pGeoprocessor.Execute(pPolygonToRaster, null);
                    myListBoxContent = "转换成功!";
                    textBox3.Text   += Environment.NewLine + Environment.NewLine + myListBoxContent;
                }
                else
                {
                    MessageBox.Show("请选择正确的ShapeFile文件");
                }

                if (MessageBox.Show("是否将生成的新Raster文件添加到图层", "Confirm Message",
                                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    IRasterLayer myRasterLayer = new RasterLayerClass();
                    try
                    {
                        myRasterLayer.CreateFromFilePath(myFilePath + "\\" + textBox2.Text + ".tif");
                        mainForm.axMapControl1.AddLayer(myRasterLayer, 0);
                    }
                    catch
                    {
                        MessageBox.Show("打开文件错误!");
                    }
                }
            }
        }