Esempio n. 1
0
        public void JoinLayers(string layerName1, string layerName2)
        {
            SpatialJoin sj = new SpatialJoin();

            sj.target_features   = layerName1;
            sj.join_features     = layerName2;
            sj.out_feature_class = @"C:\map\stepmaps.shp";
            sj.match_option      = "IS_WITHIN";
            this.RunProcess(sj, null);
            //return fc;
        }
Esempio n. 2
0
        public void makeHomeRangeSelectionMap(string stepMapName, string animalMemoryMapName)
        {
            this.MakeLayer(stepMapName, this.tempLayer1);
            this.MakeLayer(animalMemoryMapName, this.tempLayer2);
            string      path = System.IO.Path.GetDirectoryName(animalMemoryMapName);
            SpatialJoin sj   = new SpatialJoin();

            sj.target_features   = this.tempLayer1;
            sj.join_features     = this.tempLayer2;
            sj.match_option      = "IS_WITHIN";
            sj.out_feature_class = path + "\\HomeSteps.shp";
            this.RunProcess(sj, null);
        }
Esempio n. 3
0
        public static void SpatialJoin(object targetFC, object joinFC, string outputFcPath, JoinType joinType, string matchOption, IGPFieldMapping fieldMapping = null)
        {
            var geoprocessor = new Geoprocessor()
            {
                OverwriteOutput = true,
                AddOutputsToMap = false
            };

            try
            {
                Delete(outputFcPath);

                SpatialJoin spatialJoin = new SpatialJoin
                {
                    join_features     = joinFC,
                    target_features   = targetFC,
                    join_type         = joinType.ToString(),
                    match_option      = matchOption,
                    out_feature_class = outputFcPath,
                    field_mapping     = fieldMapping
                };

                var result = geoprocessor.Execute(spatialJoin, null);
            }
            catch (Exception)
            {
                if (geoprocessor.MessageCount > 0)
                {
                    string excMessage = null;
                    for (int i = 0; i < geoprocessor.MessageCount; i++)
                    {
                        excMessage += geoprocessor.GetMessage(i) + Environment.NewLine;
                    }

                    throw new Exception(excMessage);
                }
            }
        }
        private void ConstructPolygon(string folder, string from, int index, string layerField, string layer)
        {
            var select = new Select
            {
                in_features       = string.Format("{0}\\{1}.shp", folder, from),
                out_feature_class = string.Format("{0}\\{1}_{2}_1.shp", folder, from, index),
                where_clause      = string.Format("{0} = '{1}'", layerField, layer)
            };

            ExecuteProcess(select);

            var array = new string[] { string.Format("{0}\\{1}_{2}_1.shp", folder, from, index) };
            var f2p   = new FeatureToPolygon
            {
                in_features       = string.Join(";", array),
                out_feature_class = string.Format("{0}\\{1}_{2}_2.shp", folder, from, index)
            };


            ExecuteProcess(f2p);

            var sj = new SpatialJoin
            {
                target_features   = string.Format("{0}\\{1}_{2}_3.shp", folder, from, index),
                join_features     = string.Format("{0}\\{1}_{2}_1.shp", folder, from, index),
                out_feature_class = string.Format("{0}\\{1}_{2}.shp", folder, from, index),
                join_operation    = "JOIN_ONE_TO_ONE",
                join_type         = "KEEP_ALL",
                match_option      = "INTERSECTS",
                search_radius     = "0 METERS"
            };

            ExecuteProcess(sj);

            Delete(string.Format("{0}\\{1}_{2}_1.shp", folder, from, index));
            Delete(string.Format("{0}\\{1}_{2}_2.shp", folder, from, index));
            Delete(string.Format("{0}\\{1}_{2}_3.shp", folder, from, index));
        }
Esempio n. 5
0
        /// <summary>
        ///     Joins attributes from one feature to another based on the spatial relationship. The target features and the joined
        ///     attributes from the join features are written to the output feature class.
        /// </summary>
        /// <param name="source">
        ///     The source features and the attributes from the joined features are transferred to the output
        ///     feature class. However, a subset of attributes can be defined in the field map parameter.
        /// </param>
        /// <param name="join">The attributes from the join features are joined to the attributes of the source features.</param>
        /// <param name="tableName">A new feature class containing the attributes of the target and join features.</param>
        /// <param name="operation">
        ///     Determines how joins between the target features and join features will be handled in the
        ///     output feature class if multiple join features are found that have the same spatial relationship with a single
        ///     target feature.
        /// </param>
        /// <param name="isOuterJoin">
        ///     if set to <c>true</c> if all target features will be maintained in the output feature class
        ///     (known as outer join), or only those that have the specified spatial relationship with the join features (inner
        ///     join).
        /// </param>
        /// <param name="option">Defines the criteria used to match rows.</param>
        /// <param name="fieldMappings">The attribute fields will be in the output feature class.</param>
        /// <param name="searchRadius">
        ///     Join features within this distance of a target feature will be considered for the spatial
        ///     join.
        /// </param>
        /// <param name="trackCancel">The track cancel.</param>
        /// <param name="eventHandler">The event handler.</param>
        /// <returns>Returns a <see cref="IFeatureClass" /> representing the feature class of the joined data.</returns>
        public static IFeatureClass Join(this IFeatureClass source, IFeatureClass join, string tableName, JoinOperation operation, bool isOuterJoin, SpatialOperation option, IGpFieldMappingsObject fieldMappings, double searchRadius, ITrackCancel trackCancel, IGeoProcessorEvents eventHandler)
        {
            var ds = (IDataset)source;

            SpatialJoin gp = new SpatialJoin();

            gp.join_operation = operation.ToDescription();
            gp.join_type      = isOuterJoin ? "KEEP_ALL" : "KEEP_COMMON";
            gp.match_option   = option.ToDescription();
            gp.field_mapping  = fieldMappings;
            gp.search_radius  = searchRadius;

            gp.target_features   = source;
            gp.join_features     = join;
            gp.out_feature_class = ds.Workspace.GetAbsolutePath(tableName);

            if (gp.Run(trackCancel, eventHandler) == esriJobStatus.esriJobSucceeded)
            {
                return(ds.Workspace.GetFeatureClass(tableName));
            }

            return(null);
        }
        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;
        }