第六届全国大学生GIS应用技能大赛开发题答案(非官方)
生活随笔
收集整理的這篇文章主要介紹了
第六届全国大学生GIS应用技能大赛开发题答案(非官方)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第六屆全國大學生GIS應用技能大賽開發題答案(非官方)
題目:
根據你的解決方案,開發一個應用型GIS系統,該系統需要具備以下功能:(其中模型和商業圈道路對應分析題
a. 打開地圖文檔。(5分)
b. 導航功能,包括放大、縮小、平移、全圖。(5分)
c. 調用建立的模型。(10分)
d. 對分配好的商業圈道路進行渲染,每種顏色表示一個志愿者。(10分)
e. 導出地圖,格式為jpg。(5分)
f. 保存地圖文檔。(5分)
答案:
共涉及兩個界面,一個主界面和一個模型調用的界面
由于好久不用,主界面崩了,只有代碼了。不過沒關系,可以參考前兩篇文章的界面,也可以自由發揮,不過是一些按鈕罷了。
主界面代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geoprocessing; using ESRI.ArcGIS.Geoprocessor; using ESRI.ArcGIS.Output; using ESRI.ArcGIS.SystemUI;namespace Ex3 {public partial class Form1 : Form{public Form1(){ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);InitializeComponent();axTOCControl1.SetBuddyControl(axMapControl1);}#region 文件string path = null;private void 打開文檔ToolStripMenuItem_Click(object sender, EventArgs e){openFileDialog1.Title = "打開文檔";openFileDialog1.Filter = "ArcMap Document(*.mxd)|*.mxd";openFileDialog1.Multiselect = false;if (openFileDialog1.ShowDialog() == DialogResult.OK){path = openFileDialog1.FileName;axMapControl1.LoadMxFile(path);}axMapControl1.Extent = axMapControl1.FullExtent;axMapControl1.Refresh();}private void 保存文檔ToolStripMenuItem_Click(object sender, EventArgs e){if (path != null){IMxdContents contexts = axMapControl1.Map as IMxdContents;IMapDocument mapDocumnet = new MapDocumentClass();mapDocumnet.Open(path);mapDocumnet.ReplaceContents(contexts);mapDocumnet.Save();MessageBox.Show("保存成功");}else{MessageBox.Show("請先打開文檔!");}}private void 導出圖片ToolStripMenuItem_Click(object sender, EventArgs e){saveFileDialog1.Title = "導出圖片";saveFileDialog1.Filter = "JPEG(*.jpg)|*.jpg";if (saveFileDialog1.ShowDialog() == DialogResult.OK){string pathFileName = saveFileDialog1.FileName;IActiveView activeView = axMapControl1.ActiveView;ESRI.ArcGIS.Output.IExport export = new ESRI.ArcGIS.Output.ExportJPEGClass();export.ExportFileName = pathFileName;// Microsoft Windows default DPI resolutionexport.Resolution = 96;tagRECT exportRECT = activeView.ExportFrame;ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);export.PixelBounds = envelope;System.Int32 hDC = export.StartExporting();activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null);// Finish writing the export file and cleanup any intermediate filesexport.FinishExporting();export.Cleanup();}MessageBox.Show("導出成功");}#endregion#region 地圖導航private void 指針ToolStripMenuItem_Click(object sender, EventArgs e){axMapControl1.CurrentTool = null;}private void 放大ToolStripMenuItem_Click(object sender, EventArgs e){if (axMapControl1.CurrentTool == null){ICommand icc;ITool tool = new ControlsMapZoomInToolClass();axMapControl1.CurrentTool = tool;icc = tool as ICommand;icc.OnCreate(axMapControl1.Object);icc.OnClick();}else{axMapControl1.CurrentTool = null;}}private void 縮小ToolStripMenuItem_Click(object sender, EventArgs e){if (axMapControl1.CurrentTool == null){ICommand icc;ITool tool = new ControlsMapZoomOutToolClass();axMapControl1.CurrentTool = tool;icc = tool as ICommand;icc.OnCreate(axMapControl1.Object);icc.OnClick();}else{axMapControl1.CurrentTool = null;}}private void 漫游ToolStripMenuItem_Click(object sender, EventArgs e){if (axMapControl1.CurrentTool == null){ICommand icc;ITool tool = new ControlsMapPanToolClass();axMapControl1.CurrentTool = tool;icc = tool as ICommand;icc.OnCreate(axMapControl1.Object);icc.OnClick();}else{axMapControl1.CurrentTool = null;}}private void 全局ToolStripMenuItem_Click(object sender, EventArgs e){ICommand icc = new ControlsMapFullExtentCommandClass();icc.OnCreate(axMapControl1.Object);icc.OnClick();}//鷹眼private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e){axMapControl2.ClearLayers();if (axMapControl1.LayerCount != 0){ILayer layer;for (int i = axMapControl1.LayerCount - 1; i >= 0; i--){layer = axMapControl1.get_Layer(i);IObjectCopy copy = new ObjectCopyClass();ILayer layerCopy = copy.Copy(layer) as ILayer;axMapControl2.AddLayer(layerCopy);}axMapControl2.SpatialReference = axMapControl1.SpatialReference;axMapControl2.Extent = axMapControl1.FullExtent;axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}}private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e){IElement ele = new RectangleElementClass();IEnvelope env = axMapControl1.Extent;ele.Geometry = env;ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();IRgbColor color1 = new RgbColorClass();color1.Red = 255;color1.Green = 0;color1.Blue = 0;color1.Transparency = 255;lineSymbol.Color = color1;lineSymbol.Width = 2;ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();IRgbColor color2 = new RgbColorClass();color2.Red = 255;color2.Green = 0;color2.Blue = 0;color2.Transparency = 0;simpleFillSymbol.Color = color2;simpleFillSymbol.Outline = lineSymbol;IFillShapeElement shapeElement = ele as IFillShapeElement;shapeElement.Symbol = simpleFillSymbol;IGraphicsContainer gra = axMapControl2.ActiveView.FocusMap as IGraphicsContainer;gra.DeleteAllElements();gra.AddElement(shapeElement as IElement, 0);axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e){if (e.button == 1){IPoint pt = new PointClass();pt = axMapControl2.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);axMapControl1.CenterAt(pt);axMapControl1.Refresh();}else{IEnvelope env = axMapControl2.TrackRectangle();axMapControl1.Extent = env;axMapControl1.Refresh();}}private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e){if (e.button == 1){IPoint pt = new PointClass();pt = axMapControl2.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);axMapControl1.CenterAt(pt);axMapControl1.Refresh();}}#endregion#region 道路渲染//創建隨機顏色函數private IColor CreateRandomColor(int i){Random ran = new Random(i);IRgbColor color = new RgbColorClass();color.Red = ran.Next(256);color.Green= ran.Next(256);color.Blue = ran.Next(256);return color;}//根據圖層名稱獲取圖層private IFeatureLayer GetLayerByName(string name){ILayer layer;for (int i = 0; i < axMapControl1.LayerCount; i++){layer = axMapControl1.get_Layer(i);if (layer.Name.Equals(name)){return layer as IFeatureLayer;}}return null;}// 唯一值渲染private void 道路渲染ToolStripMenuItem_Click(object sender, EventArgs e){IFeatureLayer layer = GetLayerByName("street");IUniqueValueRenderer renderer = new UniqueValueRendererClass();renderer.FieldCount = 1;renderer.set_Field(0, "FID_fishne");IFeatureCursor cursor = layer.Search(null, true);IFeature feature = cursor.NextFeature();while (feature != null){int i = Convert.ToInt16(feature.get_Value(0));string value = feature.get_Value(feature.Fields.FindField("FID_fishne")).ToString();ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();lineSymbol.Color = CreateRandomColor(i);lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;renderer.AddValue(value, "FID_fishne", lineSymbol as ISymbol);feature = cursor.NextFeature();}IGeoFeatureLayer geoLayer = layer as IGeoFeatureLayer;geoLayer.Renderer = renderer as IFeatureRenderer;axMapControl1.Refresh();axTOCControl1.Update();}#endregion#region 調用模型private void 調用模型ToolStripMenuItem_Click(object sender, EventArgs e){openFileDialog1.Title = "打開模型文件";openFileDialog1.Filter = "ToolBox(*.tbx)|*.tbx";openFileDialog1.InitialDirectory = Application.StartupPath;if (openFileDialog1.ShowDialog() == DialogResult.OK){調用模型 model = new 調用模型();if (model.ShowDialog() == DialogResult.OK){//從 調用模型 窗口獲得文件string[] files = model.Files;GeoProcessor gp = new GeoProcessor();gp.OverwriteOutput = true;gp.AddToolbox(openFileDialog1.FileName);IVariantArray array = new VarArrayClass();array.Add(files[0]);array.Add(files[1]);array.Add(files[2]);object sev = null;try{//“模型” 是自己建立的模型(在AcMAP中根據分析題建立)所在路徑 模型的輸入輸出必須與界面相匹配gp.Execute("模型", array, null);Console.WriteLine(gp.GetMessages(ref sev));// 將結果顯示在地圖控件中IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();IWorkspace workspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(files[2]), 0);IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(files[2]));IFeatureLayer layer = new FeatureLayerClass();layer.FeatureClass = featureClass;layer.Name ="street";axMapControl1.AddLayer(layer);axMapControl1.Refresh();axTOCControl1.Update();MessageBox.Show("Succeed");}catch{// Print geoprocessing execution error messages.Console.WriteLine(gp.GetMessages(ref sev));return;}}}}#endregion } }調用模型界面:
代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; // 此窗體主要是為了獲取輸入輸出的文件路徑,最好與自己建立的模型窗口一致 namespace Ex3 {public partial class 調用模型 : Form{private string[] files = new string[3];public string[] Files{get{return files;}}public 調用模型(){InitializeComponent();}private void button4_Click(object sender, EventArgs e){//確定文件存在 if(System.IO.File.Exists(textBox1.Text)&&System.IO.File.Exists(textBox2.Text)){files[0] = textBox1.Text;files[1] = textBox2.Text;files[2] = textBox3.Text;this.DialogResult = DialogResult.OK;}else{MessageBox.Show("文件不存在!");}}private void button1_Click(object sender, EventArgs e){openFileDialog1.Title = "選擇商業街道路要素文件";openFileDialog1.Filter = "ShapeFile(*.shp)|*.shp";openFileDialog1.Multiselect = false;if (openFileDialog1.ShowDialog() == DialogResult.OK){textBox1.Text = openFileDialog1.FileName;}}private void button2_Click(object sender, EventArgs e){openFileDialog1.Title = "選擇志愿者表";openFileDialog1.Filter = "Excel(*.xls;*.xlsx)|*.xls;*.xlsx";openFileDialog1.Multiselect = false;if (openFileDialog1.ShowDialog() == DialogResult.OK){textBox2.Text = openFileDialog1.FileName;}}private void button3_Click(object sender, EventArgs e){saveFileDialog1.Title = "保存分配好的文件";saveFileDialog1.Filter = "ShapeFile(*.shp)|*.shp";if (saveFileDialog1.ShowDialog() == DialogResult.OK){textBox3.Text = saveFileDialog1.FileName;}}} }總結
以上是生活随笔為你收集整理的第六届全国大学生GIS应用技能大赛开发题答案(非官方)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#using static
- 下一篇: 支付宝app支付java后台流程、原理分