private IEnumerable <INetworkEdge> EnumerateAdjacentTurnEdges(OSMTurnInfo osmTurn, bool useToJunction)
        {
            INetworkQuery query = (INetworkQuery)_networkDataset;

            // get turn FROM-edge
            INetworkSource      source          = _networkDataset.get_SourceByName(((IDataset)osmTurn.FromFeature.Class).Name);
            IEnumNetworkElement enumNetElements = query.get_EdgesByPosition(source.ID, osmTurn.FromFeature.OID, 0.0, false);
            INetworkEdge        edgeFrom        = enumNetElements.Next() as INetworkEdge;

            // get the FROM-edge Junctions
            INetworkJunction fromJunction = query.CreateNetworkElement(esriNetworkElementType.esriNETJunction) as INetworkJunction;
            INetworkJunction toJunction   = query.CreateNetworkElement(esriNetworkElementType.esriNETJunction) as INetworkJunction;

            edgeFrom.QueryJunctions(fromJunction, toJunction);

            // Get adjacent edges from the turn center junction
            INetworkJunction junction = ((useToJunction) ? toJunction : fromJunction);

            for (int n = 0; n < junction.EdgeCount; ++n)
            {
                INetworkEdge edge = query.CreateNetworkElement(esriNetworkElementType.esriNETEdge) as INetworkEdge;
                junction.QueryEdge(n, true, edge);

                if ((edge.OID == osmTurn.FromFeature.OID) || (edge.OID == osmTurn.ToFeature.OID))
                {
                    continue;
                }

                yield return(edge);
            }
        }
