第六届全国大学生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 resolution
export.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 files
export.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;
}
}
}
}