Example #1
0
        public static void Parse(string filePath)
        {
            Debug.Log(filePath);
            if (!filePath.EndsWith("geojson"))
            {
                return;
            }
            string  text    = System.IO.File.ReadAllText(filePath);
            GeoJson rawData = JsonConvert.DeserializeObject <GeoJson>(text);

            RoadGraph.Instance.Init(rawData);
        }
Example #2
0
        public void Init(GeoJson rawData)
        {
            List <List <int> > nodeConnects           = new List <List <int> >();
            Dictionary <Vector2Int, RoadNode> nodeDic = new Dictionary <Vector2Int, RoadNode>();

            roadNodes    = new List <RoadNode>();
            roadSegments = new List <RoadSegment>();
            //iterate over each road
            foreach (var feature in rawData.features)
            {
                List <RoadNode>      nodes       = new List <RoadNode>();
                HashSet <Vector2Int> uniqueNodes = new HashSet <Vector2Int>();
                int len = feature.geometry.coordinates.Length;
                //add nodes
                for (int i = 0; i < len / 2; i++)
                {
                    Vector2 latlongPos = new Vector2(
                        feature.geometry.coordinates[i, 0],
                        feature.geometry.coordinates[i, 1]);
                    Vector2    worldPos = Conversion.GeoToWorldPosition(latlongPos, RefCenter);
                    Vector2Int intpos   = new Vector2Int((int)worldPos.x, (int)worldPos.y);
                    if (!uniqueNodes.Contains(intpos))
                    {
                        uniqueNodes.Add(intpos);
                    }
                    else
                    {
                        continue;
                    }
                    RoadNode node;
                    if (!nodeDic.TryGetValue(intpos, out node))
                    {
                        node          = new RoadNode(RoadNodes.Count);
                        node.Position = new float3()
                        {
                            x = worldPos.x, y = 0, z = worldPos.y
                        };
                        nodeDic.Add(intpos, node);
                        RoadNodes.Add(node);
                        nodeConnects.Add(new List <int>());
                    }

                    /*else
                     * {
                     *  Debug.Log("alreadyexist");
                     * }*/
                    nodes.Add(node);
                }
                //add segments
                for (int i = 0; i < nodes.Count - 1; i++)
                {
                    RoadSegment seg = new RoadSegment();
                    seg.StartNodeId = nodes[i].NodeId;
                    seg.EndNodeId   = nodes[i + 1].NodeId;
                    float3 dir = nodes[i + 1].Position - nodes[i].Position;
                    seg.Length         = Vector3.Magnitude(dir);
                    dir                = Vector3.Normalize(dir);
                    seg.Direction      = dir;
                    seg.RightDirection = math.cross(dir, new float3(0, 1, 0));
                    seg.IsOneWay       = feature.properties.oneway == "yes" ? 1 : 0;
                    seg.LaneNumber     = feature.properties.lanes == null ? 1 : int.Parse(feature.properties.lanes);
                    seg.Level          = GetRoadLevel(feature.properties.highway);
                    seg.LaneWidth      = GetLaneWidth(seg.Level);
                    seg.SegmentId      = roadSegments.Count;
                    float2 speedRange = GetSpeedRange(seg.Level);
                    seg.MaxSpeed     = speedRange.y / 3.6f;
                    seg.NameHashcode = feature.id == null ? 0 : feature.id.GetHashCode();
                    roadSegments.Add(seg);
                    nodeConnects[nodes[i].NodeId].Add(seg.SegmentId);
                    nodeConnects[nodes[i + 1].NodeId].Add(seg.SegmentId);
                }
            }

            //fill Nodes' ConnectionSegIds and ConnectionGroups field
            for (int i = 0; i < RoadNodes.Count; i++)
            {
                RoadNode nd = RoadNodes[i];
                //calculate ConnectionGroups field
                int  namehash      = roadSegments[nodeConnects[i][0]].NameHashcode;
                int  count         = 0;
                int  groupid       = 0;
                int2 roadPairSegId = -1;
                foreach (var segId in nodeConnects[i])
                {
                    if (roadSegments[segId].NameHashcode == namehash)
                    {
                        if (count == 0)
                        {
                            roadPairSegId.x = segId;
                        }
                        else
                        {
                            roadPairSegId.y = segId;
                        }
                        count++;
                    }
                    else
                    {
                        if (groupid < 3)
                        {
                            nd.ConnectionSegIds[groupid] = roadPairSegId;
                        }
                        roadPairSegId   = -1;
                        roadPairSegId.x = segId;
                        count           = 1;
                        groupid++;
                        namehash = roadSegments[segId].NameHashcode;
                    }
                }
                if (groupid < 3)
                {
                    nd.ConnectionSegIds[groupid] = roadPairSegId;
                }

                //prevent triple traffic light on crossroad
                if (nd.ConnectionSegIds[1].x != -1 && nd.ConnectionSegIds[1].y == -1 &&
                    nd.ConnectionSegIds[2].x != -1 && nd.ConnectionSegIds[2].y == -1)
                {
                    nd.ConnectionSegIds[1] = new int2()
                    {
                        x = nd.ConnectionSegIds[1].x, y = nd.ConnectionSegIds[2].x
                    };
                    nd.ConnectionSegIds[2] = -1;
                }

                RoadNodes[i] = nd;
            }
        }