/// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            // Close the requested job
            try
            {
                IJTXJobManager     jobManager = this.WmxDatabase.JobManager;
                IJTXJob3           job        = jobManager.GetJob(m_jobToClose) as IJTXJob3;
                IJTXConfiguration3 configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;

                if (job.Stage != jtxJobStage.jtxJobStageClosed && !job.CanClose())
                {
                    throw new WmauException(WmauErrorCodes.C_CANNOT_CLOSE_JOB_ERROR);
                }

                msgs.AddMessage("Closing job " + m_jobToClose + " (" + job.Name + ")");
                job.Close();

                // Once the job is closed, do the other things that still need to be handled
                // separately (status updates, notifications, ...)
                Common.WmauHelperFunctions.UpdateJobStatus(this.WmxDatabase, job);
                job.Store();

                job.LogJobAction(
                    configMgr.GetActivityType(ESRI.ArcGIS.JTX.Utilities.Constants.ACTTYPE_CLOSE_JOB),
                    null,
                    string.Empty);
                Common.WmauHelperFunctions.SendNotification(
                    ESRI.ArcGIS.JTX.Utilities.Constants.NOTIF_JOB_CLOSED,
                    this.WmxDatabase,
                    job);

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_JOB_CLOSED);
                IGPLong           outValue     = new GPLongClass();
                outValue.Value     = m_jobToClose;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Update the internal parameters used by this GP tool
                string styleFileName = this.DetermineStyleFileName(this.m_xmlFilePath);

                // Retrieve the TA workbook
                IJTXConfiguration3 defaultDbReadonly      = WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXTaskAssistantWorkflowRecord tamRecord = defaultDbReadonly.GetTaskAssistantWorkflowRecord(this.m_sourceName);

                // Delete any existing workflow or style files that we're going to replace
                this.DeleteFile(this.m_xmlFilePath);
                this.DeleteFile(styleFileName);

                // Save the TAM workbook data out to file
                this.SaveStringToXmlFile(tamRecord.WorkflowXML, this.m_xmlFilePath);
                this.SaveStringToXmlFile(tamRecord.StyleXML, styleFileName);

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (System.IO.IOException ioEx)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_FILE_ACCESS_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ioEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_TAM_DOWNLOAD_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                // Retrieve the parameter in which the list of MXDs will be stored
                WmauParameterMap  paramMap  = new WmauParameterMap(paramValues);
                IGPParameter3     param     = paramMap.GetParam(C_PARAM_MAP_DOCUMENT_LIST);
                IGPParameterEdit3 paramEdit = paramMap.GetParamEdit(C_PARAM_MAP_DOCUMENT_LIST);

                // Set up the multi-value objects
                IGPMultiValue mvValue = new GPMultiValueClass();
                mvValue.MemberDataType = param.DataType;

                // Get the list of MXD names and add them all to the multivalue
                SortedList <string, string> mapDocuments = this.ListMapDocumentsInDatabase();
                foreach (string mapDocName in mapDocuments.Keys)
                {
                    IGPString strVal = new GPStringClass();
                    strVal.Value = mapDocName;
                    mvValue.AddValue(strVal as IGPValue);
                    msgs.AddMessage("Map Document: " + mapDocName);
                }

                paramEdit.Value = (IGPValue)mvValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                WmauParameterMap   paramMap  = new WmauParameterMap(paramValues);
                IJTXConfiguration3 configMgr = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;

                // Determine which query the user has selected
                SortedList <string, IJTXJobQuery> queryMap = new SortedList <string, IJTXJobQuery>();
                AddQueriesFromContainer(configMgr.GetPublicQueryContainer(), string.Empty, queryMap);
                if (!queryMap.Keys.Contains(m_queryName))
                {
                    throw new WmauException(WmauErrorCodes.C_UNKNOWN_QUERY_ERROR);
                }

                // Run the selected job query
                IJTXJobQuery tempQuery = queryMap[m_queryName];

                // TODO: Change this to use ".Evaluate()" once it's fixed
                List <int> jobIds = ParseJobIdsFromXml(tempQuery.EvaluateXML());
                jobIds.Sort();

                // Store the job IDs from the query into the output GP param
                IGPMultiValue outputValues = new GPMultiValueClass();
                outputValues.MemberDataType = paramMap.GetParam(C_PARAM_OUT_JOB_ID_LIST).DataType;
                for (int i = 0; i < jobIds.Count; i++)
                {
                    IGPLong jobIdVal = new GPLongClass();
                    jobIdVal.Value = jobIds[i];
                    outputValues.AddValue(jobIdVal as IGPValue);
                    msgs.AddMessage("Found job: " + jobIds[i]);
                }

                paramMap.GetParamEdit(C_PARAM_OUT_JOB_ID_LIST).Value = (IGPValue)outputValues;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                // Retrieve the MXD and delete it
                IJTXConfiguration3 configMgr = WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXMap            map       = configMgr.GetJTXMap(m_mxdName);
                configMgr.DeleteJTXMap(map.ID);

                // Update the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_MXD_NAME);
                IGPString         outValue     = new GPStringClass();
                outValue.Value     = m_mxdName;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_DELETE_MXD_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            finally
            {
                // Release any COM objects here!
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Retrieve the parameter in which the list of workbook names will be stored
                WmauParameterMap  paramMap  = new WmauParameterMap(paramValues);
                IGPParameter3     param     = paramMap.GetParam(C_PARAM_TAM_WORKBOOK_LIST);
                IGPParameterEdit3 paramEdit = paramMap.GetParamEdit(C_PARAM_TAM_WORKBOOK_LIST);

                // Set up the multi-value objects
                IGPMultiValue mvValue = new GPMultiValueClass();
                mvValue.MemberDataType = param.DataType;

                // Get the list of TA workbook names and add them all to the multivalue
                SortedList <string, string> tamWorkbooks = this.ListTamWorkbooksInDatabase();
                foreach (string workbookAlias in tamWorkbooks.Keys)
                {
                    IGPString strVal = new GPStringClass();
                    strVal.Value = workbookAlias;
                    mvValue.AddValue(strVal as IGPValue);
                    msgs.AddMessage("Workbook: " + workbookAlias);
                }

                paramEdit.Value = (IGPValue)mvValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                WmauParameterMap paramMap = new WmauParameterMap(paramValues);

                // Retrieve the parameter in which the list of workbook names will be stored
                IGPParameter3     param       = paramMap.GetParam(C_PARAM_JOB_ID_LIST);
                IGPParameterEdit3 paramEdit   = paramMap.GetParamEdit(C_PARAM_JOB_ID_LIST);
                IGPParameter3     filterParam = paramMap.GetParam(C_PARAM_SQL_QUERY_FILTER);

                // Get the multivalue object into which the output will be stored
                IGPMultiValue outputValues = new GPMultiValueClass();
                outputValues.MemberDataType = param.DataType;
                for (int i = 0; i < outputValues.Count; i++)
                {
                    outputValues.Remove(i);
                }

                // Get the list of job IDs and add them all to the multivalue
                SortedList <int, string> jobs = this.ListJobsInDatabase(filterParam.Value.GetAsText());
                msgs.AddMessage("Jobs matching query:");
                foreach (KeyValuePair <int, string> item in jobs)
                {
                    IGPLong value = new GPLongClass();
                    value.Value = item.Key;
                    outputValues.AddValue(value as IGPValue);
                    msgs.AddMessage("  " + value.Value.ToString() + " (" + item.Value + ")");
                }

                paramEdit.Value = (IGPValue)outputValues;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            // Assign the requested job
            try
            {
                IJTXJobManager     jobManager = this.WmxDatabase.JobManager;
                IJTXConfiguration3 configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXJob3           job        = jobManager.GetJob(m_jobId) as IJTXJob3;

                // As of Jan. 2011, the core Workflow Manager libraries do not
                // seem to check if the user has the privilege to add a comment
                // if a job as a hold on it.  So run the check here.
                IJTXJobHolds jobHolds = job as IJTXJobHolds;
                if (jobHolds.Holds != null &&
                    jobHolds.Holds.Count > 0 &&
                    !CurrentUserHasPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_CAN_ADD_COMMENTS_FOR_HELD_JOBS))
                {
                    throw new WmauException(WmauErrorCodes.C_NO_ADD_COMMENTS_HELD_JOBS_ERROR);
                }

                // If we get this far, then add the comment to the job.
                IJTXActivityType commentType = configMgr.GetActivityType(ESRI.ArcGIS.JTX.Utilities.Constants.ACTTYPE_COMMENT);
                job.LogJobAction(commentType, null, m_comment);
                job.Store();

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_JOB_ID);
                IGPLong           outValue     = new GPLongClass();
                outValue.Value     = m_jobId;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            // Assign the requested job
            try
            {
                IJTXJobManager         jobManager = this.WmxDatabase.JobManager;
                IJTXWorkflowExecution3 jobExec    = jobManager.GetJob(m_jobId) as IJTXWorkflowExecution3;

                // Don't try to deal with the case of multiple active steps
                int[] currentStepIds = jobExec.GetCurrentSteps();
                if (currentStepIds.Length != 1)
                {
                    throw new WmauException(WmauErrorCodes.C_NO_OR_MULTIPLE_STEPS_ERROR);
                }

                jobExec.RunStepChecks(currentStepIds[0], true);
                IJTXExecuteInfo execInfo = jobExec.RunStep(currentStepIds[0], false, true, false, this);
                if (execInfo.ThrewError)
                {
                    throw new WmauException(
                              WmauErrorCodes.C_JOB_EXECUTION_ERROR,
                              new Exception(execInfo.ErrorCode.ToString() + ": " + execInfo.ErrorDescription));
                }

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_JOB_ID);
                IGPLong           outValue     = new GPLongClass();
                outValue.Value     = m_jobId;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
Exemplo n.º 10
0
        public IGPMessages Validate(IArray paramValues,
                                    bool updateValues,
                                    IGPEnvironmentManager envMgr)
        {
            if (m_util == null)               // delay constructor activities.... they segfault arcmap
            {
                m_util        = new GPUtilitiesClass();
                m_outp_schema = new AoTable(
                    AoField.Integer("left_fid"),
                    AoField.Integer("right_fid")
                    );
            }
            IGPMessages ms = m_util.InternalValidate(ParameterInfo,
                                                     paramValues,
                                                     updateValues,
                                                     true,
                                                     envMgr);
            bool passed = AoGPParameter.ValidateAll(m_parms, paramValues, ms);

            IGPMessage m = (IGPMessage)ms;

            if (!passed || m.IsError())
            {
                ms.AddError(2, "An error here.");
                return(ms);
            }

            // TODO: Set up the output schema.
            IGPParameter    outparam = paramValues.get_Element(3) as IGPParameter;
            IGPFeatureLayer fl       = outparam.Value as IGPFeatureLayer;

            if (fl == null)
            {
                fl = new GPFeatureLayerClass();
            }

            fl.DEFeatureClass = new DEFeatureClassClass();
            (fl.DEFeatureClass as IDETable).Fields = m_outp_schema.Fields;

            // Set up its catalog path / name..
            //			IDataElement de = (fc as IDataElement;
            //			if ( (old_val == null || old_val.DEFeatureClass == null) && src_path != null && src_path.DEFeatureClass != null) {
            //				//src_path = m_util.UnpackGPValue(src_path) as IGPFeatureLayer;
            //				IDataElement srcde = src_path.DEFeatureClass as IDataElement; // lazy cut-n-paste shortcut
            //				de.CatalogPath = srcde.GetPath() + "\\" + srcde.GetBaseName() + "_bndgen." + srcde.GetExtension();// old_val.CatalogPath;
            //				de.Children = srcde.Children;
            //				de.ChildrenExpanded = srcde.ChildrenExpanded;
            //				de.MetadataRetrieved = srcde.MetadataRetrieved;
            //				//de.Name = old_val.Name + "bndgen";
            //				de.Type = srcde.Type;
            //			}
            m_util.PackGPValue(fl as IGPValue,
                               outparam);
            return(ms);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Mandatory override. Provide GP tool body and logic.
        /// </summary>
        public virtual void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Basic error checking; ensure a Workflow Manager database is defined
            if (!IsWorkflowManagerDatabaseSet())
            {
                WmauError error = new WmauError(WmauErrorCodes.C_INVALID_WMX_DB_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message);
                throw new WmxDefaultDbNotSetException();
            }

            // Update the internal parameters used by this GP tool
            this.ExtractParameters(paramValues);
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Post validates the given set of values.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="environmentManager">Provides access to all the current environments and settings of the current client.</param>
        /// <param name="messages">The messages that are reported to the user.</param>
        /// <remarks>
        ///     This method is called after returning from the internal validation routine performed by the geoprocessing
        ///     framework. This method is where you can examine the messages created from internal validation and change them if
        ///     desired.
        ///     You should only change existing messages here and should not add any new messages.
        /// </remarks>
        public void UpdateMessages(IArray parameters, IGPEnvironmentManager environmentManager, IGPMessages messages)
        {
            try
            {
                var list = parameters.AsEnumerable <IGPParameter>().ToDictionary(o => o.Name, o => o);

                this.UpdateMessages(list, environmentManager, messages, this.Utilities);
            }
            catch (Exception ex)
            {
                messages.AddError(-1, ex.StackTrace);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Send the notification
                IJTXJobManager jobManager = this.WmxDatabase.JobManager;
                Common.WmauHelperFunctions.SendNotification(
                    m_notificationName, this.WmxDatabase, jobManager.GetJob(m_jobId));

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_JOB_ID);
                IGPLong           outValue     = new GPLongClass();
                outValue.Value     = m_jobId;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        ///     Executes the geoprocessing function using the given array of parameter values.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="trackCancel">The track cancel.</param>
        /// <param name="environmentManager">Provides access to all the current environments and settings of the current client.</param>
        /// <param name="messages">The messages.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">
        ///     parameters;A function tool must always have an output. At the minimum,
        ///     your function should output a Boolean value containing success or failure.
        /// </exception>
        public virtual void Execute(IArray parameters, ITrackCancel trackCancel, IGPEnvironmentManager environmentManager, IGPMessages messages)
        {
            try
            {
                if (parameters.AsEnumerable <IGPParameter>().All(o => o.Direction != esriGPParameterDirection.esriGPParameterDirectionOutput))
                {
                    throw new ArgumentOutOfRangeException(nameof(parameters), @"A function tool must always have an output. At the minimum, your function should output a Boolean value containing success or failure.");
                }

                var list = this.Unpack(parameters);

                this.Execute(list, trackCancel, environmentManager, messages, this.Utilities);
            }
            catch (Exception ex)
            {
                messages.AddError(-1, ex.StackTrace);
            }
        }
        // Execute: Execute the function given the array of the parameters
        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            
            // Get the first Input Parameter
            IGPParameter parameter = (IGPParameter)paramvalues.get_Element(0);

            // UnPackGPValue. This ensures you get the value either form the dataelement or GpVariable (ModelBuilder)
           IGPValue parameterValue = m_GPUtilities.UnpackGPValue(parameter);

            // Open Input Dataset
            IFeatureClass inputFeatureClass;
            IQueryFilter qf;
            m_GPUtilities.DecodeFeatureLayer(parameterValue, out inputFeatureClass, out qf);

            if (inputFeatureClass == null)
            {
                message.AddError(2, "Could not open input dataset.");
                return;
            } 
                
            // Add the field if it does not exist.
            int indexA;

            parameter = (IGPParameter)paramvalues.get_Element(1);
            string field = parameter.Value.GetAsText();

            
            indexA = inputFeatureClass.FindField(field);
            if (indexA < 0)
            {
                IFieldEdit fieldEdit = new FieldClass();
                fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                fieldEdit.Name_2 = field;
                inputFeatureClass.AddField(fieldEdit);
            }

             int featcount = inputFeatureClass.FeatureCount(null);     
      
             //Set the properties of the Step Progressor
            IStepProgressor pStepPro = (IStepProgressor)trackcancel;
            pStepPro.MinRange = 0;
            pStepPro.MaxRange = featcount;
            pStepPro.StepValue = (1);
            pStepPro.Message = "Calculating Area";
            pStepPro.Position = 0;
            pStepPro.Show();

            // Create an Update Cursor
            indexA = inputFeatureClass.FindField(field);
            IFeatureCursor updateCursor = inputFeatureClass.Update(qf, false);
            IFeature updateFeature = updateCursor.NextFeature();
            IGeometry geometry;
            IArea area;
            double dArea;   
            
            while (updateFeature != null)
            {
                geometry = updateFeature.Shape;
                area = (IArea)geometry;
                dArea = area.Area;
                updateFeature.set_Value(indexA, dArea);
                updateCursor.UpdateFeature(updateFeature);
                updateFeature.Store();
                updateFeature = updateCursor.NextFeature();
                pStepPro.Step();
            }

           pStepPro.Hide();
            
            // Release the update cursor to remove the lock on the input data.
           System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
        }
Exemplo n.º 16
0
        public void Execute(IArray paramvalues, ITrackCancel TrackCancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            IAoInitialize     aoInitialize = new AoInitializeClass();
            esriLicenseStatus naStatus     = esriLicenseStatus.esriLicenseUnavailable;

            IGPUtilities2 gpUtil     = null;
            IDataset      osmDataset = null;

            try
            {
                if (!aoInitialize.IsExtensionCheckedOut(esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork))
                {
                    naStatus = aoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork);
                }

                gpUtil = new GPUtilitiesClass();

                // OSM Dataset Param
                IGPParameter osmDatasetParam = paramvalues.get_Element(in_osmFeatureDataset) as IGPParameter;
                IDEDataset2  osmDEDataset    = gpUtil.UnpackGPValue(osmDatasetParam) as IDEDataset2;
                if (osmDEDataset == null)
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), osmDatasetParam.Name));
                    return;
                }

                osmDataset = gpUtil.OpenDatasetFromLocation(((IDataElement)osmDEDataset).CatalogPath) as IDataset;

                // Network Config File Param
                IGPParameter osmNetConfigParam = paramvalues.get_Element(in_NetworkConfigurationFile) as IGPParameter;
                IGPValue     osmNetConfigFile  = gpUtil.UnpackGPValue(osmNetConfigParam) as IGPValue;
                if ((osmNetConfigFile == null) || (string.IsNullOrEmpty(osmNetConfigFile.GetAsText())))
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), osmNetConfigParam.Name));
                    return;
                }

                // Target Network Dataset Param
                IGPParameter ndsParam = paramvalues.get_Element(out_NetworkDataset) as IGPParameter;
                IDataElement deNDS    = gpUtil.UnpackGPValue(ndsParam) as IDataElement;
                if (deNDS == null)
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), ndsParam.Name));
                    return;
                }

                // Create Network Dataset
                using (NetworkDataset nd = new NetworkDataset(osmNetConfigFile.GetAsText(), osmDataset, deNDS.Name, message, TrackCancel))
                {
                    if (nd.CanCreateNetworkDataset())
                    {
                        nd.CreateNetworkDataset();
                    }
                }
            }
            catch (UserCancelException ex)
            {
                message.AddWarning(ex.Message);
            }
            catch (Exception ex)
            {
                message.AddError(120008, ex.Message);
#if DEBUG
                message.AddError(120008, ex.StackTrace);
#endif
            }
            finally
            {
                if (osmDataset != null)
                {
                    ComReleaser.ReleaseCOMObject(osmDataset);
                }

                if (naStatus == esriLicenseStatus.esriLicenseCheckedOut)
                {
                    aoInitialize.CheckInExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork);
                }

                if (gpUtil != null)
                {
                    ComReleaser.ReleaseCOMObject(gpUtil);
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            // Back up the database
            IJTXTransfer transfer = WmxDatabase as IJTXTransfer;

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                msgs.AddMessage("Retrieving data from Workflow Manager database...");
                string xml = transfer.ExportConfiguration();

                // Pretty-print the JXL file, if the user has selected that option
                if (this.m_prettyPrint)
                {
                    msgs.AddMessage("Making the data more easily human-readable...");
                    XmlDocument   xmlDoc        = new XmlDocument();
                    XmlTextReader xmlTextReader = new XmlTextReader(xml, XmlNodeType.Document, null);
                    xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;
                    xmlDoc.Load(xmlTextReader);
                    xmlTextReader.Close();

                    msgs.AddMessage("Saving data to file...");
                    System.IO.TextWriter textWriter    = new System.IO.StreamWriter(this.m_jxlFilePath, false, System.Text.Encoding.UTF8, xml.Length);
                    XmlTextWriter        xmlTextWriter = new XmlTextWriter(textWriter);
                    xmlTextWriter.Formatting = Formatting.Indented;
                    xmlDoc.Save(xmlTextWriter);
                    xmlTextWriter.Close();
                    textWriter.Close();
                }
                else
                {
                    msgs.AddMessage("Saving data to file...");
                    System.IO.TextWriter textWriter = new System.IO.StreamWriter(this.m_jxlFilePath, false, System.Text.Encoding.UTF8, xml.Length);
                    textWriter.Write(xml);
                    textWriter.Close();
                }

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens }
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_JXL_BACKUP_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens }
                }
            }
        }
        public void Execute(IArray paramvalues, ITrackCancel trackcancel,
                            IGPEnvironmentManager envMgr, IGPMessages messages)
        {
            // Remember the original GP environment settings and temporarily override these settings

            var gpSettings = envMgr as IGeoProcessorSettings;
            bool origAddOutputsToMapSetting = gpSettings.AddOutputsToMap;
            bool origLogHistorySetting = gpSettings.LogHistory;
            gpSettings.AddOutputsToMap = false;
            gpSettings.LogHistory = false;

            // Create the Geoprocessor

            Geoprocessor gp = new Geoprocessor();

            try
            {
                // Validate our values

                IGPMessages validateMessages = ((IGPFunction2)this).Validate(paramvalues, false, envMgr);
                if ((validateMessages as IGPMessage).IsError())
                {
                    messages.AddError(1, "Validate failed");
                    return;
                }

                // Unpack values

                IGPParameter gpParam = paramvalues.get_Element(InputMtdDSTTable) as IGPParameter;
                IGPValue inputMtdDSTTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputMtdCntryRefTable) as IGPParameter;
                IGPValue inputMtdCntryRefTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputMtdAreaTable) as IGPParameter;
                IGPValue inputMtdAreaTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputAdminBndyFeatureClasses) as IGPParameter;
                var inputAdminBndyFeatureClassesMultiValue = m_gpUtils.UnpackGPValue(gpParam) as IGPMultiValue;
                gpParam = paramvalues.get_Element(OutputFileGDB) as IGPParameter;
                IGPValue outputFileGDBValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputStreetsFeatureClass) as IGPParameter;
                IGPValue inputStreetsFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTimeZoneIDBaseFieldName) as IGPParameter;
                IGPValue inputTimeZoneIDBaseFieldNameValue = m_gpUtils.UnpackGPValue(gpParam);

                bool processStreetsFC = (!(inputStreetsFeatureClassValue.IsEmpty()));
                string timeZoneIDBaseFieldName = "";
                if (!(inputTimeZoneIDBaseFieldNameValue.IsEmpty()))
                    timeZoneIDBaseFieldName = inputTimeZoneIDBaseFieldNameValue.GetAsText();

                // Get the path to the output file GDB

                string outputFileGdbPath = outputFileGDBValue.GetAsText();

                // Create the new file geodatabase

                AddMessage("Creating the file geodatabase...", messages, trackcancel);

                int lastBackslash = outputFileGdbPath.LastIndexOf("\\");
                CreateFileGDB createFGDBTool = new CreateFileGDB();
                createFGDBTool.out_folder_path = outputFileGdbPath.Remove(lastBackslash);
                createFGDBTool.out_name = outputFileGdbPath.Substring(lastBackslash + 1);
                gp.Execute(createFGDBTool, trackcancel);

                // Copy the MtdDST table to the file geodatabase and add the ADMIN_LVL and AREACODE fields to it

                AddMessage("Copying the MtdDST table to the file geodatabase...", messages, trackcancel);

                TableToTable importTableTool = new TableToTable();
                string inputMtdDSTTablePath = inputMtdDSTTableValue.GetAsText();
                importTableTool.in_rows = inputMtdDSTTablePath;
                importTableTool.out_path = outputFileGdbPath;
                importTableTool.out_name = "MtdDST";
                importTableTool.field_mapping = "AREA_ID \"AREA_ID\" true true false 4 Long 0 0 ,First,#," + inputMtdDSTTablePath + ",AREA_ID,-1,-1;" +
                                                "TIME_ZONE \"TIME_ZONE\" true true false 4 Text 0 0 ,First,#," + inputMtdDSTTablePath + ",TIME_ZONE,-1,-1;" +
                                                "DST_EXIST \"DST_EXIST\" true true false 1 Text 0 0 ,First,#," + inputMtdDSTTablePath + ",DST_EXIST,-1,-1";
                gp.Execute(importTableTool, trackcancel);

                string mtdDSTTablePath = outputFileGdbPath + "\\MtdDST";

                AddField addFieldTool = new AddField();
                addFieldTool.in_table = mtdDSTTablePath;
                addFieldTool.field_name = "ADMIN_LVL";
                addFieldTool.field_type = "SHORT";
                gp.Execute(addFieldTool, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = mtdDSTTablePath;
                addFieldTool.field_name = "AREACODE";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 76;
                gp.Execute(addFieldTool, trackcancel);

                // Copy the MtdArea table to the file geodatabase and index the AREA_ID field

                AddMessage("Copying the MtdArea table to the file geodatabase...", messages, trackcancel);

                importTableTool = new TableToTable();
                importTableTool.in_rows = inputMtdAreaTableValue.GetAsText();
                importTableTool.out_path = outputFileGdbPath;
                importTableTool.out_name = "MtdArea";
                gp.Execute(importTableTool, trackcancel);

                string mtdAreaTablePath = outputFileGdbPath + "\\MtdArea";

                AddIndex addIndexTool = new AddIndex();
                addIndexTool.in_table = mtdAreaTablePath;
                addIndexTool.fields = "AREA_ID";
                addIndexTool.index_name = "AREA_ID";
                gp.Execute(addIndexTool, trackcancel);

                // Calculate the ADMIN_LVL and AREACODE fields on the MtdDST table

                MakeTableView makeTableViewTool = new MakeTableView();
                makeTableViewTool.in_table = mtdDSTTablePath;
                makeTableViewTool.out_view = "MtdDST_Layer";
                gp.Execute(makeTableViewTool, trackcancel);

                AddJoin addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "MtdDST_Layer";
                addJoinTool.in_field = "AREA_ID";
                addJoinTool.join_table = mtdAreaTablePath;
                addJoinTool.join_field = "AREA_ID";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the ADMIN_LVL field...", messages, trackcancel);

                CalculateField calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "MtdDST_Layer";
                calcFieldTool.field = "MtdDST.ADMIN_LVL";
                calcFieldTool.expression = "[MtdArea.ADMIN_LVL]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Calculating the AREACODE field...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "MtdDST_Layer";
                calcFieldTool.field = "MtdDST.AREACODE";
                calcFieldTool.code_block = "lvl = [MtdArea.ADMIN_LVL]\n" +
                                           "s = CStr([MtdArea.AREACODE_1])\n" +
                                           "If lvl >= 2 Then s = s & \".\" & CStr([MtdArea.AREACODE_2])\n" +
                                           "If lvl >= 3 Then s = s & \".\" & CStr([MtdArea.AREACODE_3])\n" +
                                           "If lvl >= 4 Then s = s & \".\" & CStr([MtdArea.AREACODE_4])\n" +
                                           "If lvl >= 5 Then s = s & \".\" & CStr([MtdArea.AREACODE_5])\n" +
                                           "If lvl >= 6 Then s = s & \".\" & CStr([MtdArea.AREACODE_6])\n" +
                                           "If lvl >= 7 Then s = s & \".\" & CStr([MtdArea.AREACODE_7])";
                calcFieldTool.expression = "s";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                RemoveJoin removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "MtdDST_Layer";
                removeJoinTool.join_name = "MtdArea";
                gp.Execute(removeJoinTool, trackcancel);

                Delete deleteTool = new Delete();
                deleteTool.in_data = "MtdDST_Layer";
                gp.Execute(deleteTool, trackcancel);

                // Create the MtdDST# tables by admin levels and index the AREACODE field

                TableSelect tableSelectTool = null;
                for (int i = 1; i <= 7; i++)
                {
                    string iAsString = Convert.ToString(i, System.Globalization.CultureInfo.InvariantCulture);

                    AddMessage("Extracting level " + iAsString + " MtdDST rows...", messages, trackcancel);

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = mtdDSTTablePath;
                    tableSelectTool.out_table = mtdDSTTablePath + iAsString;
                    tableSelectTool.where_clause = "ADMIN_LVL = " + iAsString;
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = mtdDSTTablePath + iAsString;
                    addIndexTool.fields = "AREACODE";
                    addIndexTool.index_name = "AREACODE";
                    gp.Execute(addIndexTool, trackcancel);
                }

                deleteTool = new Delete();
                deleteTool.in_data = mtdDSTTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Copy the MtdCntryRef table to the file geodatabase (use Statistics tool to remove duplicate rows)

                AddMessage("Copying the MtdCntryRef table to the file geodatabase...", messages, trackcancel);

                string inputMtdCntryRefTablePath = inputMtdCntryRefTableValue.GetAsText();
                string mtdCntryRefTablePath = outputFileGdbPath + "\\MtdCntryRef";
                Statistics statsTool = new Statistics();
                statsTool.in_table = inputMtdCntryRefTablePath;
                statsTool.out_table = mtdCntryRefTablePath;
                statsTool.statistics_fields = "ISO_CODE COUNT";
                statsTool.case_field = "GOVT_CODE;ISO_CODE;DRIVING_SD;ADMINLEVEL";
                gp.Execute(statsTool, trackcancel);

                DeleteField deleteFieldTool = new DeleteField();
                deleteFieldTool.in_table = mtdCntryRefTablePath;
                deleteFieldTool.drop_field = "FREQUENCY;COUNT_ISO_CODE";
                gp.Execute(deleteFieldTool, trackcancel);

                // Index the GOVT_CODE field

                addIndexTool = new AddIndex();
                addIndexTool.in_table = mtdCntryRefTablePath;
                addIndexTool.fields = "GOVT_CODE";
                addIndexTool.index_name = "GOVT_CODE";
                gp.Execute(addIndexTool, trackcancel);

                // Extract the top level (country) records from the MtdArea table and index the AREACODE_1 field

                AddMessage("Extracting the top-level rows from the MtdArea table...", messages, trackcancel);

                string mtdTopAreaTablePath = outputFileGdbPath + "\\TopArea";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = mtdAreaTablePath;
                tableSelectTool.out_table = mtdTopAreaTablePath;
                tableSelectTool.where_clause = "AREACODE_2 = 0 AND AREA_TYPE = 'B'";
                gp.Execute(tableSelectTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = mtdTopAreaTablePath;
                addIndexTool.fields = "AREACODE_1";
                addIndexTool.index_name = "AREACODE_1";
                gp.Execute(addIndexTool, trackcancel);

                // Create and calculate the TOP_GOVT_CODE field on the MtdArea table

                addFieldTool = new AddField();
                addFieldTool.in_table = mtdAreaTablePath;
                addFieldTool.field_name = "TOP_GOVT_CODE";
                addFieldTool.field_type = "LONG";
                gp.Execute(addFieldTool, trackcancel);

                makeTableViewTool = new MakeTableView();
                makeTableViewTool.in_table = mtdAreaTablePath;
                makeTableViewTool.out_view = "MtdArea_Layer";
                gp.Execute(makeTableViewTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "MtdArea_Layer";
                addJoinTool.in_field = "AREACODE_1";
                addJoinTool.join_table = mtdTopAreaTablePath;
                addJoinTool.join_field = "AREACODE_1";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the TOP_GOVT_CODE field...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "MtdArea_Layer";
                calcFieldTool.field = "MtdArea.TOP_GOVT_CODE";
                calcFieldTool.expression = "[TopArea.GOVT_CODE]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "MtdArea_Layer";
                removeJoinTool.join_name = "TopArea";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = mtdTopAreaTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Create and calculate the ISO_CODE and DRIVING_SD string fields

                addFieldTool = new AddField();
                addFieldTool.in_table = mtdAreaTablePath;
                addFieldTool.field_name = "ISO_CODE";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 3;
                gp.Execute(addFieldTool, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = mtdAreaTablePath;
                addFieldTool.field_name = "DRIVING_SD";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 1;
                gp.Execute(addFieldTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "MtdArea_Layer";
                addJoinTool.in_field = "TOP_GOVT_CODE";
                addJoinTool.join_table = mtdCntryRefTablePath;
                addJoinTool.join_field = "GOVT_CODE";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the ISO_CODE field...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "MtdArea_Layer";
                calcFieldTool.field = "MtdArea.ISO_CODE";
                calcFieldTool.expression = "[MtdCntryRef.ISO_CODE]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Calculating the DRIVING_SD field...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "MtdArea_Layer";
                calcFieldTool.field = "MtdArea.DRIVING_SD";
                calcFieldTool.expression = "[MtdCntryRef.DRIVING_SD]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "MtdArea_Layer";
                removeJoinTool.join_name = "MtdCntryRef";
                gp.Execute(removeJoinTool, trackcancel);

                // Create and calculate the FullAREACODE# string fields and the UTCOffset and DST fields

                addFieldTool = new AddField();
                addFieldTool.in_table = mtdAreaTablePath;
                addFieldTool.field_type = "SHORT";
                addFieldTool.field_name = "UTCOffset";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_name = "DST";
                gp.Execute(addFieldTool, trackcancel);

                string codeBlock = "lvl = [ADMIN_LVL]\ns = CStr([AREACODE_1])";
                for (int i = 1; i <= 7; i++)
                {
                    string iAsString = Convert.ToString(i, System.Globalization.CultureInfo.InvariantCulture);
                    string iPlusOne = Convert.ToString(i+1, System.Globalization.CultureInfo.InvariantCulture);
                    string fullAreaCodeFieldName = "FullAREACODE" + iAsString;
                    addFieldTool = new AddField();
                    addFieldTool.in_table = mtdAreaTablePath;
                    addFieldTool.field_name = fullAreaCodeFieldName;
                    addFieldTool.field_type = "TEXT";
                    addFieldTool.field_length = 76;
                    gp.Execute(addFieldTool, trackcancel);

                    AddMessage("Calculating the FullAREACODE" + iAsString + " field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = mtdAreaTablePath;
                    calcFieldTool.field = fullAreaCodeFieldName;
                    calcFieldTool.code_block = codeBlock;
                    calcFieldTool.expression = "s";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);
                    codeBlock = codeBlock + "\nIf lvl >= " + iPlusOne + " Then s = s & \".\" & CStr([AREACODE_" + iPlusOne + "])";

                    string dstJoinTableName = "MtdDST" + iAsString;
                    string dstJoinTablePath = outputFileGdbPath + "\\" + dstJoinTableName;

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "MtdArea_Layer";
                    addJoinTool.in_field = fullAreaCodeFieldName;
                    addJoinTool.join_table = dstJoinTablePath;
                    addJoinTool.join_field = "AREACODE";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the UTCOffset field (" + iAsString + " of 7)...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "MtdArea_Layer";
                    calcFieldTool.field = "MtdArea.UTCOffset";
                    calcFieldTool.code_block = "s = [MtdArea.UTCOffset]\n" +
                                               "joinValue = [" + dstJoinTableName + ".TIME_ZONE]\n" +
                                               "If Not IsNull(joinValue) Then\n" +
                                               "  If Trim(joinValue) <> \"\" Then s = CInt(joinValue) * 6\n" +
                                               "End If";
                    calcFieldTool.expression = "s";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the DST field (" + iAsString + " of 7)...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "MtdArea_Layer";
                    calcFieldTool.field = "MtdArea.DST";
                    calcFieldTool.code_block = "s = [MtdArea.DST]\n" +
                                               "joinValue = [" + dstJoinTableName + ".DST_EXIST]\n" +
                                               "If Not IsNull(joinValue) Then\n" +
                                               "  Select Case Trim(joinValue)\n" +
                                               "    Case \"Y\": s = 1\n    Case \"N\": s = 0\n" +
                                               "  End Select\n" +
                                               "End If";
                    calcFieldTool.expression = "s";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "MtdArea_Layer";
                    removeJoinTool.join_name = dstJoinTableName;
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = dstJoinTablePath;
                    gp.Execute(deleteTool, trackcancel);
                }

                deleteTool = new Delete();
                deleteTool.in_data = "MtdArea_Layer";
                gp.Execute(deleteTool, trackcancel);

                // Create and calculate the sortable MSTIMEZONE field on the MtdArea table

                addFieldTool = new AddField();
                addFieldTool.in_table = mtdAreaTablePath;
                addFieldTool.field_name = "SortableMSTIMEZONE";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 60;
                gp.Execute(addFieldTool, trackcancel);

                AddMessage("Calculating the time zones...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = mtdAreaTablePath;
                calcFieldTool.field = "SortableMSTIMEZONE";
                calcFieldTool.code_block = TimeZoneUtilities.MakeSortableMSTIMEZONECode("ISO_CODE");
                calcFieldTool.expression = "z";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                // Extract the MtdArea rows to be used for generating the time zone polygons and index the AREA_ID field

                string mtdAreaForTZPolysTablePath = outputFileGdbPath + "\\MtdAreaForTZPolys";
                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = mtdAreaTablePath;
                tableSelectTool.out_table = mtdAreaForTZPolysTablePath;
                tableSelectTool.where_clause = CreateWhereClauseForAdminLvlByCountry(outputFileGdbPath, "MtdCntryRef");
                gp.Execute(tableSelectTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = mtdAreaForTZPolysTablePath;
                addIndexTool.fields = "AREA_ID";
                addIndexTool.index_name = "AREA_ID";
                gp.Execute(addIndexTool, trackcancel);

                // We no longer need the MtdCntryRef table anymore

                deleteTool = new Delete();
                deleteTool.in_data = mtdCntryRefTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Merge the AdminBndy feature classes together into one feature class

                int numAdminBndyFCs = inputAdminBndyFeatureClassesMultiValue.Count;
                string mergeToolInputs = "";
                for (int i = 0; i < numAdminBndyFCs; i++)
                {
                    mergeToolInputs = mergeToolInputs + inputAdminBndyFeatureClassesMultiValue.get_Value(i).GetAsText() + ";";
                }
                mergeToolInputs = mergeToolInputs.Remove(mergeToolInputs.Length - 1);
                string adminBndyFCPath = outputFileGdbPath + "\\AdminBndy";

                AddMessage("Merging the Administrative Boundary feature classes...", messages, trackcancel);

                Merge mergeTool = new Merge();
                mergeTool.inputs = mergeToolInputs;
                mergeTool.output = adminBndyFCPath;
                gp.Execute(mergeTool, trackcancel);

                // Join the AdminBndy polygons to the MtdArea rows to be used for generating the time zone polygons

                MakeFeatureLayer makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = adminBndyFCPath;
                makeFeatureLayerTool.out_layer = "AdminBndy_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "AdminBndy_Layer";
                addJoinTool.in_field = "AREA_ID";
                addJoinTool.join_table = mtdAreaForTZPolysTablePath;
                addJoinTool.join_field = "AREA_ID";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                FeatureClassToFeatureClass importFCTool = new FeatureClassToFeatureClass();
                importFCTool.in_features = "AdminBndy_Layer";
                importFCTool.out_path = outputFileGdbPath;
                importFCTool.out_name = "UndissolvedTZPolys";
                importFCTool.field_mapping = "SortableMSTIMEZONE \"SortableMSTIMEZONE\" true true false 60 Text 0 0 ,First,#," +
                                             mtdAreaForTZPolysTablePath + ",MtdAreaForTZPolys.SortableMSTIMEZONE,-1,-1";
                gp.Execute(importFCTool, trackcancel);
                string undissolvedTZPolysFCPath = outputFileGdbPath + "\\UndissolvedTZPolys";

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "AdminBndy_Layer";
                removeJoinTool.join_name = "MtdAreaForTZPolys";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "AdminBndy_Layer";
                gp.Execute(deleteTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = adminBndyFCPath;
                gp.Execute(deleteTool, trackcancel);
                deleteTool.in_data = mtdAreaForTZPolysTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Dissolve the time zone polygons together

                AddMessage("Dissolving the time zones...", messages, trackcancel);

                string timeZoneFCPath = outputFileGdbPath + "\\" + TimeZoneFCName;
                Dissolve dissolveTool = new Dissolve();
                dissolveTool.in_features = undissolvedTZPolysFCPath;
                dissolveTool.out_feature_class = timeZoneFCPath;
                dissolveTool.dissolve_field = "SortableMSTIMEZONE";
                dissolveTool.multi_part = "MULTI_PART";
                gp.Execute(dissolveTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = undissolvedTZPolysFCPath;
                gp.Execute(deleteTool, trackcancel);

                // Create and calculate the MSTIMEZONE field

                addFieldTool = new AddField();
                addFieldTool.in_table = timeZoneFCPath;
                addFieldTool.field_name = "MSTIMEZONE";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 50;
                gp.Execute(addFieldTool, trackcancel);

                AddMessage("Calculating the time zones...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = timeZoneFCPath;
                calcFieldTool.field = "MSTIMEZONE";
                calcFieldTool.expression = "Mid([SortableMSTIMEZONE], 7)";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                // Delete the old sortable MSTIMEZONE field

                deleteFieldTool = new DeleteField();
                deleteFieldTool.in_table = timeZoneFCPath;
                deleteFieldTool.drop_field = "SortableMSTIMEZONE";
                gp.Execute(deleteFieldTool, trackcancel);

                if (processStreetsFC)
                {
                    // Create the network dataset time zone table

                    AddMessage("Creating the time zones table...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = timeZoneFCPath;
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = TimeZonesTableName;
                    importTableTool.field_mapping = "MSTIMEZONE \"MSTIMEZONE\" true true false 50 Text 0 0 ,First,#," +
                                                    timeZoneFCPath + ",MSTIMEZONE,-1,-1";
                    gp.Execute(importTableTool, trackcancel);

                    // Separate the MtdArea table by driving side and index the AREA_ID field on each

                    AddMessage("Extracting rows for the left-side driving areas...", messages, trackcancel);

                    string drivingLTablePath = mtdAreaTablePath + "DrivingL";
                    string drivingRTablePath = mtdAreaTablePath + "DrivingR";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = mtdAreaTablePath;
                    tableSelectTool.out_table = drivingLTablePath;
                    tableSelectTool.where_clause = "DRIVING_SD = 'L'";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = drivingLTablePath;
                    addIndexTool.fields = "AREA_ID";
                    addIndexTool.index_name = "AREA_ID";
                    gp.Execute(addIndexTool, trackcancel);

                    AddMessage("Extracting rows for the right-side driving areas...", messages, trackcancel);

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = mtdAreaTablePath;
                    tableSelectTool.out_table = drivingRTablePath;
                    tableSelectTool.where_clause = "DRIVING_SD = 'R'";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = drivingRTablePath;
                    addIndexTool.fields = "AREA_ID";
                    addIndexTool.index_name = "AREA_ID";
                    gp.Execute(addIndexTool, trackcancel);

                    // Import the Streets feature class to the file geodatabase and
                    // add the FT_TimeZoneID and TF_TimeZoneID fields

                    AddMessage("Copying the Streets feature class to the geodatabase...", messages, trackcancel);

                    importFCTool = new FeatureClassToFeatureClass();
                    importFCTool.in_features = inputStreetsFeatureClassValue.GetAsText();
                    importFCTool.out_path = outputFileGdbPath;
                    importFCTool.out_name = StreetsFCName;
                    gp.Execute(importFCTool, trackcancel);

                    string pathToStreetsFC = outputFileGdbPath + "\\" + StreetsFCName;

                    addFieldTool = new AddField();
                    addFieldTool.in_table = pathToStreetsFC;
                    addFieldTool.field_name = "FT_" + timeZoneIDBaseFieldName;
                    addFieldTool.field_type = "SHORT";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_" + timeZoneIDBaseFieldName;
                    addFieldTool.field_type = "SHORT";
                    gp.Execute(addFieldTool, trackcancel);

                    // Calculate the TimeZoneID fields

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = pathToStreetsFC;
                    makeFeatureLayerTool.out_layer = "Streets_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "R_AREA_ID";
                    addJoinTool.join_table = drivingLTablePath;
                    addJoinTool.join_field = "AREA_ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the FT_" + timeZoneIDBaseFieldName + " field for left driving side roads...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".FT_" + timeZoneIDBaseFieldName;
                    calcFieldTool.code_block = TimeZoneUtilities.MakeTimeZoneIDCode(outputFileGdbPath, TimeZonesTableName, "MtdAreaDrivingL.SortableMSTIMEZONE");
                    calcFieldTool.expression = "tzID";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "MtdAreaDrivingL";
                    gp.Execute(removeJoinTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "L_AREA_ID";
                    addJoinTool.join_table = drivingRTablePath;
                    addJoinTool.join_field = "AREA_ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the FT_" + timeZoneIDBaseFieldName + " field for right driving side roads...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".FT_" + timeZoneIDBaseFieldName;
                    calcFieldTool.code_block = TimeZoneUtilities.MakeTimeZoneIDCode(outputFileGdbPath, TimeZonesTableName, "MtdAreaDrivingR.SortableMSTIMEZONE");
                    calcFieldTool.expression = "tzID";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "MtdAreaDrivingR";
                    gp.Execute(removeJoinTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "L_AREA_ID";
                    addJoinTool.join_table = drivingLTablePath;
                    addJoinTool.join_field = "AREA_ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the TF_" + timeZoneIDBaseFieldName + " field for left driving side roads...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".TF_" + timeZoneIDBaseFieldName;
                    calcFieldTool.code_block = TimeZoneUtilities.MakeTimeZoneIDCode(outputFileGdbPath, TimeZonesTableName, "MtdAreaDrivingL.SortableMSTIMEZONE");
                    calcFieldTool.expression = "tzID";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "MtdAreaDrivingL";
                    gp.Execute(removeJoinTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "R_AREA_ID";
                    addJoinTool.join_table = drivingRTablePath;
                    addJoinTool.join_field = "AREA_ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the TF_" + timeZoneIDBaseFieldName + " field for right driving side roads...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".TF_" + timeZoneIDBaseFieldName;
                    calcFieldTool.code_block = TimeZoneUtilities.MakeTimeZoneIDCode(outputFileGdbPath, TimeZonesTableName, "MtdAreaDrivingR.SortableMSTIMEZONE");
                    calcFieldTool.expression = "tzID";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "MtdAreaDrivingR";
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = drivingLTablePath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = drivingRTablePath;
                    gp.Execute(deleteTool, trackcancel);
                }
                else
                {
                    // Create a dummy TimeZones table and a dummy Streets feature class

                    CreateTable createTableTool = new CreateTable();
                    createTableTool.out_path = outputFileGdbPath;
                    createTableTool.out_name = TimeZonesTableName;
                    gp.Execute(createTableTool, trackcancel);

                    CreateFeatureclass createFCTool = new CreateFeatureclass();
                    createFCTool.out_path = outputFileGdbPath;
                    createFCTool.out_name = StreetsFCName;
                    createFCTool.geometry_type = "POLYLINE";
                    gp.Execute(createFCTool, trackcancel);
                }

                deleteTool = new Delete();
                deleteTool.in_data = mtdAreaTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Compact the output file geodatabase

                AddMessage("Compacting the output file geodatabase...", messages, trackcancel);

                Compact compactTool = new Compact();
                compactTool.in_workspace = outputFileGdbPath;
                gp.Execute(compactTool, trackcancel);
            }
            catch (Exception e)
            {
                if (gp.MaxSeverity == 2)
                {
                    object missing = System.Type.Missing;
                    messages.AddError(1, gp.GetMessages(ref missing));
                }
                messages.AddError(1, e.Message);
                messages.AddError(1, e.StackTrace);
            }
            finally
            {
                // Restore the original GP environment settings

                gpSettings.AddOutputsToMap = origAddOutputsToMapSetting;
                gpSettings.LogHistory = origLogHistorySetting;
            }
            GC.Collect();
            return;
        }
Exemplo n.º 19
0
        /// <summary>
        /// Returns a list of all of the workspaces in the C_DB_WORKSHEET_NAME worksheet.
        /// Workspaces that could not be opened will not be included.
        /// </summary>
        /// <returns></returns>
        public List <Common.WorkspaceInfo> GetWorkspacesFromSpreadsheet()
        {
            // Error checking: make sure that the required worksheet can be found
            if (!this.Open())
            {
                WmauError error = new WmauError(WmauErrorCodes.C_EXCEL_WORKBOOK_ERROR);
                m_gpMessages.AddError(error.ErrorCodeAsInt, error.Message);
                throw new Exception(error.Message);
            }
            Worksheet worksheet = this.GetWorksheetByName(C_DB_WORKSHEET_NAME);

            if (worksheet == null)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_EXCEL_WORKSHEET_ERROR);
                m_gpMessages.AddError(error.ErrorCodeAsInt, error.Message);
                throw new Exception(error.Message);
            }

            // Create the output element
            List <Common.WorkspaceInfo> workspaceList = new List <Common.WorkspaceInfo>();

            // Do some math and set up the "column header" range (used to determine which field is which)
            int    numColumns      = worksheet.UsedRange.Columns.Count;
            int    numRows         = worksheet.UsedRange.Rows.Count;
            int    dataStartRowNum = 2;
            int    dataEndRowNum   = dataStartRowNum + numRows;
            string headerStartCell = "A1";
            string headerEndCell   = GetExcelColumnFromIndex(numColumns) + "1";
            Range  headerRange     = worksheet.get_Range(headerStartCell, headerEndCell);

            // Iterate through all of the data rows (i.e., workspaces) in the worksheet
            for (int rowNum = dataStartRowNum; rowNum < dataEndRowNum; rowNum++)
            {
                string name = string.Empty;
                try
                {
                    // Create a range for this row of cells
                    string dataStartCell = "A" + rowNum.ToString();
                    string dataEndCell   = GetExcelColumnFromIndex(numColumns) + rowNum.ToString();
                    Range  dataRange     = worksheet.get_Range(dataStartCell, dataEndCell);

                    // Initialize the various values of the workspace configuration object
                    name = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_DB_ALIAS).Column);
                    if (string.IsNullOrEmpty(name))
                    {
                        // Stop reading through the worksheet once we've found a row without any workspace listed
                        break;
                    }

                    // Create the workspace configuration object that will be set up from this row
                    Common.WorkspaceInfo workspaceInfo = new Common.WorkspaceInfo();
                    workspaceInfo.Name                = name;
                    workspaceInfo.Server              = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_SERVER).Column);
                    workspaceInfo.Instance            = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_INSTANCE).Column);
                    workspaceInfo.Database            = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_DATABASE).Column);
                    workspaceInfo.Version             = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_VERSION).Column);
                    workspaceInfo.UseOsAuthentication = GetValueAsBool(dataRange, FindCellMatchingText(headerRange, C_OS_AUTH).Column);
                    workspaceInfo.UseIndividualLogins = GetValueAsBool(dataRange, FindCellMatchingText(headerRange, C_INDIVIDUAL_LOGINS).Column);
                    if (workspaceInfo.UseOsAuthentication)
                    {
                        if (workspaceInfo.UseIndividualLogins)
                        {
                            throw new Exception("Workspace '" + name + "': OS authentication and individual logins are mutually exclusive");
                        }

                        // Nothing else to do if OS authentication is being used
                    }
                    else
                    {
                        if (workspaceInfo.UseIndividualLogins)
                        {
                            List <Common.WorkspaceInfo.LoginInfo> loginList =
                                GetLoginInfoFromSpreadsheet(workspaceInfo.Name);
                            foreach (Common.WorkspaceInfo.LoginInfo login in loginList)
                            {
                                workspaceInfo.AddLogin(
                                    login.WmxUsername,
                                    login.DatabaseUsername,
                                    login.DatabasePassword,
                                    login.IsPasswordEncrypted);
                            }
                        }
                        else
                        {
                            string username    = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_USERNAME).Column);
                            string password    = GetValueAsText(dataRange, FindCellMatchingText(headerRange, C_PASSWORD).Column);
                            bool   isEncrypted = GetValueAsBool(dataRange, FindCellMatchingText(headerRange, C_IS_ENCRYPTED).Column);

                            // Only base64-encoded encrypted passwords are supported by this
                            // utility, so unencode the password before sending it along to
                            // the WMX object
                            if (isEncrypted)
                            {
                                byte[] pwBytes = System.Convert.FromBase64String(password);
                                password = System.Text.UTF8Encoding.UTF8.GetString(pwBytes);
                            }

                            // Store the shared username and password
                            workspaceInfo.AddLogin(string.Empty, username, password, isEncrypted);
                        }
                    }

                    workspaceList.Add(workspaceInfo);
                }
                catch (Exception ex)
                {
                    WmauError error  = new WmauError(WmauErrorCodes.C_WORKSPACE_FETCH_ERROR);
                    string    errMsg = error.Message;
                    errMsg += System.Environment.NewLine + ex.Message;
                    errMsg += System.Environment.NewLine + "Skipping workspace '" + name + "'";
                    m_gpMessages.AddError(error.ErrorCodeAsInt, errMsg);
                    continue;
                }
            }

            this.Close();
            return(workspaceList);
        }
