static void Main(string[] args) { try { // Initialize ArcObjects ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); LicenseInitializer aoLicenseInitializer = new LicenseInitializer(); if (!aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced }, new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork })) { System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage()); aoLicenseInitializer.ShutdownApplication(); return; } // Get the network dataset string networkDatasetPath = args[0]; INetworkDataset nd = GetNetworkDatasetFromPath(networkDatasetPath); var networkQuery = nd as INetworkQuery3; // Name of source containing transit lines. Probably hard-wired to "TransitLines" string sourceName = args[1]; // Get the source object from the network INetworkSource networkSource = nd.get_SourceByName(sourceName); int networkSourceID = networkSource.ID; // The SQLDbase containing the transit schedules string SQLDbase_path = args[2]; // Connect to the SQL database, loop through the network's features, and add EID values to the table for each SourceOID. string workspaceConnectionString = @"Data Source=" + SQLDbase_path + "; Version=3;"; using (SQLiteConnection conn = new SQLiteConnection(workspaceConnectionString)) { conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(conn)) { using (var transaction = conn.BeginTransaction()) { List<int> BadSourceOIDs = new List<int>(); // Get all the transit lines from the network IEnumNetworkElement transit_lines = networkQuery.get_ElementsForSource(networkSourceID); // Loop through all the transit lines and add their EIDs to the SQL table INetworkElement transit_line = transit_lines.Next(); while (transit_line != null) { // Note: We are assuming that there is a one-to-one mapping between SourceOID and EID. This should always be // the case for transit lines feature classes correctly created using the Add GTFS to a Network Dataset toolbox. int EID = transit_line.EID; int SourceOID = transit_line.OID; try { string updateStmt = string.Format("UPDATE linefeatures SET eid={0} WHERE SourceOID={1}", EID.ToString(), SourceOID.ToString()); cmd.CommandText = updateStmt; cmd.ExecuteNonQuery(); } catch { // For some reason, the item couldn't be inserted into the table, likely because SourceOID couldn't be found. BadSourceOIDs.Add(SourceOID); continue; } transit_line = transit_lines.Next(); } // Add the eids to the table in batch. transaction.Commit(); if (BadSourceOIDs.Count != 0) { // Write out an error if something went wrong while filling the table. Console.Error.WriteLine("The network EID value could not be determined for the following transit line source OIDs:"); foreach (int BadSourceOID in BadSourceOIDs) { Console.Error.WriteLine(BadSourceOID); } Console.Error.WriteLine("You probably need to recreate your transit lines feature class."); } } } } } catch (Exception e) { Console.Error.WriteLine(e.Message); } }
static void Main(string[] args) { try { // Initialize ArcObjects ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); LicenseInitializer aoLicenseInitializer = new LicenseInitializer(); if (!aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced }, new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork })) { System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage()); aoLicenseInitializer.ShutdownApplication(); return; } // Get the network dataset string networkDatasetPath = args[0]; INetworkDataset nd = GetNetworkDatasetFromPath(networkDatasetPath); var networkQuery = nd as INetworkQuery3; // Name of source containing transit lines. Probably hard-wired to "TransitLines" string sourceName = args[1]; // Get the source object from the network INetworkSource networkSource = nd.get_SourceByName(sourceName); int networkSourceID = networkSource.ID; // The SQLDbase containing the transit schedules string SQLDbase_path = args[2]; // Connect to the SQL database, loop through the network's features, and add EID values to the table for each SourceOID. string workspaceConnectionString = @"Data Source=" + SQLDbase_path + "; Version=3;"; using (SQLiteConnection conn = new SQLiteConnection(workspaceConnectionString)) { conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(conn)) { using (var transaction = conn.BeginTransaction()) { List <int> BadSourceOIDs = new List <int>(); // Get all the transit lines from the network IEnumNetworkElement transit_lines = networkQuery.get_ElementsForSource(networkSourceID); // Loop through all the transit lines and add their EIDs to the SQL table INetworkElement transit_line = transit_lines.Next(); while (transit_line != null) { // Note: We are assuming that there is a one-to-one mapping between SourceOID and EID. This should always be // the case for transit lines feature classes correctly created using the Add GTFS to a Network Dataset toolbox. int EID = transit_line.EID; int SourceOID = transit_line.OID; try { string updateStmt = string.Format("UPDATE linefeatures SET eid={0} WHERE SourceOID={1}", EID.ToString(), SourceOID.ToString()); cmd.CommandText = updateStmt; cmd.ExecuteNonQuery(); } catch { // For some reason, the item couldn't be inserted into the table, likely because SourceOID couldn't be found. BadSourceOIDs.Add(SourceOID); continue; } transit_line = transit_lines.Next(); } // Add the eids to the table in batch. transaction.Commit(); if (BadSourceOIDs.Count != 0) { // Write out an error if something went wrong while filling the table. Console.Error.WriteLine("The network EID value could not be determined for the following transit line source OIDs:"); foreach (int BadSourceOID in BadSourceOIDs) { Console.Error.WriteLine(BadSourceOID); } Console.Error.WriteLine("You probably need to recreate your transit lines feature class."); } } } } } catch (Exception e) { Console.Error.WriteLine(e.Message); } }