示例#2
0
        private IEnumerable <INetworkEdge> EnumerateAdjacentTurnEdges(OSMTurnInfo osmTurn, bool useToJunction)
        {
            INetworkQuery query = (INetworkQuery)_networkDataset;

            // get turn FROM-edge
            INetworkSource      source          = _networkDataset.get_SourceByName(((IDataset)osmTurn.FromFeature.Class).Name);
            IEnumNetworkElement enumNetElements = null;

            IRelationalOperator relationalOperator = ((IPolyline)osmTurn.ToFeature.Shape).FromPoint as IRelationalOperator;

            bool useFromPointOfToFeature = relationalOperator.Contains(osmTurn.ViaFeature.Shape);

            if (useFromPointOfToFeature)
            {
                enumNetElements = query.get_EdgesByPosition(source.ID, osmTurn.ToFeature.OID, 0, false);
            }
            else
            {
                enumNetElements = query.get_EdgesByPosition(source.ID, osmTurn.ToFeature.OID, 1, false);
            }

            INetworkEdge edgeFrom = enumNetElements.Next() as INetworkEdge;

            // get the FROM-edge Junctions
            INetworkJunction fromJunction = query.CreateNetworkElement(esriNetworkElementType.esriNETJunction) as INetworkJunction;
            INetworkJunction toJunction   = query.CreateNetworkElement(esriNetworkElementType.esriNETJunction) as INetworkJunction;

            edgeFrom.QueryJunctions(fromJunction, toJunction);

            // Get adjacent edges from the turn center junction
            INetworkJunction junction = ((useFromPointOfToFeature) ? fromJunction : toJunction);

            for (int n = 0; n < junction.EdgeCount; ++n)
            {
                INetworkEdge edge = query.CreateNetworkElement(esriNetworkElementType.esriNETEdge) as INetworkEdge;
                if (useFromPointOfToFeature)
                {
                    junction.QueryEdge(n, true, edge);
                }
                else
                {
                    junction.QueryEdge(n, false, edge);
                }

                //if ((edge.SourceID == osmTurn.FromFeature.OID) || (edge.SourceID == osmTurn.ToFeature.OID))
                if ((edge.OID == osmTurn.ToFeature.OID))
                {
                    continue;
                }

                yield return(edge);
            }
        }
        private static Dictionary <string, List <int> > GetEIDListsBySourceName(INetworkAnalystExtension nax, object searchObject, string baseName)
        {
            if (nax == null)
            {
                return(null);
            }

            bool             naxEnabled = false;
            IExtensionConfig naxConfig  = nax as IExtensionConfig;

            naxEnabled = naxConfig.State == esriExtensionState.esriESEnabled;

            if (!naxEnabled)
            {
                return(null);
            }

            INAWindow       naWindow  = nax.NAWindow;
            INALayer        naLayer   = null;
            INAContext      naContext = null;
            INetworkDataset nds       = null;

            naLayer = naWindow.ActiveAnalysis;
            if (naLayer != null)
            {
                naContext = naLayer.Context;
            }

            if (naContext != null)
            {
                nds = naContext.NetworkDataset;
            }

            INetworkQuery netQuery = nds as INetworkQuery;

            if (netQuery == null)
            {
                return(null);
            }

            bool oidSearch      = false;
            bool geometrySearch = false;

            if (searchObject == null)
            {
                return(null);
            }
            else if (searchObject is Dictionary <string, ILongArray> )
            {
                oidSearch = true;
            }
            else if (searchObject is IGeometry)
            {
                geometrySearch = true;
            }
            else
            {
                return(null);
            }

            VarType       vt          = GetEIDArrayParameterType();
            List <string> sourceNames = FindParameterizedSourceNames(nds, baseName, vt);
            Dictionary <string, List <int> > eidsBySourceName = new Dictionary <string, List <int> >();

            foreach (string sourceName in sourceNames)
            {
                INetworkSource netSource = nds.get_SourceByName(sourceName);
                int            sourceID  = netSource.ID;
                List <int>     eids      = new List <int>();

                if (oidSearch)
                {
                    Dictionary <string, ILongArray> oidArraysBySourceName = (Dictionary <string, ILongArray>)searchObject;
                    ILongArray          oids = null;
                    IEnumNetworkElement enumNetElement;
                    INetworkElement     netElement;

                    if (oidArraysBySourceName.TryGetValue(sourceName, out oids))
                    {
                        enumNetElement = netQuery.get_ElementsByOIDs(sourceID, oids);
                        enumNetElement.Reset();
                        netElement = enumNetElement.Next();
                        while (netElement != null)
                        {
                            eids.Add(netElement.EID);
                            netElement = enumNetElement.Next();
                        }
                    }
                }
                else if (geometrySearch)
                {
                    IGeometry searchGeometry = (IGeometry)searchObject;
                    if (searchGeometry != null && !searchGeometry.IsEmpty)
                    {
                        IGeometry elementGeometry          = null;
                        esriNetworkElementType elementType = esriNetworkElementType.esriNETEdge;
                        int eid = -1;

                        // Search for the network dataset layer associated with the active analysis layer or create one using the
                        // network dataset if matching one not found.
                        // If, for example, multiple network dataset layers are added to the map, the active analysis layer
                        // might not reference the current network dataset layer (nax.CurrentNetworkLayer).

                        INetworkLayer ndsLayer = new NetworkLayerClass();
                        ndsLayer.NetworkDataset = nds;

                        int count = nax.NetworkLayerCount;
                        for (int i = 0; i < count; ++i)
                        {
                            ndsLayer = nax.get_NetworkLayer(i);
                            if (ndsLayer.NetworkDataset == nds)
                            {
                                break;
                            }
                            else
                            {
                                ndsLayer = null;
                            }
                        }

                        if (ndsLayer == null)
                        {
                            ndsLayer = new NetworkLayerClass();
                            ndsLayer.NetworkDataset = nds;
                        }

                        IEnumLocatedNetworkElement enumLocatedNetElement = null;
                        if (ndsLayer != null)
                        {
                            enumLocatedNetElement = ndsLayer.SearchLocatedNetworkElements(sourceName, searchGeometry);
                            enumLocatedNetElement.Reset();
                            eid = enumLocatedNetElement.Next(ref elementGeometry, ref elementType);
                            while (eid != -1)
                            {
                                eids.Add(eid);
                                eid = enumLocatedNetElement.Next(ref elementGeometry, ref elementType);
                            }
                        }
                    }
                }

                eidsBySourceName.Add(sourceName, eids);
            }

            return(eidsBySourceName);
        }
示例#4
0
        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);
            }
        }