Exemplo n.º 20
0
        // Execute: Execute the function given the array of the parameters
        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            IFeatureClass outputFeatureClass = null;
            try
            {
                // get the input feature class
                IGPMultiValue inputFeatureClasses_Parameter = (IGPMultiValue)m_GPUtilities.UnpackGPValue(paramvalues.get_Element(0));
                layer[] input_featureClasses = new layer[inputFeatureClasses_Parameter.Count];
                for (int i = 0; i < inputFeatureClasses_Parameter.Count; i++)
                {
                    IGPValue inputFeatureClass_Parameter = inputFeatureClasses_Parameter.get_Value(i);

                    IFeatureClass inputFeatureClass;
                    IQueryFilter inputQF;

                    m_GPUtilities.DecodeFeatureLayer(inputFeatureClass_Parameter, out inputFeatureClass, out inputQF);

                    input_featureClasses[i] = new layer() { featureclass = inputFeatureClass, qFilter = inputQF};
                }

                if (input_featureClasses.Length == 0 || input_featureClasses.Any(w=> w.featureclass == null))
                {
                    message.AddError(2, "Could not open one or more input dataset.");
                    return;
                }

                //IFields additionalFields = new FieldsClass();
                //additionalFields.AddField(FEATURE_SOURCE_FIELD_NAME, esriFieldType.esriFieldTypeString);
                //additionalFields.AddField(FEATURE_ID_FIELD_NAME, esriFieldType.esriFieldTypeInteger);
                //additionalFields.AddField(
                //    input_featureClasses[0].featureclass.Fields.get_Field(
                //    input_featureClasses[0].featureclass.Fields.FindField(
                //    input_featureClasses[0].featureclass.ShapeFieldName)));

                // create the output feature class
                IGPValue outputFeatureClass_Parameter = m_GPUtilities.UnpackGPValue(paramvalues.get_Element(1));
                outputFeatureClass = GPHelperFunctions.CreateFeatureClass(outputFeatureClass_Parameter, envMgr);

                if (outputFeatureClass == null)
                {
                    message.AddError(2, "Could not create output dataset.");
                    return;
                }

                IGPString curveTypeParameter = (IGPString)m_GPUtilities.UnpackGPValue(paramvalues.get_Element(2));
                ArcConstructionMethods method;
                if (!Enum.TryParse<ArcConstructionMethods>(curveTypeParameter.Value, true, out method))
                {
                    message.AddError(2, string.Format("The value {0} is not expected.  Expected values are: {1}.",
                        curveTypeParameter.Value,
                        string.Join(",", Enum.GetNames(typeof(ArcConstructionMethods)))));
                    return;
                }

                IStepProgressor stepPro = (IStepProgressor)trackcancel;
                GPHelperFunctions.dropSpatialIndex(outputFeatureClass);

                BoostVoronoi bv = new BoostVoronoi(100);

                double minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
                List<site_key> point_sites = new List<site_key>();
                List<site_key> segment_sites = new List<site_key>();

                for (short i = 0; i < input_featureClasses.Length; i++)
                {
                    layer l = input_featureClasses[i];
                    int featcount = l.featureclass.FeatureCount(l.qFilter);

                    stepPro.MinRange = 0;
                    stepPro.MaxRange = featcount;
                    stepPro.StepValue = (1);
                    stepPro.Message = "Reading features";
                    stepPro.Position = 0;
                    stepPro.Show();

                    IFeatureCursor cursor = null;
                    IFeature row = null;

                    try
                    {
                        cursor = l.featureclass.Search(l.qFilter, false);
                        while ((row = cursor.NextFeature()) != null)
                        {
                            stepPro.Step();
                            IPoint point = row.Shape as IPoint;
                            if (point != null)
                            {
                                double X = point.X;
                                double Y = point.Y;

                                minX = Math.Min(minX, X);
                                maxX = Math.Max(maxX, X);

                                minY = Math.Min(minY, Y);
                                maxY = Math.Max(maxY, Y);

                                bv.AddPoint(point.X, point.Y);
                                point_sites.Add(new site_key(i, row.OID));
                            }

                            IMultipoint multipoint = row.Shape as IMultipoint;
                            if (multipoint != null)
                            {
                                IPointCollection pointCollection = (IPointCollection)multipoint;
                                IEnumVertex vertices = pointCollection.EnumVertices;

                                IPoint vertex = null; int part, index;
                                vertices.Next(out vertex, out part, out index);

                                minX = Math.Min(minX, multipoint.Envelope.XMin);
                                maxX = Math.Max(maxX, multipoint.Envelope.XMax);

                                minY = Math.Min(minY, multipoint.Envelope.YMin);
                                maxY = Math.Max(maxY, multipoint.Envelope.YMax);

                                while (vertex != null)
                                {
                                    bv.AddPoint(vertex.X, vertex.Y);
                                    point_sites.Add(new site_key(i, row.OID));

                                    vertices.Next(out vertex, out part, out index);
                                }
                            }

                            IPolyline polyline = row.Shape as IPolyline;
                            if (polyline != null)
                            {
                                double fromX = polyline.FromPoint.X;
                                double fromY = polyline.FromPoint.Y;
                                double toX = polyline.ToPoint.X;
                                double toY = polyline.ToPoint.Y;

                                if (toX < fromX)
                                {
                                    minX = Math.Min(minX, toX);
                                    maxX = Math.Max(maxX, fromX);
                                }
                                else
                                {
                                    minX = Math.Min(minX, fromX);
                                    maxX = Math.Max(maxX, toX);
                                }

                                if (toY < fromY)
                                {
                                    minY = Math.Min(minY, toY);
                                    maxY = Math.Max(maxY, fromY);
                                }
                                else
                                {
                                    minY = Math.Min(minY, fromY);
                                    maxY = Math.Max(maxY, toY);
                                }

                                bv.AddSegment(
                                    polyline.FromPoint.X, polyline.FromPoint.Y,
                                    polyline.ToPoint.X, polyline.ToPoint.Y
                                );

                                segment_sites.Add(new site_key(i, row.OID));
                            }

                            Marshal.ReleaseComObject(row);
                        }
                    }
                    finally
                    {
                        if (row != null) Marshal.ReleaseComObject(row);
                        if (cursor != null) Marshal.ReleaseComObject(cursor);

                        stepPro.Hide();
                    }
                }

                message.AddMessage(String.Format("{0}, {1} -> {2}, {3}", minX, minY, maxX, maxY));

                int width = Math.Max((int)((maxX - minX) * 0.1), 1);
                int height = Math.Max((int)((maxY - minY) * 0.1), 1);

                maxX = maxX + width;
                minX = minX - width;
                maxY = maxY + height;
                minY = minY - height;

                message.AddMessage(String.Format("{0}, {1} -> {2}, {3}", minX, minY, maxX, maxY));
                bv.AddSegment(minX, minY, maxX, minY);
                segment_sites.Add(new site_key(-1, -1));
                bv.AddSegment(maxX, minY, maxX, maxY);
                segment_sites.Add(new site_key(-1, -1));
                bv.AddSegment(maxX, maxY, minX, maxY);
                segment_sites.Add(new site_key(-1, -1));
                bv.AddSegment(minX, maxY, minX, minY);
                segment_sites.Add(new site_key(-1, -1));

                stepPro.Message = "Solve Voronoi";
                stepPro.MaxRange = 0;
                stepPro.MaxRange = 0;
                stepPro.Show();

                bv.Construct();

                stepPro.Hide();

                int featureSourceIndx = outputFeatureClass.Fields.FindField(FEATURE_SOURCE_FIELD_NAME);
                int featureIDIndx = outputFeatureClass.Fields.FindField(FEATURE_ID_FIELD_NAME);

                IFeatureCursor inserts = null;
                IFeatureBuffer buffer = null;
                try
                {
                    object missing = Type.Missing;
                    ISpatialReference spatialReference = ((IGeoDataset)outputFeatureClass).SpatialReference;
                    inserts = outputFeatureClass.Insert(false);
                    buffer = outputFeatureClass.CreateFeatureBuffer();

                    List<Cell> cells = bv.Cells;
                    message.AddMessage(string.Format("{0} cells calculated", cells.Count));
                    List<Edge> edges = bv.Edges;
                    message.AddMessage(string.Format("{0} edges calculated", edges.Count));
                    List<Vertex> vertices = bv.Vertices;
                    message.AddMessage(string.Format("{0} vertexes calculated", vertices.Count));

                    stepPro.Message = "Write cells";
                    stepPro.MaxRange = 0;
                    stepPro.MaxRange = cells.Count;
                    stepPro.Show();

                    for (int cellIndex = 0; cellIndex < cells.Count; cellIndex++)
                    {
                        try
                        {
                            if(cellIndex % 5000 == 0) message.AddMessage(String.Format("{0}. {1} cells processed.", DateTime.Now, cellIndex));

                            Cell cell = cells[cellIndex];
                            int currentSite = cell.Site;
                            IGeometryCollection geometryCollection = new GeometryBagClass() { SpatialReference = spatialReference };

                            //ignores any sliver cells
                            if (cell.IsOpen || cell.EdgesIndex.Count < 3)
                                continue;

                            ISegmentCollection segmentCollection = createSegments(cell, bv, method, spatialReference);

                            if (((IArea)segmentCollection).Area <= 0)
                            {

                                message.AddMessage("A invalid geometry has been detected, try reversing the orientation.");
                                ISegmentCollection reversed_segmentCollection = new PolygonClass() { SpatialReference = spatialReference };
                                for (int i = segmentCollection.SegmentCount - 1; i >= 0; i--)
                                {
                                    ISegment segment = (ISegment)segmentCollection.get_Segment(i);
                                    segment.ReverseOrientation();
                                    reversed_segmentCollection.AddSegment(segment);
                                }
                                segmentCollection = reversed_segmentCollection;
                            }

                            ((IPolygon)segmentCollection).SpatialReference = spatialReference;
                            if (((IArea)segmentCollection).Area <= 0)
                            {
                                message.AddWarning("An empty shell has been created");

                                for (int i = 0; i < segmentCollection.SegmentCount; i++)
                                {
                                    ISegment segment = (ISegment)segmentCollection.get_Segment(i);
                                    message.AddMessage(String.Format("From {0}, {1} To {2},{3}",
                                    segment.FromPoint.X, segment.FromPoint.Y,
                                    segment.ToPoint.X, segment.ToPoint.Y));
                                }

                            }

                            //set attributes
                            site_key sk = (currentSite >= point_sites.Count) ? segment_sites[currentSite - point_sites.Count] : point_sites[currentSite];
                            if (!sk.isEmpty)
                            {
                                buffer.set_Value(featureSourceIndx, input_featureClasses[sk.featureClassIndex].featureclass.AliasName);
                                buffer.set_Value(featureIDIndx, sk.objectID);
                            }
                            else
                            {
                                buffer.set_Value(featureSourceIndx, DBNull.Value);
                                buffer.set_Value(featureIDIndx, DBNull.Value);
                            }

                            IPolygon voronoiPolygon = (IPolygon)segmentCollection;
                            buffer.Shape = (IPolygon)voronoiPolygon;
                            inserts.InsertFeature(buffer);
                        }
                        catch (Exception e)
                        {
                            message.AddWarning("Failed to create a cell");
                        }
                    }
                }
                finally
                {
                    if (buffer != null) Marshal.ReleaseComObject(buffer);
                    if (inserts != null) Marshal.ReleaseComObject(inserts);
                }

                GPHelperFunctions.createSpatialIndex(outputFeatureClass);

            }
            catch (Exception exx)
            {
                message.AddError(2, exx.Message);
                message.AddMessage(exx.ToString());
            }
            finally
            {
                if (outputFeatureClass != null) Marshal.ReleaseComObject(outputFeatureClass);

                ((IProgressor)trackcancel).Hide();
            }
        }
        public void Execute(IArray paramvalues, ITrackCancel trackcancel,
                            IGPEnvironmentManager envMgr, IGPMessages messages)
        {
            // Remember the original GP environment settings and temporarily override these settings

            var gpSettings = envMgr as IGeoProcessorSettings;
            bool origAddOutputsToMapSetting = gpSettings.AddOutputsToMap;
            bool origLogHistorySetting = gpSettings.LogHistory;
            gpSettings.AddOutputsToMap = false;
            gpSettings.LogHistory = false;

            // Create the Geoprocessor

            Geoprocessor gp = new Geoprocessor();

            try
            {
                // Validate our values

                IGPMessages validateMessages = ((IGPFunction2)this).Validate(paramvalues, false, envMgr);
                if ((validateMessages as IGPMessage).IsError())
                {
                    messages.AddError(1, "Validate failed");
                    return;
                }

                // Unpack values

                IGPParameter gpParam = paramvalues.get_Element(InputStreetsFeatureClass) as IGPParameter;
                IGPValue inputStreetsFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputAltStreetsFeatureClass) as IGPParameter;
                IGPValue inputAltStreetsFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputZLevelsFeatureClass) as IGPParameter;
                IGPValue inputZLevelsFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCdmsTable) as IGPParameter;
                IGPValue inputCdmsTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputRdmsTable) as IGPParameter;
                IGPValue inputRdmsTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputSignsTable) as IGPParameter;
                IGPValue inputSignsTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(OutputFileGDB) as IGPParameter;
                IGPValue outputFileGDBValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(OutputFileGDBVersion) as IGPParameter;
                IGPValue outputFileGDBVersionValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputFeatureDatasetName) as IGPParameter;
                IGPValue inputFeatureDatasetNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputNetworkDatasetName) as IGPParameter;
                IGPValue inputNetworkDatasetNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCreateNetworkAttributesInMetric) as IGPParameter;
                IGPValue inputCreateNetworkAttributeInMetricValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCreateArcGISOnlineNetworkAttributes) as IGPParameter;
                IGPValue inputCreateArcGISOnlineNetworkAttributesValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTimeZoneIDBaseFieldName) as IGPParameter;
                IGPValue inputTimeZoneIDBaseFieldNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTimeZoneTable) as IGPParameter;
                IGPValue inputTimeZoneTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCommonTimeZoneForTheEntireDataset) as IGPParameter;
                IGPValue inputCommonTimeZoneForTheEntireDatasetValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTrafficTable) as IGPParameter;
                IGPValue inputTrafficTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLinkReferenceFileFC14) as IGPParameter;
                IGPValue inputLinkReferenceFileFC14Value = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLinkReferenceFileFC5) as IGPParameter;
                IGPValue inputLinkReferenceFileFC5Value = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTMCReferenceFile) as IGPParameter;
                IGPValue inputTMCReferenceFileValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputSPDFile) as IGPParameter;
                IGPValue inputSPDFileValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedFolder) as IGPParameter;
                IGPValue inputLiveTrafficFeedFolderValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedArcGISServerConnection) as IGPParameter;
                IGPValue inputLiveTrafficFeedArcGISServerConnectionValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedGeoprocessingServiceName) as IGPParameter;
                IGPValue inputLiveTrafficFeedGeoprocessingServiceNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedGeoprocessingTaskName) as IGPParameter;
                IGPValue inputLiveTrafficFeedGeoprocessingTaskNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputUSCndModTable) as IGPParameter;
                IGPValue inputUSCndModTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputNonUSCndModTable) as IGPParameter;
                IGPValue inputNonUSCndModTableValue = m_gpUtils.UnpackGPValue(gpParam);

                double fgdbVersion = 0.0;
                if (!(outputFileGDBVersionValue.IsEmpty()))
                    fgdbVersion = Convert.ToDouble(outputFileGDBVersionValue.GetAsText(), System.Globalization.CultureInfo.InvariantCulture);

                bool createNetworkAttributesInMetric = false;
                if (!(inputCreateNetworkAttributeInMetricValue.IsEmpty()))
                    createNetworkAttributesInMetric = ((inputCreateNetworkAttributeInMetricValue.GetAsText()).ToUpper() == "TRUE");

                bool createArcGISOnlineNetworkAttributes = false;
                if (!(inputCreateArcGISOnlineNetworkAttributesValue.IsEmpty()))
                    createArcGISOnlineNetworkAttributes = ((inputCreateArcGISOnlineNetworkAttributesValue.GetAsText()).ToUpper() == "TRUE");

                if (inputTimeZoneIDBaseFieldNameValue.IsEmpty() ^ inputTimeZoneTableValue.IsEmpty())
                {
                    messages.AddError(1, "The Time Zone ID Base Field Name and the Time Zone Table must be specified together.");
                    return;
                }

                string timeZoneIDBaseFieldName = "";
                bool directedTimeZoneIDFields = false;
                if (!(inputTimeZoneIDBaseFieldNameValue.IsEmpty()))
                {
                    timeZoneIDBaseFieldName = inputTimeZoneIDBaseFieldNameValue.GetAsText();
                    IDETable inputTable = m_gpUtils.DecodeDETable(inputStreetsFeatureClassValue);
                    if (inputTable.Fields.FindField(timeZoneIDBaseFieldName) == -1)
                    {
                        directedTimeZoneIDFields = true;
                        if (((inputTable.Fields.FindField("FT_" + timeZoneIDBaseFieldName) == -1) || (inputTable.Fields.FindField("TF_" + timeZoneIDBaseFieldName) == -1)))
                        {
                            messages.AddError(1, "Field named " + timeZoneIDBaseFieldName + " does not exist, nor the pair of fields with FT_ and TF_ prefixing " + timeZoneIDBaseFieldName + " does not exist.");
                            return;
                        }
                    }
                }

                string commonTimeZone = "";
                if (!(inputCommonTimeZoneForTheEntireDatasetValue.IsEmpty()))
                    commonTimeZone = inputCommonTimeZoneForTheEntireDatasetValue.GetAsText();

                if (inputLinkReferenceFileFC14Value.IsEmpty() ^ inputLinkReferenceFileFC5Value.IsEmpty())
                {
                    messages.AddError(1, "Both Traffic Patterns Link Reference .csv files must be specified together.");
                    return;
                }

                bool usesHistoricalTraffic = !(inputSPDFileValue.IsEmpty());
                bool usesNTPFullCoverage = !(inputLinkReferenceFileFC5Value.IsEmpty());
                if ((inputTMCReferenceFileValue.IsEmpty() & !usesNTPFullCoverage) ^ inputSPDFileValue.IsEmpty())
                {
                    messages.AddError(1, "The Traffic Patterns SPD .csv file must be specified with the Traffic Patterns Link/TMC Reference .csv file(s).");
                    return;
                }

                if (inputTrafficTableValue.IsEmpty() & !inputTMCReferenceFileValue.IsEmpty())
                {
                    messages.AddError(1, "If the TMC Reference .csv file is specified, then the Traffic table must also be specified.");
                    return;
                }

                bool agsConnectionIsEmpty = inputLiveTrafficFeedArcGISServerConnectionValue.IsEmpty();
                bool gpServiceNameIsEmpty = inputLiveTrafficFeedGeoprocessingServiceNameValue.IsEmpty();
                bool gpTaskNameIsEmpty = inputLiveTrafficFeedGeoprocessingTaskNameValue.IsEmpty();
                if ((agsConnectionIsEmpty | gpServiceNameIsEmpty | gpTaskNameIsEmpty) ^
                    (agsConnectionIsEmpty & gpServiceNameIsEmpty & gpTaskNameIsEmpty))
                {
                    messages.AddError(1, "The ArcGIS Server Connection, Geoprocessing Service Name, and Geoprocessing Task Name must all be specified together.");
                    return;
                }

                bool feedFolderIsEmpty = inputLiveTrafficFeedFolderValue.IsEmpty();
                if (!(feedFolderIsEmpty | agsConnectionIsEmpty))
                {
                    messages.AddError(1, "The live traffic feed folder and live traffic feed connection cannot be specified together.");
                    return;
                }

                if (inputTrafficTableValue.IsEmpty() & !(feedFolderIsEmpty & agsConnectionIsEmpty))
                {
                    messages.AddError(1, "The Traffic table must be specified to use Live Traffic.");
                    return;
                }

                bool usesTransport = true;
                if (inputUSCndModTableValue.IsEmpty() && inputNonUSCndModTableValue.IsEmpty())
                    usesTransport = false;

                ITrafficFeedLocation trafficFeedLocation = null;
                if (!agsConnectionIsEmpty)
                {
                    // We're using an ArcGIS Server Connection and Geoprocessing Service/Task

                    ITrafficFeedGPService tfgps = new TrafficFeedGPServiceClass();
                    IName trafficFeedGPServiceName = m_gpUtils.GetNameObject(inputLiveTrafficFeedArcGISServerConnectionValue as IDataElement);
                    tfgps.ConnectionProperties = ((IAGSServerConnectionName)trafficFeedGPServiceName).ConnectionProperties;
                    tfgps.ServiceName = inputLiveTrafficFeedGeoprocessingServiceNameValue.GetAsText();
                    tfgps.TaskName = inputLiveTrafficFeedGeoprocessingTaskNameValue.GetAsText();
                    trafficFeedLocation = tfgps as ITrafficFeedLocation;
                }
                else if (!feedFolderIsEmpty)
                {
                    // We're using a Traffic Feed Folder

                    ITrafficFeedDirectory tfd = new TrafficFeedDirectoryClass();
                    tfd.TrafficDirectory = inputLiveTrafficFeedFolderValue.GetAsText();
                    trafficFeedLocation = tfd as ITrafficFeedLocation;
                }

                // Get the path to the output file GDB, and feature dataset and network dataset names

                string outputFileGdbPath = outputFileGDBValue.GetAsText();
                string fdsName = inputFeatureDatasetNameValue.GetAsText();
                string ndsName = inputNetworkDatasetNameValue.GetAsText();

                // Create the new file geodatabase and feature dataset

                AddMessage("Creating the file geodatabase and feature dataset...", messages, trackcancel);

                int lastBackslash = outputFileGdbPath.LastIndexOf("\\");
                CreateFileGDB createFGDBTool = new CreateFileGDB();
                createFGDBTool.out_folder_path = outputFileGdbPath.Remove(lastBackslash);
                createFGDBTool.out_name = outputFileGdbPath.Substring(lastBackslash + 1);
                createFGDBTool.out_version = "9.3";
                gp.Execute(createFGDBTool, trackcancel);

                CreateFeatureDataset createFDSTool = new CreateFeatureDataset();
                createFDSTool.out_dataset_path = outputFileGdbPath;
                createFDSTool.out_name = fdsName;
                createFDSTool.spatial_reference = inputStreetsFeatureClassValue.GetAsText();
                gp.Execute(createFDSTool, trackcancel);

                // Import the Streets feature class to the file geodatabase
                // If we're using ArcInfo, also sort the feature class

                string pathToFds = outputFileGdbPath + "\\" + fdsName;
                string streetsFeatureClassPath = pathToFds + "\\" + StreetsFCName;

                IAoInitialize aoi = new AoInitializeClass();
                if (aoi.InitializedProduct() == esriLicenseProductCode.esriLicenseProductCodeAdvanced)
                {
                    AddMessage("Importing and spatially sorting the Streets feature class...", messages, trackcancel);

                    Sort sortTool = new Sort();
                    sortTool.in_dataset = inputStreetsFeatureClassValue.GetAsText();
                    sortTool.out_dataset = streetsFeatureClassPath;
                    sortTool.sort_field = "Shape";
                    sortTool.spatial_sort_method = "PEANO";
                    gp.Execute(sortTool, trackcancel);
                }
                else
                {
                    AddMessage("Importing the Streets feature class...", messages, trackcancel);

                    FeatureClassToFeatureClass importFCTool = new FeatureClassToFeatureClass();
                    importFCTool.in_features = inputStreetsFeatureClassValue.GetAsText();
                    importFCTool.out_path = pathToFds;
                    importFCTool.out_name = StreetsFCName;
                    gp.Execute(importFCTool, trackcancel);
                }

                // Add an index to the Streets feature class's LINK_ID field

                AddMessage("Indexing the LINK_ID field...", messages, trackcancel);

                AddIndex addIndexTool = new AddIndex();
                addIndexTool.in_table = streetsFeatureClassPath;
                addIndexTool.fields = "LINK_ID";
                addIndexTool.index_name = "LINK_ID";
                gp.Execute(addIndexTool, trackcancel);

                // Add the alternate street name fields to the Streets feature class

                AddMessage("Creating fields on the Streets feature class for the alternate street names...", messages, trackcancel);

                AddField addFieldTool = new AddField();
                addFieldTool.in_table = streetsFeatureClassPath;

                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 240;
                addFieldTool.field_name = "ST_NAME_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 3;
                addFieldTool.field_name = "ST_LANGCD_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 6;
                addFieldTool.field_name = "ST_NM_PREF_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 90;
                addFieldTool.field_name = "ST_TYP_BEF_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 105;
                addFieldTool.field_name = "ST_NM_BASE_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 6;
                addFieldTool.field_name = "ST_NM_SUFF_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 90;
                addFieldTool.field_name = "ST_TYP_AFT_Alt";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_length = 1;
                addFieldTool.field_name = "DIRONSIGN_Alt";
                gp.Execute(addFieldTool, trackcancel);

                // Extract the explicatable street names

                string ExplicatblTablePath = outputFileGdbPath + "\\Explicatbl";

                AddMessage("Extracting the explicatable street names...", messages, trackcancel);

                TableSelect tableSelectTool = new TableSelect();
                tableSelectTool.in_table = inputAltStreetsFeatureClassValue.GetAsText();
                tableSelectTool.out_table = ExplicatblTablePath;
                tableSelectTool.where_clause = "EXPLICATBL = 'Y'";
                gp.Execute(tableSelectTool, trackcancel);

                // Determine which explicatable names are alternate names and extract them

                MakeTableView makeTableViewTool = new MakeTableView();
                makeTableViewTool.in_table = ExplicatblTablePath;
                makeTableViewTool.out_view = "Explicatbl_View";
                gp.Execute(makeTableViewTool, trackcancel);

                AddJoin addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Explicatbl_View";
                addJoinTool.in_field = "LINK_ID";
                addJoinTool.join_table = streetsFeatureClassPath;
                addJoinTool.join_field = "LINK_ID";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Extracting the alternate street names...", messages, trackcancel);

                TableToTable importTableTool = new TableToTable();
                importTableTool.in_rows = "Explicatbl_View";
                importTableTool.out_path = outputFileGdbPath;
                importTableTool.out_name = "AltNames";
                importTableTool.where_clause = "Explicatbl.ST_TYP_BEF <> " + StreetsFCName + ".ST_TYP_BEF OR " +
                                               "Explicatbl.ST_NM_BASE <> " + StreetsFCName + ".ST_NM_BASE OR " +
                                               "Explicatbl.ST_TYP_AFT <> " + StreetsFCName + ".ST_TYP_AFT";
                importTableTool.field_mapping = "LINK_ID \"LINK_ID\" true true false 4 Long 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.LINK_ID,-1,-1;" +
                                                "ST_NAME \"ST_NAME\" true true false 240 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_NAME,-1,-1;" +
                                                "ST_LANGCD \"ST_LANGCD\" true true false 3 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_LANGCD,-1,-1;" +
                                                "ST_NM_PREF \"ST_NM_PREF\" true true false 6 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_NM_PREF,-1,-1;" +
                                                "ST_TYP_BEF \"ST_TYP_BEF\" true true false 90 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_TYP_BEF,-1,-1;" +
                                                "ST_NM_BASE \"ST_NM_BASE\" true true false 105 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_NM_BASE,-1,-1;" +
                                                "ST_NM_SUFF \"ST_NM_SUFF\" true true false 6 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_NM_SUFF,-1,-1;" +
                                                "ST_TYP_AFT \"ST_TYP_AFT\" true true false 90 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.ST_TYP_AFT,-1,-1;" +
                                                "DIRONSIGN \"DIRONSIGN\" true true false 1 Text 0 0 ,First,#," + ExplicatblTablePath + ",Explicatbl.DIRONSIGN,-1,-1";
                gp.Execute(importTableTool, trackcancel);

                RemoveJoin removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Explicatbl_View";
                removeJoinTool.join_name = StreetsFCName;
                gp.Execute(removeJoinTool, trackcancel);

                Delete deleteTool = new Delete();
                deleteTool.in_data = "Explicatbl_View";
                gp.Execute(deleteTool, trackcancel);
                deleteTool.in_data = ExplicatblTablePath;
                gp.Execute(deleteTool, trackcancel);

                string AltNamesTablePath = outputFileGdbPath + "\\AltNames";

                addIndexTool = new AddIndex();
                addIndexTool.in_table = AltNamesTablePath;
                addIndexTool.fields = "LINK_ID";
                addIndexTool.index_name = "LINK_ID";
                gp.Execute(addIndexTool, trackcancel);

                // Calculate the alternate street name fields

                MakeFeatureLayer makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                makeFeatureLayerTool.out_layer = "Streets_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Streets_Layer";
                addJoinTool.in_field = "LINK_ID";
                addJoinTool.join_table = AltNamesTablePath;
                addJoinTool.join_field = "LINK_ID";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Copying over the alternate ST_NAME values...", messages, trackcancel);

                CalculateField calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "Streets_Layer";
                calcFieldTool.expression_type = "VB";
                calcFieldTool.field = StreetsFCName + ".ST_NAME_Alt";
                calcFieldTool.expression = "[AltNames.ST_NAME]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate ST_LANGCD values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".ST_LANGCD_Alt";
                calcFieldTool.expression = "[AltNames.ST_LANGCD]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate ST_NM_PREF values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".ST_NM_PREF_Alt";
                calcFieldTool.expression = "[AltNames.ST_NM_PREF]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate ST_TYP_BEF values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".ST_TYP_BEF_Alt";
                calcFieldTool.expression = "[AltNames.ST_TYP_BEF]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate ST_NM_BASE values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".ST_NM_BASE_Alt";
                calcFieldTool.expression = "[AltNames.ST_NM_BASE]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate ST_NM_SUFF values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".ST_NM_SUFF_Alt";
                calcFieldTool.expression = "[AltNames.ST_NM_SUFF]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate ST_TYP_AFT values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".ST_TYP_AFT_Alt";
                calcFieldTool.expression = "[AltNames.ST_TYP_AFT]";
                gp.Execute(calcFieldTool, trackcancel);

                AddMessage("Copying over the alternate DIRONSIGN values...", messages, trackcancel);

                calcFieldTool.field = StreetsFCName + ".DIRONSIGN_Alt";
                calcFieldTool.expression = "[AltNames.DIRONSIGN]";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Streets_Layer";
                removeJoinTool.join_name = "AltNames";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "Streets_Layer";
                gp.Execute(deleteTool, trackcancel);
                deleteTool.in_data = AltNamesTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Add fields for the Z-Level values to the Streets feature class

                AddMessage("Creating fields on the Streets feature class for the Z-Level values...", messages, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = streetsFeatureClassPath;

                addFieldTool.field_type = "SHORT";
                addFieldTool.field_name = "F_ZLEV";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_name = "T_ZLEV";
                gp.Execute(addFieldTool, trackcancel);

                // Separate out the From and To Z-Level values into separate tables and index the LINK_ID fields

                string FromZsTablePath = outputFileGdbPath + "\\FromZs";
                string ToZsTablePath = outputFileGdbPath + "\\ToZs";

                AddMessage("Extracting the From Z-Level information...", messages, trackcancel);

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = inputZLevelsFeatureClassValue.GetAsText();
                tableSelectTool.out_table = FromZsTablePath;
                tableSelectTool.where_clause = "INTRSECT = 'Y' AND POINT_NUM = 1";
                gp.Execute(tableSelectTool, trackcancel);

                AddMessage("Extracting the To Z-Level information...", messages, trackcancel);

                tableSelectTool.out_table = ToZsTablePath;
                tableSelectTool.where_clause = "INTRSECT = 'Y' AND POINT_NUM > 1";
                gp.Execute(tableSelectTool, trackcancel);

                AddMessage("Analyzing the Z-Level information...", messages, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = FromZsTablePath;
                addIndexTool.fields = "LINK_ID";
                addIndexTool.index_name = "LINK_ID";
                gp.Execute(addIndexTool, trackcancel);
                addIndexTool.in_table = ToZsTablePath;
                gp.Execute(addIndexTool, trackcancel);

                // Calculate the Z-Level fields

                makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                makeFeatureLayerTool.out_layer = "Streets_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Streets_Layer";
                addJoinTool.in_field = "LINK_ID";
                addJoinTool.join_table = FromZsTablePath;
                addJoinTool.join_field = "LINK_ID";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Copying over the F_ZLEV values...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "Streets_Layer";
                calcFieldTool.field = StreetsFCName + ".F_ZLEV";
                calcFieldTool.expression = "[FromZs.Z_LEVEL]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Streets_Layer";
                removeJoinTool.join_name = "FromZs";
                gp.Execute(removeJoinTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Streets_Layer";
                addJoinTool.in_field = "LINK_ID";
                addJoinTool.join_table = ToZsTablePath;
                addJoinTool.join_field = "LINK_ID";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Copying over the T_ZLEV values...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "Streets_Layer";
                calcFieldTool.field = StreetsFCName + ".T_ZLEV";
                calcFieldTool.expression = "[ToZs.Z_LEVEL]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Streets_Layer";
                removeJoinTool.join_name = "ToZs";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "Streets_Layer";
                gp.Execute(deleteTool, trackcancel);

                deleteTool.in_data = FromZsTablePath;
                gp.Execute(deleteTool, trackcancel);
                deleteTool.in_data = ToZsTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Add fields for the distance (Meters), language, and speed (KPH) values to the Streets feature class

                AddMessage("Creating fields on the Streets feature class for distance, speed, and language...", messages, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = streetsFeatureClassPath;

                addFieldTool.field_type = "DOUBLE";
                addFieldTool.field_name = "Meters";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_type = "FLOAT";
                addFieldTool.field_name = "KPH";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_name = "Language";
                addFieldTool.field_length = 2;
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_name = "Language_Alt";
                addFieldTool.field_length = 2;
                gp.Execute(addFieldTool, trackcancel);

                // Calculate the distance (Meters), language, and speed (KPH) fields

                AddMessage("Calculating the distance, speed, and language fields...", messages, trackcancel);

                CalculateMetersKPHAndLanguageFields(outputFileGdbPath);

                if (createArcGISOnlineNetworkAttributes)
                {
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_AUTO", streetsFeatureClassPath, gp, messages, trackcancel);
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_BUS", streetsFeatureClassPath, gp, messages, trackcancel);
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_TAXIS", streetsFeatureClassPath, gp, messages, trackcancel);
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_TRUCKS", streetsFeatureClassPath, gp, messages, trackcancel);
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_DELIV", streetsFeatureClassPath, gp, messages, trackcancel);
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_EMERVEH", streetsFeatureClassPath, gp, messages, trackcancel);
                    CreateAndPopulateDirectionalVehicleAccessFields("AR_MOTOR", streetsFeatureClassPath, gp, messages, trackcancel);
                }

                // Add a field to the Streets feature class indicating roads closed for construction

                AddMessage("Creating the ClosedForConstruction field on the Streets feature class...", messages, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = streetsFeatureClassPath;
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_name = "ClosedForConstruction";
                addFieldTool.field_length = 1;
                gp.Execute(addFieldTool, trackcancel);

                // Create a table for looking up roads closed for construction

                AddMessage("Creating and indexing the table containing roads closed for construction...", messages, trackcancel);

                string closedForConstructionTablePath = outputFileGdbPath + "\\ClosedForConstruction";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = inputCdmsTableValue.GetAsText();
                tableSelectTool.out_table = closedForConstructionTablePath;
                tableSelectTool.where_clause = "COND_TYPE = 3";
                gp.Execute(tableSelectTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = closedForConstructionTablePath;
                addIndexTool.fields = "LINK_ID";
                addIndexTool.index_name = "LINK_ID";
                gp.Execute(addIndexTool, trackcancel);

                makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                makeFeatureLayerTool.out_layer = "Streets_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Streets_Layer";
                addJoinTool.in_field = "LINK_ID";
                addJoinTool.join_table = closedForConstructionTablePath;
                addJoinTool.join_field = "LINK_ID";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the ClosedForConstruction field...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "Streets_Layer";
                calcFieldTool.field = "Streets.ClosedForConstruction";
                calcFieldTool.expression = "\"Y\"";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Streets_Layer";
                removeJoinTool.join_name = "ClosedForConstruction";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "Streets_Layer";
                gp.Execute(deleteTool, trackcancel);
                deleteTool.in_data = closedForConstructionTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Add fields to the Streets feature class indicating roads with usage fees

                string[] arFieldSuffixes = new string[] { "AUTO", "BUS", "TAXIS", "CARPOOL", "PEDSTRN",
                                                          "TRUCKS", "THRUTR", "DELIVER", "EMERVEH", "MOTOR" };

                AddMessage("Creating the UFR fields on the Streets feature class...", messages, trackcancel);

                foreach (string arFieldSuffix in arFieldSuffixes)
                {
                    addFieldTool = new AddField();
                    addFieldTool.in_table = streetsFeatureClassPath;
                    addFieldTool.field_name = "UFR_" + arFieldSuffix;
                    addFieldTool.field_type = "TEXT";
                    addFieldTool.field_length = 1;
                    gp.Execute(addFieldTool, trackcancel);
                }

                // Create a table for looking up roads with usage fees

                AddMessage("Creating and indexing the table containing roads with usage fees...", messages, trackcancel);

                string usageFeeRequiredTablePath = outputFileGdbPath + "\\UsageFeeRequired";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = inputCdmsTableValue.GetAsText();
                tableSelectTool.out_table = usageFeeRequiredTablePath;
                tableSelectTool.where_clause = "COND_TYPE = 12";
                gp.Execute(tableSelectTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = usageFeeRequiredTablePath;
                addIndexTool.fields = "LINK_ID";
                addIndexTool.index_name = "LINK_ID";
                gp.Execute(addIndexTool, trackcancel);

                makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                makeFeatureLayerTool.out_layer = "Streets_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Streets_Layer";
                addJoinTool.in_field = "LINK_ID";
                addJoinTool.join_table = usageFeeRequiredTablePath;
                addJoinTool.join_field = "LINK_ID";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the ClosedForConstruction field...", messages, trackcancel);

                foreach (string arFieldSuffix in arFieldSuffixes)
                {
                    AddMessage("Calculating the UFR_" + arFieldSuffix + " field on the Streets feature class...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = "Streets.UFR_" + arFieldSuffix;
                    calcFieldTool.expression = "[UsageFeeRequired.AR_" + arFieldSuffix + "]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);
                }

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Streets_Layer";
                removeJoinTool.join_name = "UsageFeeRequired";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "Streets_Layer";
                gp.Execute(deleteTool, trackcancel);
                deleteTool.in_data = usageFeeRequiredTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Copy the time zones table to the file geodatabase

                if (timeZoneIDBaseFieldName != "")
                {
                    AddMessage("Copying the TimeZones table...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = inputTimeZoneTableValue.GetAsText();
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = "TimeZones";
                    gp.Execute(importTableTool, trackcancel);
                }

                #region Process Historical Traffic Tables

                if (usesHistoricalTraffic)
                {
                    // Create the Patterns table and index its PatternID field

                    AddMessage("Creating the Patterns table...", messages, trackcancel);

                    string listOfConstantPatterns = CreatePatternsTable(inputSPDFileValue.GetAsText(), outputFileGdbPath, fgdbVersion);

                    string patternsTablePath = outputFileGdbPath + "\\" + ProfilesTableName;

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = patternsTablePath;
                    addIndexTool.fields = "PatternID";
                    addIndexTool.index_name = "PatternID";
                    gp.Execute(addIndexTool, trackcancel);

                    string histTrafficJoinTablePath = outputFileGdbPath + "\\" + HistTrafficJoinTableName;

                    // Add fields for the average speeds to the Streets feature class

                    AddMessage("Creating fields for the average speeds and travel times...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = streetsFeatureClassPath;

                    addFieldTool.field_type = "FLOAT";
                    addFieldTool.field_name = "FT_AverageSpeed";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_AverageSpeed";
                    gp.Execute(addFieldTool, trackcancel);

                    if (usesNTPFullCoverage)
                    {
                        // Convert the Link Reference files to a FileGDB table

                        AddMessage("Converting the Link Reference files to a table...", messages, trackcancel);

                        ConvertLinkReferenceFilesToFGDBTable(inputLinkReferenceFileFC14Value.GetAsText(),
                                                             inputLinkReferenceFileFC5Value.GetAsText(),
                                                             outputFileGdbPath);

                        string referenceTablePath = outputFileGdbPath + "\\TempRefTable";

                        // Extract out the rows we want in our historical traffic table

                        AddMessage("Creating the historical traffic join table...", messages, trackcancel);

                        if (listOfConstantPatterns == "")
                        {
                            Rename renameTool = new Rename();
                            renameTool.in_data = referenceTablePath;
                            renameTool.out_data = histTrafficJoinTablePath;
                            gp.Execute(renameTool, trackcancel);
                        }
                        else
                        {
                            tableSelectTool = new TableSelect();
                            tableSelectTool.in_table = referenceTablePath;
                            tableSelectTool.out_table = histTrafficJoinTablePath;
                            tableSelectTool.where_clause = "NOT ( IS_FC5 = 'Y' AND U IN (" + listOfConstantPatterns +
                                                           ") AND U = M AND M = T AND T = W AND W = R AND R = F AND F = S )";
                            gp.Execute(tableSelectTool, trackcancel);

                            deleteTool = new Delete();
                            deleteTool.in_data = referenceTablePath;
                            gp.Execute(deleteTool, trackcancel);
                        }
                    }
                    else
                    {
                        // Convert the TMC Reference file to a FileGDB table and index its TMC field

                        AddMessage("Converting the TMC Reference file to a table...", messages, trackcancel);

                        ConvertTMCReferenceFileToFGDBTable(inputTMCReferenceFileValue.GetAsText(), outputFileGdbPath);

                        string referenceTablePath = outputFileGdbPath + "\\TempRefTable";

                        addIndexTool = new AddIndex();
                        addIndexTool.in_table = referenceTablePath;
                        addIndexTool.fields = "TMC";
                        addIndexTool.index_name = "TMC";
                        gp.Execute(addIndexTool, trackcancel);

                        // Copy over the Traffic table to the file geodatabase

                        AddMessage("Copying Traffic table to the file geodatabase...", messages, trackcancel);

                        importTableTool = new TableToTable();
                        importTableTool.in_rows = inputTrafficTableValue.GetAsText();
                        importTableTool.out_path = outputFileGdbPath;
                        importTableTool.out_name = "Street";
                        gp.Execute(importTableTool, trackcancel);

                        string trafficTablePath = outputFileGdbPath + "\\Street";

                        // Add the TMC field and calculate it

                        AddMessage("Calculating TMC values for historical traffic...", messages, trackcancel);

                        addFieldTool = new AddField();
                        addFieldTool.in_table = trafficTablePath;
                        addFieldTool.field_type = "TEXT";
                        addFieldTool.field_length = 9;
                        addFieldTool.field_name = "TMC";
                        gp.Execute(addFieldTool, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = trafficTablePath;
                        calcFieldTool.field = "TMC";
                        calcFieldTool.expression = "Replace(Replace(Right([TRAFFIC_CD], 9), \"+\", \"P\"), \"-\", \"N\")";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);

                        AddMessage("Creating the historical traffic join table...", messages, trackcancel);

                        // Join the Traffic table to the Reference table...

                        makeTableViewTool = new MakeTableView();
                        makeTableViewTool.in_table = trafficTablePath;
                        makeTableViewTool.out_view = "Traffic_View";
                        gp.Execute(makeTableViewTool, trackcancel);

                        addJoinTool = new AddJoin();
                        addJoinTool.in_layer_or_view = "Traffic_View";
                        addJoinTool.in_field = "TMC";
                        addJoinTool.join_table = referenceTablePath;
                        addJoinTool.join_field = "TMC";
                        addJoinTool.join_type = "KEEP_COMMON";
                        gp.Execute(addJoinTool, trackcancel);

                        // ...then create the Streets_Patterns table by copying out the joined rows

                        importTableTool = new TableToTable();
                        importTableTool.in_rows = "Traffic_View";
                        importTableTool.out_path = outputFileGdbPath;
                        importTableTool.out_name = HistTrafficJoinTableName;
                        importTableTool.field_mapping = "LINK_ID \"LINK_ID\" true true false 4 Long 0 0 ,First,#," + trafficTablePath + ",Street.LINK_ID,-1,-1;" +
                                                        "TRAFFIC_CD \"TRAFFIC_CD\" true true false 10 Text 0 0 ,First,#," + trafficTablePath + ",Street.TRAFFIC_CD,-1,-1;" +
                                                        "TMC \"TMC\" true true false 9 Text 0 0 ,First,#," + trafficTablePath + ",Street.TMC,-1,-1;" +
                                                        "U \"U\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.U,-1,-1;" +
                                                        "M \"M\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.M,-1,-1;" +
                                                        "T \"T\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.T,-1,-1;" +
                                                        "W \"W\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.W,-1,-1;" +
                                                        "R \"R\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.R,-1,-1;" +
                                                        "F \"F\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.F,-1,-1;" +
                                                        "S \"S\" true true false 2 Short 0 0 ,First,#," + trafficTablePath + ",TempRefTable.S,-1,-1";
                        gp.Execute(importTableTool, trackcancel);

                        // Delete the join, view, and temporary tables

                        removeJoinTool = new RemoveJoin();
                        removeJoinTool.in_layer_or_view = "Traffic_View";
                        removeJoinTool.join_name = "TempRefTable";
                        gp.Execute(removeJoinTool, trackcancel);

                        deleteTool = new Delete();
                        deleteTool.in_data = "Traffic_View";
                        gp.Execute(deleteTool, trackcancel);

                        deleteTool = new Delete();
                        deleteTool.in_data = referenceTablePath;
                        gp.Execute(deleteTool, trackcancel);

                        deleteTool = new Delete();
                        deleteTool.in_data = trafficTablePath;
                        gp.Execute(deleteTool, trackcancel);
                    }

                    AddMessage("Creating fields on the historical traffic join table...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = histTrafficJoinTablePath;

                    // Add FCID, FID, position, AverageSpeed[_X], and BaseSpeed[_X] fields to the Streets_Patterns table

                    addFieldTool.field_type = "LONG";
                    addFieldTool.field_name = "EdgeFCID";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeFID";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "DOUBLE";
                    addFieldTool.field_name = "EdgeFrmPos";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeToPos";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "FLOAT";
                    addFieldTool.field_name = "AverageSpeed";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_U";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_M";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_T";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_W";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_R";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_F";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "AverageSpeed_S";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "FLOAT";
                    addFieldTool.field_name = "BaseSpeed";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_U";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_M";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_T";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_W";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_R";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_F";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "BaseSpeed_S";
                    gp.Execute(addFieldTool, trackcancel);

                    // If we're creating 10.0, then we also need to create the BaseMinutes field

                    if (fgdbVersion == 10.0)
                    {
                        addFieldTool.field_type = "DOUBLE";
                        addFieldTool.field_name = "BaseMinutes";
                        gp.Execute(addFieldTool, trackcancel);
                    }

                    // Populate the AverageSpeed_X and BaseSpeed_X fields

                    makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = histTrafficJoinTablePath;
                    makeTableViewTool.out_view = "Streets_Patterns_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "U", gp, messages, trackcancel);
                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "M", gp, messages, trackcancel);
                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "T", gp, messages, trackcancel);
                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "W", gp, messages, trackcancel);
                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "R", gp, messages, trackcancel);
                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "F", gp, messages, trackcancel);
                    PopulateAverageSpeedAndBaseSpeedFields(patternsTablePath, "S", gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Patterns_View";
                    gp.Execute(deleteTool, trackcancel);

                    // Calculate the AverageSpeed and BaseSpeed fields

                    AddMessage("Calculating the AverageSpeed field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = histTrafficJoinTablePath;
                    calcFieldTool.field = "AverageSpeed";
                    calcFieldTool.expression = "( (1/[AverageSpeed_U]) + (1/[AverageSpeed_M]) + (1/[AverageSpeed_T]) + (1/[AverageSpeed_W]) + (1/[AverageSpeed_R]) + (1/[AverageSpeed_F]) + (1/[AverageSpeed_S]) ) / " +
                                               "( (1/[AverageSpeed_U]/[AverageSpeed_U]) + (1/[AverageSpeed_M]/[AverageSpeed_M]) + (1/[AverageSpeed_T]/[AverageSpeed_T]) + (1/[AverageSpeed_W]/[AverageSpeed_W]) + (1/[AverageSpeed_R]/[AverageSpeed_R]) + (1/[AverageSpeed_F]/[AverageSpeed_F]) + (1/[AverageSpeed_S]/[AverageSpeed_S]) )";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the BaseSpeed field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = histTrafficJoinTablePath;
                    calcFieldTool.field = "BaseSpeed";
                    calcFieldTool.expression = "( (1/[BaseSpeed_U]) + (1/[BaseSpeed_M]) + (1/[BaseSpeed_T]) + (1/[BaseSpeed_W]) + (1/[BaseSpeed_R]) + (1/[BaseSpeed_F]) + (1/[BaseSpeed_S]) ) / " +
                                               "( (1/[BaseSpeed_U]/[BaseSpeed_U]) + (1/[BaseSpeed_M]/[BaseSpeed_M]) + (1/[BaseSpeed_T]/[BaseSpeed_T]) + (1/[BaseSpeed_W]/[BaseSpeed_W]) + (1/[BaseSpeed_R]/[BaseSpeed_R]) + (1/[BaseSpeed_F]/[BaseSpeed_F]) + (1/[BaseSpeed_S]/[BaseSpeed_S]) )";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    DeleteField deleteFieldTool = new DeleteField();
                    deleteFieldTool.in_table = histTrafficJoinTablePath;
                    deleteFieldTool.drop_field = "AverageSpeed_U;AverageSpeed_M;AverageSpeed_T;AverageSpeed_W;AverageSpeed_R;AverageSpeed_F;AverageSpeed_S;BaseSpeed_U;BaseSpeed_M;BaseSpeed_T;BaseSpeed_W;BaseSpeed_R;BaseSpeed_F;BaseSpeed_S";
                    gp.Execute(deleteFieldTool, trackcancel);

                    makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = histTrafficJoinTablePath;
                    makeTableViewTool.out_view = "Streets_Patterns_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Patterns_View";
                    addJoinTool.in_field = (usesNTPFullCoverage ? "LINK_PVID" : "LINK_ID");
                    addJoinTool.join_table = streetsFeatureClassPath;
                    addJoinTool.join_field = "LINK_ID";
                    gp.Execute(addJoinTool, trackcancel);

                    // Calculate the BaseMinutes field (if 10.0)

                    if (fgdbVersion == 10.0)
                    {
                        AddMessage("Calculating the BaseMinutes field...", messages, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = "Streets_Patterns_View";
                        calcFieldTool.field = HistTrafficJoinTableName + ".BaseMinutes";
                        calcFieldTool.expression = "[" + StreetsFCName + ".Meters] * 0.06 / [" + HistTrafficJoinTableName + ".BaseSpeed]";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);
                    }

                    // Calculate the FCID, FID, and position fields

                    AddMessage("Calculating the EdgeFID field for historical traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Patterns_View";
                    calcFieldTool.field = HistTrafficJoinTableName + ".EdgeFID";
                    calcFieldTool.expression = "[" + StreetsFCName + ".OBJECTID]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Patterns_View";
                    removeJoinTool.join_name = StreetsFCName;
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Patterns_View";
                    gp.Execute(deleteTool, trackcancel);

                    AddMessage("Calculating the EdgeFCID field for historical traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = histTrafficJoinTablePath;
                    calcFieldTool.field = "EdgeFCID";
                    calcFieldTool.expression = "1";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFrmPos field for historical traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = histTrafficJoinTablePath;

                    calcFieldTool.field = "EdgeFrmPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = (usesNTPFullCoverage ? "Select Case [TRAVEL_DIRECTION]\n  Case \"F\": x = 0\n  Case \"T\": x = 1\nEnd Select" :
                                                                      "Select Case Left([TRAFFIC_CD], 1)\n  Case \"+\": x = 0\n  Case \"-\": x = 1\nEnd Select");
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeToPos field for historical traffic...", messages, trackcancel);

                    calcFieldTool.field = "EdgeToPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = (usesNTPFullCoverage ? "Select Case [TRAVEL_DIRECTION]\n  Case \"T\": x = 0\n  Case \"F\": x = 1\nEnd Select" :
                                                                      "Select Case Left([TRAFFIC_CD], 1)\n  Case \"-\": x = 0\n  Case \"+\": x = 1\nEnd Select");
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    // Write the average speeds to the streets feature class

                    WriteAverageSpeedsToStreets(outputFileGdbPath, streetsFeatureClassPath, usesNTPFullCoverage, gp, messages, trackcancel);

                    // Add fields for the average travel times to the Streets feature class

                    AddMessage("Creating fields for the average travel times...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = streetsFeatureClassPath;

                    addFieldTool.field_type = "FLOAT";
                    addFieldTool.field_name = "FT_Minutes";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_Minutes";
                    gp.Execute(addFieldTool, trackcancel);

                    // Calculate the average travel time fields

                    AddMessage("Calculating the FT travel times...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = streetsFeatureClassPath;

                    calcFieldTool.field = "FT_Minutes";
                    calcFieldTool.expression = "[Meters] * 0.06 / s";
                    calcFieldTool.code_block = "s = [FT_AverageSpeed]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the TF travel times...", messages, trackcancel);

                    calcFieldTool.field = "TF_Minutes";
                    calcFieldTool.expression = "[Meters] * 0.06 / s";
                    calcFieldTool.code_block = "s = [TF_AverageSpeed]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    // Offset the daily pattern fields (U,M,T,W,R,F,S) on the historical traffic join table

                    OffsetDailyPatternFields(histTrafficJoinTablePath, patternsTablePath);

                    // Add an index to the Streets feature class's FUNC_CLASS field

                    AddMessage("Indexing the FUNC_CLASS field...", messages, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = streetsFeatureClassPath;
                    addIndexTool.fields = "FUNC_CLASS";
                    addIndexTool.index_name = "FUNC_CLASS";
                    gp.Execute(addIndexTool, trackcancel);
                }
                else
                {
                    // Create and calculate the generic Minutes field from speed categories

                    addFieldTool = new AddField();
                    addFieldTool.in_table = streetsFeatureClassPath;
                    addFieldTool.field_type = "FLOAT";
                    addFieldTool.field_name = "Minutes";
                    gp.Execute(addFieldTool, trackcancel);

                    AddMessage("Calculating the Minutes field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = streetsFeatureClassPath;
                    calcFieldTool.field = "Minutes";
                    calcFieldTool.expression = "[Meters] * 0.06 / [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);
                }
                #endregion

                #region Process Live Traffic Table

                // Create the live traffic table (for 10.1 or later)

                if (fgdbVersion >= 10.1 && !inputTrafficTableValue.IsEmpty())
                {
                    // Copy over the Traffic table to the file geodatabase

                    AddMessage("Copying Traffic table to the file geodatabase...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = inputTrafficTableValue.GetAsText();
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = TMCJoinTableName;
                    gp.Execute(importTableTool, trackcancel);

                    string TMCJoinTablePath = outputFileGdbPath + "\\" + TMCJoinTableName;

                    // Add the TMC field and calculate it

                    AddMessage("Calculating TMC values for live traffic...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = TMCJoinTablePath;
                    addFieldTool.field_type = "TEXT";
                    addFieldTool.field_length = 9;
                    addFieldTool.field_name = "TMC";
                    gp.Execute(addFieldTool, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = TMCJoinTablePath;
                    calcFieldTool.field = "TMC";
                    calcFieldTool.expression = "Right([TRAFFIC_CD], 9)";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Creating the live traffic join table...", messages, trackcancel);

                    // Add FCID, FID, and position fields to the Streets_TMC table

                    AddMessage("Creating fields on the live traffic join table...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = TMCJoinTablePath;

                    addFieldTool.field_type = "LONG";
                    addFieldTool.field_name = "EdgeFCID";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeFID";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "DOUBLE";
                    addFieldTool.field_name = "EdgeFrmPos";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeToPos";
                    gp.Execute(addFieldTool, trackcancel);

                    // Calculate the fields

                    AddMessage("Calculating the EdgeFrmPos field for live traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = TMCJoinTablePath;

                    calcFieldTool.field = "EdgeFrmPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = "Select Case Left([TRAFFIC_CD], 1)\n  Case \"+\": x = 0\n  Case \"-\": x = 1\nEnd Select";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeToPos field for live traffic...", messages, trackcancel);

                    calcFieldTool.field = "EdgeToPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = "Select Case Left([TRAFFIC_CD], 1)\n  Case \"-\": x = 0\n  Case \"+\": x = 1\nEnd Select";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFCID field for live traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = TMCJoinTablePath;
                    calcFieldTool.field = "EdgeFCID";
                    calcFieldTool.expression = "1";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFID field for live traffic...", messages, trackcancel);

                    makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = TMCJoinTablePath;
                    makeTableViewTool.out_view = "Streets_TMC_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_TMC_View";
                    addJoinTool.in_field = "LINK_ID";
                    addJoinTool.join_table = streetsFeatureClassPath;
                    addJoinTool.join_field = "LINK_ID";
                    gp.Execute(addJoinTool, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_TMC_View";
                    calcFieldTool.field = TMCJoinTableName + ".EdgeFID";
                    calcFieldTool.expression = "[" + StreetsFCName + ".OBJECTID]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_TMC_View";
                    removeJoinTool.join_name = StreetsFCName;
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_TMC_View";
                    gp.Execute(deleteTool, trackcancel);

                    // If Historical Traffic is not being used, then we need to create placeholder historical traffic tables

                    if (!usesHistoricalTraffic)
                    {
                        // Create the Streets_Patterns table by starting with a copy of the Streets_TMC table

                        AddMessage("Creating the historical traffic join table...", messages, trackcancel);

                        importTableTool = new TableToTable();
                        importTableTool.in_rows = TMCJoinTablePath;
                        importTableTool.out_path = outputFileGdbPath;
                        importTableTool.out_name = HistTrafficJoinTableName;
                        gp.Execute(importTableTool, trackcancel);

                        string histTrafficJoinTablePath = outputFileGdbPath + "\\" + HistTrafficJoinTableName;

                        AddMessage("Creating and calculating the KPH field on the historical traffic join table...", messages, trackcancel);

                        addFieldTool = new AddField();
                        addFieldTool.in_table = histTrafficJoinTablePath;
                        addFieldTool.field_type = "FLOAT";
                        addFieldTool.field_name = "KPH";
                        gp.Execute(addFieldTool, trackcancel);

                        makeTableViewTool = new MakeTableView();
                        makeTableViewTool.in_table = histTrafficJoinTablePath;
                        makeTableViewTool.out_view = "Streets_Patterns_View";
                        gp.Execute(makeTableViewTool, trackcancel);

                        addJoinTool = new AddJoin();
                        addJoinTool.in_layer_or_view = "Streets_Patterns_View";
                        addJoinTool.in_field = "EdgeFID";
                        addJoinTool.join_table = streetsFeatureClassPath;
                        addJoinTool.join_field = "OBJECTID";
                        gp.Execute(addJoinTool, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = "Streets_Patterns_View";
                        calcFieldTool.field = HistTrafficJoinTableName + ".KPH";
                        calcFieldTool.expression = "[" + StreetsFCName + ".KPH]";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);

                        removeJoinTool = new RemoveJoin();
                        removeJoinTool.in_layer_or_view = "Streets_Patterns_View";
                        removeJoinTool.join_name = StreetsFCName;
                        gp.Execute(removeJoinTool, trackcancel);

                        deleteTool = new Delete();
                        deleteTool.in_data = "Streets_Patterns_View";
                        gp.Execute(deleteTool, trackcancel);

                        AddMessage("Creating and calculating the daily patterns fields on the historical traffic join table...", messages, trackcancel);

                        string[] fieldNames = new string[] { "S", "M", "T", "W", "R", "F", "S" };
                        foreach (string f in fieldNames)
                        {
                            addFieldTool = new AddField();
                            addFieldTool.in_table = histTrafficJoinTablePath;
                            addFieldTool.field_type = "SHORT";
                            addFieldTool.field_name = f;
                            gp.Execute(addFieldTool, trackcancel);

                            calcFieldTool = new CalculateField();
                            calcFieldTool.in_table = histTrafficJoinTablePath;
                            calcFieldTool.field = f;
                            calcFieldTool.expression = "1";
                            calcFieldTool.expression_type = "VB";
                            gp.Execute(calcFieldTool, trackcancel);
                        }

                        // Create the Patterns table

                        CreateNonHistoricalPatternsTable(outputFileGdbPath);
                    }
                }
                #endregion

                // Copy the Cdms table to the file geodatabase

                AddMessage("Copying the Condition/Driving Manoeuvres (CDMS) table and indexing...", messages, trackcancel);

                importTableTool = new TableToTable();
                importTableTool.in_rows = inputCdmsTableValue.GetAsText();
                importTableTool.out_path = outputFileGdbPath;
                importTableTool.out_name = "Cdms";
                gp.Execute(importTableTool, trackcancel);

                string cdmsTablePath = outputFileGdbPath + "\\Cdms";

                addIndexTool = new AddIndex();
                addIndexTool.in_table = cdmsTablePath;
                addIndexTool.fields = "COND_ID";
                addIndexTool.index_name = "COND_ID";
                gp.Execute(addIndexTool, trackcancel);

                // Copy the Rdms table to the file geodatabase

                AddMessage("Copying the Restricted Driving Manoeuvres (RDMS) table...", messages, trackcancel);

                importTableTool = new TableToTable();
                importTableTool.in_rows = inputRdmsTableValue.GetAsText();
                importTableTool.out_path = outputFileGdbPath;
                importTableTool.out_name = "Rdms";
                gp.Execute(importTableTool, trackcancel);

                string rdmsTablePath = outputFileGdbPath + "\\Rdms";

                // Add and calculate the end of link and condition type fields to the Rdms table

                AddMessage("Creating and calculating fields for the manoeuvre types and ends of link...", messages, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = rdmsTablePath;
                addFieldTool.field_name = "COND_TYPE";
                addFieldTool.field_type = "LONG";
                gp.Execute(addFieldTool, trackcancel);

                addFieldTool.field_name = "END_OF_LK";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 1;
                gp.Execute(addFieldTool, trackcancel);

                makeTableViewTool = new MakeTableView();
                makeTableViewTool.in_table = rdmsTablePath;
                makeTableViewTool.out_view = "Rdms_View";
                gp.Execute(makeTableViewTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Rdms_View";
                addJoinTool.in_field = "COND_ID";
                addJoinTool.join_table = cdmsTablePath;
                addJoinTool.join_field = "COND_ID";
                gp.Execute(addJoinTool, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "Rdms_View";
                calcFieldTool.field = "Rdms.COND_TYPE";
                calcFieldTool.expression = "[Cdms.COND_TYPE]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                calcFieldTool.field = "Rdms.END_OF_LK";
                calcFieldTool.expression = "[Cdms.END_OF_LK]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Rdms_View";
                removeJoinTool.join_name = "Cdms";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "Rdms_View";
                gp.Execute(deleteTool, trackcancel);

                // Extract only the gates (condition type 4) and prohibitied manoeuvres (condition type 7)
                // If using Transport, also extract the Transport manoeuvres (condition type 26)

                AddMessage("Extracting restricted driving manoeuvres...", messages, trackcancel);

                string prohibRdmsWEndOfLkTablePath = outputFileGdbPath + "\\ProhibRdmsWEndOfLk";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = rdmsTablePath;
                tableSelectTool.out_table = prohibRdmsWEndOfLkTablePath;
                if (usesTransport)
                    tableSelectTool.where_clause = "COND_TYPE IN (4, 7, 26)";
                else
                    tableSelectTool.where_clause = "COND_TYPE IN (4, 7)";
                gp.Execute(tableSelectTool, trackcancel);

                AddMessage("Creating turn feature class...", messages, trackcancel);

                // Create the turn feature class

                string tempStatsTablePath = outputFileGdbPath + "\\tempStatsTable";

                Statistics statsTool = new Statistics();
                statsTool.in_table = prohibRdmsWEndOfLkTablePath;
                statsTool.out_table = tempStatsTablePath;
                statsTool.statistics_fields = "SEQ_NUMBER MAX";
                gp.Execute(statsTool, null);

                CreateAndPopulateTurnFeatureClass(outputFileGdbPath, fdsName, "ProhibRdmsWEndOfLk", "tempStatsTable",
                                                  messages, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = tempStatsTablePath;
                gp.Execute(deleteTool, trackcancel);

                string pathToTurnFC = pathToFds + "\\" + TurnFCName;

                // Create and calculate condition type and access restriction fields on the turn feature class

                addFieldTool = new AddField();
                addFieldTool.in_table = pathToTurnFC;
                addFieldTool.field_name = "COND_TYPE";
                addFieldTool.field_type = "LONG";
                gp.Execute(addFieldTool, trackcancel);

                AddMessage("Creating access restriction fields on the turn feature class...", messages, trackcancel);

                foreach (string arFieldSuffix in arFieldSuffixes)
                {
                    addFieldTool = new AddField();
                    addFieldTool.in_table = pathToTurnFC;
                    addFieldTool.field_name = "AR_" + arFieldSuffix;
                    addFieldTool.field_type = "TEXT";
                    addFieldTool.field_length = 1;
                    gp.Execute(addFieldTool, trackcancel);
                }

                makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = pathToTurnFC;
                makeFeatureLayerTool.out_layer = "Turn_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "Turn_Layer";
                addJoinTool.in_field = "COND_ID";
                addJoinTool.join_table = cdmsTablePath;
                addJoinTool.join_field = "COND_ID";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the COND_TYPE field on the turn feature class...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "Turn_Layer";
                calcFieldTool.field = TurnFCName + ".COND_TYPE";
                calcFieldTool.expression = "[Cdms.COND_TYPE]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                foreach (string arFieldSuffix in arFieldSuffixes)
                {
                    AddMessage("Calculating the AR_" + arFieldSuffix + " field on the turn feature class...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Turn_Layer";
                    calcFieldTool.field = TurnFCName + ".AR_" + arFieldSuffix;
                    calcFieldTool.expression = "[Cdms.AR_" + arFieldSuffix + "]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);
                }

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "Turn_Layer";
                removeJoinTool.join_name = "Cdms";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "Turn_Layer";
                gp.Execute(deleteTool, trackcancel);

                deleteTool.in_data = cdmsTablePath;
                gp.Execute(deleteTool, trackcancel);

                deleteTool.in_data = prohibRdmsWEndOfLkTablePath;
                gp.Execute(deleteTool, trackcancel);

                GC.Collect();

                #region Process Transport Tables

                if (usesTransport)
                {
                    string nonDimensionalCndModTablePath = outputFileGdbPath + "\\nonDimensionalCndMod";
                    string dimensionalCndModTablePath = outputFileGdbPath + "\\dimensionalCndMod";

                    if (!(inputUSCndModTableValue.IsEmpty()))
                    {
                        // Extract the Transport information for the US

                        AddMessage("Extracting the US non-dimensional conditions...", messages, trackcancel);

                        tableSelectTool = new TableSelect();
                        tableSelectTool.in_table = inputUSCndModTableValue.GetAsText();
                        tableSelectTool.out_table = nonDimensionalCndModTablePath;
                        tableSelectTool.where_clause = "MOD_TYPE IN (38, 39, 46, 49, 60, 75)";
                        gp.Execute(tableSelectTool, trackcancel);

                        AddMessage("Extracting the US dimensional conditions...", messages, trackcancel);

                        tableSelectTool.out_table = dimensionalCndModTablePath;
                        tableSelectTool.where_clause = "MOD_TYPE IN (41, 42, 43, 44, 45, 48, 81)";
                        gp.Execute(tableSelectTool, trackcancel);

                        // Create a new field to hold the dimension in a common unit (m or kg or KPH)

                        addFieldTool = new AddField();
                        addFieldTool.in_table = dimensionalCndModTablePath;
                        addFieldTool.field_name = "MetersOrKilogramsOrKPH";
                        addFieldTool.field_type = "DOUBLE";
                        gp.Execute(addFieldTool, trackcancel);

                        AddMessage("Calculating common units of measure...", messages, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = dimensionalCndModTablePath;
                        calcFieldTool.field = "MetersOrKilogramsOrKPH";
                        calcFieldTool.code_block = "x = CLng([MOD_VAL])\nSelect Case [MOD_TYPE]\n" +
                                                   "  Case 41, 44, 45, 81: x = x * 0.0254\n" +
                                                   "  Case 42, 43: x = x * 0.45359237\n" +
                                                   "  Case 48: x = x * 1.609344\nEnd Select";
                        calcFieldTool.expression = "x";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);

                        if (!(inputNonUSCndModTableValue.IsEmpty()))
                        {
                            string tempNonDimensionalCndModTablePath = outputFileGdbPath + "\\tempNonDimensionalCndMod";
                            string tempDimensionalCndModTablePath = outputFileGdbPath + "\\tempDimensionalCndMod";

                            // Extract the Transport information for outside the US to temporary tables

                            AddMessage("Extracting the non-US non-dimensional conditions...", messages, trackcancel);

                            tableSelectTool = new TableSelect();
                            tableSelectTool.in_table = inputNonUSCndModTableValue.GetAsText();
                            tableSelectTool.out_table = tempNonDimensionalCndModTablePath;
                            tableSelectTool.where_clause = "MOD_TYPE IN (38, 39, 46, 49, 60, 75)";
                            gp.Execute(tableSelectTool, trackcancel);

                            AddMessage("Extracting the non-US dimensional conditions...", messages, trackcancel);

                            tableSelectTool.out_table = tempDimensionalCndModTablePath;
                            tableSelectTool.where_clause = "MOD_TYPE IN (41, 42, 43, 44, 45, 48, 81)";
                            gp.Execute(tableSelectTool, trackcancel);

                            // Create a new field to hold the dimension in a common unit (m or kg)

                            addFieldTool = new AddField();
                            addFieldTool.in_table = tempDimensionalCndModTablePath;
                            addFieldTool.field_name = "MetersOrKilogramsOrKPH";
                            addFieldTool.field_type = "DOUBLE";
                            gp.Execute(addFieldTool, trackcancel);

                            AddMessage("Calculating common units of measure...", messages, trackcancel);

                            calcFieldTool = new CalculateField();
                            calcFieldTool.in_table = dimensionalCndModTablePath;
                            calcFieldTool.field = "MetersOrKilogramsOrKPH";
                            calcFieldTool.code_block = "x = CLng([MOD_VAL])\nSelect Case [MOD_TYPE]\n" +
                                                       "  Case 41, 44, 45, 81: x = x * 0.01\nEnd Select";
                            calcFieldTool.expression = "x";
                            calcFieldTool.expression_type = "VB";
                            gp.Execute(calcFieldTool, trackcancel);

                            // Append the temporary tables to the main table containing US attributes

                            Append appendTool = new Append();
                            appendTool.inputs = tempNonDimensionalCndModTablePath;
                            appendTool.target = nonDimensionalCndModTablePath;
                            gp.Execute(appendTool, trackcancel);

                            appendTool.inputs = tempDimensionalCndModTablePath;
                            appendTool.target = dimensionalCndModTablePath;
                            gp.Execute(appendTool, trackcancel);

                            deleteTool = new Delete();
                            deleteTool.in_data = tempNonDimensionalCndModTablePath;
                            gp.Execute(deleteTool, trackcancel);
                            deleteTool.in_data = tempDimensionalCndModTablePath;
                            gp.Execute(deleteTool, trackcancel);
                        }
                    }
                    else
                    {
                        // Extract the Transport information for outside the US

                        AddMessage("Extracting the non-US non-dimensional conditions...", messages, trackcancel);

                        tableSelectTool = new TableSelect();
                        tableSelectTool.in_table = inputNonUSCndModTableValue.GetAsText();
                        tableSelectTool.out_table = nonDimensionalCndModTablePath;
                        tableSelectTool.where_clause = "MOD_TYPE IN (38, 39, 46, 49, 60, 75)";
                        gp.Execute(tableSelectTool, trackcancel);

                        AddMessage("Extracting the non-US dimensional conditions...", messages, trackcancel);

                        tableSelectTool.out_table = dimensionalCndModTablePath;
                        tableSelectTool.where_clause = "MOD_TYPE IN (41, 42, 43, 44, 45, 48, 81)";
                        gp.Execute(tableSelectTool, trackcancel);

                        // Create a new field to hold the dimension in a common unit (m or kg)

                        addFieldTool = new AddField();
                        addFieldTool.in_table = dimensionalCndModTablePath;
                        addFieldTool.field_name = "MetersOrKilogramsOrKPH";
                        addFieldTool.field_type = "DOUBLE";
                        gp.Execute(addFieldTool, trackcancel);

                        AddMessage("Calculating common units of measure...", messages, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = dimensionalCndModTablePath;
                        calcFieldTool.field = "MetersOrKilogramsOrKPH";
                        calcFieldTool.code_block = "x = CLng([MOD_VAL])\nSelect Case [MOD_TYPE]\n" +
                                                   "  Case 41, 44, 45, 81: x = x * 0.01\nEnd Select";
                        calcFieldTool.expression = "x";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);
                    }

                    // Create a table for looking up LINK_IDs from COND_IDs

                    AddMessage("Creating and indexing LINK_ID/COND_ID look-up table for preferred roads...", messages, trackcancel);

                    string preferredLinkIDLookupTablePath = outputFileGdbPath + "\\preferredLinkIDLookupTable";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputCdmsTableValue.GetAsText();
                    tableSelectTool.out_table = preferredLinkIDLookupTablePath;
                    tableSelectTool.where_clause = "COND_TYPE = 27";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = preferredLinkIDLookupTablePath;
                    addIndexTool.fields = "COND_ID";
                    addIndexTool.index_name = "COND_ID";
                    gp.Execute(addIndexTool, trackcancel);

                    // Create a table for looking up the condition's direction

                    AddMessage("Creating and indexing direction look-up table for preferred roads...", messages, trackcancel);

                    string preferredDirectionLookupTablePath = outputFileGdbPath + "\\preferredDirectionLookupTable";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = nonDimensionalCndModTablePath;
                    tableSelectTool.out_table = preferredDirectionLookupTablePath;
                    tableSelectTool.where_clause = "MOD_TYPE = 60";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = preferredDirectionLookupTablePath;
                    addIndexTool.fields = "COND_ID";
                    addIndexTool.index_name = "COND_ID";
                    gp.Execute(addIndexTool, trackcancel);

                    // Create a table for looking up LINK_IDs from COND_IDs

                    AddMessage("Creating and indexing LINK_ID/COND_ID look-up table for restrictions...", messages, trackcancel);

                    string restrictionLinkIDLookupTablePath = outputFileGdbPath + "\\restrictionLinkIDLookupTable";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputCdmsTableValue.GetAsText();
                    tableSelectTool.out_table = restrictionLinkIDLookupTablePath;
                    tableSelectTool.where_clause = "COND_TYPE = 23";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = restrictionLinkIDLookupTablePath;
                    addIndexTool.fields = "COND_ID";
                    addIndexTool.index_name = "COND_ID";
                    gp.Execute(addIndexTool, trackcancel);

                    // Create a table for looking up the condition's direction

                    AddMessage("Creating and indexing direction look-up table for restrictions...", messages, trackcancel);

                    string restrictionDirectionLookupTablePath = outputFileGdbPath + "\\restrictionDirectionLookupTable";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = nonDimensionalCndModTablePath;
                    tableSelectTool.out_table = restrictionDirectionLookupTablePath;
                    tableSelectTool.where_clause = "MOD_TYPE = 38";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = restrictionDirectionLookupTablePath;
                    addIndexTool.fields = "COND_ID";
                    addIndexTool.index_name = "COND_ID";
                    gp.Execute(addIndexTool, trackcancel);

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                    makeFeatureLayerTool.out_layer = "Streets_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    CreateAndPopulateTruckFCOverrideField(outputFileGdbPath, gp, messages, trackcancel);

                    if (fgdbVersion >= 10.1)
                    {
                        // Create and calculate the preferred fields for Streets

                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "STAAPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '1'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "TruckDesignatedPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '2'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "NRHMPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '3'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "ExplosivesPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '4'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "PIHPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '5'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "MedicalWastePreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '6'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "RadioactivePreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '7'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "HazmatPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '8'", gp, messages, trackcancel);
                        CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, true, "LocallyPreferred",
                                                                  "MOD_TYPE = 49 AND MOD_VAL = '9'", gp, messages, trackcancel);

                        if (createArcGISOnlineNetworkAttributes)
                        {
                            CreateAndPopulateAGOLTextTransportFieldsOnStreets(outputFileGdbPath, "PreferredTruckRoute",
                                                                              new string[] { "STAAPreferred", "TruckDesignatedPreferred", "LocallyPreferred" },
                                                                              gp, messages, trackcancel);
                            CreateAndPopulateAGOLTextTransportFieldsOnStreets(outputFileGdbPath, "PreferredHazmatRoute",
                                                                              new string[] { "NRHMPreferred", "ExplosivesPreferred", "PIHPreferred", "MedicalWastePreferred", "RadioactivePreferred", "HazmatPreferred" },
                                                                              gp, messages, trackcancel);
                        }
                    }

                    // Create and calculate the HazMat restriction fields for Streets

                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "ExplosivesProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '1'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "GasProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '2'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "FlammableProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '3'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "CombustibleProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '4'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "OrganicProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '5'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "PoisonProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '6'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "RadioactiveProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '7'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "CorrosiveProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '8'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "OtherHazmatProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '9'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "AnyHazmatProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '20'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "PIHProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '21'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "HarmfulToWaterProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '22'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "ExplosiveAndFlammableProhibited",
                                                              "MOD_TYPE = 39 AND MOD_VAL = '23'", gp, messages, trackcancel);

                    if (createArcGISOnlineNetworkAttributes)
                    {
                        CreateAndPopulateAGOLTextTransportFieldsOnStreets(outputFileGdbPath, "AGOL_AnyHazmatProhibited",
                                                                          new string[] { "ExplosivesProhibited", "GasProhibited", "FlammableProhibited", "CombustibleProhibited", "OrganicProhibited", "PoisonProhibited", "RadioactiveProhibited", "CorrosiveProhibited",
                                                                                         "OtherHazmatProhibited", "AnyHazmatProhibited", "PIHProhibited", "HarmfulToWaterProhibited", "ExplosiveAndFlammableProhibited" },
                                                                          gp, messages, trackcancel);
                    }

                    // Create and calculate the other restriction fields for Streets

                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, false, "HeightLimit_Meters",
                                                              "MOD_TYPE = 41", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, false, "WeightLimit_Kilograms",
                                                              "MOD_TYPE = 42", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, false, "WeightLimitPerAxle_Kilograms",
                                                              "MOD_TYPE = 43", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, false, "LengthLimit_Meters",
                                                              "MOD_TYPE = 44", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, false, "WidthLimit_Meters",
                                                              "MOD_TYPE = 45", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, true, false, "MaxTrailersAllowedOnTruck",
                                                              "MOD_TYPE = 46 AND MOD_VAL IN ('1', '2', '3')", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "SemiOrTractorWOneOrMoreTrailersProhibited",
                                                              "MOD_TYPE = 46 AND MOD_VAL = '4'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, true, false, "MaxAxlesAllowed",
                                                              "MOD_TYPE = 75 AND MOD_VAL IN ('1', '2', '3', '4', '5')", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "SingleAxleProhibited",
                                                              "MOD_TYPE = 75 AND MOD_VAL = '6'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, false, false, false, "TandemAxleProhibited",
                                                              "MOD_TYPE = 75 AND MOD_VAL = '7'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, false, "KingpinToRearAxleLengthLimit_Meters",
                                                              "MOD_TYPE = 81", gp, messages, trackcancel);

                    // Create and calculate the truck speed fields for Streets

                    CreateAndPopulateTransportFieldsOnStreets(outputFileGdbPath, true, false, true, "TruckKPH",
                                                              "MOD_TYPE = 48", gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool.in_data = preferredLinkIDLookupTablePath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = preferredDirectionLookupTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool.in_data = restrictionLinkIDLookupTablePath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = restrictionDirectionLookupTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = pathToTurnFC;
                    makeFeatureLayerTool.out_layer = "RestrictedTurns_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    // Create and calculate the HazMat restriction fields for RestrictedTurns

                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "ExplosivesProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '1'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "GasProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '2'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "FlammableProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '3'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "CombustibleProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '4'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "OrganicProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '5'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "PoisonProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '6'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "RadioactiveProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '7'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "CorrosiveProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '8'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "OtherHazmatProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '9'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "AnyHazmatProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '20'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "PIHProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '21'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "HarmfulToWaterProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '22'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "ExplosiveAndFlammableProhibited",
                                                           "MOD_TYPE = 39 AND MOD_VAL = '23'", gp, messages, trackcancel);

                    if (createArcGISOnlineNetworkAttributes)
                    {
                        CreateAndPopulateAGOLTextTransportFieldOnTurns(outputFileGdbPath, "AGOL_AnyHazmatProhibited",
                                                                       new string[] { "ExplosivesProhibited", "GasProhibited", "FlammableProhibited", "CombustibleProhibited", "OrganicProhibited", "PoisonProhibited", "RadioactiveProhibited", "CorrosiveProhibited",
                                                                                      "OtherHazmatProhibited", "AnyHazmatProhibited", "PIHProhibited", "HarmfulToWaterProhibited", "ExplosiveAndFlammableProhibited" },
                                                                       gp, messages, trackcancel);
                    }

                    // Create and calculate the other restriction fields for RestrictedTurns

                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, true, false, "HeightLimit_Meters",
                                                           "MOD_TYPE = 41", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, true, false, "WeightLimit_Kilograms",
                                                           "MOD_TYPE = 42", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, true, false, "WeightLimitPerAxle_Kilograms",
                                                           "MOD_TYPE = 43", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, true, false, "LengthLimit_Meters",
                                                           "MOD_TYPE = 44", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, true, false, "WidthLimit_Meters",
                                                           "MOD_TYPE = 45", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, true, "MaxTrailersAllowedOnTruck",
                                                           "MOD_TYPE = 46 AND MOD_VAL IN ('1', '2', '3')", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "SemiOrTractorWOneOrMoreTrailersProhibited",
                                                           "MOD_TYPE = 46 AND MOD_VAL = '4'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, true, "MaxAxlesAllowed",
                                                           "MOD_TYPE = 75 AND MOD_VAL IN ('1', '2', '3', '4', '5')", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "SingleAxleProhibited",
                                                           "MOD_TYPE = 75 AND MOD_VAL = '6'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, false, false, "TandemAxleProhibited",
                                                           "MOD_TYPE = 75 AND MOD_VAL = '7'", gp, messages, trackcancel);
                    CreateAndPopulateTransportFieldOnTurns(outputFileGdbPath, true, false, "KingpinToRearAxleLengthLimit_Meters",
                                                           "MOD_TYPE = 81", gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "RestrictedTurns_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    // Create and calculate the AllTransportProhibited field

                    addFieldTool = new AddField();
                    addFieldTool.in_table = pathToTurnFC;
                    addFieldTool.field_name = "AllTransportProhibited";
                    addFieldTool.field_type = "TEXT";
                    addFieldTool.field_length = 1;
                    gp.Execute(addFieldTool, trackcancel);

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = pathToTurnFC;
                    makeFeatureLayerTool.out_layer = "RestrictedTurns_Layer";
                    makeFeatureLayerTool.where_clause = "COND_TYPE = 26 AND ExplosivesProhibited IS NULL AND GasProhibited IS NULL AND " +
                                                        "FlammableProhibited IS NULL AND CombustibleProhibited IS NULL AND OrganicProhibited IS NULL AND " +
                                                        "PoisonProhibited IS NULL AND RadioactiveProhibited IS NULL AND CorrosiveProhibited IS NULL AND " +
                                                        "OtherHazmatProhibited IS NULL AND AnyHazmatProhibited IS NULL AND PIHProhibited IS NULL AND " +
                                                        "HarmfulToWaterProhibited IS NULL AND ExplosiveAndFlammableProhibited IS NULL AND " +
                                                        "HeightLimit_Meters IS NULL AND WeightLimit_Kilograms IS NULL AND WeightLimitPerAxle_Kilograms IS NULL AND " +
                                                        "LengthLimit_Meters IS NULL AND WidthLimit_Meters IS NULL AND MaxTrailersAllowedOnTruck IS NULL AND " +
                                                        "SemiOrTractorWOneOrMoreTrailersProhibited IS NULL AND MaxAxlesAllowed IS NULL AND " +
                                                        "SingleAxleProhibited IS NULL AND TandemAxleProhibited IS NULL AND KingpinToRearAxleLengthLimit_Meters IS NULL";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    AddMessage("Calculating the AllTransportProhibited field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "RestrictedTurns_Layer";
                    calcFieldTool.field = "AllTransportProhibited";
                    calcFieldTool.expression = "\"Y\"";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "RestrictedTurns_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool.in_data = nonDimensionalCndModTablePath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = dimensionalCndModTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    GC.Collect();
                }
                #endregion

                // Extract the special explications (condition type 9) from the Rdms Table and create RoadSplits table

                AddMessage("Creating RoadSplits table...", messages, trackcancel);

                string specialExplicationRdmsWEndOfLkTablePath = outputFileGdbPath + "\\SpecialExplicationRdmsWEndOfLk";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = rdmsTablePath;
                tableSelectTool.out_table = specialExplicationRdmsWEndOfLkTablePath;
                tableSelectTool.where_clause = "COND_TYPE = 9";
                gp.Execute(tableSelectTool, trackcancel);

                deleteTool.in_data = rdmsTablePath;
                gp.Execute(deleteTool, trackcancel);

                CreateRoadSplitsTable("SpecialExplicationRdmsWEndOfLk", outputFileGdbPath, messages, trackcancel);

                deleteTool.in_data = specialExplicationRdmsWEndOfLkTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Create Signpost feature class and table

                AddMessage("Creating signpost feature class and table...", messages, trackcancel);

                CreateSignposts(inputSignsTableValue.GetAsText(), outputFileGdbPath, messages, trackcancel);

                AddSpatialIndex addSpatialIndexTool = new AddSpatialIndex();
                addSpatialIndexTool.in_features = pathToFds + "\\" + SignpostFCName;
                gp.Execute(addSpatialIndexTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = outputFileGdbPath + "\\" + SignpostJoinTableName;
                addIndexTool.fields = "SignpostID";
                addIndexTool.index_name = "SignpostID";
                gp.Execute(addIndexTool, trackcancel);

                addIndexTool.fields = "Sequence";
                addIndexTool.index_name = "Sequence";
                gp.Execute(addIndexTool, trackcancel);

                addIndexTool.fields = "EdgeFCID";
                addIndexTool.index_name = "EdgeFCID";
                gp.Execute(addIndexTool, trackcancel);

                addIndexTool.fields = "EdgeFID";
                addIndexTool.index_name = "EdgeFID";
                gp.Execute(addIndexTool, trackcancel);

                GC.Collect();

                // Upgrade the geodatabase (if not 9.3)

                if (fgdbVersion > 9.3)
                {
                    UpgradeGDB upgradeGdbTool = new UpgradeGDB();
                    upgradeGdbTool.input_workspace = outputFileGdbPath;
                    gp.Execute(upgradeGdbTool, trackcancel);
                }

                // Create and build the network dataset, then pack it in a GPValue

                AddMessage("Creating and building the network dataset...", messages, trackcancel);

                CreateAndBuildNetworkDataset(outputFileGdbPath, fgdbVersion, fdsName, ndsName, createNetworkAttributesInMetric,
                                             createArcGISOnlineNetworkAttributes, timeZoneIDBaseFieldName, directedTimeZoneIDFields, commonTimeZone,
                                             usesHistoricalTraffic, trafficFeedLocation, usesTransport);

                // Write the build errors to the turn feature class

                TurnGeometryUtilities.WriteBuildErrorsToTurnFC(outputFileGdbPath, fdsName, TurnFCName, messages, trackcancel);

                // Compact the output file geodatabase

                AddMessage("Compacting the output file geodatabase...", messages, trackcancel);

                Compact compactTool = new Compact();
                compactTool.in_workspace = outputFileGdbPath;
                gp.Execute(compactTool, trackcancel);
            }
            catch (Exception e)
            {
                if (gp.MaxSeverity == 2)
                {
                    object missing = System.Type.Missing;
                    messages.AddError(1, gp.GetMessages(ref missing));
                }
                messages.AddError(1, e.Message);
                messages.AddError(1, e.StackTrace);
            }
            finally
            {
                // Restore the original GP environment settings

                gpSettings.AddOutputsToMap = origAddOutputsToMapSetting;
                gpSettings.LogHistory = origLogHistorySetting;
            }
            GC.Collect();
            return;
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                // Stash away the executing user's information, if appropriate
                string             username      = ESRI.ArcGIS.JTXUI.ConfigurationCache.GetCurrentSystemUser(ESRI.ArcGIS.JTXUI.ConfigurationCache.UseUserDomain);
                IJTXConfiguration3 configMgr     = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXUser3          executingUser = configMgr.GetUser(username) as IJTXUser3;

                // Import the AD information
                string domain         = System.Environment.UserDomainName;
                string domainUsername = string.Empty;
                string domainPassword = string.Empty;
                int    numUsers       = 0;
                int    numGroups      = 0;
                ActiveDirectoryHelper.SyncronizeJTXDatabaseWithActiveDirectory(this.WmxDatabase, domain, domainUsername, domainPassword, m_userGroup, m_groupGroup, out numGroups, out numUsers);

                // If the tool was set to preserve the current user's account and the user
                // was removed from the DB, then re-add their account
                if (configMgr.GetUser(username) == null)
                {
                    if (m_preserveCurrentUser)
                    {
                        IJTXConfigurationEdit2 configEdit = this.WmxDatabase.ConfigurationManager as IJTXConfigurationEdit2;
                        IJTXUserConfig         newUser    = configEdit.CreateUser() as IJTXUserConfig;
                        newUser.FirstName_2 = executingUser.FirstName;
                        newUser.FullName_2  = executingUser.FullName;
                        newUser.LastName_2  = executingUser.LastName;
                        newUser.UserName_2  = executingUser.UserName;
                        (newUser as IJTXUser3).IsAdministrator = executingUser.IsAdministrator;
                        newUser.Store();

                        msgs.AddMessage("User '" + username + "' not found in Active Directory group '" + m_userGroup + "'; re-added placeholder to Workflow Manager database");
                    }
                    else
                    {
                        msgs.AddWarning("User '" + username + "' removed from Workflow Manager database");
                    }
                }

                // Update the output parameters
                WmauParameterMap  paramMap = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParam = paramMap.GetParamEdit(C_PARAM_OUT_NUM_USERS);
                IGPLong           value    = new GPLongClass();
                value.Value    = numUsers;
                outParam.Value = value as IGPValue;

                outParam       = paramMap.GetParamEdit(C_PARAM_OUT_NUM_GROUPS);
                value          = new GPLongClass();
                value.Value    = numGroups;
                outParam.Value = value as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            finally
            {
                // Release any COM objects here!
            }
        }
Exemplo n.º 23
0
        private void parseOSMDocument(string osmFileLocation, ref IGPMessages message, ref List<string> nodeList, ref List<string> wayList, ref List<string> relationList, ref List<string> downloadedDocuments, string baseURL, api apiCapabilities)
        {
            try
            {
                XmlSerializer nodeSerializer = new XmlSerializer(typeof(node));
                XmlSerializer waySerializer = new XmlSerializer(typeof(way));
                XmlSerializer relationSerializer = new XmlSerializer(typeof(relation));

                string requestURL = String.Empty;

                using (System.Xml.XmlReader osmFileXmlReader = System.Xml.XmlReader.Create(osmFileLocation))
                {
                    osmFileXmlReader.MoveToContent();

                    while (osmFileXmlReader.Read())
                    {
                        if (osmFileXmlReader.IsStartElement())
                        {
                            if (osmFileXmlReader.Name == "node")
                            {
                                string currentNodeString = osmFileXmlReader.ReadOuterXml();
                                // turn the xml node representation into a node class representation
                                node currentNode = nodeSerializer.Deserialize(new System.IO.StringReader(currentNodeString)) as node;
                                if (!nodeList.Contains(currentNode.id))
                                {
                                    nodeList.Add(currentNode.id);
                                }
                            }
                            else if (osmFileXmlReader.Name == "way")
                            {
                                string currentWayString = osmFileXmlReader.ReadOuterXml();
                                // turn the xml way representation into a way class representation
                                way currentWay = waySerializer.Deserialize(new System.IO.StringReader(currentWayString)) as way;
                                if (!wayList.Contains(currentWay.id))
                                {
                                    wayList.Add(currentWay.id);
                                }

                                //if (currentWay.nd != null)
                                //{
                                //    foreach (nd wayNd in currentWay.nd)
                                //    {
                                //        if (!nodeList.Contains(wayNd.@ref))
                                //        {
                                //            string resolveMessage = string.Format(resourceManager.GetString("GPTools_OSMGPDownload_resolveid"), resourceManager.GetString("GPTools_OSM_node"), wayNd.@ref);
                                //            message.AddMessage(resolveMessage);
                                //            requestURL = baseURL + "/api/0.6/node/" + wayNd.@ref + "/full";
                                //            string nodeDownloadDocument = downloadOSMDocument(ref message, requestURL, apiCapabilities);
                                //            parseOSMDocument(nodeDownloadDocument, ref message, ref nodeList, ref wayList, ref relationList, ref downloadedDocuments, baseURL, apiCapabilities);
                                //            downloadedDocuments.Insert(1, nodeDownloadDocument);
                                //        }
                                //    }
                                //}
                            }
                            else if (osmFileXmlReader.Name == "relation")
                            {
                                string currentRelationString = osmFileXmlReader.ReadOuterXml();
                                // turn the xml way representation into a way class representation
                                relation currentRelation = relationSerializer.Deserialize(new System.IO.StringReader(currentRelationString)) as relation;
                                if (!relationList.Contains(currentRelation.id))
                                {
                                    relationList.Add(currentRelation.id);
                                }

                                foreach (object relationItem in currentRelation.Items)
                                {
                                    if (relationItem is member)
                                    {
                                        //if (((member)relationItem).type == memberType.node)
                                        //{
                                        //    string memberNodeID = ((member)relationItem).@ref;
                                        //    if (!nodeList.Contains(memberNodeID))
                                        //    {
                                        //        string resolveMessage = string.Format(resourceManager.GetString("GPTools_OSMGPDownload_resolveid"), resourceManager.GetString("GPTools_OSM_node"), memberNodeID);
                                        //        message.AddMessage(resolveMessage);
                                        //        requestURL = baseURL + "/api/0.6/node/" + memberNodeID + "/full";
                                        //        string nodeDownloadDocument = downloadOSMDocument(ref message, requestURL, apiCapabilities);
                                        //        parseOSMDocument(nodeDownloadDocument, ref message, ref nodeList, ref wayList, ref relationList, ref downloadedDocuments, baseURL, apiCapabilities);
                                        //        downloadedDocuments.Insert(1, nodeDownloadDocument);
                                        //    }
                                        //}
                                        if (((member)relationItem).type == memberType.way)
                                        {
                                            string memberWayID = ((member)relationItem).@ref;
                                            if (!wayList.Contains(memberWayID))
                                            {
                                                string resolveMessage = string.Format(resourceManager.GetString("GPTools_OSMGPDownload_resolveid"), resourceManager.GetString("GPTools_OSM_way"), memberWayID);
                                                message.AddMessage(resolveMessage);
                                                requestURL = baseURL + "/api/0.6/way/" + memberWayID + "/full";
                                                string wayDownloadDocument = downloadOSMDocument(ref message, requestURL, apiCapabilities);
                                                if (!String.IsNullOrEmpty(wayDownloadDocument))
                                                {
                                                    parseOSMDocument(wayDownloadDocument, ref message, ref nodeList, ref wayList, ref relationList, ref downloadedDocuments, baseURL, apiCapabilities);
                                                    downloadedDocuments.Insert(1, wayDownloadDocument);
                                                }
                                            }
                                        }
                                        else if (((member)relationItem).type == memberType.relation)
                                        {
                                            string memberRelationID = ((member)relationItem).@ref;
                                            if (!wayList.Contains(memberRelationID))
                                            {
                                                string resolveMessage = string.Format(resourceManager.GetString("GPTools_OSMGPDownload_resolveid"), resourceManager.GetString("GPTools_OSM_relation"), memberRelationID);
                                                message.AddMessage(resolveMessage);
                                                requestURL = baseURL + "/api/0.6/relation/" + memberRelationID + "/full";
                                                string relationDownloadDocument = downloadOSMDocument(ref message, requestURL, apiCapabilities);
                                                if (!String.IsNullOrEmpty(relationDownloadDocument))
                                                {
                                                    parseOSMDocument(relationDownloadDocument, ref message, ref nodeList, ref wayList, ref relationList, ref downloadedDocuments, baseURL, apiCapabilities);
                                                    downloadedDocuments.Insert(1, relationDownloadDocument);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    osmFileXmlReader.Close();
                }
            }
            catch (Exception ex)
            {
                message.AddError(120051, ex.Message);
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                IJTXConfiguration3 configMgr = WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXMapEdit        wmxMapDoc = configMgr.GetJTXMap(this.m_targetName) as IJTXMapEdit;

                // If we're not allowed to overwrite an existing map document, then do some error checking
                if (!this.m_overwriteExisting && wmxMapDoc != null)
                {
                    msgs.AddWarning("Did not overwrite Map Document: " + this.m_targetName);
                    return;
                }
                else if (wmxMapDoc != null)
                {
                    msgs.AddMessage("Replacing Map Document '" + this.m_targetName + "' in database...");
                }
                else // wmxMapDoc == null
                {
                    msgs.AddMessage("Adding Map Document '" + this.m_targetName + "' to database...");
                    wmxMapDoc = configMgr.CreateJTXMap() as IJTXMapEdit;
                }

                IMapDocument mapDoc = new MapDocumentClass() as IMapDocument;
                mapDoc.Open(this.m_mxdFilePath, string.Empty);
                wmxMapDoc.Name = this.m_targetName;
                if (!string.IsNullOrEmpty(this.m_targetCategory))
                {
                    wmxMapDoc.Category = this.m_targetCategory;
                }
                if (!string.IsNullOrEmpty(this.m_description))
                {
                    wmxMapDoc.Description = this.m_description;
                }
                wmxMapDoc.Directory   = string.Empty;
                wmxMapDoc.FileName    = string.Empty;
                wmxMapDoc.MapDocument = mapDoc;
                wmxMapDoc.Store();
                mapDoc.Close();

                // Update the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_TARGET_NAME);
                IGPString         outValue     = new GPStringClass();
                outValue.Value     = m_targetName;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_MXD_UPLOAD_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }
Exemplo n.º 25
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                // Create the object needed to get at the data workspaces
                IJTXDatabaseConnectionManager dbConnectionManager = new JTXDatabaseConnectionManagerClass();
                IJTXDatabaseConnection        dbConnection        = dbConnectionManager.GetConnection(this.WmxDatabase.Alias);

                // Loop through the data workspaces until we find the one we're looking for, then
                // delete it
                bool removedWorkspace = false;
                for (int i = 0; i < dbConnection.DataWorkspaceCount; i++)
                {
                    IJTXWorkspaceConfiguration workspaceCfg = dbConnection.get_DataWorkspace(i);
                    if (workspaceCfg.Name.Equals(m_dataWorkspace))
                    {
                        dbConnection.RemoveDataWorkspace(i);
                        removedWorkspace = true;
                        break;
                    }
                }

                // Raise an error if we somehow couldn't find it (should never happen with
                // the domain on the input param)
                if (!removedWorkspace)
                {
                    throw new WmauException(WmauErrorCodes.C_WORKSPACE_NOT_FOUND_ERROR);
                }

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_DATA_WORKSPACE);
                IGPString         outParamStr  = new GPStringClass();
                outParamStr.Value  = m_dataWorkspace;
                outParamEdit.Value = outParamStr as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            finally
            {
                // Release any COM objects here!
            }
        }
Exemplo n.º 26
0
        internal void loadOSMNodes(string osmFileLocation, ref ITrackCancel TrackCancel, ref IGPMessages message,IGPValue targetGPValue,  IFeatureClass osmPointFeatureClass, bool conserveMemory, bool fastLoad, int nodeCapacity, ref Dictionary<string, simplePointRef> osmNodeDictionary, IFeatureWorkspace featureWorkspace, ISpatialReference downloadSpatialReference, OSMDomains availableDomains, bool checkForExisting)
        {
            XmlReader osmFileXmlReader = null;
            XmlSerializer nodeSerializer = null;

            try
            {
                osmFileXmlReader = System.Xml.XmlReader.Create(osmFileLocation);
                nodeSerializer = new XmlSerializer(typeof(node));

                ISpatialReferenceFactory spatialRef = new SpatialReferenceEnvironmentClass();
                ISpatialReference wgs84 = spatialRef.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

                bool shouldProject = !((IClone)wgs84).IsEqual((IClone)downloadSpatialReference);

                int osmPointIDFieldIndex = osmPointFeatureClass.FindField("OSMID");
                Dictionary<string, int> osmPointDomainAttributeFieldIndices = new Dictionary<string, int>();
                Dictionary<string, int> osmPointDomainAttributeFieldLength = new Dictionary<string, int>();

                foreach (var domains in availableDomains.domain)
                {
                    int currentFieldIndex = osmPointFeatureClass.FindField(domains.name);

                    if (currentFieldIndex != -1)
                    {
                        osmPointDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex);
                        osmPointDomainAttributeFieldLength.Add(domains.name, osmPointFeatureClass.Fields.get_Field(currentFieldIndex).Length);
                    }
                }

                int tagCollectionPointFieldIndex = osmPointFeatureClass.FindField("osmTags");
                int osmUserPointFieldIndex = osmPointFeatureClass.FindField("osmuser");
                int osmUIDPointFieldIndex = osmPointFeatureClass.FindField("osmuid");
                int osmVisiblePointFieldIndex = osmPointFeatureClass.FindField("osmvisible");
                int osmVersionPointFieldIndex = osmPointFeatureClass.FindField("osmversion");
                int osmChangesetPointFieldIndex = osmPointFeatureClass.FindField("osmchangeset");
                int osmTimeStampPointFieldIndex = osmPointFeatureClass.FindField("osmtimestamp");
                int osmMemberOfPointFieldIndex = osmPointFeatureClass.FindField("osmMemberOf");
                int osmSupportingElementPointFieldIndex = osmPointFeatureClass.FindField("osmSupportingElement");
                int osmWayRefCountFieldIndex = osmPointFeatureClass.FindField("wayRefCount");

                // set up the progress indicator
                IStepProgressor stepProgressor = TrackCancel as IStepProgressor;

                if (stepProgressor != null)
                {
                    stepProgressor.MinRange = 0;
                    stepProgressor.MaxRange = nodeCapacity;
                    stepProgressor.StepValue = (1);
                    stepProgressor.Message = _resourceManager.GetString("GPTools_OSMGPFileReader_loadingNodes");
                    stepProgressor.Position = 0;
                    stepProgressor.Show();
                }

                // flag to determine if a computation of indices is required
                bool indexBuildRequired = false;
                if (nodeCapacity > 0)
                    indexBuildRequired = true;

                int pointCount = 0;

                // let's insert all the points first
                if (osmPointFeatureClass != null)
                {
                    IFeatureBuffer pointFeature = null;
                    IFeatureClassLoad pointFeatureLoad = null;

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        using (SchemaLockManager schemaLockManager = new SchemaLockManager(osmPointFeatureClass as ITable))
                        {

                            if (((IWorkspace)featureWorkspace).WorkspaceFactory.WorkspaceType == esriWorkspaceType.esriRemoteDatabaseWorkspace)
                            {
                                pointFeatureLoad = osmPointFeatureClass as IFeatureClassLoad;
                            }

                            IFeatureCursor pointInsertCursor = osmPointFeatureClass.Insert(true);
                            comReleaser.ManageLifetime(pointInsertCursor);

                            pointFeature = osmPointFeatureClass.CreateFeatureBuffer();
                            comReleaser.ManageLifetime(pointFeature);

                            if (pointFeatureLoad != null)
                            {
                                pointFeatureLoad.LoadOnlyMode = true;
                            }

                            osmFileXmlReader.MoveToContent();

                            while (osmFileXmlReader.Read())
                            {
                                if (osmFileXmlReader.IsStartElement())
                                {
                                    if (osmFileXmlReader.Name == "node")
                                    {
                                        string currentNodeString = osmFileXmlReader.ReadOuterXml();
                                        // turn the xml node representation into a node class representation
                                        ESRI.ArcGIS.OSM.OSMClassExtension.node currentNode = null;
                                        using (StringReader nodeReader = new System.IO.StringReader(currentNodeString))
                                        {
                                            currentNode = nodeSerializer.Deserialize(nodeReader) as ESRI.ArcGIS.OSM.OSMClassExtension.node;
                                        }

                                        // check if a feature with the same OSMID already exists, because the can only be one
                                        if (checkForExisting == true)
                                        {
                                            if (CheckIfExists(osmPointFeatureClass as ITable, currentNode.id))
                                            {
                                                continue;
                                            }
                                        }

                                        try
                                        {
                                            //if (pointFeature == null)
                                            //{
                                            pointFeature = osmPointFeatureClass.CreateFeatureBuffer();
                                            comReleaser.ManageLifetime(pointFeature);
                                            //}

                                            IPoint pointGeometry = new PointClass();
                                            comReleaser.ManageLifetime(pointGeometry);

                                            pointGeometry.X = Convert.ToDouble(currentNode.lon, new CultureInfo("en-US"));
                                            pointGeometry.Y = Convert.ToDouble(currentNode.lat, new CultureInfo("en-US"));
                                            pointGeometry.SpatialReference = wgs84;

                                            if (shouldProject)
                                            {
                                                pointGeometry.Project(downloadSpatialReference);
                                            }

                                            pointFeature.Shape = pointGeometry;

                                            pointFeature.set_Value(osmPointIDFieldIndex, currentNode.id);

                                            string isSupportingNode = "";
                                            if (_osmUtility.DoesHaveKeys(currentNode))
                                            {
                                                // if case it has tags I assume that the node presents an entity of it own,
                                                // hence it is not a supporting node in the context of supporting a way or relation
                                                isSupportingNode = "no";

                                                if (conserveMemory == false)
                                                {
                                                    osmNodeDictionary[currentNode.id] = new simplePointRef(Convert.ToSingle(currentNode.lon, new CultureInfo("en-US")), Convert.ToSingle(currentNode.lat, new CultureInfo("en-US")), 0, 0);
                                                }
                                            }
                                            else
                                            {
                                                // node has no tags -- at this point I assume that the absence of tags indicates that it is a supporting node
                                                // for a way or a relation
                                                isSupportingNode = "yes";

                                                if (conserveMemory == false)
                                                {
                                                    osmNodeDictionary[currentNode.id] = new simplePointRef(Convert.ToSingle(currentNode.lon, new CultureInfo("en-US")), Convert.ToSingle(currentNode.lat, new CultureInfo("en-US")), 0, 0);
                                                }
                                            }

                                            insertTags(osmPointDomainAttributeFieldIndices, osmPointDomainAttributeFieldLength, tagCollectionPointFieldIndex, pointFeature, currentNode.tag);

                                            if (fastLoad == false)
                                            {
                                                if (osmSupportingElementPointFieldIndex > -1)
                                                {
                                                    pointFeature.set_Value(osmSupportingElementPointFieldIndex, isSupportingNode);
                                                }

                                                if (osmWayRefCountFieldIndex > -1)
                                                {
                                                    pointFeature.set_Value(osmWayRefCountFieldIndex, 0);
                                                }

                                                // store the administrative attributes
                                                // user, uid, version, changeset, timestamp, visible
                                                if (osmUserPointFieldIndex > -1)
                                                {
                                                    if (!String.IsNullOrEmpty(currentNode.user))
                                                    {
                                                        pointFeature.set_Value(osmUserPointFieldIndex, currentNode.user);
                                                    }
                                                }

                                                if (osmUIDPointFieldIndex > -1)
                                                {
                                                    if (!String.IsNullOrEmpty(currentNode.uid))
                                                    {
                                                        pointFeature.set_Value(osmUIDPointFieldIndex, Convert.ToInt32(currentNode.uid));
                                                    }
                                                }

                                                if (osmVisiblePointFieldIndex > -1)
                                                {
                                                    pointFeature.set_Value(osmVisiblePointFieldIndex, currentNode.visible.ToString());
                                                }

                                                if (osmVersionPointFieldIndex > -1)
                                                {
                                                    if (!String.IsNullOrEmpty(currentNode.version))
                                                    {
                                                        pointFeature.set_Value(osmVersionPointFieldIndex, Convert.ToInt32(currentNode.version));
                                                    }
                                                }

                                                if (osmChangesetPointFieldIndex > -1)
                                                {
                                                    if (!String.IsNullOrEmpty(currentNode.changeset))
                                                    {
                                                        pointFeature.set_Value(osmChangesetPointFieldIndex, Convert.ToInt32(currentNode.changeset));
                                                    }
                                                }

                                                if (osmTimeStampPointFieldIndex > -1)
                                                {
                                                    if (!String.IsNullOrEmpty(currentNode.timestamp))
                                                    {
                                                        try
                                                        {
                                                            pointFeature.set_Value(osmTimeStampPointFieldIndex, Convert.ToDateTime(currentNode.timestamp));
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_invalidTimeFormat"), ex.Message));
                                                        }
                                                    }
                                                }
                                            }
                                            try
                                            {
                                                pointInsertCursor.InsertFeature(pointFeature);
                                                pointCount = pointCount + 1;

                                                if (stepProgressor != null)
                                                {
                                                    stepProgressor.Position = pointCount;
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                System.Diagnostics.Debug.WriteLine(ex.Message);
                                                message.AddWarning(ex.Message);
                                            }

                                            if (TrackCancel.Continue() == false)
                                            {
                                                return;
                                            }

                                            if ((pointCount % 50000) == 0)
                                            {
                                                message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_pointsloaded"), pointCount));
                                                pointInsertCursor.Flush();
                                                System.GC.Collect();
                                            }

                                            if (pointGeometry != null)
                                                Marshal.ReleaseComObject(pointGeometry);
                                        }
                                        catch (Exception ex)
                                        {
                                            System.Diagnostics.Debug.WriteLine(ex.Message);
                                            message.AddWarning(ex.Message);
                                        }
                                        finally
                                        {
                                            if (pointFeature != null)
                                            {
                                                Marshal.ReleaseComObject(pointFeature);

                                                if (pointFeature != null)
                                                    pointFeature = null;

                                            }
                                        }

                                        currentNode = null;
                                    }
                                }
                            }

                            if (stepProgressor != null)
                            {
                                stepProgressor.Hide();
                            }

                            pointInsertCursor.Flush();
                            osmFileXmlReader.Close();

                            message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_pointsloaded"), pointCount));
                            message.AddMessage(_resourceManager.GetString("GPTools_buildingpointidx"));

                            if (pointFeatureLoad != null)
                            {
                                pointFeatureLoad.LoadOnlyMode = false;
                            }
                        }
                    }

                    if (TrackCancel.Continue() == false)
                    {
                        return;
                    }

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        IFeatureCursor updatePoints = osmPointFeatureClass.Update(null, false);
                        comReleaser.ManageLifetime(updatePoints);

                        IFeature feature2Update = updatePoints.NextFeature();

                        while (feature2Update != null)
                        {
                            IPoint pointGeometry = feature2Update.Shape as IPoint;
                            pointGeometry.ID = feature2Update.OID;
                            feature2Update.Shape = pointGeometry;

                            if (conserveMemory == false)
                            {
                                string osmid = Convert.ToString(feature2Update.get_Value(osmPointIDFieldIndex));
                                if (osmNodeDictionary.ContainsKey(osmid))
                                    osmNodeDictionary[osmid].pointObjectID = feature2Update.OID;
                            }

                            updatePoints.UpdateFeature(feature2Update);

                            if (TrackCancel.Continue() == false)
                            {
                                return;
                            }

                            if (feature2Update != null)
                                Marshal.ReleaseComObject(feature2Update);

                            if (pointGeometry != null)
                                Marshal.ReleaseComObject(pointGeometry);

                            feature2Update = updatePoints.NextFeature();
                        }
                    }

                    if (indexBuildRequired)
                    {

                        IGeoProcessor2 geoProcessor = new GeoProcessorClass();
                        bool storedOriginal = geoProcessor.AddOutputsToMap;

                        try
                        {
                            IGPUtilities3 gpUtilities3 = new GPUtilitiesClass();

                            IGPValue pointFeatureClass = gpUtilities3.MakeGPValueFromObject(osmPointFeatureClass);

                            string fcLocation = GetLocationString(targetGPValue, osmPointFeatureClass);

                            IIndexes featureClassIndexes = osmPointFeatureClass.Indexes;
                            int indexPosition = -1;
                            featureClassIndexes.FindIndex("osmID_IDX", out indexPosition);

                            if (indexPosition == -1)
                            {
                                {
                                    geoProcessor.AddOutputsToMap = false;

                                    IVariantArray parameterArrary = CreateAddIndexParameterArray(fcLocation, "OSMID", "osmID_IDX", "UNIQUE", "");
                                    IGeoProcessorResult2 gpResults2 = geoProcessor.Execute("AddIndex_management", parameterArrary, TrackCancel) as IGeoProcessorResult2;
                                }
                            }
                            if (pointCount > 500)
                            {
                                if (pointFeatureLoad == null)
                                {
                                    UpdateSpatialGridIndex(TrackCancel, message, geoProcessor, fcLocation);
                                }
                            }
                        }
                        catch (COMException comEx)
                        {
                            message.AddWarning(comEx.Message);
                        }
                        catch (Exception ex)
                        {
                            message.AddWarning(ex.Message);
                        }
                        finally
                        {
                            geoProcessor.AddOutputsToMap = storedOriginal;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                message.AddError(120100, String.Format(_resourceManager.GetString("GPTools_Utility_NodeLoadError"), ex.Message));
            }
            finally
            {
                if (osmFileXmlReader != null)
                    osmFileXmlReader = null;

                if (nodeSerializer != null)
                    nodeSerializer = null;
            }
        }
        // Execute: Execute the function given the array of the parameters
        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            // Get the first Input Parameter
            IGPParameter parameter = (IGPParameter)paramvalues.get_Element(0);

            // UnPackGPValue. This ensures you get the value either form the dataelement or GpVariable (ModelBuilder)
            IGPValue parameterValue = m_GPUtilities.UnpackGPValue(parameter);

            // Open Input Dataset
            IFeatureClass inputFeatureClass;
            IQueryFilter  qf;

            m_GPUtilities.DecodeFeatureLayer(parameterValue, out inputFeatureClass, out qf);

            if (inputFeatureClass == null)
            {
                message.AddError(2, "Could not open input dataset.");
                return;
            }

            // Add the field if it does not exist.
            int indexA;

            parameter = (IGPParameter)paramvalues.get_Element(1);
            string field = parameter.Value.GetAsText();


            indexA = inputFeatureClass.FindField(field);
            if (indexA < 0)
            {
                IFieldEdit fieldEdit = new FieldClass();
                fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                fieldEdit.Name_2 = field;
                inputFeatureClass.AddField(fieldEdit);
            }

            int featcount = inputFeatureClass.FeatureCount(null);

            //Set the properties of the Step Progressor
            IStepProgressor pStepPro = (IStepProgressor)trackcancel;

            pStepPro.MinRange  = 0;
            pStepPro.MaxRange  = featcount;
            pStepPro.StepValue = (1);
            pStepPro.Message   = "Calculating Area";
            pStepPro.Position  = 0;
            pStepPro.Show();

            // Create an Update Cursor
            indexA = inputFeatureClass.FindField(field);
            IFeatureCursor updateCursor  = inputFeatureClass.Update(qf, false);
            IFeature       updateFeature = updateCursor.NextFeature();
            IGeometry      geometry;
            IArea          area;
            double         dArea;

            while (updateFeature != null)
            {
                geometry = updateFeature.Shape;
                area     = (IArea)geometry;
                dArea    = area.Area;
                updateFeature.set_Value(indexA, dArea);
                updateCursor.UpdateFeature(updateFeature);
                updateFeature.Store();
                updateFeature = updateCursor.NextFeature();
                pStepPro.Step();
            }

            pStepPro.Hide();

            // Release the update cursor to remove the lock on the input data.
            System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
        }
        public void Execute(IArray paramvalues, ITrackCancel trackcancel,
                            IGPEnvironmentManager envMgr, IGPMessages messages)
        {
            // Remember the original GP environment settings and temporarily override these settings

            var gpSettings = envMgr as IGeoProcessorSettings;
            bool origAddOutputsToMapSetting = gpSettings.AddOutputsToMap;
            bool origLogHistorySetting = gpSettings.LogHistory;
            gpSettings.AddOutputsToMap = false;
            gpSettings.LogHistory = false;

            // Create the Geoprocessor

            Geoprocessor gp = new Geoprocessor();

            try
            {
                // Validate our values

                IGPMessages validateMessages = ((IGPFunction2)this).Validate(paramvalues, false, envMgr);
                if ((validateMessages as IGPMessage).IsError())
                {
                    messages.AddError(1, "Validate failed");
                    return;
                }

                // Unpack values

                IGPParameter gpParam = paramvalues.get_Element(InputNWFeatureClass) as IGPParameter;
                IGPValue inputNWFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputMNFeatureClass) as IGPParameter;
                IGPValue inputMNFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputMPTable) as IGPParameter;
                IGPValue inputMPTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputSITable) as IGPParameter;
                IGPValue inputSITableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputSPTable) as IGPParameter;
                IGPValue inputSPTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(OutputFileGDB) as IGPParameter;
                IGPValue outputFileGDBValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(OutputFileGDBVersion) as IGPParameter;
                IGPValue outputFileGDBVersionValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputFeatureDatasetName) as IGPParameter;
                IGPValue inputFeatureDatasetNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputNetworkDatasetName) as IGPParameter;
                IGPValue inputNetworkDatasetNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCreateNetworkAttributesInMetric) as IGPParameter;
                IGPValue inputCreateNetworkAttributeInMetricValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCreateTwoDistanceAttributes) as IGPParameter;
                IGPValue inputCreateTwoDistanceAttributesValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputRSTable) as IGPParameter;
                IGPValue inputRSTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTimeZoneIDBaseFieldName) as IGPParameter;
                IGPValue inputTimeZoneIDBaseFieldNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTimeZoneTable) as IGPParameter;
                IGPValue inputTimeZoneTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputCommonTimeZoneForTheEntireDataset) as IGPParameter;
                IGPValue inputCommonTimeZoneForTheEntireDatasetValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputHSNPTable) as IGPParameter;
                IGPValue inputHSNPTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputHSPRTable) as IGPParameter;
                IGPValue inputHSPRTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputRDTable) as IGPParameter;
                IGPValue inputRDTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedFolder) as IGPParameter;
                IGPValue inputLiveTrafficFeedFolderValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedArcGISServerConnection) as IGPParameter;
                IGPValue inputLiveTrafficFeedArcGISServerConnectionValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedGeoprocessingServiceName) as IGPParameter;
                IGPValue inputLiveTrafficFeedGeoprocessingServiceNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLiveTrafficFeedGeoprocessingTaskName) as IGPParameter;
                IGPValue inputLiveTrafficFeedGeoprocessingTaskNameValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLTRTable) as IGPParameter;
                IGPValue inputLTRTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLRSTable) as IGPParameter;
                IGPValue inputLRSTableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputLVCTable) as IGPParameter;
                IGPValue inputLVCTableValue = m_gpUtils.UnpackGPValue(gpParam);

                double fgdbVersion = 0.0;
                if (!(outputFileGDBVersionValue.IsEmpty()))
                    fgdbVersion = Convert.ToDouble(outputFileGDBVersionValue.GetAsText(), System.Globalization.CultureInfo.InvariantCulture);

                bool createNetworkAttributesInMetric = false;
                if (!(inputCreateNetworkAttributeInMetricValue.IsEmpty()))
                    createNetworkAttributesInMetric = ((inputCreateNetworkAttributeInMetricValue.GetAsText()).ToUpper() == "TRUE");

                bool createTwoDistanceAttributes = false;
                if (!(inputCreateTwoDistanceAttributesValue.IsEmpty()))
                    createTwoDistanceAttributes = ((inputCreateTwoDistanceAttributesValue.GetAsText()).ToUpper() == "TRUE");

                if (inputTimeZoneIDBaseFieldNameValue.IsEmpty() ^ inputTimeZoneTableValue.IsEmpty())
                {
                    messages.AddError(1, "The Time Zone ID Base Field Name and the Time Zone Table must be specified together.");
                    return;
                }

                string timeZoneIDBaseFieldName = "";
                bool directedTimeZoneIDFields = false;
                if (!(inputTimeZoneIDBaseFieldNameValue.IsEmpty()))
                {
                    timeZoneIDBaseFieldName = inputTimeZoneIDBaseFieldNameValue.GetAsText();
                    IDETable inputTable = m_gpUtils.DecodeDETable(inputNWFeatureClassValue);
                    if (inputTable.Fields.FindField(timeZoneIDBaseFieldName) == -1)
                    {
                        directedTimeZoneIDFields = true;
                        if (((inputTable.Fields.FindField("FT_" + timeZoneIDBaseFieldName) == -1) || (inputTable.Fields.FindField("TF_" + timeZoneIDBaseFieldName) == -1)))
                        {
                            messages.AddError(1, "Field named " + timeZoneIDBaseFieldName + " does not exist, nor the pair of fields with FT_ and TF_ prefixing " + timeZoneIDBaseFieldName + " does not exist.");
                            return;
                        }
                    }
                }

                string commonTimeZone = "";
                if (!(inputCommonTimeZoneForTheEntireDatasetValue.IsEmpty()))
                    commonTimeZone = inputCommonTimeZoneForTheEntireDatasetValue.GetAsText();

                if (inputHSNPTableValue.IsEmpty() ^ inputHSPRTableValue.IsEmpty())
                {
                    messages.AddError(1, "The HSNP and HSPR tables must be specified together.");
                    return;
                }

                bool agsConnectionIsEmpty = inputLiveTrafficFeedArcGISServerConnectionValue.IsEmpty();
                bool gpServiceNameIsEmpty = inputLiveTrafficFeedGeoprocessingServiceNameValue.IsEmpty();
                bool gpTaskNameIsEmpty = inputLiveTrafficFeedGeoprocessingTaskNameValue.IsEmpty();
                if ((agsConnectionIsEmpty | gpServiceNameIsEmpty | gpTaskNameIsEmpty) ^
                    (agsConnectionIsEmpty & gpServiceNameIsEmpty & gpTaskNameIsEmpty))
                {
                    messages.AddError(1, "The ArcGIS Server Connection, Geoprocessing Service Name, and Geoprocessing Task Name must all be specified together.");
                    return;
                }

                bool feedFolderIsEmpty = inputLiveTrafficFeedFolderValue.IsEmpty();
                if (!(feedFolderIsEmpty | agsConnectionIsEmpty))
                {
                    messages.AddError(1, "The live traffic feed folder and live traffic feed connection cannot be specified together.");
                    return;
                }

                bool usesLiveTraffic = !(inputRDTableValue.IsEmpty());
                if (!usesLiveTraffic ^ (feedFolderIsEmpty & agsConnectionIsEmpty))
                {
                    messages.AddError(1, "The RD table and live traffic feed folder or connection must be specified together.");
                    return;
                }

                ITrafficFeedLocation trafficFeedLocation = null;
                if (usesLiveTraffic)
                {
                    if (feedFolderIsEmpty)
                    {
                        // We're using an ArcGIS Server Connection and Geoprocessing Service/Task

                        ITrafficFeedGPService tfgps = new TrafficFeedGPServiceClass();
                        IName trafficFeedGPServiceName = m_gpUtils.GetNameObject(inputLiveTrafficFeedArcGISServerConnectionValue as IDataElement);
                        tfgps.ConnectionProperties = ((IAGSServerConnectionName)trafficFeedGPServiceName).ConnectionProperties;
                        tfgps.ServiceName = inputLiveTrafficFeedGeoprocessingServiceNameValue.GetAsText();
                        tfgps.TaskName = inputLiveTrafficFeedGeoprocessingTaskNameValue.GetAsText();
                        trafficFeedLocation = tfgps as ITrafficFeedLocation;
                    }
                    else
                    {
                        // We're using a Traffic Feed Folder

                        ITrafficFeedDirectory tfd = new TrafficFeedDirectoryClass();
                        tfd.TrafficDirectory = inputLiveTrafficFeedFolderValue.GetAsText();
                        trafficFeedLocation = tfd as ITrafficFeedLocation;
                    }
                }

                if (inputLRSTableValue.IsEmpty() ^ inputLVCTableValue.IsEmpty())
                {
                    messages.AddError(1, "The LRS and LVC tables must be specified together.");
                    return;
                }

                // Get the path to the output file GDB, and feature dataset and network dataset names

                string outputFileGdbPath = outputFileGDBValue.GetAsText();
                string fdsName = inputFeatureDatasetNameValue.GetAsText();
                string ndsName = inputNetworkDatasetNameValue.GetAsText();

                // Create the new file geodatabase and feature dataset

                AddMessage("Creating the file geodatabase and feature dataset...", messages, trackcancel);

                int lastBackslash = outputFileGdbPath.LastIndexOf("\\");
                CreateFileGDB createFGDBTool = new CreateFileGDB();
                createFGDBTool.out_folder_path = outputFileGdbPath.Remove(lastBackslash);
                createFGDBTool.out_name = outputFileGdbPath.Substring(lastBackslash + 1);
                createFGDBTool.out_version = "9.3";
                gp.Execute(createFGDBTool, trackcancel);

                CreateFeatureDataset createFDSTool = new CreateFeatureDataset();
                createFDSTool.out_dataset_path = outputFileGdbPath;
                createFDSTool.out_name = fdsName;
                createFDSTool.spatial_reference = inputNWFeatureClassValue.GetAsText();
                gp.Execute(createFDSTool, trackcancel);

                // Import the NW feature class to the file geodatabase
                // If we're using ArcInfo, also sort the feature class

                MakeFeatureLayer makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = inputNWFeatureClassValue.GetAsText();
                makeFeatureLayerTool.out_layer = "nw_Layer";
                makeFeatureLayerTool.where_clause = "FEATTYP <> 4165";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                string pathToFds = outputFileGdbPath + "\\" + fdsName;
                string streetsFeatureClassPath = pathToFds + "\\" + StreetsFCName;
                FeatureClassToFeatureClass importFCTool = null;

                IAoInitialize aoi = new AoInitializeClass();
                if (aoi.InitializedProduct() == esriLicenseProductCode.esriLicenseProductCodeAdvanced)
                {
                    AddMessage("Importing and spatially sorting the Streets feature class...", messages, trackcancel);

                    Sort sortTool = new Sort();
                    sortTool.in_dataset = "nw_Layer";
                    sortTool.out_dataset = streetsFeatureClassPath;
                    sortTool.sort_field = "Shape";
                    sortTool.spatial_sort_method = "PEANO";
                    gp.Execute(sortTool, trackcancel);
                }
                else
                {
                    AddMessage("Importing the Streets feature class...", messages, trackcancel);

                    importFCTool = new FeatureClassToFeatureClass();
                    importFCTool.in_features = "nw_Layer";
                    importFCTool.out_path = pathToFds;
                    importFCTool.out_name = StreetsFCName;
                    gp.Execute(importFCTool, trackcancel);
                }

                Delete deleteTool = new Delete();
                deleteTool.in_data = "nw_Layer";
                gp.Execute(deleteTool, trackcancel);

                // Add an index to the Streets feature class's ID field

                AddMessage("Indexing the ID field...", messages, trackcancel);

                AddIndex addIndexTool = new AddIndex();
                addIndexTool.in_table = streetsFeatureClassPath;
                addIndexTool.fields = "ID";
                addIndexTool.index_name = "ID";
                gp.Execute(addIndexTool, trackcancel);

                // Copy the time zones table to the file geodatabase

                TableToTable importTableTool = null;
                if (timeZoneIDBaseFieldName != "")
                {
                    AddMessage("Copying the TimeZones table...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = inputTimeZoneTableValue.GetAsText();
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = "TimeZones";
                    gp.Execute(importTableTool, trackcancel);
                }

                // Initialize common variables that will be used for both
                // processing historical traffic and maneuvers
                AddField addFieldTool = null;
                AddJoin addJoinTool = null;
                RemoveJoin removeJoinTool = null;
                CalculateField calcFieldTool = null;
                TableSelect tableSelectTool = null;
                MakeTableView makeTableViewTool = null;

                #region Process Historical Traffic Tables

                bool usesHistoricalTraffic = false;

                if (!(inputHSNPTableValue.IsEmpty()))
                {
                    usesHistoricalTraffic = true;

                    // Add fields for the weekday/weekend/all-week averages to the Streets feature class

                    AddMessage("Creating fields for the weekday/weekend/all-week averages...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = streetsFeatureClassPath;

                    addFieldTool.field_type = "SHORT";
                    addFieldTool.field_name = "FT_Weekday";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_Weekday";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "FT_Weekend";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_Weekend";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "FT_AllWeek";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_AllWeek";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "FLOAT";
                    addFieldTool.field_name = "FT_WeekdayMinutes";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_WeekdayMinutes";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "FT_WeekendMinutes";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_WeekendMinutes";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "FT_AllWeekMinutes";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_AllWeekMinutes";
                    gp.Execute(addFieldTool, trackcancel);

                    // Separate out the FT and TF speeds into separate tables and index the NETWORK_ID fields

                    AddMessage("Extracting speed information...", messages, trackcancel);

                    string FTSpeedsTablePath = outputFileGdbPath + "\\FT_Speeds";
                    string TFSpeedsTablePath = outputFileGdbPath + "\\TF_Speeds";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputHSNPTableValue.GetAsText();
                    tableSelectTool.out_table = FTSpeedsTablePath;
                    tableSelectTool.where_clause = "VAL_DIR = 2";
                    gp.Execute(tableSelectTool, trackcancel);

                    tableSelectTool.out_table = TFSpeedsTablePath;
                    tableSelectTool.where_clause = "VAL_DIR = 3";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = FTSpeedsTablePath;
                    addIndexTool.fields = "NETWORK_ID";
                    addIndexTool.index_name = "NETWORK_ID";
                    gp.Execute(addIndexTool, trackcancel);
                    addIndexTool.in_table = TFSpeedsTablePath;
                    gp.Execute(addIndexTool, trackcancel);

                    // Calculate the speeds fields

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                    makeFeatureLayerTool.out_layer = "Streets_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "ID";
                    addJoinTool.join_table = FTSpeedsTablePath;
                    addJoinTool.join_field = "NETWORK_ID";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Copying over the FT weekday speeds...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".FT_Weekday";
                    calcFieldTool.expression = "[FT_Speeds.SPWEEKDAY]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Copying over the FT weekend speeds...", messages, trackcancel);

                    calcFieldTool.field = StreetsFCName + ".FT_Weekend";
                    calcFieldTool.expression = "[FT_Speeds.SPWEEKEND]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Copying over the FT all-week speeds...", messages, trackcancel);

                    calcFieldTool.field = StreetsFCName + ".FT_AllWeek";
                    calcFieldTool.expression = "[FT_Speeds.SPWEEK]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "FT_Speeds";
                    gp.Execute(removeJoinTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "ID";
                    addJoinTool.join_table = TFSpeedsTablePath;
                    addJoinTool.join_field = "NETWORK_ID";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Copying over the TF weekday speeds...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".TF_Weekday";
                    calcFieldTool.expression = "[TF_Speeds.SPWEEKDAY]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Copying over the TF weekend speeds...", messages, trackcancel);

                    calcFieldTool.field = StreetsFCName + ".TF_Weekend";
                    calcFieldTool.expression = "[TF_Speeds.SPWEEKEND]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Copying over the TF all-week speeds...", messages, trackcancel);

                    calcFieldTool.field = StreetsFCName + ".TF_AllWeek";
                    calcFieldTool.expression = "[TF_Speeds.SPWEEK]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "TF_Speeds";
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool.in_data = FTSpeedsTablePath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = TFSpeedsTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    // Calculate the travel time fields

                    AddMessage("Calculating the FT weekday travel times...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = streetsFeatureClassPath;

                    calcFieldTool.field = "FT_WeekdayMinutes";
                    calcFieldTool.expression = "[METERS] * 0.06 / s";
                    calcFieldTool.code_block = "s = [FT_Weekday]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the FT weekend travel times...", messages, trackcancel);

                    calcFieldTool.field = "FT_WeekendMinutes";
                    calcFieldTool.expression = "[METERS] * 0.06 / s";
                    calcFieldTool.code_block = "s = [FT_Weekend]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the FT all-week travel times...", messages, trackcancel);

                    calcFieldTool.field = "FT_AllWeekMinutes";
                    calcFieldTool.expression = "[METERS] * 0.06 / s";
                    calcFieldTool.code_block = "s = [FT_AllWeek]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the TF weekday travel times...", messages, trackcancel);

                    calcFieldTool.field = "TF_WeekdayMinutes";
                    calcFieldTool.expression = "[METERS] * 0.06 / s";
                    calcFieldTool.code_block = "s = [TF_Weekday]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the TF weekend travel times...", messages, trackcancel);

                    calcFieldTool.field = "TF_WeekendMinutes";
                    calcFieldTool.expression = "[METERS] * 0.06 / s";
                    calcFieldTool.code_block = "s = [TF_Weekend]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the TF all-week travel times...", messages, trackcancel);

                    calcFieldTool.field = "TF_AllWeekMinutes";
                    calcFieldTool.expression = "[METERS] * 0.06 / s";
                    calcFieldTool.code_block = "s = [TF_AllWeek]\nIf IsNull(s) Then s = [KPH]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    // Create the DailyProfiles table

                    AddMessage("Creating the profiles table...", messages, trackcancel);

                    CreateDailyProfilesTable(m_gpUtils.OpenDataset(inputHSPRTableValue) as ITable, outputFileGdbPath, fgdbVersion);

                    // Copy over the historical traffic records of the HSNP table to the file geodatabase

                    AddMessage("Copying HSNP table to the file geodatabase...", messages, trackcancel);

                    string histTrafficJoinTablePath = outputFileGdbPath + "\\" + HistTrafficJoinTableName;

                    tableSelectTool.out_table = histTrafficJoinTablePath;
                    tableSelectTool.where_clause = "SPFREEFLOW > 0";
                    gp.Execute(tableSelectTool, trackcancel);

                    // Add FCID, FID, and position fields to the Streets_DailyProfiles table

                    AddMessage("Creating fields on the historical traffic join table...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = histTrafficJoinTablePath;

                    addFieldTool.field_type = "LONG";
                    addFieldTool.field_name = "EdgeFCID";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeFID";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "DOUBLE";
                    addFieldTool.field_name = "EdgeFrmPos";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeToPos";
                    gp.Execute(addFieldTool, trackcancel);

                    // If we're creating 10.0, then also create the FreeflowMinutes field

                    if (fgdbVersion == 10.0)
                    {
                        addFieldTool.field_type = "DOUBLE";
                        addFieldTool.field_name = "FreeflowMinutes";
                        gp.Execute(addFieldTool, trackcancel);
                    }

                    // Calculate the fields

                    AddMessage("Calculating the EdgeFrmPos field for historical traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = histTrafficJoinTablePath;

                    calcFieldTool.field = "EdgeFrmPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = "Select Case [VAL_DIR]\n  Case 2: x = 0\n  Case 3: x = 1\nEnd Select";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeToPos field for historical traffic...", messages, trackcancel);

                    calcFieldTool.field = "EdgeToPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = "Select Case [VAL_DIR]\n  Case 2: x = 1\n  Case 3: x = 0\nEnd Select";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFCID field for historical traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = histTrafficJoinTablePath;
                    calcFieldTool.field = "EdgeFCID";
                    calcFieldTool.expression = "1";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFID field for historical traffic...", messages, trackcancel);

                    makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = histTrafficJoinTablePath;
                    makeTableViewTool.out_view = "Streets_DailyProfiles_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_DailyProfiles_View";
                    addJoinTool.in_field = "NETWORK_ID";
                    addJoinTool.join_table = streetsFeatureClassPath;
                    addJoinTool.join_field = "ID";
                    gp.Execute(addJoinTool, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_DailyProfiles_View";
                    calcFieldTool.field = HistTrafficJoinTableName + ".EdgeFID";
                    calcFieldTool.expression = "[" + StreetsFCName + ".OBJECTID]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    if (fgdbVersion == 10.0)
                    {
                        AddMessage("Calculating the FreeflowMinutes field...", messages, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = "Streets_DailyProfiles_View";
                        calcFieldTool.field = HistTrafficJoinTableName + ".FreeflowMinutes";
                        calcFieldTool.expression = "[" + StreetsFCName + ".METERS] * 0.06 / [" + HistTrafficJoinTableName + ".SPFREEFLOW]";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);
                    }

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_DailyProfiles_View";
                    removeJoinTool.join_name = StreetsFCName;
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_DailyProfiles_View";
                    gp.Execute(deleteTool, trackcancel);

                    // Add an index to the Streets feature class's NET2CLASS field

                    AddMessage("Indexing the NET2CLASS field...", messages, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = streetsFeatureClassPath;
                    addIndexTool.fields = "NET2CLASS";
                    addIndexTool.index_name = "NET2CLASS";
                    gp.Execute(addIndexTool, trackcancel);
                }
                #endregion

                #region Process Live Traffic Table

                if (usesLiveTraffic)
                {
                    // Copy the RD table to the file geodatabase

                    AddMessage("Creating the live traffic join table...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = inputRDTableValue.GetAsText();
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = TMCJoinTableName;
                    gp.Execute(importTableTool, trackcancel);

                    string TMCJoinTablePath = outputFileGdbPath + "\\" + TMCJoinTableName;

                    // Add FCID, FID, position, and TMC fields to the Streets_TMC table

                    AddMessage("Creating fields on the live traffic join table...", messages, trackcancel);

                    addFieldTool = new AddField();
                    addFieldTool.in_table = TMCJoinTablePath;

                    addFieldTool.field_type = "LONG";
                    addFieldTool.field_name = "EdgeFCID";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeFID";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "DOUBLE";
                    addFieldTool.field_name = "EdgeFrmPos";
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "EdgeToPos";
                    gp.Execute(addFieldTool, trackcancel);

                    addFieldTool.field_type = "TEXT";
                    addFieldTool.field_length = 9;
                    addFieldTool.field_name = "TMC";
                    gp.Execute(addFieldTool, trackcancel);

                    // Calculate the fields

                    AddMessage("Calculating the TMC field for live traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = TMCJoinTablePath;
                    calcFieldTool.field = "TMC";
                    calcFieldTool.expression = "Right([RDSTMC], 9)";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFrmPos field for live traffic...", messages, trackcancel);

                    calcFieldTool.field = "EdgeFrmPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = "Select Case Left([RDSTMC], 1)\n  Case \"+\": x = 0\n  Case \"-\": x = 1\nEnd Select";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeToPos field for live traffic...", messages, trackcancel);

                    calcFieldTool.field = "EdgeToPos";
                    calcFieldTool.expression = "x";
                    calcFieldTool.code_block = "Select Case Left([RDSTMC], 1)\n  Case \"-\": x = 0\n  Case \"+\": x = 1\nEnd Select";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFCID field for live traffic...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = TMCJoinTablePath;
                    calcFieldTool.field = "EdgeFCID";
                    calcFieldTool.expression = "1";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the EdgeFID field for live traffic...", messages, trackcancel);

                    makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = TMCJoinTablePath;
                    makeTableViewTool.out_view = "Streets_TMC_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_TMC_View";
                    addJoinTool.in_field = "ID";
                    addJoinTool.join_table = streetsFeatureClassPath;
                    addJoinTool.join_field = "ID";
                    gp.Execute(addJoinTool, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_TMC_View";
                    calcFieldTool.field = TMCJoinTableName + ".EdgeFID";
                    calcFieldTool.expression = "[" + StreetsFCName + ".OBJECTID]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_TMC_View";
                    removeJoinTool.join_name = StreetsFCName;
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_TMC_View";
                    gp.Execute(deleteTool, trackcancel);

                    // If Historical Traffic is not being used, then we need to create placeholder historical traffic tables

                    if (!usesHistoricalTraffic)
                    {
                        // Create the Streets_DailyProfiles table by starting with a copy of the Streets_TMC table

                        AddMessage("Creating the historical traffic join table...", messages, trackcancel);

                        importTableTool = new TableToTable();
                        importTableTool.in_rows = TMCJoinTablePath;
                        importTableTool.out_path = outputFileGdbPath;
                        importTableTool.out_name = HistTrafficJoinTableName;
                        gp.Execute(importTableTool, trackcancel);

                        string histTrafficJoinTablePath = outputFileGdbPath + "\\" + HistTrafficJoinTableName;

                        AddMessage("Creating and calculating the KPH field on the historical traffic join table...", messages, trackcancel);

                        addFieldTool = new AddField();
                        addFieldTool.in_table = histTrafficJoinTablePath;
                        addFieldTool.field_type = "SHORT";
                        addFieldTool.field_name = "KPH";
                        gp.Execute(addFieldTool, trackcancel);

                        makeTableViewTool = new MakeTableView();
                        makeTableViewTool.in_table = histTrafficJoinTablePath;
                        makeTableViewTool.out_view = "Streets_DailyProfiles_View";
                        gp.Execute(makeTableViewTool, trackcancel);

                        addJoinTool = new AddJoin();
                        addJoinTool.in_layer_or_view = "Streets_DailyProfiles_View";
                        addJoinTool.in_field = "EdgeFID";
                        addJoinTool.join_table = streetsFeatureClassPath;
                        addJoinTool.join_field = "OBJECTID";
                        gp.Execute(addJoinTool, trackcancel);

                        calcFieldTool = new CalculateField();
                        calcFieldTool.in_table = "Streets_DailyProfiles_View";
                        calcFieldTool.field = HistTrafficJoinTableName + ".KPH";
                        calcFieldTool.expression = "[" + StreetsFCName + ".KPH]";
                        calcFieldTool.expression_type = "VB";
                        gp.Execute(calcFieldTool, trackcancel);

                        removeJoinTool = new RemoveJoin();
                        removeJoinTool.in_layer_or_view = "Streets_DailyProfiles_View";
                        removeJoinTool.join_name = StreetsFCName;
                        gp.Execute(removeJoinTool, trackcancel);

                        deleteTool = new Delete();
                        deleteTool.in_data = "Streets_DailyProfiles_View";
                        gp.Execute(deleteTool, trackcancel);

                        AddMessage("Creating and calculating the PROFILE fields on the historical traffic join table...", messages, trackcancel);

                        for (int i = 1; i <= 7; i++)
                        {
                            string fieldName = "PROFILE_" + Convert.ToString(i, System.Globalization.CultureInfo.InvariantCulture);

                            addFieldTool = new AddField();
                            addFieldTool.in_table = histTrafficJoinTablePath;
                            addFieldTool.field_type = "SHORT";
                            addFieldTool.field_name = fieldName;
                            gp.Execute(addFieldTool, trackcancel);

                            calcFieldTool = new CalculateField();
                            calcFieldTool.in_table = histTrafficJoinTablePath;
                            calcFieldTool.field = fieldName;
                            calcFieldTool.expression = "1";
                            calcFieldTool.expression_type = "VB";
                            gp.Execute(calcFieldTool, trackcancel);
                        }

                        // Create the Profiles table

                        CreateNonHistoricalDailyProfilesTable(outputFileGdbPath);
                    }
                }
                #endregion

                // Copy the MN feature class to the file geodatabase

                AddMessage("Copying the Maneuvers feature class and indexing...", messages, trackcancel);

                importFCTool = new FeatureClassToFeatureClass();
                importFCTool.in_features = inputMNFeatureClassValue.GetAsText();
                importFCTool.out_path = outputFileGdbPath;
                importFCTool.out_name = "mn";
                gp.Execute(importFCTool, trackcancel);

                string mnFeatureClassPath = outputFileGdbPath + "\\mn";

                addIndexTool = new AddIndex();
                addIndexTool.in_table = mnFeatureClassPath;
                addIndexTool.fields = "ID";
                addIndexTool.index_name = "ID";
                gp.Execute(addIndexTool, trackcancel);

                // Copy the MP table to the file geodatabase

                AddMessage("Copying the Maneuver Path table...", messages, trackcancel);

                importTableTool = new TableToTable();
                importTableTool.in_rows = inputMPTableValue.GetAsText();
                importTableTool.out_path = outputFileGdbPath;
                importTableTool.out_name = "mp";
                gp.Execute(importTableTool, trackcancel);

                string mpTablePath = outputFileGdbPath + "\\mp";

                // Add and calculate the at junction and feature type fields to the MP table

                AddMessage("Creating and calculating fields for the maneuver types and at junctions...", messages, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = mpTablePath;
                addFieldTool.field_name = "JNCTID";
                addFieldTool.field_type = "DOUBLE";
                gp.Execute(addFieldTool, trackcancel);

                addFieldTool.field_name = "FEATTYP";
                addFieldTool.field_type = "SHORT";
                gp.Execute(addFieldTool, trackcancel);

                makeTableViewTool = new MakeTableView();
                makeTableViewTool.in_table = mpTablePath;
                makeTableViewTool.out_view = "mp_View";
                gp.Execute(makeTableViewTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "mp_View";
                addJoinTool.in_field = "ID";
                addJoinTool.join_table = mnFeatureClassPath;
                addJoinTool.join_field = "ID";
                gp.Execute(addJoinTool, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "mp_View";
                calcFieldTool.field = "mp.JNCTID";
                calcFieldTool.expression = "[mn.JNCTID]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                calcFieldTool.field = "mp.FEATTYP";
                calcFieldTool.expression = "[mn.FEATTYP]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "mp_View";
                removeJoinTool.join_name = "mn";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "mp_View";
                gp.Execute(deleteTool, trackcancel);

                deleteTool.in_data = mnFeatureClassPath;
                gp.Execute(deleteTool, trackcancel);

                // Extract only the prohibited maneuvers (feature types 2103 and 2101)

                AddMessage("Extracting prohibited maneuvers...", messages, trackcancel);

                string prohibMPwJnctIDTablePath = outputFileGdbPath + "\\ProhibMPwJnctID";
                string tempTablePath = outputFileGdbPath + "\\TempTable";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = mpTablePath;
                tableSelectTool.out_table = prohibMPwJnctIDTablePath;
                tableSelectTool.where_clause = "FEATTYP = 2103";
                gp.Execute(tableSelectTool, trackcancel);

                tableSelectTool.out_table = tempTablePath;
                tableSelectTool.where_clause = "FEATTYP = 2101";
                gp.Execute(tableSelectTool, trackcancel);

                Append appendTool = new Append();
                appendTool.inputs = tempTablePath;
                appendTool.target = prohibMPwJnctIDTablePath;
                appendTool.schema_type = "TEST";
                gp.Execute(appendTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = tempTablePath;
                gp.Execute(deleteTool, trackcancel);

                AddMessage("Creating turn feature class...", messages, trackcancel);

                // Create the turn feature class

                string tempStatsTablePath = outputFileGdbPath + "\\tempStatsTable";

                Statistics statsTool = new Statistics();
                statsTool.in_table = prohibMPwJnctIDTablePath;
                statsTool.out_table = tempStatsTablePath;
                statsTool.statistics_fields = "SEQNR MAX";
                gp.Execute(statsTool, null);

                CreateAndPopulateTurnFeatureClass(outputFileGdbPath, fdsName, "ProhibMPwJnctID", "tempStatsTable",
                                                  messages, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = tempStatsTablePath;
                gp.Execute(deleteTool, trackcancel);

                deleteTool.in_data = prohibMPwJnctIDTablePath;
                gp.Execute(deleteTool, trackcancel);

                GC.Collect();

                // Extract the bifurcations (feature type 9401)

                AddMessage("Extracting bifurcations...", messages, trackcancel);

                string bifurcationMPwJnctIDTablePath = outputFileGdbPath + "\\BifurcationMPwJnctID";

                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = mpTablePath;
                tableSelectTool.out_table = bifurcationMPwJnctIDTablePath;
                tableSelectTool.where_clause = "FEATTYP = 9401";
                gp.Execute(tableSelectTool, trackcancel);

                deleteTool.in_data = mpTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Add and calculate the StreetsOID and the from-/to-junction ID fields to the MP table

                AddMessage("Creating and calculating fields for the StreetsOID and the from-/to-junction IDs...", messages, trackcancel);

                addFieldTool = new AddField();
                addFieldTool.in_table = bifurcationMPwJnctIDTablePath;
                addFieldTool.field_type = "LONG";
                addFieldTool.field_name = "StreetsOID";
                gp.Execute(addFieldTool, trackcancel);

                addFieldTool.field_type = "DOUBLE";
                addFieldTool.field_name = "F_JNCTID";
                gp.Execute(addFieldTool, trackcancel);
                addFieldTool.field_name = "T_JNCTID";
                gp.Execute(addFieldTool, trackcancel);

                makeTableViewTool = new MakeTableView();
                makeTableViewTool.in_table = bifurcationMPwJnctIDTablePath;
                makeTableViewTool.out_view = "bifurcationMP_View";
                gp.Execute(makeTableViewTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "bifurcationMP_View";
                addJoinTool.in_field = "TRPELID";
                addJoinTool.join_table = streetsFeatureClassPath;
                addJoinTool.join_field = "ID";
                gp.Execute(addJoinTool, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "bifurcationMP_View";
                calcFieldTool.field = "BifurcationMPwJnctID.StreetsOID";
                calcFieldTool.expression = "[Streets.OBJECTID]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                calcFieldTool.field = "BifurcationMPwJnctID.F_JNCTID";
                calcFieldTool.expression = "[Streets.F_JNCTID]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                calcFieldTool.field = "BifurcationMPwJnctID.T_JNCTID";
                calcFieldTool.expression = "[Streets.T_JNCTID]";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "bifurcationMP_View";
                removeJoinTool.join_name = "Streets";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "bifurcationMP_View";
                gp.Execute(deleteTool, trackcancel);

                AddMessage("Creating RoadSplits table...", messages, trackcancel);

                // Create the RoadSplits table

                CreateAndPopulateRoadSplitsTable(outputFileGdbPath, "BifurcationMPwJnctID", messages, trackcancel);

                deleteTool.in_data = bifurcationMPwJnctIDTablePath;
                gp.Execute(deleteTool, trackcancel);

                GC.Collect();

                #region Process Restrictions Table

                bool usesRSTable = false;

                if (!(inputRSTableValue.IsEmpty()))
                {
                    usesRSTable = true;

                    // Extract the information from the Restrictions table

                    AddMessage("Extracting information from the Restrictions table...", messages, trackcancel);

                    string rsTablePath = outputFileGdbPath + "\\rs";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputRSTableValue.GetAsText();
                    tableSelectTool.out_table = rsTablePath;
                    tableSelectTool.where_clause = "RESTRTYP = 'DF' OR FEATTYP IN (2101, 2103)";
                    gp.Execute(tableSelectTool, trackcancel);

                    // Create and populate fields for the Streets

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                    makeFeatureLayerTool.out_layer = "Streets_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    CreateAndPopulateRSField(outputFileGdbPath, false, "FT_AllVehicles_Restricted",
                                             "DIR_POS IN (1, 2) AND VT = 0", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "TF_AllVehicles_Restricted",
                                             "DIR_POS IN (1, 3) AND VT = 0", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "FT_PassengerCars_Restricted",
                                             "DIR_POS IN (1, 2) AND VT = 11", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "TF_PassengerCars_Restricted",
                                             "DIR_POS IN (1, 3) AND VT = 11", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "FT_ResidentialVehicles_Restricted",
                                             "DIR_POS IN (1, 2) AND VT = 12", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "TF_ResidentialVehicles_Restricted",
                                             "DIR_POS IN (1, 3) AND VT = 12", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "FT_Taxis_Restricted",
                                             "DIR_POS IN (1, 2) AND VT = 16", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "TF_Taxis_Restricted",
                                             "DIR_POS IN (1, 3) AND VT = 16", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "FT_PublicBuses_Restricted",
                                             "DIR_POS IN (1, 2) AND VT = 17", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, false, "TF_PublicBuses_Restricted",
                                             "DIR_POS IN (1, 3) AND VT = 17", gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    // Create and populate fields for the RestrictedTurns

                    string pathToTurnFC = pathToFds + "\\" + TurnFCName;

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = pathToTurnFC;
                    makeFeatureLayerTool.out_layer = "RestrictedTurns_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    CreateAndPopulateRSField(outputFileGdbPath, true, "AllVehicles_Restricted", "VT = 0 OR RESTRTYP = '8I'", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, true, "PassengerCars_Restricted", "VT = 11", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, true, "ResidentialVehicles_Restricted", "VT = 12", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, true, "Taxis_Restricted", "VT = 16", gp, messages, trackcancel);
                    CreateAndPopulateRSField(outputFileGdbPath, true, "PublicBuses_Restricted", "VT = 17", gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "RestrictedTurns_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = rsTablePath;
                    gp.Execute(deleteTool, trackcancel);
                }
                #endregion

                #region Process Logistics Truck Routes Table

                bool usesLTRTable = false;

                if (!(inputLTRTableValue.IsEmpty()))
                {
                    usesLTRTable = true;

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = streetsFeatureClassPath;
                    makeFeatureLayerTool.out_layer = "Streets_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    if (fgdbVersion >= 10.1)
                    {
                        CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                                  "NationalSTAARoute", "PREFERRED = 1", gp, messages, trackcancel);
                        CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                                  "NationalRouteAccess", "PREFERRED = 2", gp, messages, trackcancel);
                        CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                                  "DesignatedTruckRoute", "PREFERRED = 3", gp, messages, trackcancel);
                        CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                                  "TruckBypassRoad", "PREFERRED = 4", gp, messages, trackcancel);
                    }

                    CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                              "NoCommercialVehicles", "RESTRICTED = 1", gp, messages, trackcancel);
                    CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                              "ImmediateAccessOnly", "RESTRICTED = 2", gp, messages, trackcancel);
                    CreateAndPopulateLTRField(outputFileGdbPath, inputLTRTableValue.GetAsText(),
                                              "TrucksRestricted", "RESTRICTED = 3", gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Layer";
                    gp.Execute(deleteTool, trackcancel);
                }
                #endregion

                #region Process Logistics Restrictions Table

                bool usesLRSTable = false;
                string lrsStatsTablePath = outputFileGdbPath + "\\lrs_Stats";

                if (!(inputLRSTableValue.IsEmpty()))
                {
                    usesLRSTable = true;

                    // Copy the LRS table to the file geodatabase

                    AddMessage("Copying the Logistics Restrictions (LRS) table...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = inputLRSTableValue.GetAsText();
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = "lrs";
                    gp.Execute(importTableTool, trackcancel);

                    string lrsTablePath = outputFileGdbPath + "\\lrs";

                    // Copy the SUBSEQNR 1 rows of the LVC table to the file geodatabase

                    AddMessage("Copying the Logistics Vehicle Characteristics (LVC) table...", messages, trackcancel);

                    string lvcTablePath = outputFileGdbPath + "\\lvc";

                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputLVCTableValue.GetAsText();
                    tableSelectTool.out_table = lvcTablePath;
                    tableSelectTool.where_clause = "SUBSEQNR = 1";
                    gp.Execute(tableSelectTool, trackcancel);

                    // Add and calculate join fields on the LRS and LVC tables

                    addFieldTool = new AddField();
                    addFieldTool.field_name = "ID_SEQNR";
                    addFieldTool.field_length = 19;
                    addFieldTool.field_type = "TEXT";
                    addFieldTool.in_table = lrsTablePath;
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.in_table = lvcTablePath;
                    gp.Execute(addFieldTool, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.field = "ID_SEQNR";
                    calcFieldTool.expression = "[ID] & \".\" & [SEQNR]";
                    calcFieldTool.expression_type = "VB";

                    AddMessage("Calculating the join field on the LRS table...", messages, trackcancel);

                    calcFieldTool.in_table = lrsTablePath;
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the join field on the LVC table...", messages, trackcancel);

                    calcFieldTool.in_table = lvcTablePath;
                    gp.Execute(calcFieldTool, trackcancel);

                    // Index the join field on the LVC table

                    AddMessage("Indexing the join field...", messages, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = lvcTablePath;
                    addIndexTool.fields = "ID_SEQNR";
                    addIndexTool.index_name = "ID_SEQNR";
                    gp.Execute(addIndexTool, trackcancel);

                    // Join the LRS and LVC tables together, and only extract those rows from
                    // the LRS table that do not have an accompanying row in the LVC table

                    AddMessage("Simplifying the LRS table...", messages, trackcancel);

                    makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = lrsTablePath;
                    makeTableViewTool.out_view = "lrs_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "lrs_View";
                    addJoinTool.in_field = "ID_SEQNR";
                    addJoinTool.join_table = lvcTablePath;
                    addJoinTool.join_field = "ID_SEQNR";
                    gp.Execute(addJoinTool, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = "lrs_View";
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = "lrs_simplified";
                    importTableTool.where_clause = "lvc.OBJECTID IS NULL";
                    importTableTool.field_mapping = "ID \"ID\" true true false 8 Double 0 0 ,First,#," + lrsTablePath + ",lrs.ID,-1,-1;" +
                                                    "SEQNR \"SEQNR\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.SEQNR,-1,-1;" +
                                                    "FEATTYP \"FEATTYP\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.FEATTYP,-1,-1;" +
                                                    "RESTRTYP \"RESTRTYP\" true true false 2 Text 0 0 ,First,#," + lrsTablePath + ",lrs.RESTRTYP,-1,-1;" +
                                                    "VT \"VT\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.VT,-1,-1;" +
                                                    "RESTRVAL \"RESTRVAL\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.RESTRVAL,-1,-1;" +
                                                    "LIMIT \"LIMIT\" true true false 8 Double 0 0 ,First,#," + lrsTablePath + ",lrs.LIMIT,-1,-1;" +
                                                    "UNIT_MEAS \"UNIT_MEAS\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.UNIT_MEAS,-1,-1;" +
                                                    "LANE_VALID \"LANE_VALID\" true true false 20 Text 0 0 ,First,#," + lrsTablePath + ",lrs.LANE_VALID,-1,-1;" +
                                                    "VALDIRPOS \"VALDIRPOS\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.VALDIRPOS,-1,-1;" +
                                                    "VERIFIED \"VERIFIED\" true true false 2 Short 0 0 ,First,#," + lrsTablePath + ",lrs.VERIFIED,-1,-1";
                    gp.Execute(importTableTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "lrs_View";
                    removeJoinTool.join_name = "lvc";
                    gp.Execute(removeJoinTool, trackcancel);

                    string lrsSimplifiedTablePath = outputFileGdbPath + "\\lrs_simplified";

                    deleteTool = new Delete();
                    deleteTool.in_data = "lrs_View";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool.in_data = lrsTablePath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = lvcTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    // Get statistics on the simplified LRS table

                    AddMessage("Analyzing the LRS table...", messages, trackcancel);

                    statsTool = new Statistics();
                    statsTool.in_table = lrsSimplifiedTablePath;
                    statsTool.out_table = lrsStatsTablePath;
                    statsTool.statistics_fields = "ID COUNT";
                    statsTool.case_field = "RESTRTYP;VT;RESTRVAL";
                    gp.Execute(statsTool, trackcancel);

                    // Create and populate the logistics restriction fields

                    CreateAndPopulateLogisticsRestrictionFields(outputFileGdbPath, "lrs_Stats", lrsSimplifiedTablePath, gp, messages, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = lrsSimplifiedTablePath;
                    gp.Execute(deleteTool, trackcancel);
                }
                #endregion

                GC.Collect();

                // Create Signpost feature class and table

                AddMessage("Creating signpost feature class and table...", messages, trackcancel);

                CreateSignposts(inputSITableValue.GetAsText(), inputSPTableValue.GetAsText(), outputFileGdbPath,
                                messages, trackcancel);

                AddSpatialIndex addSpatialIndexTool = new AddSpatialIndex();
                addSpatialIndexTool.in_features = pathToFds + "\\" + SignpostFCName;
                gp.Execute(addSpatialIndexTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = outputFileGdbPath + "\\" + SignpostJoinTableName;
                addIndexTool.fields = "SignpostID";
                addIndexTool.index_name = "SignpostID";
                gp.Execute(addIndexTool, trackcancel);

                addIndexTool.fields = "Sequence";
                addIndexTool.index_name = "Sequence";
                gp.Execute(addIndexTool, trackcancel);

                addIndexTool.fields = "EdgeFCID";
                addIndexTool.index_name = "EdgeFCID";
                gp.Execute(addIndexTool, trackcancel);

                addIndexTool.fields = "EdgeFID";
                addIndexTool.index_name = "EdgeFID";
                gp.Execute(addIndexTool, trackcancel);

                GC.Collect();

                // Upgrade the geodatabase (if not 9.3)

                if (fgdbVersion > 9.3)
                {
                    UpgradeGDB upgradeGdbTool = new UpgradeGDB();
                    upgradeGdbTool.input_workspace = outputFileGdbPath;
                    gp.Execute(upgradeGdbTool, trackcancel);
                }

                // Create and build the network dataset, then pack it in a GPValue

                AddMessage("Creating and building the network dataset...", messages, trackcancel);

                CreateAndBuildNetworkDataset(outputFileGdbPath, fgdbVersion, fdsName, ndsName, createNetworkAttributesInMetric,
                                             createTwoDistanceAttributes, timeZoneIDBaseFieldName, directedTimeZoneIDFields, commonTimeZone,
                                             usesHistoricalTraffic, trafficFeedLocation, usesRSTable, usesLTRTable, usesLRSTable, "lrs_Stats");

                // Once the network dataset is built, we can delete the stats table

                if (usesLRSTable)
                {
                    deleteTool = new Delete();
                    deleteTool.in_data = lrsStatsTablePath;
                    gp.Execute(deleteTool, trackcancel);
                }

                // Write the build errors to the turn feature class

                TurnGeometryUtilities.WriteBuildErrorsToTurnFC(outputFileGdbPath, fdsName, TurnFCName, messages, trackcancel);

                // Compact the output file geodatabase

                AddMessage("Compacting the output file geodatabase...", messages, trackcancel);

                Compact compactTool = new Compact();
                compactTool.in_workspace = outputFileGdbPath;
                gp.Execute(compactTool, trackcancel);
            }
            catch (Exception e)
            {
                if (gp.MaxSeverity == 2)
                {
                    object missing = System.Type.Missing;
                    messages.AddError(1, gp.GetMessages(ref missing));
                }
                messages.AddError(1, e.Message);
                messages.AddError(1, e.StackTrace);
            }
            finally
            {
                // Restore the original GP environment settings

                gpSettings.AddOutputsToMap = origAddOutputsToMapSetting;
                gpSettings.LogHistory = origLogHistorySetting;
            }
            GC.Collect();
            return;
        }
Exemplo n.º 29
0
        public void Execute(IArray paramvalues,
                            ITrackCancel trackcancel,
                            IGPEnvironmentManager envMgr,
                            IGPMessages messages)
        {
            // Re-validate.
            IGPMessages ms = m_util.InternalValidate(ParameterInfo,
                                                     paramvalues,
                                                     false, false,
                                                     envMgr);


            messages.AddMessage("MWSW Boundary Generator, v. 0.0");

            if (((IGPMessage)(ms)).IsError())
            {
                messages.AddMessages(ms);
                return;
            }
            if (!AoGPParameter.ValidateAll(m_parms, paramvalues, messages))
            {
                return;
            }

            try {
                IGPValue  inlyr    = m_util.UnpackGPValue((paramvalues.get_Element(0) as IGPParameter).Value);
                IGPDouble ang_tol  = m_util.UnpackGPValue((paramvalues.get_Element(1) as IGPParameter).Value) as IGPDouble;
                IGPDouble dist_tol = m_util.UnpackGPValue((paramvalues.get_Element(2) as IGPParameter).Value) as IGPDouble;
                IGPValue  outlyr   = m_util.UnpackGPValue((paramvalues.get_Element(3) as IGPParameter).Value);

                //		DumpIt("In layer",inlyr,messages);
                //		DumpIt("Out layer",outlyr,typeof(IGPValue),messages);

                IFeatureClass inputfc;
                IQueryFilter  inputqf;
                m_util.DecodeFeatureLayer(inlyr, out inputfc, out inputqf);

                //			DumpIt("In Featureclass",inputfc,typeof(IFeatureClass),messages);
                //			DumpIt("In QF",inputqf,typeof(IQueryFilter),messages);

                messages.AddMessage("In angle tolerance: " + ang_tol.Value);
                messages.AddMessage("In distance tolerance: " + dist_tol.Value);
                messages.AddMessage("Input featureclass: " + inputfc.AliasName);
                messages.AddMessage("Output path: " + outlyr.GetAsText());

                trackcancel.Progressor.Show();
                trackcancel.Progressor.Message = "Processing...";

                string             outp_txt = outlyr.GetAsText();
                AoFeatureClassName fcname   = new AoFeatureClassName(outp_txt);
                ISpatialReference  spref    = AoTable.From(inputfc)[inputfc.ShapeFieldName].Field.GeometryDef.SpatialReference;

                string shapename = fcname.ShapeFieldName != "" ? fcname.ShapeFieldName : "Shape";
                m_outp_schema[shapename] = AoField.Shape(shapename,
                                                         esriGeometryType.esriGeometryPolyline,
                                                         spref);


                // TODO: shapefiles seem to get an FID automatically,
                //   while geodb needs an 'OBJECTID',
                //   who knows about other workspace types?
                // Is there a way to figure this out w/o looking at the type?

                IFeatureClass outputfc =
                    fcname.Workspace.CreateFeatureClass(fcname.Basename,
                                                        m_outp_schema.Fields,
                                                        null,
                                                        null,
                                                        esriFeatureType.esriFTSimple,
                                                        shapename,
                                                        "");

                IStepProgressor progressor = trackcancel.Progressor as IStepProgressor;
                progressor.MaxRange = 200;

                BGenImp implementation = new BGenImp(spref, ang_tol.Value, dist_tol.Value);
                implementation.Run(inputfc, outputfc, delegate(double howfar) {
                    progressor.Position = (int)(200.0 * howfar);
                });

                messages.AddMessage("Finished, worked through " + implementation.TotalSegments + " line segments total.");
                return;
            } catch (Exception e) {
                while (e != null)
                {
                    messages.AddError(1, "Exception: " + e.Message);
                    e = e.InnerException;
                }
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            // Set the default properties for the specified job type
            try
            {
                IJTXConfiguration3     configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXConfigurationEdit2 configEdit = this.WmxDatabase.ConfigurationManager as IJTXConfigurationEdit2;
                IJTXJobTypeEdit3       jobType    = configEdit.GetJobType(m_jobTypeName) as IJTXJobTypeEdit3;

                // Set the default data workspace for the selected job type
                if (m_dataWorkspaceName.Equals(C_OPT_NONE))
                {
                    jobType.DefaultDataWorkspace = null;
                    msgs.AddMessage("Clearing default data workspace for job type '" + m_jobTypeName + "'");
                }
                else
                {
                    // Translate the workspace name to a DB name object
                    IJTXDataWorkspaceName dwName =
                        Common.WmauHelperFunctions.LookupWorkspaceNameObj(this.WmxDatabase, m_dataWorkspaceName);

                    msgs.AddMessage("Default data workspace for job type '" + m_jobTypeName + "' is now '" + dwName.Name + "'");
                    jobType.DefaultDataWorkspace = dwName;
                }

                // Set the default parent version for the selected job type
                if (m_parentVersion.Equals(C_OPT_NONE))
                {
                    msgs.AddMessage("Clearing default parent version for job type '" + m_jobTypeName + "'");
                    jobType.DefaultParentVersionName_2 = string.Empty;
                }
                else
                {
                    msgs.AddMessage("Default parent version for job type '" + m_jobTypeName + "' is now '" + m_parentVersion + "'");
                    jobType.DefaultParentVersionName_2 = m_parentVersion;
                }

                // Save the changes to the job type
                jobType.Store();

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_DATA_WORKSPACE);
                IGPString         outValue     = new GPStringClass();
                outValue.Value     = m_jobTypeName;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
Exemplo n.º 31
0
        // Execute: Execute the function given the array of the parameters
        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            IFeatureClass outputFeatureClass = null;

            try
            {
                // get the input feature class
                IGPMultiValue inputFeatureClasses_Parameter = (IGPMultiValue)m_GPUtilities.UnpackGPValue(paramvalues.get_Element(0));
                layer[]       input_featureClasses          = new layer[inputFeatureClasses_Parameter.Count];
                for (int i = 0; i < inputFeatureClasses_Parameter.Count; i++)
                {
                    IGPValue inputFeatureClass_Parameter = inputFeatureClasses_Parameter.get_Value(i);

                    IFeatureClass inputFeatureClass;
                    IQueryFilter  inputQF;

                    m_GPUtilities.DecodeFeatureLayer(inputFeatureClass_Parameter, out inputFeatureClass, out inputQF);

                    input_featureClasses[i] = new layer()
                    {
                        featureclass = inputFeatureClass, qFilter = inputQF
                    };
                }


                if (input_featureClasses.Length == 0 || input_featureClasses.Any(w => w.featureclass == null))
                {
                    message.AddError(2, "Could not open one or more input dataset.");
                    return;
                }

                //IFields additionalFields = new FieldsClass();
                //additionalFields.AddField(FEATURE_SOURCE_FIELD_NAME, esriFieldType.esriFieldTypeString);
                //additionalFields.AddField(FEATURE_ID_FIELD_NAME, esriFieldType.esriFieldTypeInteger);
                //additionalFields.AddField(
                //    input_featureClasses[0].featureclass.Fields.get_Field(
                //    input_featureClasses[0].featureclass.Fields.FindField(
                //    input_featureClasses[0].featureclass.ShapeFieldName)));

                // create the output feature class
                IGPValue outputFeatureClass_Parameter = m_GPUtilities.UnpackGPValue(paramvalues.get_Element(1));
                outputFeatureClass = GPHelperFunctions.CreateFeatureClass(outputFeatureClass_Parameter, envMgr);

                if (outputFeatureClass == null)
                {
                    message.AddError(2, "Could not create output dataset.");
                    return;
                }



                IGPString curveTypeParameter = (IGPString)m_GPUtilities.UnpackGPValue(paramvalues.get_Element(2));
                ArcConstructionMethods method;
                if (!Enum.TryParse <ArcConstructionMethods>(curveTypeParameter.Value, true, out method))
                {
                    message.AddError(2, string.Format("The value {0} is not expected.  Expected values are: {1}.",
                                                      curveTypeParameter.Value,
                                                      string.Join(",", Enum.GetNames(typeof(ArcConstructionMethods)))));
                    return;
                }

                IStepProgressor stepPro = (IStepProgressor)trackcancel;
                GPHelperFunctions.dropSpatialIndex(outputFeatureClass);

                BoostVoronoi bv = new BoostVoronoi(100);

                double          minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
                List <site_key> point_sites   = new List <site_key>();
                List <site_key> segment_sites = new List <site_key>();

                for (short i = 0; i < input_featureClasses.Length; i++)
                {
                    layer l         = input_featureClasses[i];
                    int   featcount = l.featureclass.FeatureCount(l.qFilter);

                    stepPro.MinRange  = 0;
                    stepPro.MaxRange  = featcount;
                    stepPro.StepValue = (1);
                    stepPro.Message   = "Reading features";
                    stepPro.Position  = 0;
                    stepPro.Show();

                    IFeatureCursor cursor = null;
                    IFeature       row    = null;

                    try
                    {
                        cursor = l.featureclass.Search(l.qFilter, false);
                        while ((row = cursor.NextFeature()) != null)
                        {
                            stepPro.Step();
                            IPoint point = row.Shape as IPoint;
                            if (point != null)
                            {
                                double X = point.X;
                                double Y = point.Y;

                                minX = Math.Min(minX, X);
                                maxX = Math.Max(maxX, X);

                                minY = Math.Min(minY, Y);
                                maxY = Math.Max(maxY, Y);

                                bv.AddPoint(point.X, point.Y);
                                point_sites.Add(new site_key(i, row.OID));
                            }

                            IMultipoint multipoint = row.Shape as IMultipoint;
                            if (multipoint != null)
                            {
                                IPointCollection pointCollection = (IPointCollection)multipoint;
                                IEnumVertex      vertices        = pointCollection.EnumVertices;

                                IPoint vertex = null; int part, index;
                                vertices.Next(out vertex, out part, out index);

                                minX = Math.Min(minX, multipoint.Envelope.XMin);
                                maxX = Math.Max(maxX, multipoint.Envelope.XMax);

                                minY = Math.Min(minY, multipoint.Envelope.YMin);
                                maxY = Math.Max(maxY, multipoint.Envelope.YMax);

                                while (vertex != null)
                                {
                                    bv.AddPoint(vertex.X, vertex.Y);
                                    point_sites.Add(new site_key(i, row.OID));

                                    vertices.Next(out vertex, out part, out index);
                                }
                            }

                            IPolyline polyline = row.Shape as IPolyline;
                            if (polyline != null)
                            {
                                double fromX = polyline.FromPoint.X;
                                double fromY = polyline.FromPoint.Y;
                                double toX   = polyline.ToPoint.X;
                                double toY   = polyline.ToPoint.Y;

                                if (toX < fromX)
                                {
                                    minX = Math.Min(minX, toX);
                                    maxX = Math.Max(maxX, fromX);
                                }
                                else
                                {
                                    minX = Math.Min(minX, fromX);
                                    maxX = Math.Max(maxX, toX);
                                }

                                if (toY < fromY)
                                {
                                    minY = Math.Min(minY, toY);
                                    maxY = Math.Max(maxY, fromY);
                                }
                                else
                                {
                                    minY = Math.Min(minY, fromY);
                                    maxY = Math.Max(maxY, toY);
                                }

                                bv.AddSegment(
                                    polyline.FromPoint.X, polyline.FromPoint.Y,
                                    polyline.ToPoint.X, polyline.ToPoint.Y
                                    );


                                segment_sites.Add(new site_key(i, row.OID));
                            }

                            Marshal.ReleaseComObject(row);
                        }
                    }
                    finally
                    {
                        if (row != null)
                        {
                            Marshal.ReleaseComObject(row);
                        }
                        if (cursor != null)
                        {
                            Marshal.ReleaseComObject(cursor);
                        }

                        stepPro.Hide();
                    }
                }

                message.AddMessage(String.Format("{0}, {1} -> {2}, {3}", minX, minY, maxX, maxY));

                int width  = Math.Max((int)((maxX - minX) * 0.1), 1);
                int height = Math.Max((int)((maxY - minY) * 0.1), 1);

                maxX = maxX + width;
                minX = minX - width;
                maxY = maxY + height;
                minY = minY - height;

                message.AddMessage(String.Format("{0}, {1} -> {2}, {3}", minX, minY, maxX, maxY));
                bv.AddSegment(minX, minY, maxX, minY);
                segment_sites.Add(new site_key(-1, -1));
                bv.AddSegment(maxX, minY, maxX, maxY);
                segment_sites.Add(new site_key(-1, -1));
                bv.AddSegment(maxX, maxY, minX, maxY);
                segment_sites.Add(new site_key(-1, -1));
                bv.AddSegment(minX, maxY, minX, minY);
                segment_sites.Add(new site_key(-1, -1));

                stepPro.Message  = "Solve Voronoi";
                stepPro.MaxRange = 0;
                stepPro.MaxRange = 0;
                stepPro.Show();

                bv.Construct();

                stepPro.Hide();

                int featureSourceIndx = outputFeatureClass.Fields.FindField(FEATURE_SOURCE_FIELD_NAME);
                int featureIDIndx     = outputFeatureClass.Fields.FindField(FEATURE_ID_FIELD_NAME);

                IFeatureCursor inserts = null;
                IFeatureBuffer buffer  = null;
                try
                {
                    object            missing          = Type.Missing;
                    ISpatialReference spatialReference = ((IGeoDataset)outputFeatureClass).SpatialReference;
                    inserts = outputFeatureClass.Insert(false);
                    buffer  = outputFeatureClass.CreateFeatureBuffer();

                    List <Cell> cells = bv.Cells;
                    message.AddMessage(string.Format("{0} cells calculated", cells.Count));
                    List <Edge> edges = bv.Edges;
                    message.AddMessage(string.Format("{0} edges calculated", edges.Count));
                    List <Vertex> vertices = bv.Vertices;
                    message.AddMessage(string.Format("{0} vertexes calculated", vertices.Count));

                    stepPro.Message  = "Write cells";
                    stepPro.MaxRange = 0;
                    stepPro.MaxRange = cells.Count;
                    stepPro.Show();


                    for (int cellIndex = 0; cellIndex < cells.Count; cellIndex++)
                    {
                        try
                        {
                            if (cellIndex % 5000 == 0)
                            {
                                message.AddMessage(String.Format("{0}. {1} cells processed.", DateTime.Now, cellIndex));
                            }

                            Cell cell        = cells[cellIndex];
                            int  currentSite = cell.Site;
                            IGeometryCollection geometryCollection = new GeometryBagClass()
                            {
                                SpatialReference = spatialReference
                            };

                            //ignores any sliver cells
                            if (cell.IsOpen || cell.EdgesIndex.Count < 3)
                            {
                                continue;
                            }

                            ISegmentCollection segmentCollection = createSegments(cell, bv, method, spatialReference);

                            if (((IArea)segmentCollection).Area <= 0)
                            {
                                message.AddMessage("A invalid geometry has been detected, try reversing the orientation.");
                                ISegmentCollection reversed_segmentCollection = new PolygonClass()
                                {
                                    SpatialReference = spatialReference
                                };
                                for (int i = segmentCollection.SegmentCount - 1; i >= 0; i--)
                                {
                                    ISegment segment = (ISegment)segmentCollection.get_Segment(i);
                                    segment.ReverseOrientation();
                                    reversed_segmentCollection.AddSegment(segment);
                                }
                                segmentCollection = reversed_segmentCollection;
                            }

                            ((IPolygon)segmentCollection).SpatialReference = spatialReference;
                            if (((IArea)segmentCollection).Area <= 0)
                            {
                                message.AddWarning("An empty shell has been created");

                                for (int i = 0; i < segmentCollection.SegmentCount; i++)
                                {
                                    ISegment segment = (ISegment)segmentCollection.get_Segment(i);
                                    message.AddMessage(String.Format("From {0}, {1} To {2},{3}",
                                                                     segment.FromPoint.X, segment.FromPoint.Y,
                                                                     segment.ToPoint.X, segment.ToPoint.Y));
                                }
                            }


                            //set attributes
                            site_key sk = (currentSite >= point_sites.Count) ? segment_sites[currentSite - point_sites.Count] : point_sites[currentSite];
                            if (!sk.isEmpty)
                            {
                                buffer.set_Value(featureSourceIndx, input_featureClasses[sk.featureClassIndex].featureclass.AliasName);
                                buffer.set_Value(featureIDIndx, sk.objectID);
                            }
                            else
                            {
                                buffer.set_Value(featureSourceIndx, DBNull.Value);
                                buffer.set_Value(featureIDIndx, DBNull.Value);
                            }


                            IPolygon voronoiPolygon = (IPolygon)segmentCollection;
                            buffer.Shape = (IPolygon)voronoiPolygon;
                            inserts.InsertFeature(buffer);
                        }
                        catch (Exception e)
                        {
                            message.AddWarning("Failed to create a cell");
                        }
                    }
                }
                finally
                {
                    if (buffer != null)
                    {
                        Marshal.ReleaseComObject(buffer);
                    }
                    if (inserts != null)
                    {
                        Marshal.ReleaseComObject(inserts);
                    }
                }

                GPHelperFunctions.createSpatialIndex(outputFeatureClass);
            }
            catch (Exception exx)
            {
                message.AddError(2, exx.Message);
                message.AddMessage(exx.ToString());
            }
            finally
            {
                if (outputFeatureClass != null)
                {
                    Marshal.ReleaseComObject(outputFeatureClass);
                }

                ((IProgressor)trackcancel).Hide();
            }
        }
Exemplo n.º 32
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                IJTXSpatialNotificationManager snManager          = this.WmxDatabase.SpatialNotificationManager;
                IJTXSpatialNotifierNameSet     allSnNames         = snManager.SpatialNotifiers;
                IJTXNotificationConfiguration  notificationConfig = this.WmxDatabase.ConfigurationManager as IJTXNotificationConfiguration;

                // Create a new spatial notification
                IJTXChangeRule2 changeRule = snManager.AddChangeRule() as IJTXChangeRule2;

                // Set the name
                changeRule.Name = m_snName;

                // Set the notifier
                IJTXNotificationType srcNotifType = notificationConfig.GetNotificationType(m_emailNotifier);

                // Set the properties of the spatial notification's e-mail notification to match that
                // of the source e-mail notification (phew!)
                IJTXEmailSpatialNotifier emailNotifier = this.CreateSpatialNotifierByName(C_TYPE_EMAIL_NOTIFIER) as IJTXEmailSpatialNotifier;
                emailNotifier.Subject           = srcNotifType.SubjectTemplate;
                emailNotifier.Body              = srcNotifType.MessageTemplate;
                emailNotifier.SenderEmail       = srcNotifType.SenderTemplate;
                emailNotifier.SenderDisplayName = srcNotifType.SenderDisplayNameTemplate;

                string[]     subscribers    = srcNotifType.get_Subscribers();
                IStringArray subscribersObj = new StrArrayClass();
                foreach (string subscriber in subscribers)
                {
                    subscribersObj.Add(subscriber);
                }
                emailNotifier.Subscribers = subscribersObj;

                changeRule.Notifier = emailNotifier as IJTXSpatialNotifier;

                // Set the description, if applicable
                if (!string.IsNullOrEmpty(m_snDescription))
                {
                    changeRule.Description = m_snDescription;
                }

                // Set the summarization behavior
                changeRule.SummarizeNotifications = m_summarize;

                // Store the resulting change rule
                changeRule.Store();

                // Update the output parameter
                WmauParameterMap  paramMap = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParam = paramMap.GetParamEdit(C_PARAM_OUT_NAME);
                IGPString         strValue = new GPStringClass();
                strValue.Value = m_snName;
                outParam.Value = strValue as IGPValue;

                msgs.AddWarning("To avoid database corruption, at least one dataset or area evaluator must be added to notification '" + m_snName + "' immediately!");
                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_SN_CREATION_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            finally
            {
                // Release any COM objects here!
            }
        }
        public void Execute(IArray paramvalues, ITrackCancel trackcancel,
                            IGPEnvironmentManager envMgr, IGPMessages messages)
        {
            // Remember the original GP environment settings and temporarily override these settings

            var gpSettings = envMgr as IGeoProcessorSettings;
            bool origAddOutputsToMapSetting = gpSettings.AddOutputsToMap;
            bool origLogHistorySetting = gpSettings.LogHistory;
            gpSettings.AddOutputsToMap = false;
            gpSettings.LogHistory = false;

            // Create the Geoprocessor

            Geoprocessor gp = new Geoprocessor();

            try
            {
                // Validate our values

                IGPMessages validateMessages = ((IGPFunction2)this).Validate(paramvalues, false, envMgr);
                if ((validateMessages as IGPMessage).IsError())
                {
                    messages.AddError(1, "Validate failed");
                    return;
                }

                // Unpack values

                IGPParameter gpParam = paramvalues.get_Element(InputAETable) as IGPParameter;
                IGPValue inputAETableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputAdminAreaFeatureClasses) as IGPParameter;
                var inputAdminAreaFeatureClassesMultiValue = m_gpUtils.UnpackGPValue(gpParam) as IGPMultiValue;
                gpParam = paramvalues.get_Element(OutputFileGDB) as IGPParameter;
                IGPValue outputFileGDBValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTATable) as IGPParameter;
                IGPValue inputTATableValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputNWFeatureClass) as IGPParameter;
                IGPValue inputNWFeatureClassValue = m_gpUtils.UnpackGPValue(gpParam);
                gpParam = paramvalues.get_Element(InputTimeZoneIDBaseFieldName) as IGPParameter;
                IGPValue inputTimeZoneIDBaseFieldNameValue = m_gpUtils.UnpackGPValue(gpParam);

                if (inputTATableValue.IsEmpty() ^ inputNWFeatureClassValue.IsEmpty())
                {
                    messages.AddError(1, "The TA table and NW feature class must be specified together.");
                    return;
                }

                bool processStreetsFC = (!(inputNWFeatureClassValue.IsEmpty()));
                string timeZoneIDBaseFieldName = "";
                if (!(inputTimeZoneIDBaseFieldNameValue.IsEmpty()))
                    timeZoneIDBaseFieldName = inputTimeZoneIDBaseFieldNameValue.GetAsText();

                // Get the path to the output file GDB

                string outputFileGdbPath = outputFileGDBValue.GetAsText();

                // Create the new file geodatabase

                AddMessage("Creating the file geodatabase...", messages, trackcancel);

                int lastBackslash = outputFileGdbPath.LastIndexOf("\\");
                CreateFileGDB createFGDBTool = new CreateFileGDB();
                createFGDBTool.out_folder_path = outputFileGdbPath.Remove(lastBackslash);
                createFGDBTool.out_name = outputFileGdbPath.Substring(lastBackslash + 1);
                gp.Execute(createFGDBTool, trackcancel);

                // Copy the admin area feature classes

                int numAdminAreaFCs = inputAdminAreaFeatureClassesMultiValue.Count;
                string mergeToolInputs = "";
                for (int i = 0; i < numAdminAreaFCs; i++)
                {
                    AddMessage("Copying the Administrative Area feature classes (" + Convert.ToString(i+1) + " of " + Convert.ToString(numAdminAreaFCs) + ")...", messages, trackcancel);

                    string origAdminFCPath = inputAdminAreaFeatureClassesMultiValue.get_Value(i).GetAsText();
                    FeatureClassToFeatureClass importFCTool = new FeatureClassToFeatureClass();
                    importFCTool.in_features = origAdminFCPath;
                    importFCTool.out_path = outputFileGdbPath;
                    importFCTool.out_name = "Admin" + Convert.ToString(i, System.Globalization.CultureInfo.InvariantCulture);
                    importFCTool.field_mapping = "ID \"ID\" true true false 8 Double 0 0 ,First,#," + origAdminFCPath + ",ID,-1,-1;" +
                                                 "FEATTYP \"FEATTYP\" true true false 2 Short 0 0 ,First,#," + origAdminFCPath + ",FEATTYP,-1,-1;" +
                                                 "ORDER00 \"ORDER00\" true true false 3 Text 0 0 ,First,#," + origAdminFCPath + ",ORDER00,-1,-1;" +
                                                 "NAME \"NAME\" true true false 100 Text 0 0 ,First,#," + origAdminFCPath + ",NAME,-1,-1;" +
                                                 "NAMELC \"NAMELC\" true true false 3 Text 0 0 ,First,#," + origAdminFCPath + ",NAMELC,-1,-1";
                    gp.Execute(importFCTool, trackcancel);

                    mergeToolInputs = mergeToolInputs + outputFileGdbPath + "\\Admin" + Convert.ToString(i, System.Globalization.CultureInfo.InvariantCulture) + ";";
                }
                mergeToolInputs = mergeToolInputs.Remove(mergeToolInputs.Length - 1);

                // Merge the admin area feature classes together into one feature class

                AddMessage("Merging the Administrative Area feature classes...", messages, trackcancel);

                string adminFCPath = outputFileGdbPath + "\\AdminFC";

                Merge mergeTool = new Merge();
                mergeTool.inputs = mergeToolInputs;
                mergeTool.output = adminFCPath;
                gp.Execute(mergeTool, trackcancel);

                Delete deleteTool = null;
                for (int i = 0; i < numAdminAreaFCs; i++)
                {
                    deleteTool = new Delete();
                    deleteTool.in_data = outputFileGdbPath + "\\Admin" + Convert.ToString(i, System.Globalization.CultureInfo.InvariantCulture);
                    gp.Execute(deleteTool, trackcancel);
                }

                // Extract the time zone information and index it

                AddMessage("Extracting the time zone information...", messages, trackcancel);

                string tzTablePath = outputFileGdbPath + "\\TZ";
                TableSelect tableSelectTool = new TableSelect();
                tableSelectTool.in_table = inputAETableValue.GetAsText();
                tableSelectTool.out_table = tzTablePath;
                tableSelectTool.where_clause = "ATTTYP = 'TZ'";
                gp.Execute(tableSelectTool, trackcancel);

                AddIndex addIndexTool = new AddIndex();
                addIndexTool.in_table = tzTablePath;
                addIndexTool.fields = "ID";
                addIndexTool.index_name = "ID";
                gp.Execute(addIndexTool, trackcancel);

                // Add the UTCOffset field and calculate it

                AddField addFieldTool = new AddField();
                addFieldTool.in_table = adminFCPath;
                addFieldTool.field_name = "UTCOffset";
                addFieldTool.field_type = "SHORT";
                gp.Execute(addFieldTool, trackcancel);

                MakeFeatureLayer makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = adminFCPath;
                makeFeatureLayerTool.out_layer = "AdminFC_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                AddJoin addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "AdminFC_Layer";
                addJoinTool.in_field = "ID";
                addJoinTool.join_table = tzTablePath;
                addJoinTool.join_field = "ID";
                addJoinTool.join_type = "KEEP_COMMON";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Calculating the UTCOffset information on the Administrative Area feature class...", messages, trackcancel);

                CalculateField calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "AdminFC_Layer";
                calcFieldTool.field = "AdminFC.UTCOffset";
                calcFieldTool.code_block = "u = Null\ns = Trim([TZ.ATTVALUE])\nIf Not IsNull(s) Then\n" +
                                           "  sign = 1\n  If Left(s, 1) = \"-\" Then sign = -1\n" +
                                           "  u = sign * ( (60 * Abs(CInt(Mid(s, 1, Len(s) - 3)))) + CInt(Right(s, 2)) )\n" +
                                           "End If";
                calcFieldTool.expression = "u";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                RemoveJoin removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "AdminFC_Layer";
                removeJoinTool.join_name = "TZ";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "AdminFC_Layer";
                gp.Execute(deleteTool, trackcancel);
                deleteTool = new Delete();
                deleteTool.in_data = tzTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Extract out only the admin areas that have time zone information

                AddMessage("Extracting Administrative Areas with time zone information...", messages, trackcancel);

                string adminTZFCPath = outputFileGdbPath + "\\AdminFCwTZ";
                Select selectTool = new Select();
                selectTool.in_features = adminFCPath;
                selectTool.out_feature_class = adminTZFCPath;
                selectTool.where_clause = "NOT UTCOffset IS NULL";
                gp.Execute(selectTool, trackcancel);

                // Extract the daylight saving time information and index it

                AddMessage("Extracting the daylight saving time information...", messages, trackcancel);

                string suTablePath = outputFileGdbPath + "\\SU";
                tableSelectTool = new TableSelect();
                tableSelectTool.in_table = inputAETableValue.GetAsText();
                tableSelectTool.out_table = suTablePath;
                tableSelectTool.where_clause = "ATTTYP = 'SU'";
                gp.Execute(tableSelectTool, trackcancel);

                addIndexTool = new AddIndex();
                addIndexTool.in_table = suTablePath;
                addIndexTool.fields = "ID";
                addIndexTool.index_name = "ID";
                gp.Execute(addIndexTool, trackcancel);

                // Add the daylight saving field and calculate it

                addFieldTool = new AddField();
                addFieldTool.in_table = adminTZFCPath;
                addFieldTool.field_name = "DST";
                addFieldTool.field_type = "SHORT";
                gp.Execute(addFieldTool, trackcancel);

                makeFeatureLayerTool = new MakeFeatureLayer();
                makeFeatureLayerTool.in_features = adminTZFCPath;
                makeFeatureLayerTool.out_layer = "AdminFCwTZ_Layer";
                gp.Execute(makeFeatureLayerTool, trackcancel);

                addJoinTool = new AddJoin();
                addJoinTool.in_layer_or_view = "AdminFCwTZ_Layer";
                addJoinTool.in_field = "ID";
                addJoinTool.join_table = suTablePath;
                addJoinTool.join_field = "ID";
                addJoinTool.join_type = "KEEP_ALL";
                gp.Execute(addJoinTool, trackcancel);

                AddMessage("Copying the DST information to the Administrative Area feature class...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = "AdminFCwTZ_Layer";
                calcFieldTool.field = "AdminFCwTZ.DST";
                calcFieldTool.code_block = "s = 0\nIf Not IsNull( [SU.ATTVALUE] ) Then s = CInt( [SU.ATTVALUE] )";
                calcFieldTool.expression = "s";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                removeJoinTool = new RemoveJoin();
                removeJoinTool.in_layer_or_view = "AdminFCwTZ_Layer";
                removeJoinTool.join_name = "SU";
                gp.Execute(removeJoinTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = "AdminFCwTZ_Layer";
                gp.Execute(deleteTool, trackcancel);
                deleteTool = new Delete();
                deleteTool.in_data = suTablePath;
                gp.Execute(deleteTool, trackcancel);

                // Create and calculate the sortable MSTIMEZONE field

                addFieldTool = new AddField();
                addFieldTool.in_table = adminTZFCPath;
                addFieldTool.field_name = "SortableMSTIMEZONE";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 60;
                gp.Execute(addFieldTool, trackcancel);

                AddMessage("Calculating the time zones...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = adminTZFCPath;
                calcFieldTool.field = "SortableMSTIMEZONE";
                calcFieldTool.code_block = TimeZoneUtilities.MakeSortableMSTIMEZONECode("ORDER00");
                calcFieldTool.expression = "z";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                Rename renameTool = new Rename();
                renameTool.in_data = adminTZFCPath;
                renameTool.out_data = adminTZFCPath + "wNulls";
                gp.Execute(renameTool, trackcancel);

                selectTool = new Select();
                selectTool.in_features = adminTZFCPath + "wNulls";
                selectTool.out_feature_class = adminTZFCPath;
                selectTool.where_clause = "NOT SortableMSTIMEZONE IS NULL";
                gp.Execute(selectTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = adminTZFCPath + "wNulls";
                gp.Execute(deleteTool, trackcancel);

                // Dissolve the time zone polygons together

                AddMessage("Dissolving the time zones...", messages, trackcancel);

                string timeZoneFCPath = outputFileGdbPath + "\\" + TimeZoneFCName;
                Dissolve dissolveTool = new Dissolve();
                dissolveTool.in_features = adminTZFCPath;
                dissolveTool.out_feature_class = timeZoneFCPath;
                dissolveTool.dissolve_field = "SortableMSTIMEZONE";
                dissolveTool.multi_part = "MULTI_PART";
                gp.Execute(dissolveTool, trackcancel);

                // Create and calculate the MSTIMEZONE field

                addFieldTool = new AddField();
                addFieldTool.in_table = timeZoneFCPath;
                addFieldTool.field_name = "MSTIMEZONE";
                addFieldTool.field_type = "TEXT";
                addFieldTool.field_length = 50;
                gp.Execute(addFieldTool, trackcancel);

                AddMessage("Calculating the time zones...", messages, trackcancel);

                calcFieldTool = new CalculateField();
                calcFieldTool.in_table = timeZoneFCPath;
                calcFieldTool.field = "MSTIMEZONE";
                calcFieldTool.expression = "Mid([SortableMSTIMEZONE], 7)";
                calcFieldTool.expression_type = "VB";
                gp.Execute(calcFieldTool, trackcancel);

                // Delete the old sortable MSTIMEZONE field

                DeleteField deleteFieldTool = new DeleteField();
                deleteFieldTool.in_table = timeZoneFCPath;
                deleteFieldTool.drop_field = "SortableMSTIMEZONE";
                gp.Execute(deleteFieldTool, trackcancel);

                if (processStreetsFC)
                {
                    // Create the network dataset time zone table

                    AddMessage("Creating the time zones table...", messages, trackcancel);

                    TableToTable importTableTool = new TableToTable();
                    importTableTool.in_rows = timeZoneFCPath;
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = TimeZonesTableName;
                    importTableTool.field_mapping = "MSTIMEZONE \"MSTIMEZONE\" true true false 50 Text 0 0 ,First,#," +
                                                    timeZoneFCPath + ",MSTIMEZONE,-1,-1";
                    gp.Execute(importTableTool, trackcancel);

                    // Import the NW feature class to the file geodatabase

                    AddMessage("Copying the NW feature class to the geodatabase...", messages, trackcancel);

                    FeatureClassToFeatureClass importFCTool = new FeatureClassToFeatureClass();
                    importFCTool.in_features = inputNWFeatureClassValue.GetAsText();
                    importFCTool.out_path = outputFileGdbPath;
                    importFCTool.out_name = "nw";
                    gp.Execute(importFCTool, trackcancel);

                    string pathToLocalNW = outputFileGdbPath + "\\nw";

                    // Create Join polygon feature class

                    AddMessage("Creating the join polygon feature class...", messages, trackcancel);

                    string joinPolygonFCPath = outputFileGdbPath + "\\JoinPolygonFC";

                    MultipartToSinglepart multipartToSinglepartTool = new MultipartToSinglepart();
                    multipartToSinglepartTool.in_features = timeZoneFCPath;
                    multipartToSinglepartTool.out_feature_class = joinPolygonFCPath;
                    gp.Execute(multipartToSinglepartTool, trackcancel);

                    // Add and calculate the time zone ID fields to the join polygons

                    addFieldTool = new AddField();
                    addFieldTool.in_table = joinPolygonFCPath;
                    addFieldTool.field_type = "SHORT";
                    addFieldTool.field_name = "FT_" + timeZoneIDBaseFieldName;
                    gp.Execute(addFieldTool, trackcancel);
                    addFieldTool.field_name = "TF_" + timeZoneIDBaseFieldName;
                    gp.Execute(addFieldTool, trackcancel);

                    AddMessage("Calculating the FT_" + timeZoneIDBaseFieldName + " field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = joinPolygonFCPath;
                    calcFieldTool.field = "FT_" + timeZoneIDBaseFieldName;
                    calcFieldTool.expression = "[ORIG_FID]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    AddMessage("Calculating the TF_" + timeZoneIDBaseFieldName + " field...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = joinPolygonFCPath;
                    calcFieldTool.field = "TF_" + timeZoneIDBaseFieldName;
                    calcFieldTool.expression = "[ORIG_FID]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    // Delete the MSTIMEZONE and ORIG_FID fields from the join polygon FC

                    deleteFieldTool = new DeleteField();
                    deleteFieldTool.in_table = joinPolygonFCPath;
                    deleteFieldTool.drop_field = "MSTIMEZONE;ORIG_FID";
                    gp.Execute(deleteFieldTool, trackcancel);

                    // Perform a spatial join between the Streets and the join polygons

                    AddMessage("Creating Streets feature class with time zone ID fields...", messages, trackcancel);

                    string outputStreetsFCPath = outputFileGdbPath + "\\" + StreetsFCName;
                    SpatialJoin spatialJoinTool = new SpatialJoin();
                    spatialJoinTool.target_features = pathToLocalNW;
                    spatialJoinTool.join_features = joinPolygonFCPath;
                    spatialJoinTool.out_feature_class = outputStreetsFCPath;
                    spatialJoinTool.match_option = "IS_WITHIN";
                    gp.Execute(spatialJoinTool, trackcancel);

                    // Delete the extraneous fields

                    deleteFieldTool = new DeleteField();
                    deleteFieldTool.in_table = outputStreetsFCPath;
                    deleteFieldTool.drop_field = "Join_Count;TARGET_FID;Shape_Length_1";
                    gp.Execute(deleteFieldTool, trackcancel);

                    // Delete the temporary NW and Join feature classes

                    deleteTool = new Delete();
                    deleteTool.in_data = pathToLocalNW;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool = new Delete();
                    deleteTool.in_data = joinPolygonFCPath;
                    gp.Execute(deleteTool, trackcancel);

                    // Extract the drive side information and index it

                    AddMessage("Extracting the drive side information...", messages, trackcancel);

                    string driveSideTablePath = outputFileGdbPath + "\\DriveSide";
                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputAETableValue.GetAsText();
                    tableSelectTool.out_table = driveSideTablePath;
                    tableSelectTool.where_clause = "ATTTYP = '3D'";
                    gp.Execute(tableSelectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = driveSideTablePath;
                    addIndexTool.fields = "ID";
                    addIndexTool.index_name = "ID";
                    gp.Execute(addIndexTool, trackcancel);

                    // Add the DriveSide field and calculate it

                    addFieldTool = new AddField();
                    addFieldTool.in_table = adminFCPath;
                    addFieldTool.field_name = "DriveSide";
                    addFieldTool.field_type = "SHORT";
                    gp.Execute(addFieldTool, trackcancel);

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = adminFCPath;
                    makeFeatureLayerTool.out_layer = "AdminFC_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "AdminFC_Layer";
                    addJoinTool.in_field = "ID";
                    addJoinTool.join_table = driveSideTablePath;
                    addJoinTool.join_field = "ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Copying the drive side information to the Administrative Area feature class...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "AdminFC_Layer";
                    calcFieldTool.field = "AdminFC.DriveSide";
                    calcFieldTool.expression = "CInt( [DriveSide.ATTVALUE] )";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "AdminFC_Layer";
                    removeJoinTool.join_name = "DriveSide";
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "AdminFC_Layer";
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool = new Delete();
                    deleteTool.in_data = driveSideTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    // Extract out only the admin areas that have drive side information and index the ORDER00 field

                    AddMessage("Extracting Administrative Areas with drive side information...", messages, trackcancel);

                    string adminFCwDriveSidePath = outputFileGdbPath + "\\AdminFCwDriveSide";
                    selectTool = new Select();
                    selectTool.in_features = adminFCPath;
                    selectTool.out_feature_class = adminFCwDriveSidePath;
                    selectTool.where_clause = "NOT DriveSide IS NULL";
                    gp.Execute(selectTool, trackcancel);

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = adminFCwDriveSidePath;
                    addIndexTool.fields = "ORDER00";
                    addIndexTool.index_name = "ORDER00";
                    gp.Execute(addIndexTool, trackcancel);

                    // Add the DriveSide field to the AdminFCwTZ feature class and calculate it.

                    addFieldTool = new AddField();
                    addFieldTool.in_table = adminTZFCPath;
                    addFieldTool.field_name = "DriveSide";
                    addFieldTool.field_type = "SHORT";
                    gp.Execute(addFieldTool, trackcancel);

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = adminTZFCPath;
                    makeFeatureLayerTool.out_layer = "AdminFCwTZ_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "AdminFCwTZ_Layer";
                    addJoinTool.in_field = "ORDER00";
                    addJoinTool.join_table = adminFCwDriveSidePath;
                    addJoinTool.join_field = "ORDER00";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the DriveSide field on the AdminFCwTZ feature class...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "AdminFCwTZ_Layer";
                    calcFieldTool.field = "AdminFCwTZ.DriveSide";
                    calcFieldTool.expression = "[AdminFCwDriveSide.DriveSide]";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "AdminFCwTZ_Layer";
                    removeJoinTool.join_name = "AdminFCwDriveSide";
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "AdminFCwTZ_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = adminFCwDriveSidePath;
                    gp.Execute(deleteTool, trackcancel);

                    // Extract the information for boundary roads

                    AddMessage("Extracting the information for boundary roads...", messages, trackcancel);

                    string taTablePath = outputFileGdbPath + "\\TA";
                    tableSelectTool = new TableSelect();
                    tableSelectTool.in_table = inputTATableValue.GetAsText();
                    tableSelectTool.out_table = taTablePath;
                    tableSelectTool.where_clause = "ARETYP <= 1120 AND SOL > 0";
                    gp.Execute(tableSelectTool, trackcancel);

                    // Join the boundary road information with the AdminFCwTZ feature class to
                    // create the FT_BoundaryTimeZones and TF_BoundaryTimeZones join tables

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = adminTZFCPath;
                    addIndexTool.fields = "ID";
                    addIndexTool.index_name = "ID";
                    gp.Execute(addIndexTool, trackcancel);

                    MakeTableView makeTableViewTool = new MakeTableView();
                    makeTableViewTool.in_table = taTablePath;
                    makeTableViewTool.out_view = "TA_View";
                    gp.Execute(makeTableViewTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "TA_View";
                    addJoinTool.in_field = "AREID";
                    addJoinTool.join_table = adminTZFCPath;
                    addJoinTool.join_field = "ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Extracting the boundary FT time zones...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = "TA_View";
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = "FT_BoundaryTimeZones";
                    importTableTool.where_clause = "TA.SOL = AdminFCwTZ.DriveSide";
                    importTableTool.field_mapping = "ID \"ID\" true true false 8 Double 0 0 ,First,#," + taTablePath + ",TA.ID,-1,-1;" +
                                                    "SortableMSTIMEZONE \"SortableMSTIMEZONE\" true true false 60 Text 0 0 ,First,#," +
                                                    adminTZFCPath + ",AdminFCwTZ.SortableMSTIMEZONE,-1,-1";
                    gp.Execute(importTableTool, trackcancel);

                    string ftBoundaryTimeZonesPath = outputFileGdbPath + "\\FT_BoundaryTimeZones";

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = ftBoundaryTimeZonesPath;
                    addIndexTool.fields = "ID";
                    addIndexTool.index_name = "ID";
                    gp.Execute(addIndexTool, trackcancel);

                    AddMessage("Extracting the boundary TF time zones...", messages, trackcancel);

                    importTableTool = new TableToTable();
                    importTableTool.in_rows = "TA_View";
                    importTableTool.out_path = outputFileGdbPath;
                    importTableTool.out_name = "TF_BoundaryTimeZones";
                    importTableTool.where_clause = "TA.SOL <> AdminFCwTZ.DriveSide";
                    importTableTool.field_mapping = "ID \"ID\" true true false 8 Double 0 0 ,First,#," + taTablePath + ",TA.ID,-1,-1;" +
                                                    "SortableMSTIMEZONE \"SortableMSTIMEZONE\" true true false 60 Text 0 0 ,First,#," +
                                                    adminTZFCPath + ",AdminFCwTZ.SortableMSTIMEZONE,-1,-1";
                    gp.Execute(importTableTool, trackcancel);

                    string tfBoundaryTimeZonesPath = outputFileGdbPath + "\\TF_BoundaryTimeZones";

                    addIndexTool = new AddIndex();
                    addIndexTool.in_table = tfBoundaryTimeZonesPath;
                    addIndexTool.fields = "ID";
                    addIndexTool.index_name = "ID";
                    gp.Execute(addIndexTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "TA_View";
                    removeJoinTool.join_name = "AdminFCwTZ";
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "TA_View";
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = taTablePath;
                    gp.Execute(deleteTool, trackcancel);

                    // Calculate the boundary time zone ID values

                    makeFeatureLayerTool = new MakeFeatureLayer();
                    makeFeatureLayerTool.in_features = outputStreetsFCPath;
                    makeFeatureLayerTool.out_layer = "Streets_Layer";
                    gp.Execute(makeFeatureLayerTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "ID";
                    addJoinTool.join_table = ftBoundaryTimeZonesPath;
                    addJoinTool.join_field = "ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the boundary FT time zones...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".FT_" + timeZoneIDBaseFieldName;
                    calcFieldTool.code_block = TimeZoneUtilities.MakeTimeZoneIDCode(outputFileGdbPath, TimeZonesTableName, "FT_BoundaryTimeZones.SortableMSTIMEZONE");
                    calcFieldTool.expression = "tzID";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "FT_BoundaryTimeZones";
                    gp.Execute(removeJoinTool, trackcancel);

                    addJoinTool = new AddJoin();
                    addJoinTool.in_layer_or_view = "Streets_Layer";
                    addJoinTool.in_field = "ID";
                    addJoinTool.join_table = tfBoundaryTimeZonesPath;
                    addJoinTool.join_field = "ID";
                    addJoinTool.join_type = "KEEP_COMMON";
                    gp.Execute(addJoinTool, trackcancel);

                    AddMessage("Calculating the boundary TF time zones...", messages, trackcancel);

                    calcFieldTool = new CalculateField();
                    calcFieldTool.in_table = "Streets_Layer";
                    calcFieldTool.field = StreetsFCName + ".TF_" + timeZoneIDBaseFieldName;
                    calcFieldTool.code_block = TimeZoneUtilities.MakeTimeZoneIDCode(outputFileGdbPath, TimeZonesTableName, "TF_BoundaryTimeZones.SortableMSTIMEZONE");
                    calcFieldTool.expression = "tzID";
                    calcFieldTool.expression_type = "VB";
                    gp.Execute(calcFieldTool, trackcancel);

                    removeJoinTool = new RemoveJoin();
                    removeJoinTool.in_layer_or_view = "Streets_Layer";
                    removeJoinTool.join_name = "TF_BoundaryTimeZones";
                    gp.Execute(removeJoinTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = "Streets_Layer";
                    gp.Execute(deleteTool, trackcancel);

                    deleteTool = new Delete();
                    deleteTool.in_data = ftBoundaryTimeZonesPath;
                    gp.Execute(deleteTool, trackcancel);
                    deleteTool.in_data = tfBoundaryTimeZonesPath;
                    gp.Execute(deleteTool, trackcancel);
                }
                else
                {
                    // Create a dummy TimeZones table and a dummy Streets feature class

                    CreateTable createTableTool = new CreateTable();
                    createTableTool.out_path = outputFileGdbPath;
                    createTableTool.out_name = TimeZonesTableName;
                    gp.Execute(createTableTool, trackcancel);

                    CreateFeatureclass createFCTool = new CreateFeatureclass();
                    createFCTool.out_path = outputFileGdbPath;
                    createFCTool.out_name = StreetsFCName;
                    createFCTool.geometry_type = "POLYLINE";
                    gp.Execute(createFCTool, trackcancel);
                }

                deleteTool = new Delete();
                deleteTool.in_data = adminFCPath;
                gp.Execute(deleteTool, trackcancel);

                deleteTool = new Delete();
                deleteTool.in_data = adminTZFCPath;
                gp.Execute(deleteTool, trackcancel);

                // Compact the output file geodatabase

                AddMessage("Compacting the output file geodatabase...", messages, trackcancel);

                Compact compactTool = new Compact();
                compactTool.in_workspace = outputFileGdbPath;
                gp.Execute(compactTool, trackcancel);
            }
            catch (Exception e)
            {
                if (gp.MaxSeverity == 2)
                {
                    object missing = System.Type.Missing;
                    messages.AddError(1, gp.GetMessages(ref missing));
                }
                messages.AddError(1, e.Message);
                messages.AddError(1, e.StackTrace);
            }
            finally
            {
                // Restore the original GP environment settings

                gpSettings.AddOutputsToMap = origAddOutputsToMapSetting;
                gpSettings.LogHistory = origLogHistorySetting;
            }
            GC.Collect();
            return;
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            WorkspaceWorksheetReader reader = null;

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                reader = new WorkspaceWorksheetReader(this.m_excelFilePath, msgs);

                // Prepare to set/build the output parameter
                WmauParameterMap  paramMap      = new WmauParameterMap(paramValues);
                IGPParameter3     outParam      = paramMap.GetParam(C_PARAM_OUT_WORKSPACES_CREATED);
                IGPParameterEdit3 outParamEdit  = paramMap.GetParamEdit(C_PARAM_OUT_WORKSPACES_CREATED);
                IGPMultiValue     outMultiValue = new GPMultiValueClass();
                outMultiValue.MemberDataType = outParam.DataType;

                // Load the workspace info from the spreadsheet
                List <Common.WorkspaceInfo> dataWorkspaces = reader.GetWorkspacesFromSpreadsheet();

                // Loop through each of the workspaces
                IJTXDatabaseConnectionManager dbConnectionManager = new JTXDatabaseConnectionManagerClass();
                IJTXDatabaseConnection        dbConnection        = dbConnectionManager.GetConnection(WmxDatabase.Alias);
                foreach (Common.WorkspaceInfo wmauWorkspaceInfo in dataWorkspaces)
                {
                    string workspaceName = wmauWorkspaceInfo.Name;
                    if (Common.WmauHelperFunctions.LookupWorkspaceNameObj(this.WmxDatabase, workspaceName) != null)
                    {
                        msgs.AddWarning("Skipping existing workspace '" + workspaceName + "'");
                    }
                    else
                    {
                        IJTXWorkspaceConfiguration workspaceInfo = dbConnection.AddDataWorkspace();
                        this.CopyDataWorkspace(wmauWorkspaceInfo, ref workspaceInfo, msgs);
                        workspaceInfo.Store();
                        msgs.AddMessage("Added new workspace '" + workspaceName + "'");

                        IGPString outElement = new GPStringClass();
                        outElement.Value = workspaceName;
                        outMultiValue.AddValue(outElement as IGPValue);
                    }
                }

                // Set the value of the output parameter
                outParamEdit.Value = outMultiValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens }
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_WORKSPACE_LOAD_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        public void Execute(IArray paramvalues, ITrackCancel TrackCancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            IAoInitialize aoInitialize = new AoInitializeClass();
            esriLicenseStatus naStatus = esriLicenseStatus.esriLicenseUnavailable;

            IGPUtilities2 gpUtil = null;
            IDataset osmDataset = null;

            try
            {
                if (!aoInitialize.IsExtensionCheckedOut(esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork))
                    naStatus = aoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork);

                gpUtil = new GPUtilitiesClass();

                // OSM Dataset Param
                IGPParameter osmDatasetParam = paramvalues.get_Element(in_osmFeatureDataset) as IGPParameter;
                IDEDataset2 osmDEDataset = gpUtil.UnpackGPValue(osmDatasetParam) as IDEDataset2;
                if (osmDEDataset == null)
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), osmDatasetParam.Name));
                    return;
                }

                osmDataset = gpUtil.OpenDatasetFromLocation(((IDataElement)osmDEDataset).CatalogPath) as IDataset;

                // Network Config File Param
                IGPParameter osmNetConfigParam = paramvalues.get_Element(in_NetworkConfigurationFile) as IGPParameter;
                IGPValue osmNetConfigFile = gpUtil.UnpackGPValue(osmNetConfigParam) as IGPValue;
                if ((osmNetConfigFile == null) || (string.IsNullOrEmpty(osmNetConfigFile.GetAsText())))
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), osmNetConfigParam.Name));
                    return;
                }

                // Target Network Dataset Param
                IGPParameter ndsParam = paramvalues.get_Element(out_NetworkDataset) as IGPParameter;
                IDataElement deNDS = gpUtil.UnpackGPValue(ndsParam) as IDataElement;
                if (deNDS == null)
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), ndsParam.Name));
                    return;
                }

                // Create Network Dataset
                using (NetworkDataset nd = new NetworkDataset(osmNetConfigFile.GetAsText(), osmDataset, deNDS.Name, message, TrackCancel))
                {
                    if (nd.CanCreateNetworkDataset())
                        nd.CreateNetworkDataset();
                }
            }
            catch (UserCancelException ex)
            {
                message.AddWarning(ex.Message);
            }
            catch (Exception ex)
            {
                message.AddError(120008, ex.Message);
#if DEBUG
                message.AddError(120008, ex.StackTrace);
#endif
            }
            finally
            {
                if (osmDataset != null)
                    ComReleaser.ReleaseCOMObject(osmDataset);

                if (naStatus == esriLicenseStatus.esriLicenseCheckedOut)
                    aoInitialize.CheckInExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork);

                if (gpUtil != null)
                    ComReleaser.ReleaseCOMObject(gpUtil);

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }
Exemplo n.º 36
0
        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            // Get Parameters
            IGPParameter3 inputParameter   = (IGPParameter3)paramvalues.get_Element(0);
            IGPParameter3 polygonParameter = (IGPParameter3)paramvalues.get_Element(1);
            IGPParameter3 outputParameter  = (IGPParameter3)paramvalues.get_Element(2);
            IGPParameter3 fieldParameter   = (IGPParameter3)paramvalues.get_Element(3);

            // UnPackGPValue. This ensures you get the value from either the dataelement or GpVariable (ModelBuilder)
            IGPValue inputParameterValue   = m_GPUtilities.UnpackGPValue(inputParameter);
            IGPValue polygonParameterValue = m_GPUtilities.UnpackGPValue(polygonParameter);
            IGPValue outputParameterValue  = m_GPUtilities.UnpackGPValue(outputParameter);
            IGPValue fieldParameterValue   = m_GPUtilities.UnpackGPValue(fieldParameter);

            // Decode Input Feature Layers
            IFeatureClass inputFeatureClass;
            IFeatureClass polygonFeatureClass;

            IQueryFilter inputFeatureClassQF;
            IQueryFilter polygonFeatureClassQF;

            m_GPUtilities.DecodeFeatureLayer(inputParameterValue, out inputFeatureClass, out inputFeatureClassQF);
            m_GPUtilities.DecodeFeatureLayer(polygonParameterValue, out polygonFeatureClass, out polygonFeatureClassQF);

            if (inputFeatureClass == null)
            {
                message.AddError(2, "Could not open input dataset.");
                return;
            }

            if (polygonFeatureClass == null)
            {
                message.AddError(2, "Could not open clipping polygon dataset.");
                return;
            }

            if (polygonFeatureClass.FeatureCount(null) > 1)
            {
                message.AddWarning("Clipping polygon feature class contains more than one feature.");
            }

            // Create the Geoprocessor
            Geoprocessor gp = new Geoprocessor();

            // Create Output Polygon Feature Class
            CreateFeatureclass cfc      = new CreateFeatureclass();
            IName               name    = m_GPUtilities.CreateFeatureClassName(outputParameterValue.GetAsText());
            IDatasetName        dsName  = name as IDatasetName;
            IFeatureClassName   fcName  = dsName as IFeatureClassName;
            IFeatureDatasetName fdsName = fcName.FeatureDatasetName as IFeatureDatasetName;

            // Check if output is in a FeatureDataset or not. Set the output path parameter for CreateFeatureClass tool.
            if (fdsName != null)
            {
                cfc.out_path = fdsName;
            }
            else
            {
                cfc.out_path = dsName.WorkspaceName.PathName;
            }

            // Set the output Coordinate System for CreateFeatureClass tool.
            // ISpatialReference3 sr = null;
            IGPEnvironment env = envMgr.FindEnvironment("outputCoordinateSystem");

            // Same as Input
            if (env.Value.IsEmpty())
            {
                IGeoDataset ds = inputFeatureClass as IGeoDataset;
                cfc.spatial_reference = ds.SpatialReference as ISpatialReference3;
            }
            // Use the environment setting
            else
            {
                IGPCoordinateSystem cs = env.Value as IGPCoordinateSystem;
                cfc.spatial_reference = cs.SpatialReference as ISpatialReference3;
            }

            // Remaining properties for Create Feature Class Tool
            cfc.out_name      = dsName.Name;
            cfc.geometry_type = "POLYGON";

            // Execute Geoprocessor
            gp.Execute(cfc, null);

            // Get Unique Field
            int    iField      = inputFeatureClass.FindField(fieldParameterValue.GetAsText());
            IField uniqueField = inputFeatureClass.Fields.get_Field(iField);

            // Extract Clipping Polygon Geometry
            IFeature polygonFeature  = polygonFeatureClass.GetFeature(0);
            IPolygon clippingPolygon = (IPolygon)polygonFeature.Shape;

            // Spatial Filter
            ISpatialFilter spatialFilter = new SpatialFilterClass();

            spatialFilter.Geometry   = polygonFeature.ShapeCopy;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

            // Debug Message
            message.AddMessage("Generating TIN...");

            // Create TIN
            ITinEdit tinEdit = new TinClass();

            // Advanced TIN Functions
            ITinAdvanced2 tinAdv = (ITinAdvanced2)tinEdit;

            try
            {
                // Initialize New TIN
                IGeoDataset gds = inputFeatureClass as IGeoDataset;
                tinEdit.InitNew(gds.Extent);

                // Add Mass Points to TIN
                tinEdit.StartEditing();
                tinEdit.AddFromFeatureClass(inputFeatureClass, spatialFilter, uniqueField, uniqueField, esriTinSurfaceType.esriTinMassPoint);
                tinEdit.Refresh();

                // Get TIN Nodes
                ITinNodeCollection tinNodeCollection = (ITinNodeCollection)tinEdit;

                // Report Node Count
                message.AddMessage("Input Node Count: " + inputFeatureClass.FeatureCount(null).ToString());
                message.AddMessage("TIN Node Count: " + tinNodeCollection.NodeCount.ToString());

                // Open Output Feature Class
                IFeatureClass outputFeatureClass = m_GPUtilities.OpenFeatureClassFromString(outputParameterValue.GetAsText());

                // Debug Message
                message.AddMessage("Generating Polygons...");

                // Create Voronoi Polygons
                tinNodeCollection.ConvertToVoronoiRegions(outputFeatureClass, null, clippingPolygon, "", "");

                // Release COM Objects
                tinEdit.StopEditing(false);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tinNodeCollection);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tinEdit);
            }
            catch (Exception ex)
            {
                message.AddError(2, ex.Message);
            }
        }
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            // Assign the requested job
            try
            {
                IJTXJobManager     jobManager = this.WmxDatabase.JobManager;
                IJTXJob3           job        = jobManager.GetJob(m_jobId) as IJTXJob3;
                IJTXConfiguration3 configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;

                // As of Jan. 2011, the core Workflow Manager libraries do not
                // seem to check if the user has the privilege to add an attachment
                // if a job as a hold on it.  So run the check here.
                IJTXJobHolds jobHolds = job as IJTXJobHolds;
                if (jobHolds.Holds != null &&
                    jobHolds.Holds.Count > 0 &&
                    !CurrentUserHasPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_CAN_ADD_ATTACHES_FOR_HELD_JOBS))
                {
                    throw new WmauException(WmauErrorCodes.C_NO_ADD_ATTACHMENTS_HELD_JOBS_ERROR);
                }

                // If we get this far, then figure out how to associate the attachment
                // with the job, and add the attachment.
                jtxFileStorageType attachmentType;
                if (m_attachmentType.Equals(C_OPT_EMBEDDED))
                {
                    attachmentType = jtxFileStorageType.jtxStoreInDB;
                }
                else
                {
                    attachmentType = jtxFileStorageType.jtxStoreAsLink;
                }

                msgs.AddMessage("Adding attachment '" + m_attachmentPath + "' to job " + m_jobId + " (" + job.Name + ")");
                job.AddAttachment(m_attachmentPath, attachmentType, m_attachmentType);
                job.Store();

                // Do the other things that still need to be handled manually, such as logging
                // the job's reassignment and sending any necessary notifications.
                IPropertySet propSet = new PropertySetClass();
                propSet.SetProperty(C_PROP_VAL_ATTACHMENT, "'" + m_attachmentPath + "'");
                job.LogJobAction(
                    configMgr.GetActivityType(ESRI.ArcGIS.JTX.Utilities.Constants.ACTTYPE_ADD_ATTACHMENT),
                    propSet,
                    string.Empty);
                Common.WmauHelperFunctions.SendNotification(
                    ESRI.ArcGIS.JTX.Utilities.Constants.NOTIF_ATTACHMENT_ADDED,
                    this.WmxDatabase,
                    job);

                // Set the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_JOB_ID);
                IGPLong           outValue     = new GPLongClass();
                outValue.Value     = m_jobId;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
            }
        }
Exemplo n.º 38
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            IJTXJob4 job = null;

            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            //////////////////////////////////////////////////////////////////////
            // TODO: Update the job-creation logic.
            //
            // In subsequent builds of Workflow Manager (post-10.0), there may be
            // a new API that creates jobs and handles much of the logic included
            // in this function (and this class at large).  If so, this GP tool
            // should be revised to make use of this simplified interface.
            //
            // Anyone using this tool as a reference, particularly with regards
            // to creating Workflow Manager jobs, should keep this in mind.
            //////////////////////////////////////////////////////////////////////

            // Try to create the job, as requested
            try
            {
                IJTXJobManager2   jobManager = this.WmxDatabase.JobManager as IJTXJobManager2;
                IJTXConfiguration configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration;
                IJTXJobType4      jobTypeObj = configMgr.GetJobType(m_jobTypeAsString) as IJTXJobType4;

                // Set up the description object to be used to create this job
                IJTXJobDescription jobDescription = new JTXJobDescriptionClass();
                jobDescription.JobTypeName = m_jobTypeAsString;
                jobDescription.AOI         = GetPolygonFromSpecifiedLayer(m_aoiLayer);

                // Set up the ownership & assignment of the job
                jobDescription.OwnedBy = m_jobOwner;
                if (m_assigneeType.Equals(C_OPT_ASSIGN_TO_GROUP))
                {
                    jobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeGroup;
                    jobDescription.AssignedTo   = m_assignee;
                }
                else if (m_assigneeType.Equals(C_OPT_ASSIGN_TO_USER))
                {
                    jobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUser;
                    jobDescription.AssignedTo   = m_assignee;
                }
                else if (m_assigneeType.Equals(C_OPT_UNASSIGNED))
                {
                    jobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUnassigned;
                    jobDescription.AssignedTo   = string.Empty;
                }
                else
                {
                    // Do nothing; let the job type defaults take over
                    msgs.AddMessage("Using job type defaults for job assignment");
                    jobDescription.AssignedType = jobTypeObj.DefaultAssignedType;
                    jobDescription.AssignedTo   = jobTypeObj.DefaultAssignedTo;
                }

                // Start date
                if (m_startDate != null && m_startDate.Value != null)
                {
                    string tempStr = m_startDate.Value.ToString();

                    // Workflow Manager stores times as UTC times; input times must
                    // therefore be pre-converted
                    DateTime tempDate = DateTime.Parse(tempStr);
                    jobDescription.StartDate = TimeZone.CurrentTimeZone.ToUniversalTime(tempDate);
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for start date");
                    jobDescription.StartDate = jobTypeObj.DefaultStartDate;
                }

                // Due date
                if (m_dueDate != null && m_dueDate.Value != null)
                {
                    string tempStr = m_dueDate.Value.ToString();

                    // Workflow Manager stores times as UTC times; input times must
                    // therefore be pre-converted
                    DateTime tempDate = DateTime.Parse(tempStr);
                    jobDescription.DueDate = TimeZone.CurrentTimeZone.ToUniversalTime(tempDate);
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for due date");
                    jobDescription.DueDate = jobTypeObj.DefaultDueDate;
                }

                // Priority
                if (!m_priority.Equals(string.Empty))
                {
                    IJTXPriority priority = configMgr.GetPriority(m_priority);
                    jobDescription.Priority = priority;
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for priority");
                    jobDescription.Priority = jobTypeObj.DefaultPriority;
                }

                // Parent job
                if (m_parentJobId > 0)
                {
                    jobDescription.ParentJobId = m_parentJobId;
                }

                // Data workspace
                if (m_dataWorkspaceId.Equals(C_OPT_VAL_NOT_SET))
                {
                    jobDescription.DataWorkspaceID = string.Empty;
                }
                else if (!m_dataWorkspaceId.Equals(string.Empty))
                {
                    jobDescription.DataWorkspaceID = m_dataWorkspaceId;
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for data workspace");
                    if (jobTypeObj.DefaultDataWorkspace != null)
                    {
                        jobDescription.DataWorkspaceID = jobTypeObj.DefaultDataWorkspace.DatabaseID;
                    }
                }

                // Parent version
                if (m_parentVersion.Equals(C_OPT_VAL_NOT_SET))
                {
                    jobDescription.ParentVersionName = string.Empty;
                }
                else if (!m_parentVersion.Equals(string.Empty))
                {
                    jobDescription.ParentVersionName = m_parentVersion;
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for parent version");
                    jobDescription.ParentVersionName = jobTypeObj.DefaultParentVersionName;
                }

                // Auto-execution
                jobDescription.AutoExecuteOnCreate = m_executeNewJob;

                // Create the new job
                int             expectedNumJobs = 1;
                bool            checkAoi        = true;
                IJTXJobSet      jobSet          = null;
                IJTXExecuteInfo execInfo;
                try
                {
                    jobSet = jobManager.CreateJobsFromDescription(jobDescription, expectedNumJobs, checkAoi, out execInfo);
                }
                catch (System.Runtime.InteropServices.COMException comEx)
                {
                    throw new WmauException(WmauErrorCodes.C_CREATE_JOB_ERROR, comEx);
                }

                if ((execInfo != null && execInfo.ThrewError) ||
                    jobSet == null ||
                    jobSet.Count != expectedNumJobs)
                {
                    if (execInfo != null && !string.IsNullOrEmpty(execInfo.ErrorDescription))
                    {
                        throw new WmauException(
                                  WmauErrorCodes.C_CREATE_JOB_ERROR,
                                  new Exception(execInfo.ErrorCode.ToString() + ": " + execInfo.ErrorDescription));
                    }
                    else
                    {
                        throw new WmauException(WmauErrorCodes.C_CREATE_JOB_ERROR);
                    }
                }

                // If it gets all the way down here without errors, set the output ID with the
                // ID of the job that was created.
                job = jobSet.Next() as IJTXJob4;
                WmauParameterMap paramMap   = new WmauParameterMap(paramValues);
                IGPValue         jobIdGpVal = new GPLongClass();
                jobIdGpVal.SetAsText(job.ID.ToString());
                IGPParameterEdit3 jobIdParam = paramMap.GetParamEdit(C_PARAM_NEWJOBID);
                jobIdParam.Value = jobIdGpVal;
                msgs.AddMessage("Created job: " + job.ID.ToString() + " (" + job.Name + ")");

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                    if (job != null)
                    {
                        this.WmxDatabase.JobManager.DeleteJob(job.ID, true);
                    }
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_CREATE_JOB_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                    if (job != null)
                    {
                        this.WmxDatabase.JobManager.DeleteJob(job.ID, true);
                    }
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }
Exemplo n.º 39
0
        private string downloadOSMDocument(ref IGPMessages message, string requestURL, api apiCapabilities)
        {
            string osmDownloadDocument = String.Empty;
            HttpWebResponse httpResponse = null;

            try
            {

                // OSM does not understand URL encoded query parameters
                HttpWebRequest httpClient = HttpWebRequest.Create(requestURL) as HttpWebRequest;
                httpClient = AssignProxyandCredentials(httpClient);

                // read the timeout parameter
                int secondsToTimeout = Convert.ToInt32(apiCapabilities.timeout.seconds);
                httpClient.Timeout = secondsToTimeout * 1000;

                httpResponse = httpClient.GetResponse() as HttpWebResponse;

                osmDownloadDocument = System.IO.Path.GetTempFileName();

                using (System.IO.FileStream fileStream = new System.IO.FileStream(osmDownloadDocument, FileMode.Append, FileAccess.Write))
                {
                    using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        UTF8Encoding encoding = new UTF8Encoding();

                        byte[] byteBuffer = encoding.GetBytes(streamReader.ReadToEnd());
                        fileStream.Write(byteBuffer, 0, byteBuffer.Length);
                    }
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                message.AddError(120009, ex.Message);

                if (ex is WebException)
                {
                    WebException webException = ex as WebException;
                    if (webException != null)
                    {
                        if (webException.Response != null)
                        {
                            string serverErrorMessage = webException.Response.Headers["Error"];
                            if (!String.IsNullOrEmpty(serverErrorMessage))
                            {
                                message.AddError(120009, serverErrorMessage);
                            }
                        }
                    }
                }
            }
            finally
            {

                if (httpResponse != null)
                {
                    httpResponse.Close();
                }
            }
            return osmDownloadDocument;
        }
Exemplo n.º 40
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                // Retrieve the TA workbook
                IJTXConfiguration3 defaultDbReadonly      = WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXTaskAssistantWorkflowRecord tamRecord = defaultDbReadonly.GetTaskAssistantWorkflowRecord(this.m_targetName);
                string styleFileName = this.DetermineStyleFileName(this.m_xmlFilePath);

                // If we're not allowed to overwrite an existing TA record, then do some error checking
                if (!this.m_overwriteExisting && tamRecord != null)
                {
                    msgs.AddWarning("Did not overwrite Task Assistant workbook: " + this.m_targetName);
                    return;
                }
                else if (tamRecord != null)
                {
                    msgs.AddMessage("Replacing Task Assistant workbook '" + m_targetName + "' in database...");
                    defaultDbReadonly.ReplaceTaskAssistantWorkflowRecord(this.m_targetName, this.m_targetName, this.m_xmlFilePath, styleFileName);
                }
                else // tamRecord == null
                {
                    msgs.AddMessage("Adding Task Assistant workbook '" + m_targetName + "' to database...");
                    defaultDbReadonly.AddTaskAssistantWorkflowRecord(this.m_targetName, this.m_xmlFilePath, styleFileName);
                }

                // Update the output parameter
                WmauParameterMap  paramMap     = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParamEdit = paramMap.GetParamEdit(C_PARAM_OUT_TARGET_NAME);
                IGPString         outValue     = new GPStringClass();
                outValue.Value     = m_targetName;
                outParamEdit.Value = outValue as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_TAM_UPLOAD_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }