[C#]
ServiceArea_GISServerAPIClass.cs
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Xml; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Server; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Location; using ESRI.ArcGIS.NetworkAnalyst; using System.Collections.Specialized; namespace ServiceArea_GISServerAPI { /// <summary> /// Summary description for Form1. /// </summary> public class ServiceArea_GISServerAPIClass : System.Windows.Forms.Form { #region Window Controls Declaration private System.ComponentModel.IContainer components = null; private TabPage tabReturnSAPolygonGeometry; private TreeView treeViewSAPolygonGeometries; private TabPage tabReturnFacilitiesFeatures; private DataGrid dataGridFacilitiesFeatures; private TabPage tabReturnBarrierFeatures; private DataGrid dataGridBarrierFeatures; private TabPage tabReturnSAPolygonFeatures; private DataGrid dataGridSAPolygonFeatures; private TabPage tabReturnMap; private PictureBox pictureBox; private TabControl tabCtrlOutput; private TabPage tabReturnSALineFeatures; private TabPage tabReturnSALineGeometry; private DataGrid dataGridSALineFeatures; private Panel panel1; private GroupBox fraINAServiceAreaSolver; private Label label30; private ComboBox cboSATrimPolygonUnits; private TextBox txtSATrimPolygonDistance; private ComboBox cboSATravelDirection; private CheckBox chkOutputLines; private CheckBox chkTrimOuterPolygon; private TextBox txtSADefaultBreaks; private Label label28; private ComboBox cboSAOutputPolygons; private CheckBox chkOutputPolygon; private GroupBox fraINAServerServiceAreaParams; private CheckBox chkReturnSAPolygons; private CheckBox chkReturnSAPolygonGeometries; private CheckBox chkReturnSALines; private CheckBox chkReturnSAFacilities; private CheckBox chkReturnSALineGeometries; private ComboBox cboLocationsLayers; private Label label6; private CheckBox chckReturnSABarriers; private GroupBox fraINAServerSolverParams; private CheckBox chkReturnMap; private ComboBox cboSnapToleranceUnits; private ComboBox cboNALayers; private Label label10; private Label label8; private TextBox txtMaxSnapTolerance; private TextBox txtSnapTolerance; private Label label9; private GroupBox fraINASolverSettings; private CheckBox chkIgnoreInvalidLocations; private ComboBox cboUturnPolicy; private Label label11; private Label label7; private CheckedListBox chklstAccumulateAttributes; private Label label5; private CheckedListBox chklstRestrictions; private ComboBox cboImpedance; private Label label4; private TextBox txtService; private TextBox txtServer; private Button cmdSolve; private Label label2; private Label label1; private Button cmdConnect; private ComboBox cboSAOverlapType; private ComboBox cboSAMultipleFacilityOptions; private Label label3; private Label label13; private Label label12; private TreeView treeViewSALineGeometries; #endregion public ServiceArea_GISServerAPIClass() { // // Required for Windows Form Designer support // InitializeComponent(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tabReturnSAPolygonGeometry = new System.Windows.Forms.TabPage(); this.treeViewSAPolygonGeometries = new System.Windows.Forms.TreeView(); this.tabReturnFacilitiesFeatures = new System.Windows.Forms.TabPage(); this.dataGridFacilitiesFeatures = new System.Windows.Forms.DataGrid(); this.tabReturnBarrierFeatures = new System.Windows.Forms.TabPage(); this.dataGridBarrierFeatures = new System.Windows.Forms.DataGrid(); this.tabReturnSAPolygonFeatures = new System.Windows.Forms.TabPage(); this.dataGridSAPolygonFeatures = new System.Windows.Forms.DataGrid(); this.tabReturnMap = new System.Windows.Forms.TabPage(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.tabCtrlOutput = new System.Windows.Forms.TabControl(); this.tabReturnSALineFeatures = new System.Windows.Forms.TabPage(); this.dataGridSALineFeatures = new System.Windows.Forms.DataGrid(); this.tabReturnSALineGeometry = new System.Windows.Forms.TabPage(); this.treeViewSALineGeometries = new System.Windows.Forms.TreeView(); this.panel1 = new System.Windows.Forms.Panel(); this.fraINAServiceAreaSolver = new System.Windows.Forms.GroupBox(); this.cboSAOverlapType = new System.Windows.Forms.ComboBox(); this.cboSAMultipleFacilityOptions = new System.Windows.Forms.ComboBox(); this.cboSAOutputPolygons = new System.Windows.Forms.ComboBox(); this.label13 = new System.Windows.Forms.Label(); this.label12 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label30 = new System.Windows.Forms.Label(); this.cboSATrimPolygonUnits = new System.Windows.Forms.ComboBox(); this.txtSATrimPolygonDistance = new System.Windows.Forms.TextBox(); this.cboSATravelDirection = new System.Windows.Forms.ComboBox(); this.chkOutputLines = new System.Windows.Forms.CheckBox(); this.chkTrimOuterPolygon = new System.Windows.Forms.CheckBox(); this.txtSADefaultBreaks = new System.Windows.Forms.TextBox(); this.label28 = new System.Windows.Forms.Label(); this.chkOutputPolygon = new System.Windows.Forms.CheckBox(); this.fraINAServerServiceAreaParams = new System.Windows.Forms.GroupBox(); this.chkReturnSAPolygons = new System.Windows.Forms.CheckBox(); this.chkReturnSAPolygonGeometries = new System.Windows.Forms.CheckBox(); this.chkReturnSALines = new System.Windows.Forms.CheckBox(); this.chkReturnSAFacilities = new System.Windows.Forms.CheckBox(); this.chkReturnSALineGeometries = new System.Windows.Forms.CheckBox(); this.cboLocationsLayers = new System.Windows.Forms.ComboBox(); this.label6 = new System.Windows.Forms.Label(); this.chckReturnSABarriers = new System.Windows.Forms.CheckBox(); this.fraINAServerSolverParams = new System.Windows.Forms.GroupBox(); this.chkReturnMap = new System.Windows.Forms.CheckBox(); this.cboSnapToleranceUnits = new System.Windows.Forms.ComboBox(); this.cboNALayers = new System.Windows.Forms.ComboBox(); this.label10 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.txtMaxSnapTolerance = new System.Windows.Forms.TextBox(); this.txtSnapTolerance = new System.Windows.Forms.TextBox(); this.label9 = new System.Windows.Forms.Label(); this.fraINASolverSettings = new System.Windows.Forms.GroupBox(); this.chkIgnoreInvalidLocations = new System.Windows.Forms.CheckBox(); this.cboUturnPolicy = new System.Windows.Forms.ComboBox(); this.label11 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.chklstAccumulateAttributes = new System.Windows.Forms.CheckedListBox(); this.label5 = new System.Windows.Forms.Label(); this.chklstRestrictions = new System.Windows.Forms.CheckedListBox(); this.cboImpedance = new System.Windows.Forms.ComboBox(); this.label4 = new System.Windows.Forms.Label(); this.txtService = new System.Windows.Forms.TextBox(); this.txtServer = new System.Windows.Forms.TextBox(); this.cmdSolve = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.cmdConnect = new System.Windows.Forms.Button(); this.tabReturnSAPolygonGeometry.SuspendLayout(); this.tabReturnFacilitiesFeatures.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridFacilitiesFeatures)).BeginInit(); this.tabReturnBarrierFeatures.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridBarrierFeatures)).BeginInit(); this.tabReturnSAPolygonFeatures.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridSAPolygonFeatures)).BeginInit(); this.tabReturnMap.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.tabCtrlOutput.SuspendLayout(); this.tabReturnSALineFeatures.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridSALineFeatures)).BeginInit(); this.tabReturnSALineGeometry.SuspendLayout(); this.panel1.SuspendLayout(); this.fraINAServiceAreaSolver.SuspendLayout(); this.fraINAServerServiceAreaParams.SuspendLayout(); this.fraINAServerSolverParams.SuspendLayout(); this.fraINASolverSettings.SuspendLayout(); this.SuspendLayout(); // // tabReturnSAPolygonGeometry // this.tabReturnSAPolygonGeometry.Controls.Add(this.treeViewSAPolygonGeometries); this.tabReturnSAPolygonGeometry.Location = new System.Drawing.Point(4, 22); this.tabReturnSAPolygonGeometry.Name = "tabReturnSAPolygonGeometry"; this.tabReturnSAPolygonGeometry.Size = new System.Drawing.Size(584, 604); this.tabReturnSAPolygonGeometry.TabIndex = 5; this.tabReturnSAPolygonGeometry.Text = "SAPolygon Geometries"; this.tabReturnSAPolygonGeometry.UseVisualStyleBackColor = true; // // treeViewSAPolygonGeometries // this.treeViewSAPolygonGeometries.Dock = System.Windows.Forms.DockStyle.Fill; this.treeViewSAPolygonGeometries.Location = new System.Drawing.Point(0, 0); this.treeViewSAPolygonGeometries.Name = "treeViewSAPolygonGeometries"; this.treeViewSAPolygonGeometries.Size = new System.Drawing.Size(584, 604); this.treeViewSAPolygonGeometries.TabIndex = 1; // // tabReturnFacilitiesFeatures // this.tabReturnFacilitiesFeatures.Controls.Add(this.dataGridFacilitiesFeatures); this.tabReturnFacilitiesFeatures.Location = new System.Drawing.Point(4, 22); this.tabReturnFacilitiesFeatures.Name = "tabReturnFacilitiesFeatures"; this.tabReturnFacilitiesFeatures.Size = new System.Drawing.Size(584, 604); this.tabReturnFacilitiesFeatures.TabIndex = 6; this.tabReturnFacilitiesFeatures.Text = "Facilities Features"; this.tabReturnFacilitiesFeatures.UseVisualStyleBackColor = true; // // dataGridFacilitiesFeatures // this.dataGridFacilitiesFeatures.DataMember = ""; this.dataGridFacilitiesFeatures.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridFacilitiesFeatures.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridFacilitiesFeatures.Location = new System.Drawing.Point(0, 0); this.dataGridFacilitiesFeatures.Name = "dataGridFacilitiesFeatures"; this.dataGridFacilitiesFeatures.Size = new System.Drawing.Size(584, 604); this.dataGridFacilitiesFeatures.TabIndex = 0; // // tabReturnBarrierFeatures // this.tabReturnBarrierFeatures.Controls.Add(this.dataGridBarrierFeatures); this.tabReturnBarrierFeatures.Location = new System.Drawing.Point(4, 22); this.tabReturnBarrierFeatures.Name = "tabReturnBarrierFeatures"; this.tabReturnBarrierFeatures.Size = new System.Drawing.Size(584, 604); this.tabReturnBarrierFeatures.TabIndex = 3; this.tabReturnBarrierFeatures.Text = "Barrier Features"; this.tabReturnBarrierFeatures.UseVisualStyleBackColor = true; // // dataGridBarrierFeatures // this.dataGridBarrierFeatures.DataMember = ""; this.dataGridBarrierFeatures.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridBarrierFeatures.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridBarrierFeatures.Location = new System.Drawing.Point(0, 0); this.dataGridBarrierFeatures.Name = "dataGridBarrierFeatures"; this.dataGridBarrierFeatures.Size = new System.Drawing.Size(584, 604); this.dataGridBarrierFeatures.TabIndex = 0; // // tabReturnSAPolygonFeatures // this.tabReturnSAPolygonFeatures.Controls.Add(this.dataGridSAPolygonFeatures); this.tabReturnSAPolygonFeatures.Location = new System.Drawing.Point(4, 22); this.tabReturnSAPolygonFeatures.Name = "tabReturnSAPolygonFeatures"; this.tabReturnSAPolygonFeatures.Size = new System.Drawing.Size(584, 604); this.tabReturnSAPolygonFeatures.TabIndex = 4; this.tabReturnSAPolygonFeatures.Text = "SAPolygon Features"; this.tabReturnSAPolygonFeatures.UseVisualStyleBackColor = true; // // dataGridSAPolygonFeatures // this.dataGridSAPolygonFeatures.DataMember = ""; this.dataGridSAPolygonFeatures.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridSAPolygonFeatures.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridSAPolygonFeatures.Location = new System.Drawing.Point(0, 0); this.dataGridSAPolygonFeatures.Name = "dataGridSAPolygonFeatures"; this.dataGridSAPolygonFeatures.Size = new System.Drawing.Size(584, 604); this.dataGridSAPolygonFeatures.TabIndex = 0; // // tabReturnMap // this.tabReturnMap.Controls.Add(this.pictureBox); this.tabReturnMap.Location = new System.Drawing.Point(4, 22); this.tabReturnMap.Name = "tabReturnMap"; this.tabReturnMap.Size = new System.Drawing.Size(584, 604); this.tabReturnMap.TabIndex = 0; this.tabReturnMap.Text = "Map"; this.tabReturnMap.UseVisualStyleBackColor = true; // // pictureBox // this.pictureBox.BackColor = System.Drawing.Color.White; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Name = "pictureBox"; this.pictureBox.Size = new System.Drawing.Size(584, 604); this.pictureBox.TabIndex = 21; this.pictureBox.TabStop = false; // // tabCtrlOutput // this.tabCtrlOutput.Controls.Add(this.tabReturnMap); this.tabCtrlOutput.Controls.Add(this.tabReturnSAPolygonFeatures); this.tabCtrlOutput.Controls.Add(this.tabReturnSALineFeatures); this.tabCtrlOutput.Controls.Add(this.tabReturnBarrierFeatures); this.tabCtrlOutput.Controls.Add(this.tabReturnFacilitiesFeatures); this.tabCtrlOutput.Controls.Add(this.tabReturnSAPolygonGeometry); this.tabCtrlOutput.Controls.Add(this.tabReturnSALineGeometry); this.tabCtrlOutput.Dock = System.Windows.Forms.DockStyle.Fill; this.tabCtrlOutput.Enabled = false; this.tabCtrlOutput.Location = new System.Drawing.Point(522, 0); this.tabCtrlOutput.Name = "tabCtrlOutput"; this.tabCtrlOutput.SelectedIndex = 0; this.tabCtrlOutput.Size = new System.Drawing.Size(592, 630); this.tabCtrlOutput.TabIndex = 30; // // tabReturnSALineFeatures // this.tabReturnSALineFeatures.Controls.Add(this.dataGridSALineFeatures); this.tabReturnSALineFeatures.Location = new System.Drawing.Point(4, 22); this.tabReturnSALineFeatures.Name = "tabReturnSALineFeatures"; this.tabReturnSALineFeatures.Size = new System.Drawing.Size(584, 604); this.tabReturnSALineFeatures.TabIndex = 7; this.tabReturnSALineFeatures.Text = "SALine Features"; this.tabReturnSALineFeatures.UseVisualStyleBackColor = true; // // dataGridSALineFeatures // this.dataGridSALineFeatures.DataMember = ""; this.dataGridSALineFeatures.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridSALineFeatures.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGridSALineFeatures.Location = new System.Drawing.Point(0, 0); this.dataGridSALineFeatures.Name = "dataGridSALineFeatures"; this.dataGridSALineFeatures.Size = new System.Drawing.Size(584, 604); this.dataGridSALineFeatures.TabIndex = 0; // // tabReturnSALineGeometry // this.tabReturnSALineGeometry.Controls.Add(this.treeViewSALineGeometries); this.tabReturnSALineGeometry.Location = new System.Drawing.Point(4, 22); this.tabReturnSALineGeometry.Name = "tabReturnSALineGeometry"; this.tabReturnSALineGeometry.Size = new System.Drawing.Size(584, 604); this.tabReturnSALineGeometry.TabIndex = 8; this.tabReturnSALineGeometry.Text = "SALine Geometries"; this.tabReturnSALineGeometry.UseVisualStyleBackColor = true; // // treeViewSALineGeometries // this.treeViewSALineGeometries.Dock = System.Windows.Forms.DockStyle.Fill; this.treeViewSALineGeometries.Location = new System.Drawing.Point(0, 0); this.treeViewSALineGeometries.Name = "treeViewSALineGeometries"; this.treeViewSALineGeometries.Size = new System.Drawing.Size(584, 604); this.treeViewSALineGeometries.TabIndex = 0; // // panel1 // this.panel1.Controls.Add(this.fraINAServiceAreaSolver); this.panel1.Controls.Add(this.fraINAServerServiceAreaParams); this.panel1.Controls.Add(this.fraINAServerSolverParams); this.panel1.Controls.Add(this.fraINASolverSettings); this.panel1.Controls.Add(this.txtService); this.panel1.Controls.Add(this.txtServer); this.panel1.Controls.Add(this.cmdSolve); this.panel1.Controls.Add(this.label2); this.panel1.Controls.Add(this.label1); this.panel1.Controls.Add(this.cmdConnect); this.panel1.Dock = System.Windows.Forms.DockStyle.Left; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(522, 630); this.panel1.TabIndex = 77; // // fraINAServiceAreaSolver // this.fraINAServiceAreaSolver.Controls.Add(this.cboSAOverlapType); this.fraINAServiceAreaSolver.Controls.Add(this.cboSAMultipleFacilityOptions); this.fraINAServiceAreaSolver.Controls.Add(this.cboSAOutputPolygons); this.fraINAServiceAreaSolver.Controls.Add(this.label13); this.fraINAServiceAreaSolver.Controls.Add(this.label12); this.fraINAServiceAreaSolver.Controls.Add(this.label3); this.fraINAServiceAreaSolver.Controls.Add(this.label30); this.fraINAServiceAreaSolver.Controls.Add(this.cboSATrimPolygonUnits); this.fraINAServiceAreaSolver.Controls.Add(this.txtSATrimPolygonDistance); this.fraINAServiceAreaSolver.Controls.Add(this.cboSATravelDirection); this.fraINAServiceAreaSolver.Controls.Add(this.chkOutputLines); this.fraINAServiceAreaSolver.Controls.Add(this.chkTrimOuterPolygon); this.fraINAServiceAreaSolver.Controls.Add(this.txtSADefaultBreaks); this.fraINAServiceAreaSolver.Controls.Add(this.label28); this.fraINAServiceAreaSolver.Controls.Add(this.chkOutputPolygon); this.fraINAServiceAreaSolver.Enabled = false; this.fraINAServiceAreaSolver.Location = new System.Drawing.Point(233, 372); this.fraINAServiceAreaSolver.Name = "fraINAServiceAreaSolver"; this.fraINAServiceAreaSolver.Size = new System.Drawing.Size(271, 245); this.fraINAServiceAreaSolver.TabIndex = 86; this.fraINAServiceAreaSolver.TabStop = false; this.fraINAServiceAreaSolver.Text = "INAServiceAreaSolver"; // // cboSAOverlapType // this.cboSAOverlapType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboSAOverlapType.ItemHeight = 13; this.cboSAOverlapType.Location = new System.Drawing.Point(102, 211); this.cboSAOverlapType.Name = "cboSAOverlapType"; this.cboSAOverlapType.Size = new System.Drawing.Size(159, 21); this.cboSAOverlapType.TabIndex = 95; // // cboSAMultipleFacilityOptions // this.cboSAMultipleFacilityOptions.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboSAMultipleFacilityOptions.ItemHeight = 13; this.cboSAMultipleFacilityOptions.Location = new System.Drawing.Point(102, 184); this.cboSAMultipleFacilityOptions.Name = "cboSAMultipleFacilityOptions"; this.cboSAMultipleFacilityOptions.Size = new System.Drawing.Size(159, 21); this.cboSAMultipleFacilityOptions.TabIndex = 94; // // cboSAOutputPolygons // this.cboSAOutputPolygons.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboSAOutputPolygons.ItemHeight = 13; this.cboSAOutputPolygons.Location = new System.Drawing.Point(102, 155); this.cboSAOutputPolygons.Name = "cboSAOutputPolygons"; this.cboSAOutputPolygons.Size = new System.Drawing.Size(159, 21); this.cboSAOutputPolygons.TabIndex = 66; // // label13 // this.label13.Location = new System.Drawing.Point(6, 214); this.label13.Name = "label13"; this.label13.Size = new System.Drawing.Size(100, 16); this.label13.TabIndex = 98; this.label13.Text = "Overlap Type"; // // label12 // this.label12.Location = new System.Drawing.Point(6, 189); this.label12.Name = "label12"; this.label12.Size = new System.Drawing.Size(100, 16); this.label12.TabIndex = 97; this.label12.Text = "Multiple Facilities"; // // label3 // this.label3.Location = new System.Drawing.Point(6, 160); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(100, 16); this.label3.TabIndex = 96; this.label3.Text = "Polygon Type"; // // label30 // this.label30.Location = new System.Drawing.Point(3, 44); this.label30.Name = "label30"; this.label30.Size = new System.Drawing.Size(131, 16); this.label30.TabIndex = 92; this.label30.Text = "Direction of Travel"; // // cboSATrimPolygonUnits // this.cboSATrimPolygonUnits.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboSATrimPolygonUnits.Location = new System.Drawing.Point(181, 124); this.cboSATrimPolygonUnits.Name = "cboSATrimPolygonUnits"; this.cboSATrimPolygonUnits.Size = new System.Drawing.Size(80, 21); this.cboSATrimPolygonUnits.TabIndex = 76; // // txtSATrimPolygonDistance // this.txtSATrimPolygonDistance.Location = new System.Drawing.Point(133, 124); this.txtSATrimPolygonDistance.Name = "txtSATrimPolygonDistance"; this.txtSATrimPolygonDistance.Size = new System.Drawing.Size(40, 20); this.txtSATrimPolygonDistance.TabIndex = 75; this.txtSATrimPolygonDistance.Text = "50"; // // cboSATravelDirection // this.cboSATravelDirection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboSATravelDirection.Location = new System.Drawing.Point(6, 63); this.cboSATravelDirection.Name = "cboSATravelDirection"; this.cboSATravelDirection.Size = new System.Drawing.Size(255, 21); this.cboSATravelDirection.TabIndex = 91; // // chkOutputLines // this.chkOutputLines.Checked = true; this.chkOutputLines.CheckState = System.Windows.Forms.CheckState.Checked; this.chkOutputLines.Location = new System.Drawing.Point(6, 86); this.chkOutputLines.Name = "chkOutputLines"; this.chkOutputLines.Size = new System.Drawing.Size(112, 22); this.chkOutputLines.TabIndex = 68; this.chkOutputLines.Text = "Generate Lines"; // // chkTrimOuterPolygon // this.chkTrimOuterPolygon.Checked = true; this.chkTrimOuterPolygon.CheckState = System.Windows.Forms.CheckState.Checked; this.chkTrimOuterPolygon.Location = new System.Drawing.Point(21, 124); this.chkTrimOuterPolygon.Name = "chkTrimOuterPolygon"; this.chkTrimOuterPolygon.Size = new System.Drawing.Size(128, 25); this.chkTrimOuterPolygon.TabIndex = 59; this.chkTrimOuterPolygon.Text = "Trim Outer Polygon"; // // txtSADefaultBreaks // this.txtSADefaultBreaks.Location = new System.Drawing.Point(70, 19); this.txtSADefaultBreaks.Name = "txtSADefaultBreaks"; this.txtSADefaultBreaks.Size = new System.Drawing.Size(191, 20); this.txtSADefaultBreaks.TabIndex = 64; // // label28 // this.label28.Location = new System.Drawing.Point(3, 22); this.label28.Name = "label28"; this.label28.Size = new System.Drawing.Size(64, 16); this.label28.TabIndex = 63; this.label28.Text = "Breaks"; // // chkOutputPolygon // this.chkOutputPolygon.Checked = true; this.chkOutputPolygon.CheckState = System.Windows.Forms.CheckState.Checked; this.chkOutputPolygon.Location = new System.Drawing.Point(6, 102); this.chkOutputPolygon.Name = "chkOutputPolygon"; this.chkOutputPolygon.Size = new System.Drawing.Size(172, 28); this.chkOutputPolygon.TabIndex = 65; this.chkOutputPolygon.Text = "Generate Polygons"; this.chkOutputPolygon.CheckedChanged += new System.EventHandler(this.chkOutputPolygon_CheckedChanged); // // fraINAServerServiceAreaParams // this.fraINAServerServiceAreaParams.Controls.Add(this.chkReturnSAPolygons); this.fraINAServerServiceAreaParams.Controls.Add(this.chkReturnSAPolygonGeometries); this.fraINAServerServiceAreaParams.Controls.Add(this.chkReturnSALines); this.fraINAServerServiceAreaParams.Controls.Add(this.chkReturnSAFacilities); this.fraINAServerServiceAreaParams.Controls.Add(this.chkReturnSALineGeometries); this.fraINAServerServiceAreaParams.Controls.Add(this.cboLocationsLayers); this.fraINAServerServiceAreaParams.Controls.Add(this.label6); this.fraINAServerServiceAreaParams.Controls.Add(this.chckReturnSABarriers); this.fraINAServerServiceAreaParams.Enabled = false; this.fraINAServerServiceAreaParams.Location = new System.Drawing.Point(12, 372); this.fraINAServerServiceAreaParams.Name = "fraINAServerServiceAreaParams"; this.fraINAServerServiceAreaParams.Size = new System.Drawing.Size(216, 205); this.fraINAServerServiceAreaParams.TabIndex = 85; this.fraINAServerServiceAreaParams.TabStop = false; this.fraINAServerServiceAreaParams.Text = "INAServerServiceAreaParams"; // // chkReturnSAPolygons // this.chkReturnSAPolygons.Checked = true; this.chkReturnSAPolygons.CheckState = System.Windows.Forms.CheckState.Checked; this.chkReturnSAPolygons.Location = new System.Drawing.Point(11, 132); this.chkReturnSAPolygons.Name = "chkReturnSAPolygons"; this.chkReturnSAPolygons.Size = new System.Drawing.Size(176, 19); this.chkReturnSAPolygons.TabIndex = 86; this.chkReturnSAPolygons.Text = "Return SAPolygons"; // // chkReturnSAPolygonGeometries // this.chkReturnSAPolygonGeometries.Checked = true; this.chkReturnSAPolygonGeometries.CheckState = System.Windows.Forms.CheckState.Checked; this.chkReturnSAPolygonGeometries.Location = new System.Drawing.Point(11, 107); this.chkReturnSAPolygonGeometries.Name = "chkReturnSAPolygonGeometries"; this.chkReturnSAPolygonGeometries.Size = new System.Drawing.Size(184, 16); this.chkReturnSAPolygonGeometries.TabIndex = 85; this.chkReturnSAPolygonGeometries.Text = "Return SAPolygon Geometries"; // // chkReturnSALines // this.chkReturnSALines.Checked = true; this.chkReturnSALines.CheckState = System.Windows.Forms.CheckState.Checked; this.chkReturnSALines.Location = new System.Drawing.Point(11, 82); this.chkReturnSALines.Name = "chkReturnSALines"; this.chkReturnSALines.Size = new System.Drawing.Size(176, 16); this.chkReturnSALines.TabIndex = 84; this.chkReturnSALines.Text = "Return SALines"; // // chkReturnSAFacilities // this.chkReturnSAFacilities.Checked = true; this.chkReturnSAFacilities.CheckState = System.Windows.Forms.CheckState.Checked; this.chkReturnSAFacilities.Location = new System.Drawing.Point(11, 160); this.chkReturnSAFacilities.Name = "chkReturnSAFacilities"; this.chkReturnSAFacilities.Size = new System.Drawing.Size(160, 16); this.chkReturnSAFacilities.TabIndex = 83; this.chkReturnSAFacilities.Text = "Return Facilities"; // // chkReturnSALineGeometries // this.chkReturnSALineGeometries.Checked = true; this.chkReturnSALineGeometries.CheckState = System.Windows.Forms.CheckState.Checked; this.chkReturnSALineGeometries.Location = new System.Drawing.Point(11, 57); this.chkReturnSALineGeometries.Name = "chkReturnSALineGeometries"; this.chkReturnSALineGeometries.Size = new System.Drawing.Size(176, 16); this.chkReturnSALineGeometries.TabIndex = 82; this.chkReturnSALineGeometries.Text = "Return SALine Geometries"; // // cboLocationsLayers // this.cboLocationsLayers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboLocationsLayers.Location = new System.Drawing.Point(85, 24); this.cboLocationsLayers.Name = "cboLocationsLayers"; this.cboLocationsLayers.Size = new System.Drawing.Size(123, 21); this.cboLocationsLayers.TabIndex = 14; // // label6 // this.label6.Location = new System.Drawing.Point(6, 27); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(82, 18); this.label6.TabIndex = 81; this.label6.Text = "Input Facilities"; // // chckReturnSABarriers // this.chckReturnSABarriers.Checked = true; this.chckReturnSABarriers.CheckState = System.Windows.Forms.CheckState.Checked; this.chckReturnSABarriers.Location = new System.Drawing.Point(11, 185); this.chckReturnSABarriers.Name = "chckReturnSABarriers"; this.chckReturnSABarriers.Size = new System.Drawing.Size(136, 16); this.chckReturnSABarriers.TabIndex = 17; this.chckReturnSABarriers.Text = "Return Barriers"; // // fraINAServerSolverParams // this.fraINAServerSolverParams.Controls.Add(this.chkReturnMap); this.fraINAServerSolverParams.Controls.Add(this.cboSnapToleranceUnits); this.fraINAServerSolverParams.Controls.Add(this.cboNALayers); this.fraINAServerSolverParams.Controls.Add(this.label10); this.fraINAServerSolverParams.Controls.Add(this.label8); this.fraINAServerSolverParams.Controls.Add(this.txtMaxSnapTolerance); this.fraINAServerSolverParams.Controls.Add(this.txtSnapTolerance); this.fraINAServerSolverParams.Controls.Add(this.label9); this.fraINAServerSolverParams.Enabled = false; this.fraINAServerSolverParams.Location = new System.Drawing.Point(12, 68); this.fraINAServerSolverParams.Name = "fraINAServerSolverParams"; this.fraINAServerSolverParams.Size = new System.Drawing.Size(492, 96); this.fraINAServerSolverParams.TabIndex = 84; this.fraINAServerSolverParams.TabStop = false; this.fraINAServerSolverParams.Text = "INAServerSolverParams"; // // chkReturnMap // this.chkReturnMap.Checked = true; this.chkReturnMap.CheckState = System.Windows.Forms.CheckState.Checked; this.chkReturnMap.Location = new System.Drawing.Point(8, 72); this.chkReturnMap.Name = "chkReturnMap"; this.chkReturnMap.Size = new System.Drawing.Size(96, 16); this.chkReturnMap.TabIndex = 7; this.chkReturnMap.Text = "Return Map"; // // cboSnapToleranceUnits // this.cboSnapToleranceUnits.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboSnapToleranceUnits.Location = new System.Drawing.Point(240, 48); this.cboSnapToleranceUnits.Name = "cboSnapToleranceUnits"; this.cboSnapToleranceUnits.Size = new System.Drawing.Size(238, 21); this.cboSnapToleranceUnits.TabIndex = 6; // // cboNALayers // this.cboNALayers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboNALayers.Location = new System.Drawing.Point(128, 24); this.cboNALayers.Name = "cboNALayers"; this.cboNALayers.Size = new System.Drawing.Size(350, 21); this.cboNALayers.TabIndex = 3; this.cboNALayers.SelectedIndexChanged += new System.EventHandler(this.cboNALayers_SelectedIndexChanged); // // label10 // this.label10.Location = new System.Drawing.Point(168, 56); this.label10.Name = "label10"; this.label10.Size = new System.Drawing.Size(16, 16); this.label10.TabIndex = 72; this.label10.Text = "to"; // // label8 // this.label8.Location = new System.Drawing.Point(8, 24); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(128, 16); this.label8.TabIndex = 71; this.label8.Text = "NALayer Name"; // // txtMaxSnapTolerance // this.txtMaxSnapTolerance.Location = new System.Drawing.Point(192, 48); this.txtMaxSnapTolerance.Name = "txtMaxSnapTolerance"; this.txtMaxSnapTolerance.Size = new System.Drawing.Size(40, 20); this.txtMaxSnapTolerance.TabIndex = 5; this.txtMaxSnapTolerance.Text = "50"; // // txtSnapTolerance // this.txtSnapTolerance.Location = new System.Drawing.Point(128, 48); this.txtSnapTolerance.Name = "txtSnapTolerance"; this.txtSnapTolerance.Size = new System.Drawing.Size(32, 20); this.txtSnapTolerance.TabIndex = 4; this.txtSnapTolerance.Text = "2"; // // label9 // this.label9.Location = new System.Drawing.Point(8, 48); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(104, 16); this.label9.TabIndex = 68; this.label9.Text = "Search Tolerance"; // // fraINASolverSettings // this.fraINASolverSettings.Controls.Add(this.chkIgnoreInvalidLocations); this.fraINASolverSettings.Controls.Add(this.cboUturnPolicy); this.fraINASolverSettings.Controls.Add(this.label11); this.fraINASolverSettings.Controls.Add(this.label7); this.fraINASolverSettings.Controls.Add(this.chklstAccumulateAttributes); this.fraINASolverSettings.Controls.Add(this.label5); this.fraINASolverSettings.Controls.Add(this.chklstRestrictions); this.fraINASolverSettings.Controls.Add(this.cboImpedance); this.fraINASolverSettings.Controls.Add(this.label4); this.fraINASolverSettings.Enabled = false; this.fraINASolverSettings.Location = new System.Drawing.Point(12, 172); this.fraINASolverSettings.Name = "fraINASolverSettings"; this.fraINASolverSettings.Size = new System.Drawing.Size(492, 192); this.fraINASolverSettings.TabIndex = 83; this.fraINASolverSettings.TabStop = false; this.fraINASolverSettings.Text = "INASolverSettings"; // // chkIgnoreInvalidLocations // this.chkIgnoreInvalidLocations.Location = new System.Drawing.Point(16, 80); this.chkIgnoreInvalidLocations.Name = "chkIgnoreInvalidLocations"; this.chkIgnoreInvalidLocations.Size = new System.Drawing.Size(144, 21); this.chkIgnoreInvalidLocations.TabIndex = 10; this.chkIgnoreInvalidLocations.Text = "Ignore Invalid Locations"; // // cboUturnPolicy // this.cboUturnPolicy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboUturnPolicy.Location = new System.Drawing.Point(128, 48); this.cboUturnPolicy.Name = "cboUturnPolicy"; this.cboUturnPolicy.Size = new System.Drawing.Size(280, 21); this.cboUturnPolicy.TabIndex = 9; // // label11 // this.label11.Location = new System.Drawing.Point(16, 56); this.label11.Name = "label11"; this.label11.Size = new System.Drawing.Size(88, 16); this.label11.TabIndex = 73; this.label11.Text = "All U-Turns"; // // label7 // this.label7.Location = new System.Drawing.Point(250, 104); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(120, 16); this.label7.TabIndex = 72; this.label7.Text = "Accumulate Attributes"; // // chklstAccumulateAttributes // this.chklstAccumulateAttributes.CheckOnClick = true; this.chklstAccumulateAttributes.Location = new System.Drawing.Point(253, 120); this.chklstAccumulateAttributes.Name = "chklstAccumulateAttributes"; this.chklstAccumulateAttributes.ScrollAlwaysVisible = true; this.chklstAccumulateAttributes.Size = new System.Drawing.Size(225, 64); this.chklstAccumulateAttributes.TabIndex = 13; // // label5 // this.label5.Location = new System.Drawing.Point(16, 104); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(72, 16); this.label5.TabIndex = 70; this.label5.Text = "Restrictions"; // // chklstRestrictions // this.chklstRestrictions.CheckOnClick = true; this.chklstRestrictions.Location = new System.Drawing.Point(16, 120); this.chklstRestrictions.Name = "chklstRestrictions"; this.chklstRestrictions.ScrollAlwaysVisible = true; this.chklstRestrictions.Size = new System.Drawing.Size(225, 64); this.chklstRestrictions.TabIndex = 12; // // cboImpedance // this.cboImpedance.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboImpedance.Location = new System.Drawing.Point(128, 24); this.cboImpedance.Name = "cboImpedance"; this.cboImpedance.Size = new System.Drawing.Size(280, 21); this.cboImpedance.TabIndex = 8; // // label4 // this.label4.Location = new System.Drawing.Point(16, 24); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(64, 16); this.label4.TabIndex = 67; this.label4.Text = "Impedance"; // // txtService // this.txtService.Location = new System.Drawing.Point(132, 36); this.txtService.Name = "txtService"; this.txtService.Size = new System.Drawing.Size(250, 20); this.txtService.TabIndex = 79; this.txtService.Text = "<Enter ArcGIS Map Server Object>"; // // txtServer // this.txtServer.Location = new System.Drawing.Point(132, 12); this.txtServer.Name = "txtServer"; this.txtServer.Size = new System.Drawing.Size(250, 20); this.txtServer.TabIndex = 77; this.txtServer.Text = "<Enter ArcGIS Server Name>"; // // cmdSolve // this.cmdSolve.Enabled = false; this.cmdSolve.Location = new System.Drawing.Point(15, 586); this.cmdSolve.Name = "cmdSolve"; this.cmdSolve.Size = new System.Drawing.Size(200, 32); this.cmdSolve.TabIndex = 82; this.cmdSolve.Text = "Solve"; this.cmdSolve.Click += new System.EventHandler(this.cmdSolve_Click); // // label2 // this.label2.Location = new System.Drawing.Point(12, 44); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(120, 16); this.label2.TabIndex = 80; this.label2.Text = "Map Server Object"; // // label1 // this.label1.Location = new System.Drawing.Point(12, 20); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(120, 16); this.label1.TabIndex = 78; this.label1.Text = "ArcGIS Server"; // // cmdConnect // this.cmdConnect.Location = new System.Drawing.Point(408, 12); this.cmdConnect.Name = "cmdConnect"; this.cmdConnect.Size = new System.Drawing.Size(96, 40); this.cmdConnect.TabIndex = 81; this.cmdConnect.Text = "Connect"; this.cmdConnect.Click += new System.EventHandler(this.cmdConnect_Click); // // ServiceArea_GISServerAPIClass // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(1114, 630); this.Controls.Add(this.tabCtrlOutput); this.Controls.Add(this.panel1); this.MinimumSize = new System.Drawing.Size(1122, 650); this.Name = "ServiceArea_GISServerAPIClass"; this.Text = "NAServer - Service Area GIS Server API Sample"; this.Load += new System.EventHandler(this.Form1_Load); this.tabReturnSAPolygonGeometry.ResumeLayout(false); this.tabReturnFacilitiesFeatures.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridFacilitiesFeatures)).EndInit(); this.tabReturnBarrierFeatures.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridBarrierFeatures)).EndInit(); this.tabReturnSAPolygonFeatures.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridSAPolygonFeatures)).EndInit(); this.tabReturnMap.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.tabCtrlOutput.ResumeLayout(false); this.tabReturnSALineFeatures.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridSALineFeatures)).EndInit(); this.tabReturnSALineGeometry.ResumeLayout(false); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); this.fraINAServiceAreaSolver.ResumeLayout(false); this.fraINAServiceAreaSolver.PerformLayout(); this.fraINAServerServiceAreaParams.ResumeLayout(false); this.fraINAServerSolverParams.ResumeLayout(false); this.fraINAServerSolverParams.PerformLayout(); this.fraINASolverSettings.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine)) { if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) { System.Console.WriteLine("This application could not load the correct version of ArcGIS."); return; } } LicenseInitializer aoLicenseInitializer = new LicenseInitializer(); //ESRI License Initializer generated code. if (!aoLicenseInitializer.InitializeApplication( new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcView, esriLicenseProductCode.esriLicenseProductCodeArcEditor, esriLicenseProductCode.esriLicenseProductCodeArcInfo }, new esriLicenseExtensionCode[] { })) { System.Console.WriteLine(aoLicenseInitializer.LicenseMessage()); System.Console.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown."); aoLicenseInitializer.ShutdownApplication(); return; } Application.Run(new ServiceArea_GISServerAPIClass()); //ESRI License Initializer generated code. //Do not make any call to ArcObjects after ShutDownApplication() aoLicenseInitializer.ShutdownApplication(); } /// <summary> /// This function /// - sets the server and solver parameters /// - populates the facilities NALocations /// - solve the service area problem /// - gets and displays the server results (map, etc.) private void cmdSolve_Click(object sender, System.EventArgs e) { this.Cursor = Cursors.WaitCursor; IServerContext serverContext = null; try { // Get ServerContext and NAServer serverContext = GetMapServerContext(); INAServer naServer = GetNAServer(serverContext); // Get SolverParams INAServerSolverParams solverParams = naServer.GetSolverParameters(cboNALayers.Text) as INAServerSolverParams; // Set Solver params SetINASolverSettings(solverParams as INASolverSettings); SetSolverSpecificInterface(solverParams); SetServerSolverParams(solverParams as INAServerSolverParams, serverContext); // Load Locations LoadLocations(solverParams, serverContext); //Solve the Service Area problem INAServerSolverResults solverResults = null; solverResults = naServer.Solve(solverParams); //Get NAServer results in the tab controls OutputResults(solverParams, solverResults); } catch (Exception exception) { MessageBox.Show(exception.Message, "An error has occurred"); } finally { // Release the ServerContext if (serverContext != null) serverContext.ReleaseContext(); } this.Cursor = Cursors.Default; } /// <summary> /// This function /// - connects to the ArcGIS Server and MapServer /// - sets all controls for the first service network analysis layer /// </summary> private void cmdConnect_Click(object sender, System.EventArgs e) { GetNetworkAnalysisLayers(); } /// <summary> /// This function gets the MapServer context reference being created on the ArcGIS Server /// </summary> private IServerContext GetMapServerContext() { try { IGISServerConnection gisServerCon = null; IServerObjectManager som = null; //Connect to ArcGIS Server gisServerCon = new GISServerConnectionClass(); gisServerCon.Connect(txtServer.Text); //Get the MapServer Object som = gisServerCon.ServerObjectManager; return som.CreateServerContext(txtService.Text, "MapServer"); } catch (Exception exception) { throw (new Exception("Error when connecting to the ArcGIS Server/MapServer Object" + "\nMessage: " + exception.Message)); } } /// <summary> /// This function gets NAServer object extension from the MapServer context /// </summary> private INAServer GetNAServer(IServerContext serverContext) { try { if (serverContext == null) throw (new Exception("Error when getting the Network Analyst Server Object" + "\n Message: The MapServer Context is null.")); // Get the NAServer IMapServer mapServer = serverContext.ServerObject as IMapServer; IServerObjectExtensionManager soex = mapServer as IServerObjectExtensionManager; IServerObjectExtension sext = soex.FindExtensionByTypeName("NAServer"); return sext as INAServer; } catch (Exception exception) { throw (new Exception("Error when getting to the Network Analyst Server Object" + "\nMessage: " + exception.Message)); } } /// <summary> /// This function /// - gets all service area network analysis layers /// - selects the first service area network analysis layer /// - sets all controls for this service area network analysis layer private void GetNetworkAnalysisLayers() { this.Cursor = Cursors.WaitCursor; IServerContext serverContext = null; try { // Get ServerContext and NAServer serverContext = GetMapServerContext(); INAServer naServer = GetNAServer(serverContext); // Enable Frame fraINAServerSolverParams.Enabled = true; //Get ServiceArea NA layer names cboNALayers.Items.Clear(); string[] naLayers = naServer.GetNALayerNames(esriNAServerLayerType.esriNAServerServiceAreaLayer); for (int i = 0; i < naLayers.Length; i++) { cboNALayers.Items.Add(naLayers[i]); } // Select the first NA Layer name if (cboNALayers.Items.Count > 0) cboNALayers.SelectedIndex = 0; else MessageBox.Show("There is no Network Analyst layer associated with this MapServer object!", "NAServer - Service Area Sample", System.Windows.Forms.MessageBoxButtons.OK); } catch (Exception exception) { MessageBox.Show(exception.Message, "An error has occurred"); } finally { // Release the ServerContext if (serverContext != null) serverContext.ReleaseContext(); this.Cursor = Cursors.Default; } } /// <summary> /// This function sets all controls for the selected service area network analysis layer /// </summary> private void cboNALayers_SelectedIndexChanged(object sender, System.EventArgs e) { string naLayerName = cboNALayers.Text; // Enable Solve Button cmdSolve.Enabled = (naLayerName.Length == 0); if (naLayerName.Length == 0) return; this.Cursor = Cursors.WaitCursor; IServerContext serverContext = null; try { // Get ServerContext and NAServer serverContext = GetMapServerContext(); INAServer naServer = GetNAServer(serverContext); INAServerSolverParams solverParams = naServer.GetSolverParameters(naLayerName); INAServerNetworkDescription networkDescription = naServer.GetNetworkDescription(naLayerName); // Setup Default Properties GetINASolverSettings(networkDescription, solverParams as INASolverSettings2); GetSolverSpecificInterface(solverParams); GetServerSolverParams(networkDescription, solverParams as INAServerSolverParams); // Get all Point feature Layers form the ServerContext GetInputLocationsLayers(serverContext); // Make frames Enable MakeFramesEnabled(solverParams); //Hide tabs HideTabs(); cmdSolve.Enabled = true; } catch (Exception exception) { MessageBox.Show(exception.Message, "An error has occurred"); } finally { // Release the ServerContext if (serverContext != null) serverContext.ReleaseContext(); this.Cursor = Cursors.Default; } } /// <summary> /// Get general NASolverSettings properties (Cost Attributes, Restrictions Attributes. etc.) and set controls /// </summary> private void GetINASolverSettings(INAServerNetworkDescription networkDescription, INASolverSettings2 solverSettings) { int ImpedanceIndex = 0; //Get Attributes cboImpedance.Items.Clear(); chklstAccumulateAttributes.Items.Clear(); chklstRestrictions.Items.Clear(); cboUturnPolicy.SelectedIndex = -1; INAServerNetworkAttribute[] attributes = networkDescription.NetworkAttributes; IStringArray accumulateAttributeNames = solverSettings.AccumulateAttributeNames; IStringArray restrictionAttributeNames = solverSettings.RestrictionAttributeNames; for (int i = 0; i < attributes.Length; i++) { INAServerNetworkAttribute networkAttribute = attributes[i]; string networkAttributeName = networkAttribute.Name; if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost) { chklstAccumulateAttributes.Items.Add(networkAttributeName, IsStringInStringArray(networkAttributeName, accumulateAttributeNames)); int index = cboImpedance.Items.Add(networkAttribute.Name + " (" + networkAttribute.Units.ToString().Substring(7) + ")"); if (networkAttributeName == solverSettings.ImpedanceAttributeName) ImpedanceIndex = index; } if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTRestriction) { chklstRestrictions.Items.Add(networkAttribute.Name, IsStringInStringArray(networkAttributeName, restrictionAttributeNames)); } } if (cboImpedance.Items.Count > 0) cboImpedance.SelectedIndex = ImpedanceIndex; chkIgnoreInvalidLocations.Checked = solverSettings.IgnoreInvalidLocations; cboUturnPolicy.SelectedIndex = System.Convert.ToInt32(solverSettings.RestrictUTurns); } private bool IsStringInStringArray(string inputString, IStringArray stringArray) { int numInArray = stringArray.Count; for (int i = 0; i < numInArray; i++) { if (inputString.Equals(stringArray.get_Element(i))) return true; } return false; } /// <summary> /// Get specific ServiceArea solver properties (breaks, TravelDirection, etc.) and set controls /// </summary> private void GetSolverSpecificInterface(INAServerSolverParams solverParams) { INAServiceAreaSolver2 saSolver = solverParams as INAServiceAreaSolver2; if (saSolver != null) { txtSADefaultBreaks.Text = ""; for (int iBreak = 0; iBreak < saSolver.DefaultBreaks.Count; iBreak++) { txtSADefaultBreaks.Text = txtSADefaultBreaks.Text + " " + saSolver.DefaultBreaks.get_Element(iBreak).ToString(); } cboSATravelDirection.SelectedIndex = Convert.ToInt32(saSolver.TravelDirection); if (saSolver.OutputLines == esriNAOutputLineType.esriNAOutputLineTrueShape || saSolver.OutputLines == esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure) this.chkOutputLines.Checked = true; else this.chkOutputLines.Checked = false; if (saSolver.OutputPolygons != esriNAOutputPolygonType.esriNAOutputPolygonNone) { chkOutputPolygon.Checked = true; cboSAOutputPolygons.Enabled = true; chkTrimOuterPolygon.Enabled = true; chkTrimOuterPolygon.Enabled = true; txtSATrimPolygonDistance.Enabled = true; cboSATrimPolygonUnits.Enabled = true; cboSAOutputPolygons.SelectedIndex = Convert.ToInt32(saSolver.OutputPolygons) - 1; } else { chkOutputPolygon.Checked = false; cboSAOutputPolygons.Enabled = false; chkTrimOuterPolygon.Enabled = false; txtSATrimPolygonDistance.Enabled = false; cboSATrimPolygonUnits.Enabled = false; } chkTrimOuterPolygon.Checked = saSolver.TrimOuterPolygon; if (saSolver.MergeSimilarPolygonRanges == true) cboSAMultipleFacilityOptions.SelectedIndex = 2; else if (saSolver.OverlapPolygons == true) cboSAMultipleFacilityOptions.SelectedIndex = 0; else cboSAMultipleFacilityOptions.SelectedIndex = 1; if (saSolver.SplitPolygonsAtBreaks == true) cboSAOverlapType.SelectedIndex = 0; else cboSAOverlapType.SelectedIndex = 1; double distance; esriUnits units; distance = saSolver.TrimPolygonDistance; units = saSolver.TrimPolygonDistanceUnits; txtSATrimPolygonDistance.Text = distance.ToString(); cboSATrimPolygonUnits.SelectedIndex = Convert.ToInt32(units); } } /// <summary> /// Get all point feature layers from the ServerContext /// Add them in the "Input Incidents" drop down list /// </summary> private void GetInputLocationsLayers(IServerContext serverContext) { IMapServer mapServer = serverContext.ServerObject as IMapServer; IMapServerObjects mapServerObjects = mapServer as IMapServerObjects; IMap map = mapServerObjects.get_Map(mapServer.DefaultMapName); cboLocationsLayers.Items.Clear(); IEnumLayer layers = map.get_Layers(null, true); layers.Reset(); ILayer layer; while ((layer = layers.Next()) != null) { IFeatureLayer featureLayer = layer as IFeatureLayer; if ( featureLayer != null && !(featureLayer.FeatureClass is INAClass) && featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint ) { cboLocationsLayers.Items.Add(featureLayer.Name); }; } // Add <None> at the end in case the user does not want to load incident Location from a Feature Layer cboLocationsLayers.Items.Add("<None>"); cboLocationsLayers.SelectedIndex = 0; } /// <summary> /// Get ServerSolverParams properties (SnapTolerance) and set controls /// </summary> private void GetServerSolverParams(INAServerNetworkDescription networkDescription, INAServerSolverParams solverParams) { chkReturnMap.Checked = true; txtSnapTolerance.Text = solverParams.SnapTolerance.ToString(); txtMaxSnapTolerance.Text = solverParams.MaxSnapTolerance.ToString(); cboSnapToleranceUnits.SelectedIndex = Convert.ToInt32(solverParams.SnapToleranceUnits); // Set SA Defaults chkReturnSALineGeometries.Checked = false; chkReturnSALines.Checked = false; chkReturnSAPolygonGeometries.Checked = false; chkReturnSAPolygons.Checked = true; chkReturnSAFacilities.Checked = true; chckReturnSABarriers.Checked = false; } /// <summary> /// Set general solver settings (Impedance, Restrictions, Accumulates, etc.) /// </summary> private void SetINASolverSettings(INASolverSettings solverSettings) { solverSettings.ImpedanceAttributeName = ExtractImpedanceName(cboImpedance.Text); IStringArray restrictionAttributes = solverSettings.RestrictionAttributeNames; restrictionAttributes.RemoveAll(); for (int i = 0; i < chklstRestrictions.CheckedItems.Count; i++) restrictionAttributes.Add(chklstRestrictions.Items[chklstRestrictions.CheckedIndices[i]].ToString()); solverSettings.RestrictionAttributeNames = restrictionAttributes; IStringArray accumulateAttributes = solverSettings.AccumulateAttributeNames; accumulateAttributes.RemoveAll(); for (int i = 0; i < chklstAccumulateAttributes.CheckedItems.Count; i++) accumulateAttributes.Add(chklstAccumulateAttributes.Items[chklstAccumulateAttributes.CheckedIndices[i]].ToString()); solverSettings.AccumulateAttributeNames = accumulateAttributes; solverSettings.RestrictUTurns = (esriNetworkForwardStarBacktrack)cboUturnPolicy.SelectedIndex; solverSettings.IgnoreInvalidLocations = chkIgnoreInvalidLocations.Checked; } /// <summary> /// Set specific solver settings (Breaks, TravelDirection, etc.) /// </summary> private void SetSolverSpecificInterface(INAServerSolverParams solverParams) { INAServiceAreaSolver2 saSolver = solverParams as INAServiceAreaSolver2; if (saSolver != null) { IDoubleArray defaultBreaks = saSolver.DefaultBreaks; defaultBreaks.RemoveAll(); string breaks = txtSADefaultBreaks.Text.Trim(); breaks.Replace(" ", " "); string[] values = breaks.Split(' '); for (int iBreak = values.GetLowerBound(0); iBreak <= values.GetUpperBound(0); iBreak++) { string breakValue = values.GetValue(iBreak) as string; defaultBreaks.Add(System.Convert.ToDouble(breakValue)); } saSolver.DefaultBreaks = defaultBreaks; saSolver.TravelDirection = (esriNATravelDirection)cboSATravelDirection.SelectedIndex; // Set Output Lines if (this.chkOutputLines.Checked) saSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShape; else saSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineNone; // Set Output Polygons if (this.chkOutputPolygon.Checked) saSolver.OutputPolygons = (esriNAOutputPolygonType)cboSAOutputPolygons.SelectedIndex + 1; else saSolver.OutputPolygons = esriNAOutputPolygonType.esriNAOutputPolygonNone; chkTrimOuterPolygon.Checked = saSolver.TrimOuterPolygon; if (cboSAMultipleFacilityOptions.SelectedIndex == 2) { saSolver.OverlapPolygons = true; saSolver.MergeSimilarPolygonRanges = true; } else if (cboSAMultipleFacilityOptions.SelectedIndex == 0) saSolver.OverlapPolygons = true; else saSolver.OverlapPolygons = false; saSolver.SplitPolygonsAtBreaks = (cboSAOverlapType.SelectedIndex == 0); // Set trimming option saSolver.TrimOuterPolygon = chkTrimOuterPolygon.Checked; saSolver.TrimPolygonDistance = Convert.ToDouble(this.txtSATrimPolygonDistance.Text); saSolver.TrimPolygonDistanceUnits = (esriUnits)cboSATrimPolygonUnits.SelectedIndex; } } /// <summary> /// Set server solver paramaters (ReturnMap, SnapTolerance, etc.) /// </summary> private void SetServerSolverParams(INAServerSolverParams solverParams, IServerContext serverContext) { solverParams.ReturnMap = chkReturnMap.Checked; solverParams.SnapTolerance = Convert.ToDouble(txtSnapTolerance.Text); solverParams.MaxSnapTolerance = Convert.ToDouble(txtMaxSnapTolerance.Text); solverParams.SnapToleranceUnits = (esriUnits)cboSnapToleranceUnits.SelectedIndex; solverParams.ImageDescription.Display.Width = pictureBox.Width; solverParams.ImageDescription.Display.Height = pictureBox.Height; // This code shows how to specify the output spatial reference in order to get the map // in a different spatial reference than the Network Dataset //IESRISpatialReferenceGEN sr = serverContext.CreateObject("esriGeometry.GeographicCoordinateSystem") as IESRISpatialReferenceGEN; //int read; //sr.ImportFromESRISpatialReference("GEOGCS[GCS_North_American_1983,DATUM[D_North_American_1983,SPHEROID[GRS_1980,6378137.0,298.257222101]],PRIMEM[Greenwich,0.0],UNIT[Degree,0.0174532925199433]]", out read); //solverParams.OutputSpatialReference = sr as ISpatialReference; INAServerServiceAreaParams saParams = solverParams as INAServerServiceAreaParams; if (saParams != null) { saParams.ReturnSALineGeometries = this.chkReturnSALineGeometries.Checked; saParams.ReturnSALines = this.chkReturnSALines.Checked; saParams.ReturnSAPolygonGeometries = this.chkReturnSAPolygonGeometries.Checked; saParams.ReturnSAPolygons = this.chkReturnSAPolygons.Checked; saParams.ReturnFacilities = this.chkReturnSAFacilities.Checked; saParams.ReturnBarriers = this.chckReturnSABarriers.Checked; } } /// <summary> /// Make frames Enabled /// </summary> private void MakeFramesEnabled(INAServerSolverParams solverParams) { fraINAServiceAreaSolver.Enabled = ((solverParams as INAServerServiceAreaParams) != null); fraINAServerServiceAreaParams.Enabled = ((solverParams as INAServerServiceAreaParams) != null); fraINASolverSettings.Enabled = ((solverParams as INAServerServiceAreaParams) != null); } /// <summary> /// Load form /// </summary> private void Form1_Load(object sender, System.EventArgs e) { cboUturnPolicy.Items.Add("Nowhere"); cboUturnPolicy.Items.Add("Everywhere"); cboUturnPolicy.Items.Add("Only at Dead Ends"); cboSnapToleranceUnits.Items.Add("Unknown Units"); cboSnapToleranceUnits.Items.Add("Inches"); cboSnapToleranceUnits.Items.Add("Points"); cboSnapToleranceUnits.Items.Add("Feet"); cboSnapToleranceUnits.Items.Add("Yards"); cboSnapToleranceUnits.Items.Add("Miles"); cboSnapToleranceUnits.Items.Add("Nautical Miles"); cboSnapToleranceUnits.Items.Add("Millimeters"); cboSnapToleranceUnits.Items.Add("Centimeters"); cboSnapToleranceUnits.Items.Add("Meters"); cboSnapToleranceUnits.Items.Add("Kilometers"); cboSnapToleranceUnits.Items.Add("DecimalDegrees"); cboSnapToleranceUnits.Items.Add("Decimeters"); // Service Area cboSATravelDirection.Items.Add("From Facility"); cboSATravelDirection.Items.Add("To Facility"); cboSAOutputPolygons.Items.Add("Generalized"); cboSAOutputPolygons.Items.Add("Detailed"); cboSATrimPolygonUnits.Items.Add("Unknown Units"); cboSATrimPolygonUnits.Items.Add("Inches"); cboSATrimPolygonUnits.Items.Add("Points"); cboSATrimPolygonUnits.Items.Add("Feet"); cboSATrimPolygonUnits.Items.Add("Yards"); cboSATrimPolygonUnits.Items.Add("Miles"); cboSATrimPolygonUnits.Items.Add("Nautical Miles"); cboSATrimPolygonUnits.Items.Add("Millimeters"); cboSATrimPolygonUnits.Items.Add("Centimeters"); cboSATrimPolygonUnits.Items.Add("Meters"); cboSATrimPolygonUnits.Items.Add("Kilometers"); cboSATrimPolygonUnits.Items.Add("DecimalDegrees"); cboSATrimPolygonUnits.Items.Add("Decimeters"); cboSAMultipleFacilityOptions.Items.Add("Overlapping Polygons"); cboSAMultipleFacilityOptions.Items.Add("Not Overlapping Polygons"); cboSAMultipleFacilityOptions.Items.Add("Merge By Break Value"); cboSAOverlapType.Items.Add("Rings"); cboSAOverlapType.Items.Add("Disks"); if (txtServer.Text != "" && txtServer.Text != "<Enter ArcGIS Server Name>" && txtService.Text != "" && txtService.Text != "<Enter ArcGIS Map Server Object>") GetNetworkAnalysisLayers(); } /// <summary> /// This function shows how to populate facility locations using two different options: /// 1) From Record Set using a Point Feature Class - Uncommented /// 2) From an Array of PropertySets - Commented out /// Uncomment the option, you would like to use /// </summary> private void LoadLocations(INAServerSolverParams solverParams, IServerContext serverContext) { // Begin Option 1 IRecordSet recSet = null; // Get Input incident Feature Class from the MapServer // Do not create Facilities if <NONE> is selected if (cboLocationsLayers.Text.ToUpper() != "<NONE>") { //Get the feature class to load in IMapServer mapServer = serverContext.ServerObject as IMapServer; IMapServerObjects mapServerObjects = mapServer as IMapServerObjects; IMap map = mapServerObjects.get_Map(mapServer.DefaultMapName); IFeatureClass featureClass = null; IEnumLayer layers = map.get_Layers(null, true); layers.Reset(); ILayer layer; while ((layer = layers.Next()) != null) if (layer.Name.ToUpper() == cboLocationsLayers.Text.ToUpper()) { IFeatureLayer featureLayer = layer as IFeatureLayer; featureClass = featureLayer.FeatureClass; break; } if (featureClass == null) throw (new Exception("Could not find the input incident layer " + cboLocationsLayers.Text)); recSet = serverContext.CreateObject("esriGeodatabase.RecordSet") as IRecordSet; IQueryFilter queryFilter = serverContext.CreateObject("esriGeodatabase.QueryFilter") as IQueryFilter; queryFilter.SubFields = "*"; IRecordSetInit recSetInit = recSet as IRecordSetInit; recSetInit.SetSourceTable(featureClass as ITable, queryFilter); } INAServerServiceAreaParams serviceareaParams = solverParams as INAServerServiceAreaParams; INAServerRecordSet facilitiesRecSet = serverContext.CreateObject("esriNetworkAnalyst.NAServerRecordSet") as INAServerRecordSet; facilitiesRecSet.RecordSet = recSet; if (serviceareaParams != null && recSet != null) serviceareaParams.Facilities = facilitiesRecSet; // End Option 1 // Begin Option 2 /* IPropertySetArray propSets = serverContext.CreateObject("esriSystem.PropertySetArray") as IPropertySetArray; // Set one incident propSets.Add(CreateLocationPropertySet(serverContext, "Incident 1", -122.49024904900, 37.74811940430, null)); // Create NAServerPropertySets INAServerPropertySets facilitiesPropSets = serverContext.CreateObject("esriNetworkAnalyst.NAServerPropertySets") as INAServerPropertySets; facilitiesPropSets.PropertySets = propSets; // Pass the facility PropertySets INAServerServiceAreaParams serviceareaParams = solverParams as INAServerServiceAreaParams; serviceareaParams.Facilities = facilitiesPropSets; */ // End Option 2 } /// <summary> /// Output Results Messages, Map, SAPolygons and SALines Geometries /// </summary> private void OutputResults(INAServerSolverParams solverParams, INAServerSolverResults solverResults) { string messagesSolverResults = ""; // Output Solve messages IGPMessages gpMessages = solverResults.SolveMessages; if (gpMessages != null) { for (int i = 0; i < gpMessages.Count; i++) { IGPMessage gpMessage = gpMessages.GetMessage(i); messagesSolverResults += "\n" + gpMessage.Description; } } // Show a message box displaying both solver messages and total_impedance per route if (messagesSolverResults.Length > 0) MessageBox.Show(messagesSolverResults, "NAServer Service Area Results"); //Output Map pictureBox.Image = null; if (solverParams.ReturnMap) { IMapImage mapImage = solverResults.MapImage; if ((mapImage != null) && (mapImage.MimeData != null)) pictureBox.Image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(mapImage.MimeData)); } pictureBox.Refresh(); if (((solverParams as INAServerServiceAreaParams) != null) && ((solverResults as INAServerServiceAreaResults) != null)) OutputServiceAreaResults(solverParams as INAServerServiceAreaParams, solverResults as INAServerServiceAreaResults); } /// <summary> /// Output service area results according to the ServerServiceAreaParams options /// </summary> private void OutputServiceAreaResults(INAServerServiceAreaParams solverParams, INAServerServiceAreaResults solverResults) { // Return SAPolygon Features as RecordSet if (solverParams.ReturnSAPolygons) { AddTabAndControl(this.tabReturnSAPolygonFeatures, this.dataGridSAPolygonFeatures); OutputRecSetToDataGrid(solverResults.SAPolygons, dataGridSAPolygonFeatures); } else tabCtrlOutput.TabPages.Remove(tabReturnSAPolygonFeatures); // Return SALine Features as RecordSet if (solverParams.ReturnSALines) { AddTabAndControl(this.tabReturnSALineFeatures, this.dataGridSALineFeatures); OutputRecSetToDataGrid(solverResults.SALines, dataGridSALineFeatures); } else tabCtrlOutput.TabPages.Remove(tabReturnSALineFeatures); // Return SAPolygons Geometries if (solverParams.ReturnSAPolygonGeometries) { AddTabAndControl(this.tabReturnSAPolygonGeometry, this.treeViewSAPolygonGeometries); OutputPolygons(solverResults.SAPolygonGeometries); } else tabCtrlOutput.TabPages.Remove(tabReturnSAPolygonGeometry); // Return SALine Geometries if (solverParams.ReturnSALineGeometries) { AddTabAndControl(this.tabReturnSALineGeometry, this.treeViewSALineGeometries); OutputPolylines(solverResults.SALineGeometries); } else tabCtrlOutput.TabPages.Remove(tabReturnSALineGeometry); // Return Facility Features as RecordSet if (solverParams.ReturnFacilities) { AddTabAndControl(this.tabReturnFacilitiesFeatures, this.dataGridFacilitiesFeatures); OutputRecSetToDataGrid(solverResults.Facilities, dataGridFacilitiesFeatures); } else tabCtrlOutput.TabPages.Remove(tabReturnFacilitiesFeatures); // Return Barrier Features as RecordSet if (solverParams.ReturnBarriers) { AddTabAndControl(this.tabReturnBarrierFeatures, this.dataGridBarrierFeatures); OutputRecSetToDataGrid(solverResults.Barriers, dataGridBarrierFeatures); } else tabCtrlOutput.TabPages.Remove(tabReturnBarrierFeatures); // Make TabControlOuput enable tabCtrlOutput.Enabled = true; } /// <summary> /// Output SALines Geometries (Polylines) in a TreeView control /// </summary> private void OutputPolylines(IPolyline[] polylines) { // Suppress repainting the TreeView until all the objects have been created. treeViewSALineGeometries.BeginUpdate(); // Clear the TreeView each time the method is called. treeViewSALineGeometries.Nodes.Clear(); for (int i = 0; i < polylines.Length; i++) { TreeNode newNode = new TreeNode("Polyline Length for SALines [" + (i + 1) + "] = " + polylines[i].Length.ToString()); treeViewSALineGeometries.Nodes.Add(newNode); IPointCollection pointCollection = polylines[i] as IPointCollection; for (int j = 0; j < pointCollection.PointCount; j++) { IPoint point = pointCollection.get_Point(j); treeViewSALineGeometries.Nodes[i].Nodes.Add(new TreeNode("Point [" + (j + 1) + "]: " + point.X + "," + point.Y)); } } // Check if Route Geometry has been generated if (polylines.Length == 0) { TreeNode newNode = new TreeNode("SALine Geometry not generated"); treeViewSALineGeometries.Nodes.Add(newNode); } // Begin repainting the TreeView. treeViewSALineGeometries.ExpandAll(); treeViewSALineGeometries.EndUpdate(); } /// <summary> /// Output SAPolygons Geometries (Polygons) in a TreeView control /// </summary> private void OutputPolygons(IPolygon[] polygons) { // Suppress repainting the TreeView until all the objects have been created. treeViewSAPolygonGeometries.BeginUpdate(); // Clear the TreeView each time the method is called. treeViewSAPolygonGeometries.Nodes.Clear(); for (int i = 0; i < polygons.Length; i++) { TreeNode newNode = new TreeNode("Polygon Length [" + (i + 1) + "] = " + polygons[i].Length.ToString()); treeViewSAPolygonGeometries.Nodes.Add(newNode); } // Check if Route Geometry has been generated if (polygons.Length == 0) { TreeNode newNode = new TreeNode("SAPolgon Geometry not generated"); treeViewSAPolygonGeometries.Nodes.Add(newNode); } // Begin repainting the TreeView. treeViewSAPolygonGeometries.ExpandAll(); treeViewSAPolygonGeometries.EndUpdate(); } /// <summary> /// Generic function to output RecordSet (Facilities, Barriers, or Routes) in a DataGrid Control /// </summary> private void OutputRecSetToDataGrid(IRecordSet recSet, System.Windows.Forms.DataGrid OutDataGrid) { IFields fields = recSet.Fields; DataSet dataSet = new DataSet("dataSet"); DataTable dataTable = new DataTable("Results"); dataSet.Tables.Add(dataTable); // Get Field Names DataColumn dataColumn = null; for (int f = 0; f < fields.FieldCount; f++) { dataColumn = new DataColumn(fields.get_Field(f).Name.ToString()); dataTable.Columns.Add(dataColumn); } // Populate DataGrid rows by RecordSet DataRow newDataRow; ICursor cur = recSet.get_Cursor(false); IRow row = cur.NextRow(); if (row != null) { while (row != null) { newDataRow = dataTable.NewRow(); for (int l = 0; l < row.Fields.FieldCount; l++) { newDataRow[l] = row.get_Value(l).ToString(); } dataTable.Rows.Add(newDataRow); row = cur.NextRow(); } } OutDataGrid.SetDataBinding(dataSet, "Results"); OutDataGrid.Visible = true; } /// <summary> /// Create a property set for NALocation /// </summary> private IPropertySet CreateLocationPropertySet(IServerContext serverContext, string name, object X, object Y, ISpatialReference sr) { IPropertySet propSet = serverContext.CreateObject("esriSystem.PropertySet") as IPropertySet; propSet.SetProperty("Name", name); propSet.SetProperty("X", X); propSet.SetProperty("Y", Y); propSet.SetProperty("SpatialReference", sr); return propSet; } /// <summary> /// Helper function to convert a string unit (e.g Miles) to an esriNetworkAnalystUnits (e.g esriNAUMiles) /// </summary> private esriNetworkAttributeUnits GetstringToesriUnits(string stresriUnits) { switch (stresriUnits.ToLower()) { case "inches": return esriNetworkAttributeUnits.esriNAUInches; case "feet": return esriNetworkAttributeUnits.esriNAUFeet; case "yards": return esriNetworkAttributeUnits.esriNAUYards; case "miles": return esriNetworkAttributeUnits.esriNAUMiles; case "nautical miles": return esriNetworkAttributeUnits.esriNAUNauticalMiles; case "millimeters": return esriNetworkAttributeUnits.esriNAUMillimeters; case "centimeters": return esriNetworkAttributeUnits.esriNAUCentimeters; case "meters": return esriNetworkAttributeUnits.esriNAUMeters; case "kilometers": return esriNetworkAttributeUnits.esriNAUKilometers; case "decimal degrees": return esriNetworkAttributeUnits.esriNAUDecimalDegrees; case "decimeters": return esriNetworkAttributeUnits.esriNAUDecimeters; case "seconds": return esriNetworkAttributeUnits.esriNAUSeconds; case "minutes": return esriNetworkAttributeUnits.esriNAUMinutes; case "hours": return esriNetworkAttributeUnits.esriNAUHours; case "days": return esriNetworkAttributeUnits.esriNAUDays; case "unknown": return esriNetworkAttributeUnits.esriNAUUnknown; default: return esriNetworkAttributeUnits.esriNAUUnknown; } } /// <summary> /// Helper function to extract the impedance name in a string like "impedancename (impedanceunits)" /// </summary> private string ExtractImpedanceName(string impedanceUnits) { int firstIndex = impedanceUnits.LastIndexOf(" "); int LastIndex = impedanceUnits.Length; return impedanceUnits.Remove(firstIndex, (LastIndex - firstIndex)); } /// <summary> /// Helper function to extract the impedance unit in a string like "impedancename (impedanceunits)" /// </summary> private string ExtractimpedanceUnits(string impedanceUnits) { int firstIndex = impedanceUnits.LastIndexOf("(") + 1; int LastIndex = impedanceUnits.LastIndexOf(")"); return impedanceUnits.Substring(firstIndex, (LastIndex - firstIndex)); } /// <summary> /// Make Enable the Polygon options /// </summary> private void chkOutputPolygon_CheckedChanged(object sender, EventArgs e) { if (chkOutputPolygon.Checked) { cboSAOutputPolygons.Enabled = true; chkTrimOuterPolygon.Enabled = true; chkTrimOuterPolygon.Enabled = true; cboSATrimPolygonUnits.Enabled = true; txtSATrimPolygonDistance.Enabled = true; cboSAMultipleFacilityOptions.Enabled = true; cboSAOverlapType.Enabled = true; cboSAOutputPolygons.SelectedIndex = 0; } else { cboSAOutputPolygons.Enabled = false; chkTrimOuterPolygon.Enabled = false; cboSATrimPolygonUnits.Enabled = false; txtSATrimPolygonDistance.Enabled = false; cboSAMultipleFacilityOptions.Enabled = false; cboSAOverlapType.Enabled = false; } } /// <summary> /// Hide tabs /// </summary> private void HideTabs() { // remove tabs initially - There are added when the result is returned. this.tabCtrlOutput.TabPages.Remove(tabReturnSAPolygonFeatures); this.tabCtrlOutput.TabPages.Remove(tabReturnSALineFeatures); this.tabCtrlOutput.TabPages.Remove(tabReturnFacilitiesFeatures); this.tabCtrlOutput.TabPages.Remove(tabReturnBarrierFeatures); this.tabCtrlOutput.TabPages.Remove(tabReturnSAPolygonGeometry); this.tabCtrlOutput.TabPages.Remove(tabReturnSALineGeometry); } /// <summary> //// Add a tab page and a tab control /// </summary> private void AddTabAndControl(TabPage tabPage, System.Windows.Forms.Control controlToAdd) { if (!tabCtrlOutput.TabPages.Contains(tabPage)) { tabCtrlOutput.TabPages.Add(tabPage); tabPage.Controls.Add(controlToAdd); } } } }
[Visual Basic .NET]
ServiceArea_GISServerAPIClass.vb
Imports System Imports System.Drawing Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports System.Data Imports System.Data.OleDb Imports System.Xml Imports ESRI.ArcGIS.Carto Imports ESRI.ArcGIS.Display Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.Geometry Imports ESRI.ArcGIS.Server Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.Location Imports ESRI.ArcGIS.NetworkAnalyst Imports System.Collections.Specialized Public Class ServiceArea_GISServerAPIClass ''' <summary> ''' This function ''' - connects to the ArcGIS Server and MapServer ''' - sets all controls for the first service area network analysis layer ''' </summary> Private Sub cmdConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdConnect.Click GetNetworkAnalysisLayers() End Sub ''' <summary> ''' This function ''' - gets all service area network analysis layers ''' - selects the first service area network analysis layer ''' - sets all controls for this service area network analysis layer ''' </summary> Private Sub GetNetworkAnalysisLayers() Me.Cursor = Cursors.WaitCursor Dim serverContext As IServerContext = Nothing Try ' Get ServerContext and NAServer serverContext = GetMapServerContext() Dim naServer As INAServer = GetNAServer(serverContext) ' Enable Frame fraINAServerSolverParams.Enabled = True 'Get service area NA layer names cboNALayers.Items.Clear() Dim naLayers() As String = naServer.GetNALayerNames(esriNAServerLayerType.esriNAServerServiceAreaLayer) For i As Integer = 0 To naLayers.Length - 1 cboNALayers.Items.Add(naLayers(i)) Next i ' Select the first NA Layer name If cboNALayers.Items.Count > 0 Then cboNALayers.SelectedIndex = 0 Else MessageBox.Show("There is no Network Analyst layer associated with this MapServer object!", "NAServer - service area Sample", System.Windows.Forms.MessageBoxButtons.OK) End If Catch exception As Exception MessageBox.Show(exception.Message, "An error has occurred") Finally ' Release the ServerContext If Not serverContext Is Nothing Then serverContext.ReleaseContext() End If Me.Cursor = Cursors.Default End Try End Sub ''' <summary> ''' This function gets the MapServer context reference being created on the ArcGIS Server ''' </summary> Private Function GetMapServerContext() As IServerContext Try Dim gisServerCon As IGISServerConnection = Nothing Dim som As IServerObjectManager = Nothing 'Connect to ArcGIS Server gisServerCon = New GISServerConnectionClass() gisServerCon.Connect(txtServer.Text) 'Get the MapServer Object som = gisServerCon.ServerObjectManager Return som.CreateServerContext(txtService.Text, "MapServer") Catch exception As Exception Throw (New Exception("Error when connecting to the ArcGIS Server/MapServer Object" + "\nMessage: " + exception.Message)) End Try End Function ''' <summary> ''' This function gets NAServer object extension from the MapServer context ''' </summary> Private Function GetNAServer(ByVal serverContext As IServerContext) As INAServer Try If serverContext Is Nothing Then Throw (New Exception("Error when getting the Network Analyst Server Object" + "\n Message: The MapServer Context is Nothing.")) End If ' Get the NAServer Dim mapServer As IMapServer = serverContext.ServerObject Dim soex As IServerObjectExtensionManager = mapServer Dim sext As IServerObjectExtension = soex.FindExtensionByTypeName("NAServer") Dim pNAServer As INAServer = sext Return pNAServer Catch exception As Exception Throw (New Exception("Error when getting to the Network Analyst Server Object" + "\nMessage: " + exception.Message)) End Try End Function ''' <summary> ''' This function sets all controls for the selected service area network analysis layer ''' </summary> Private Sub cboNALayers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboNALayers.SelectedIndexChanged Dim naLayerName As String = cboNALayers.Text ' Enable Solve Button 'cmdSolve.Enabled = (naLayerName.Length = 0) If naLayerName.Length = 0 Then Return End If Me.Cursor = Cursors.WaitCursor Dim serverContext As IServerContext = Nothing Try ' Get ServerContext and NAServer serverContext = GetMapServerContext() Dim naServer As INAServer = GetNAServer(serverContext) Dim solverParams As INAServerSolverParams = naServer.GetSolverParameters(naLayerName) Dim networkDescription As INAServerNetworkDescription = naServer.GetNetworkDescription(naLayerName) ' Setup Default Properties GetINASolverSettings(networkDescription, solverParams) GetSolverSpecificInterface(solverParams) GetServerSolverParams(networkDescription, solverParams) ' Get all Point feature Layers form the ServerContext GetInputLocationsLayers(serverContext) ' Make frames Enable MakeFramesEnabled(solverParams) ' Hide Tabs HideTabs() cmdSolve.Enabled = True Catch exception As Exception MessageBox.Show(exception.Message, "An error has occurred") Finally ' Release the ServerContext If Not serverContext Is Nothing Then serverContext.ReleaseContext() End If Me.Cursor = Cursors.Default End Try End Sub ''' <summary> ''' Get ServerSolverParams properties (SnapTolerance) and set controls ''' </summary> Private Sub GetServerSolverParams(ByVal networkDescription As INAServerNetworkDescription, ByVal solverParams As INAServerSolverParams) chkReturnMap.Checked = True txtSnapTolerance.Text = solverParams.SnapTolerance.ToString() txtMaxSnapTolerance.Text = solverParams.MaxSnapTolerance.ToString() cboSnapToleranceUnits.SelectedIndex = Convert.ToInt32(solverParams.SnapToleranceUnits) ' Set SA Defaults chkReturnSALineGeometries.Checked = False chkReturnSALines.Checked = False chkReturnSAPolygonGeometries.Checked = False chkReturnSAPolygons.Checked = True chkReturnSAFacilities.Checked = True chckReturnSABarriers.Checked = False End Sub ''' <summary> ''' Get general NASolverSettings properties (Cost Attributes, Restrictions Attributes. etc.) and set controls ''' </summary> Private Sub GetINASolverSettings(ByVal networkDescription As INAServerNetworkDescription, ByVal solverSettings As INASolverSettings2) Dim impedanceIndex As Integer = 0 'Get Attributes cboImpedance.Items.Clear() chklstAccumulateAttributes.Items.Clear() chklstRestrictions.Items.Clear() cboUturnPolicy.SelectedIndex = -1 Dim attributes() As INAServerNetworkAttribute = networkDescription.NetworkAttributes Dim accumulateAttributeNames As IStringArray = solverSettings.AccumulateAttributeNames Dim restrictionAttributeNames As IStringArray = solverSettings.RestrictionAttributeNames For i As Integer = 0 To attributes.Length - 1 Dim networkAttribute As INAServerNetworkAttribute = attributes(i) Dim networkAttributeName As String = networkAttribute.Name If networkAttribute.UsageType = esriNetworkAttributeUsageType.esriNAUTCost Then chklstAccumulateAttributes.Items.Add(networkAttribute.Name, IsStringInStringArray(networkAttributeName, accumulateAttributeNames)) Dim index As Integer = cboImpedance.Items.Add(networkAttribute.Name + " (" + networkAttribute.Units.ToString().Substring(7) + ")") If networkAttributeName = solverSettings.ImpedanceAttributeName Then impedanceIndex = index End If End If If networkAttribute.UsageType = esriNetworkAttributeUsageType.esriNAUTRestriction Then chklstRestrictions.Items.Add(networkAttribute.Name, IsStringInStringArray(networkAttributeName, restrictionAttributeNames)) End If Next If cboImpedance.Items.Count > 0 Then cboImpedance.SelectedIndex = impedanceIndex End If chkIgnoreInvalidLocations.Checked = solverSettings.IgnoreInvalidLocations cboUturnPolicy.SelectedIndex = System.Convert.ToInt32(solverSettings.RestrictUTurns) End Sub Private Function IsStringInStringArray(ByVal inputString As String, ByVal stringArray As IStringArray) As Boolean Dim numInArray As Integer = stringArray.Count For i As Integer = 0 To numInArray - 1 If inputString.Equals(stringArray.Element(i)) Then Return True End If Next Return False End Function ''' <summary> ''' Get ServiceArea solver properties (breaks, TravelDirection, etc.) and set controls ''' </summary> Private Sub GetSolverSpecificInterface(ByVal solverParams As INAServerSolverParams) Dim saSolver As INAServiceAreaSolver2 = solverParams If Not saSolver Is Nothing Then txtSADefaultBreaks.Text = "" For iBreak As Integer = 0 To saSolver.DefaultBreaks.Count - 1 Step iBreak + 1 txtSADefaultBreaks.Text = txtSADefaultBreaks.Text + " " + saSolver.DefaultBreaks.Element(iBreak).ToString() Next cboSATravelDirection.SelectedIndex = Convert.ToInt32(saSolver.TravelDirection) If saSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShape Or saSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure Then Me.chkOutputLines.Checked = True Else Me.chkOutputLines.Checked = False End If If saSolver.OutputPolygons <> esriNAOutputPolygonType.esriNAOutputPolygonNone Then chkOutputPolygon.Checked = True cboSAOutputPolygons.Enabled = True chkTrimOuterPolygon.Enabled = True chkTrimOuterPolygon.Enabled = True txtSATrimPolygonDistance.Enabled = True cboSATrimPolygonUnits.Enabled = True cboSAOutputPolygons.SelectedIndex = Convert.ToInt32(saSolver.OutputPolygons) - 1 Else chkOutputPolygon.Checked = False cboSAOutputPolygons.Enabled = False chkTrimOuterPolygon.Enabled = False txtSATrimPolygonDistance.Enabled = False cboSATrimPolygonUnits.Enabled = False End If chkTrimOuterPolygon.Checked = saSolver.TrimOuterPolygon If saSolver.MergeSimilarPolygonRanges = True Then cboSAMultipleFacilityOptions.SelectedIndex = 2 ElseIf saSolver.OverlapPolygons = True Then cboSAMultipleFacilityOptions.SelectedIndex = 0 Else cboSAMultipleFacilityOptions.SelectedIndex = 1 End If If saSolver.SplitPolygonsAtBreaks = True Then cboSAOverlapType.SelectedIndex = 0 Else cboSAOverlapType.SelectedIndex = 1 End If Dim distance As Double Dim units As esriUnits distance = saSolver.TrimPolygonDistance units = saSolver.TrimPolygonDistanceUnits txtSATrimPolygonDistance.Text = distance.ToString() cboSATrimPolygonUnits.SelectedIndex = Convert.ToInt32(units) End If End Sub ''' <summary> ''' Set general solver settings (Impedance, Restrictions, Accumulates, etc.) ''' </summary> Private Sub SetINASolverSettings(ByVal solverSettings As INASolverSettings) solverSettings.ImpedanceAttributeName = ExtractImpedanceName(cboImpedance.Text) Dim restrictionAttributes As IStringArray = solverSettings.RestrictionAttributeNames restrictionAttributes.RemoveAll() Dim i As Integer For i = 0 To chklstRestrictions.CheckedItems.Count - 1 restrictionAttributes.Add(chklstRestrictions.Items(chklstRestrictions.CheckedIndices(i)).ToString()) Next i solverSettings.RestrictionAttributeNames = restrictionAttributes Dim accumulateAttributes As IStringArray = solverSettings.AccumulateAttributeNames accumulateAttributes.RemoveAll() For i = 0 To chklstAccumulateAttributes.CheckedItems.Count - 1 accumulateAttributes.Add(chklstAccumulateAttributes.Items(chklstAccumulateAttributes.CheckedIndices(i)).ToString()) Next i solverSettings.AccumulateAttributeNames = accumulateAttributes solverSettings.RestrictUTurns = CType(cboUturnPolicy.SelectedIndex, esriNetworkForwardStarBacktrack) solverSettings.IgnoreInvalidLocations = chkIgnoreInvalidLocations.Checked End Sub ''' <summary> ''' Set server solver paramaters (ReturnMap, SnapTolerance, etc.) ''' </summary> Private Sub SetServerSolverParams(ByVal solverParams As INAServerSolverParams, ByVal serverContext As IServerContext) solverParams.ReturnMap = chkReturnMap.Checked solverParams.SnapTolerance = Convert.ToDouble(txtSnapTolerance.Text) solverParams.MaxSnapTolerance = Convert.ToDouble(txtMaxSnapTolerance.Text) solverParams.SnapToleranceUnits = CType(cboSnapToleranceUnits.SelectedIndex, esriUnits) solverParams.ImageDescription.Display.Width = pictureBox.Width solverParams.ImageDescription.Display.Height = pictureBox.Height ' This code shows how to specify the output spatial reference in order to get the map ' in a different spatial reference than the Network Dataset 'IESRISpatialReferenceGEN sr = serverContext.CreateObject("esriGeometry.GeographicCoordinateSystem") 'dim read as Integer 'sr.ImportFromESRISpatialReference("GEOGCS[GCS_North_American_1983,DATUM[D_North_American_1983,SPHEROID[GRS_1980,6378137.0,298.257222101]],PRIMEM[Greenwich,0.0],UNIT[Degree,0.0174532925199433]]", out read) 'solverParams.OutputSpatialReference = sr Dim saParams As INAServerServiceAreaParams = solverParams If Not saParams Is Nothing Then saParams.ReturnSALineGeometries = Me.chkReturnSALineGeometries.Checked saParams.ReturnSALines = Me.chkReturnSALines.Checked saParams.ReturnSAPolygonGeometries = Me.chkReturnSAPolygonGeometries.Checked saParams.ReturnSAPolygons = Me.chkReturnSAPolygons.Checked saParams.ReturnFacilities = Me.chkReturnSAFacilities.Checked saParams.ReturnBarriers = Me.chckReturnSABarriers.Checked End If End Sub ''' <summary> ''' Set specific solver settings (DefaultCutoff, DefaultTargetFacilityCount, etc.) ''' </summary> Private Sub SetSolverSpecificInterface(ByVal solverParams As INAServerSolverParams) Dim saSolver As INAServiceAreaSolver2 = solverParams If Not saSolver Is Nothing Then Dim defaultBreaks As IDoubleArray = saSolver.DefaultBreaks defaultBreaks.RemoveAll() Dim breaks As String = txtSADefaultBreaks.Text.Trim() breaks.Replace(" ", " ") Dim values() As String = breaks.Split(" "c) Dim iBreak As Integer For iBreak = values.GetLowerBound(0) To values.GetUpperBound(0) Step iBreak + 1 Dim breakValue As String = values(iBreak) defaultBreaks.Add(System.Convert.ToDouble(breakValue)) Next saSolver.DefaultBreaks = defaultBreaks saSolver.TravelDirection = CType(cboSATravelDirection.SelectedIndex, esriNATravelDirection) ' Set Output Lines If (Me.chkOutputLines.Checked) Then saSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShape Else saSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineNone End If ' Set Output Polygons If (Me.chkOutputPolygon.Checked) Then saSolver.OutputPolygons = CType(cboSAOutputPolygons.SelectedIndex + 1, esriNAOutputPolygonType) Else saSolver.OutputPolygons = esriNAOutputPolygonType.esriNAOutputPolygonNone End If chkTrimOuterPolygon.Checked = saSolver.TrimOuterPolygon If cboSAMultipleFacilityOptions.SelectedIndex = 2 Then saSolver.OverlapPolygons = True saSolver.MergeSimilarPolygonRanges = True ElseIf cboSAMultipleFacilityOptions.SelectedIndex = 0 Then saSolver.OverlapPolygons = True Else saSolver.OverlapPolygons = False End If saSolver.SplitPolygonsAtBreaks = (cboSAOverlapType.SelectedIndex = 0) ' Set trimming option saSolver.TrimOuterPolygon = chkTrimOuterPolygon.Checked saSolver.TrimPolygonDistance = Convert.ToDouble(Me.txtSATrimPolygonDistance.Text) saSolver.TrimPolygonDistanceUnits = CType(cboSATrimPolygonUnits.SelectedIndex, esriUnits) End If End Sub ''' <summary> ''' Get all point feature layers from the ServerContext ''' Add them in the "Input Incidents" and "Input Facilities" drop down lists ''' </summary> Private Sub GetInputLocationsLayers(ByVal serverContext As IServerContext) Dim mapServer As IMapServer = serverContext.ServerObject Dim mapServerObjects As IMapServerObjects = mapServer Dim map As IMap = mapServerObjects.Map(mapServer.DefaultMapName) Dim featureClass As IFeatureClass = Nothing Dim layers As IEnumLayer = map.Layers(Nothing, True) layers.Reset() Dim layer As ILayer = layers.Next() While layer IsNot Nothing If TypeOf layer Is IFeatureLayer Then Dim FeatureLayer As IFeatureLayer = layer If _ Not (TypeOf FeatureLayer.FeatureClass Is INAClass) _ And FeatureLayer.FeatureClass.ShapeType = esriGeometryType.esriGeometryPoint _ Then cboLocationsfLayers.Items.Add(FeatureLayer.Name) End If End If layer = layers.Next() End While ' Add <None> at the end in case the user does not want to load incident Location from a Feature Layer cboLocationsfLayers.Items.Add("<None>") cboLocationsfLayers.SelectedIndex = 0 End Sub ''' <summary> ''' Helper function to convert a string unit (e.g Miles) to an esriNetworkAnalystUnits (e.g esriNAUMiles) ''' </summary> Private Function GetstringToesriUnits(ByVal stresriUnits As String) As esriNetworkAttributeUnits Select Case stresriUnits.ToLower() Case "inches" Return esriNetworkAttributeUnits.esriNAUInches Case "feet" Return esriNetworkAttributeUnits.esriNAUFeet Case "yards" Return esriNetworkAttributeUnits.esriNAUYards Case "miles" Return esriNetworkAttributeUnits.esriNAUMiles Case "nautical miles" Return esriNetworkAttributeUnits.esriNAUNauticalMiles Case "millimeters" Return esriNetworkAttributeUnits.esriNAUMillimeters Case "centimeters" Return esriNetworkAttributeUnits.esriNAUCentimeters Case "meters" Return esriNetworkAttributeUnits.esriNAUMeters Case "kilometers" Return esriNetworkAttributeUnits.esriNAUKilometers Case "decimal degrees" Return esriNetworkAttributeUnits.esriNAUDecimalDegrees Case "decimeters" Return esriNetworkAttributeUnits.esriNAUDecimeters Case "seconds" Return esriNetworkAttributeUnits.esriNAUSeconds Case "minutes" Return esriNetworkAttributeUnits.esriNAUMinutes Case "hours" Return esriNetworkAttributeUnits.esriNAUHours Case "days" Return esriNetworkAttributeUnits.esriNAUDays Case "unknown" Return esriNetworkAttributeUnits.esriNAUUnknown Case Else Return esriNetworkAttributeUnits.esriNAUUnknown End Select End Function ''' <summary> ''' Helper function to extract the impedance name in a string like "impedancename (impedanceunits)" ''' </summary> Private Function ExtractImpedanceName(ByVal ImpedanceUnits As String) As String Dim firstIndex As Integer = ImpedanceUnits.LastIndexOf(" ") Dim lastIndex As Integer = ImpedanceUnits.Length Return ImpedanceUnits.Remove(firstIndex, (lastIndex - firstIndex)) End Function ''' <summary> ''' Helper function to extract the impedance unit in a string like "impedancename (impedanceunits)" ''' </summary> Private Function ExtractImpedanceUnits(ByVal ImpedanceUnits As String) As String Dim firstIndex As Integer = ImpedanceUnits.LastIndexOf("(") + 1 Dim lastIndex As Integer = ImpedanceUnits.LastIndexOf(")") Return ImpedanceUnits.Substring(firstIndex, (lastIndex - firstIndex)) End Function ''' <summary> ''' Make frames Enabled ''' </summary> Private Sub MakeFramesEnabled(ByVal solverParams As INAServerSolverParams) fraINAServiceAreaSolver.Enabled = (Not solverParams Is Nothing) fraINAServerServiceAreaParams.Enabled = (Not solverParams Is Nothing) fraINASolverSettings.Enabled = (Not solverParams Is Nothing) End Sub ''' <summary> ''' Hide tabs ''' </summary> Private Sub HideTabs() ' remove tabs initially - There are added when the result is returned. Me.tabCtrlOutput.TabPages.Remove(Me.tabReturnSAPolygonFeatures) Me.tabCtrlOutput.TabPages.Remove(Me.tabReturnSALineFeatures) Me.tabCtrlOutput.TabPages.Remove(Me.tabReturnFacilitiesFeatures) Me.tabCtrlOutput.TabPages.Remove(Me.tabReturnBarrierFeatures) Me.tabCtrlOutput.TabPages.Remove(Me.tabReturnSAPolygonGeometry) Me.tabCtrlOutput.TabPages.Remove(Me.tabReturnSALineGeometry) End Sub ''' <summary> ''' Load form ''' </summary> Private Sub Route_GISServerAPIClass_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Init combo box for SnapTolerance cboSnapToleranceUnits.Items.Add("Unknown Units") cboSnapToleranceUnits.Items.Add("Inches") cboSnapToleranceUnits.Items.Add("Points") cboSnapToleranceUnits.Items.Add("Feet") cboSnapToleranceUnits.Items.Add("Yards") cboSnapToleranceUnits.Items.Add("Miles") cboSnapToleranceUnits.Items.Add("Nautical Miles") cboSnapToleranceUnits.Items.Add("Millimeters") cboSnapToleranceUnits.Items.Add("Centimeters") cboSnapToleranceUnits.Items.Add("Meters") cboSnapToleranceUnits.Items.Add("Kilometers") cboSnapToleranceUnits.Items.Add("DecimalDegrees") cboSnapToleranceUnits.Items.Add("Decimeters") ' Init combo box for UturnPolicy cboUturnPolicy.Items.Add("Nowhere") cboUturnPolicy.Items.Add("Everywhere") cboUturnPolicy.Items.Add("Only at Dead Ends") ' Init combo box for the travel directions cboSATravelDirection.Items.Add("From Facility") cboSATravelDirection.Items.Add("To Facility") cboSAOutputPolygons.Items.Add("Generalized") cboSAOutputPolygons.Items.Add("Detailed") cboSATrimPolygonUnits.Items.Add("Unknown Units") cboSATrimPolygonUnits.Items.Add("Inches") cboSATrimPolygonUnits.Items.Add("Points") cboSATrimPolygonUnits.Items.Add("Feet") cboSATrimPolygonUnits.Items.Add("Yards") cboSATrimPolygonUnits.Items.Add("Miles") cboSATrimPolygonUnits.Items.Add("Nautical Miles") cboSATrimPolygonUnits.Items.Add("Millimeters") cboSATrimPolygonUnits.Items.Add("Centimeters") cboSATrimPolygonUnits.Items.Add("Meters") cboSATrimPolygonUnits.Items.Add("Kilometers") cboSATrimPolygonUnits.Items.Add("DecimalDegrees") cboSATrimPolygonUnits.Items.Add("Decimeters") cboSAMultipleFacilityOptions.Items.Add("Overlapping Polygons") cboSAMultipleFacilityOptions.Items.Add("Not Overlapping Polygons") cboSAMultipleFacilityOptions.Items.Add("Merge By Break Value") cboSAOverlapType.Items.Add("Rings") cboSAOverlapType.Items.Add("Disks") If (txtServer.Text <> "" And txtServer.Text <> "<Enter ArcGIS Server Name>" _ And txtService.Text <> "" And txtService.Text <> "<Enter ArcGIS Map Server Object>") Then GetNetworkAnalysisLayers() End If End Sub ''' <summary> ''' This function shows how to populate facility locations using two different options: ''' 1) From Record Set using a Point Feature Class - Uncommented ''' 2) From an Array of PropertySets - Commented out ''' Uncomment the option, you would like to use ''' </summary> Private Sub LoadLocations(ByVal solverParams As INAServerSolverParams, ByVal serverContext As IServerContext) ' Begin Option 1 Dim recSet As IRecordSet = Nothing Dim serviceAreaParams As INAServerServiceAreaParams = solverParams ' Get Input incident Feature Class from the MapServer ' Do not create facility if <NONE> is selected If cboLocationsfLayers.Text.ToUpper() <> "<NONE>" Then recSet = GetRecordSetFromFeatureLayer(serverContext, cboLocationsfLayers.Text) Dim facilitiesRecSet As INAServerRecordSet = serverContext.CreateObject("esriNetworkAnalyst.NAServerRecordSet") facilitiesRecSet.RecordSet = recSet serviceAreaParams.Facilities = facilitiesRecSet End If ' End Option 1 '' Begin Option 2 'Dim propSets As IPropertySetArray = serverContext.CreateObject("esriSystem.PropertySetArray") '' Set one facility 'propSets.Add(CreateLocationPropertySet(serverContext, "Facility 1", -122.490249049, 37.7481194043, Nothing)) '' Create NAServerPropertySets 'Dim facilitiesPropSets As INAServerPropertySets = serverContext.CreateObject("esriNetworkAnalyst.NAServerPropertySets") 'facilitiesPropSets.PropertySets = propSets '' Pass the facility PropertySets 'dim serviceAreaParams As INAServerServiceAreaParams = solverParams 'serviceAreaParams.Facilities = facilitiesPropSets '' End Option 2 End Sub ''' <summary> ''' This function returns a IRecordSet corresponding to the Layername passed in ''' </summary> Private Function GetRecordSetFromFeatureLayer(ByVal serverContext As IServerContext, ByVal LayerName As String) As IRecordSet 'Get the feature class to load in Dim mapServer As IMapServer = serverContext.ServerObject Dim mapServerObjects As IMapServerObjects = mapServer Dim map As IMap = mapServerObjects.Map(mapServer.DefaultMapName) Dim featureClass As IFeatureClass = Nothing Dim layers As IEnumLayer = map.Layers(Nothing, True) layers.Reset() Dim layer As ILayer = layers.Next() While layer IsNot Nothing If layer.Name.ToUpper() = LayerName.ToUpper() Then Dim featureLayer As IFeatureLayer = layer featureClass = featureLayer.FeatureClass Exit While End If layer = layers.Next() End While If featureClass Is Nothing Then Throw (New Exception("Could not find the input layer name " + LayerName)) End If Dim recSet As IRecordSet = Nothing recSet = serverContext.CreateObject("esriGeodatabase.RecordSet") Dim queryFilter As IQueryFilter = serverContext.CreateObject("esriGeodatabase.QueryFilter") queryFilter.SubFields = "*" Dim recSetInit As IRecordSetInit = recSet recSetInit.SetSourceTable(featureClass, queryFilter) Return recSet End Function ''' <summary> ''' Output Results Messages, Map, SAPolygons and SALines Geometries ''' </summary> Private Sub OutputResults(ByVal solverParams As INAServerSolverParams, ByVal solverResults As INAServerSolverResults) Dim messagesSolverResults As String = "" ' Output Solve messages Dim gpMessages As IGPMessages = solverResults.SolveMessages If (Not gpMessages Is Nothing) Then For i As Integer = 0 To gpMessages.Count - 1 Dim gpMessage As IGPMessage = gpMessages.GetMessage(i) messagesSolverResults += vbCr + gpMessage.Description Next i End If ' Show a message box displaying both solver messages and total_impedance per route If (messagesSolverResults.Length() > 0) Then MessageBox.Show(messagesSolverResults, "NAServer Service Area Results") End If 'Output Map pictureBox.Image = Nothing If (solverParams.ReturnMap) Then Dim mapImage As IMapImage = solverResults.MapImage If ((Not mapImage Is Nothing) And (Not mapImage.MimeData Is Nothing)) Then pictureBox.Image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(mapImage.MimeData)) End If pictureBox.Refresh() End If ' Output service area results (directions, CFRoutes geometries and more) If ((Not solverParams Is Nothing) And (Not solverResults Is Nothing)) Then OutputServiceAreaResults(solverParams, solverResults) End If End Sub ''' <summary> ''' Output service area results according to the ServerServiceAreaParams options ''' </summary> Private Sub OutputServiceAreaResults(ByVal solverParams As INAServerServiceAreaParams, ByVal solverResults As INAServerServiceAreaResults) ' Return SAPolygon Features as RecordSet If (solverParams.ReturnSAPolygons) Then AddTabAndControl(Me.tabReturnSAPolygonFeatures, Me.dataGridSAPolygonFeatures) OutputRecSetToDataGrid(solverResults.SAPolygons, dataGridSAPolygonFeatures) Else tabCtrlOutput.TabPages.Remove(tabReturnSAPolygonFeatures) End If ' Return SALine Features as RecordSet If (solverParams.ReturnSALines) Then AddTabAndControl(Me.tabReturnSALineFeatures, Me.dataGridSALineFeatures) OutputRecSetToDataGrid(solverResults.SALines, dataGridSALineFeatures) Else tabCtrlOutput.TabPages.Remove(tabReturnSALineFeatures) End If ' Return SAPolygons Geometries If (solverParams.ReturnSAPolygonGeometries) Then AddTabAndControl(Me.tabReturnSAPolygonGeometry, Me.treeViewSAPolygonGeometries) OutputPolygons(solverResults.SAPolygonGeometries) Else tabCtrlOutput.TabPages.Remove(tabReturnSAPolygonGeometry) End If ' Return SALine Geometries If (solverParams.ReturnSALineGeometries) Then AddTabAndControl(Me.tabReturnSALineGeometry, Me.treeViewSALineGeometries) OutputPolylines(solverResults.SALineGeometries) Else tabCtrlOutput.TabPages.Remove(tabReturnSALineGeometry) End If ' Return Facility Features as RecordSet If (solverParams.ReturnFacilities) Then AddTabAndControl(Me.tabReturnFacilitiesFeatures, Me.dataGridFacilitiesFeatures) OutputRecSetToDataGrid(solverResults.Facilities, dataGridFacilitiesFeatures) Else tabCtrlOutput.TabPages.Remove(tabReturnFacilitiesFeatures) End If ' Return Barriers Features as RecordSet If (solverParams.ReturnBarriers) Then AddTabAndControl(Me.tabReturnBarrierFeatures, Me.dataGridBarrierFeatures) OutputRecSetToDataGrid(solverResults.Barriers, dataGridBarrierFeatures) Else tabCtrlOutput.TabPages.Remove(tabReturnBarrierFeatures) End If ' Make TabControlOuput enable tabCtrlOutput.Enabled = True End Sub ''' <summary> ''' Add a tab page and a tab control ''' </summary> Private Sub AddTabAndControl(ByVal tabPage As TabPage, ByVal controlToAdd As System.Windows.Forms.Control) If Not tabCtrlOutput.TabPages.Contains(tabPage) Then tabCtrlOutput.TabPages.Add(tabPage) tabPage.Controls.Add(controlToAdd) End If End Sub ''' <summary> ''' Generic function to output RecordSet (Facilities, Incidents, Barriers, or CFRoutes) in a DataDrid Control ''' </summary> Private Sub OutputRecSetToDataGrid(ByVal recSet As IRecordSet, ByVal OutDataGrid As System.Windows.Forms.DataGrid) Dim fields As IFields = recSet.Fields Dim dataSet As DataSet = New DataSet("dataSet") Dim dataTable As DataTable = New DataTable("Results") dataSet.Tables.Add(dataTable) ' Get Field Names Dim dataColumn As DataColumn = Nothing For f As Integer = 0 To fields.FieldCount - 1 dataColumn = New DataColumn(fields.Field(f).Name.ToString()) dataTable.Columns.Add(dataColumn) Next f ' Populate DataGrid rows by RecordSet Dim newDataRow As DataRow Dim cur As ICursor = recSet.Cursor(False) Dim row As IRow = cur.NextRow() If Not row Is Nothing Then While Not row Is Nothing newDataRow = dataTable.NewRow() For l As Integer = 0 To row.Fields.FieldCount - 1 newDataRow(l) = row.Value(l).ToString() Next l dataTable.Rows.Add(newDataRow) row = cur.NextRow() End While End If OutDataGrid.SetDataBinding(dataSet, "Results") OutDataGrid.Visible = True End Sub ''' <summary> ''' Output SAPolygons Geometries (Polygons) in a TreeView control ''' </summary> Private Sub OutputPolygons(ByVal polygons() As IPolygon) ' Suppress repainting the TreeView until all the objects have been created. treeViewSAPolygonGeometries.BeginUpdate() ' Clear the TreeView each time the method is called. treeViewSAPolygonGeometries.Nodes.Clear() For i As Integer = 0 To polygons.Length - 1 Dim NewNode As TreeNode = New TreeNode("Polygon Length [" + System.Convert.ToString(i + 1) + "] = " + polygons(i).Length.ToString()) treeViewSAPolygonGeometries.Nodes.Add(NewNode) Next ' Check if Route Geometry has been generated If polygons.Length = 0 Then Dim NewNode As TreeNode = New TreeNode("SAPolgon Geometry not generated") treeViewSAPolygonGeometries.Nodes.Add(NewNode) End If ' Begin repainting the TreeView. treeViewSAPolygonGeometries.ExpandAll() treeViewSAPolygonGeometries.EndUpdate() End Sub ''' <summary> ''' Output Route Geometries (Polylines) in a TreeView control ''' </summary> Private Sub OutputPolylines(ByVal polylines() As IPolyline) ' Suppress repainting the TreeView until all the objects have been created. treeViewSALineGeometries.BeginUpdate() ' Clear the TreeView each time the method is called. treeViewSALineGeometries.Nodes.Clear() For i As Integer = 0 To polylines.Length - 1 Dim NewNode As TreeNode = New TreeNode("Polyline Length for SALines [" + System.Convert.ToString(i + 1) + "] = " + polylines(i).Length.ToString()) treeViewSALineGeometries.Nodes.Add(NewNode) Dim pointCollection As IPointCollection = polylines(i) For j As Integer = 0 To pointCollection.PointCount - 1 Dim point As IPoint = pointCollection.Point(j) treeViewSALineGeometries.Nodes(i).Nodes.Add(New TreeNode("Point [" + System.Convert.ToString(j + 1) + "]: " + System.Convert.ToString(point.X) + "," + System.Convert.ToString(point.Y))) Next Next ' Check if Route Geometry has been generated If polylines.Length = 0 Then Dim NewNode As TreeNode = New TreeNode("SALine Geometry not generated") treeViewSALineGeometries.Nodes.Add(NewNode) End If ' Begin repainting the TreeView. treeViewSALineGeometries.ExpandAll() treeViewSALineGeometries.EndUpdate() End Sub ''' <summary> ''' Create a property set for NALocation ''' </summary> Private Function CreateLocationPropertySet(ByVal serverContext As IServerContext, ByVal name As String, ByVal X As Object, ByVal Y As Object, ByVal sr As ISpatialReference) As IPropertySet Dim propSet As IPropertySet = serverContext.CreateObject("esriSystem.PropertySet") propSet.SetProperty("Name", name) propSet.SetProperty("X", X) propSet.SetProperty("Y", Y) propSet.SetProperty("SpatialReference", sr) Return propSet End Function ''' <summary> ''' This function ''' - sets the server and solver parameters ''' - populates the facilities NALocations ''' - solve the problem to find the closest facilities to incidents ''' - gets and displays the server results (map, directions, etc.) ''' </summary> Private Sub cmdSolve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSolve.Click Me.Cursor = Cursors.WaitCursor Dim serverContext As IServerContext = Nothing Try ' Get ServerContext and NAServer serverContext = GetMapServerContext() Dim naServer As INAServer = GetNAServer(serverContext) ' Get SolverParams Dim solverParams As INAServerSolverParams = naServer.GetSolverParameters(cboNALayers.Text) ' Set Solver params SetINASolverSettings(solverParams) SetSolverSpecificInterface(solverParams) SetServerSolverParams(solverParams, serverContext) ' Load Locations LoadLocations(solverParams, serverContext) 'Solve the service area problem Dim solverResults As INAServerSolverResults = Nothing solverResults = naServer.Solve(solverParams) 'Get NAServer results in the tab controls OutputResults(solverParams, solverResults) Catch exception As Exception MessageBox.Show(exception.Message, "An error has occurred") Finally ' Release the ServerContext If Not serverContext Is Nothing Then serverContext.ReleaseContext() End If End Try Me.Cursor = Cursors.Default End Sub Public Sub New() If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine)) Then If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) Then Console.WriteLine("This application could not load the correct version of ArcGIS.") End If End If Dim aoLicenseInitializer As LicenseInitializer = New LicenseInitializer() 'ESRI License Initializer generated code. If (Not aoLicenseInitializer.InitializeApplication(New ESRI.ArcGIS.esriSystem.esriLicenseProductCode() { _ ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcView, _ ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcEditor, _ ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcInfo}, _ New ESRI.ArcGIS.esriSystem.esriLicenseExtensionCode() {})) Then Console.WriteLine(aoLicenseInitializer.LicenseMessage()) Console.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown.") aoLicenseInitializer.ShutdownApplication() Return End If ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. End Sub End Class