/// <summary>
        /// given an index, retrieve the max and min point of its boundary in this grid
        /// </summary>
        /// <param name="index"></param>specifies the index to be retrieved
        /// <param name="maxPoint"></param>output, the maxpoint of the boundary
        /// <param name="minPoint"></param>output, the minpoint of the boundary
        public void RetrieveBoundary(int index, out LatLong maxPoint, out LatLong minPoint)
        {
            int indexLon = index % GridNumLon;
            int indexLat = index / GridNumLon;

            double minLat = this.Min.Latitude + this.GridSize * indexLat;
            double minLon = this.Min.Longitude + this.GridSize * indexLon;
            double maxLat = this.Min.Latitude + this.GridSize * (indexLat + 1);
            double maxLon = this.Min.Longitude + this.GridSize * (indexLon + 1);

            maxPoint = new LatLong(maxLat, maxLon);
            minPoint = new LatLong(minLat, minLon);
        }
        public static TotalAirQuality ReadAirQuality(string airQualityFilename, string aqiFilename, string aqiStationFilename)
        {
            Console.WriteLine("start to read the air quality file.");
            StreamReader srAQI = new StreamReader(aqiFilename);
            int          month_first = 0, month_old = 0;
            int          day_first = 0, day_old = 0;
            int          district_first = 0;
            bool         stop           = false;

            int           countDay  = 0;
            List <string> timeValue = new List <string>();

            Dictionary <Pair <int, Time>, int> aqis = new Dictionary <Pair <int, Time>, int>();

            srAQI.ReadLine();

            while (srAQI.Peek() > 0)
            {
                string[] items = srAQI.ReadLine().Split(new char[] { ',', '/', ':', ' ' });
                int      sid   = Convert.ToInt32(items[0]);
                int      month = Convert.ToInt32(items[1]);
                int      day   = Convert.ToInt32(items[2]);
                int      year  = Convert.ToInt32(items[3]);
                int      hour  = Convert.ToInt32(items[4]);
                if (hour == 12 && items[7] == "AM")
                {
                    hour = 0;
                }
                else if (items[7] == "PM" && hour != 12)
                {
                    hour = hour + 12;
                }


                Time time = new Time(year, month, day, hour);
                int  aqi  = Convert.ToInt32(items[8]);
                var  pair = new Pair <int, Time>(sid, time);

                if (!aqis.ContainsKey(pair))
                {
                    aqis.Add(pair, aqi);
                }

                //if (!stop)
                //{
                //    if (month_first == 0)
                //    {
                //        month_first = month;
                //        day_first = day;
                //        district_first = sid;
                //    }
                //    if (month == month_first && day == day_first && sid != district_first)
                //    {
                //        stop = true;
                //    }
                //    if (day != day_old || month != month_old)
                //    {
                timeValue.Add(Convert.ToInt32(items[3]).ToString("D2") + Convert.ToInt32(items[1]).ToString("D2") + Convert.ToInt32(items[2]).ToString("D2"));
                //}

                //month_old = month;
                //day_old = day;
                //}
            }
            srAQI.Close();

            timeValue = timeValue.Distinct().ToList();
            countDay  = timeValue.Count();

            FileInfo     fRead       = new FileInfo(airQualityFilename);
            var          totalSize   = fRead.Length;
            var          fs          = fRead.OpenRead();
            StreamReader sr          = new StreamReader(fs);
            long         completeOld = 0;

            List <AirQuality>[] airQualities = new List <AirQuality> [countDay * Time.NUMSLOTPERDAY];
            for (int i = 0; i < countDay * Time.NUMSLOTPERDAY; i++)
            {
                airQualities[i] = new List <AirQuality>();
            }
            sr.ReadLine();
            while (sr.Peek() > 0)
            {
                string[] items = sr.ReadLine().Split(new char[] { ',', '-', ':', ' ' });
                int      sid   = Convert.ToInt32(items[0]);
                int      year  = Convert.ToInt32(items[1]);
                int      month = Convert.ToInt32(items[2]);
                int      day   = Convert.ToInt32(items[3]);
                int      hour  = Convert.ToInt32(items[4]);


                double pm25 = items[7] == "NULL" ? -1 : Convert.ToDouble(items[7]);
                double pm10 = items[8] == "NULL" ? -1 : Convert.ToDouble(items[8]);
                double no2  = items[9] == "NULL" ? -1 : Convert.ToDouble(items[9]);
                double co   = items[10] == "NULL" ? -1 : Convert.ToDouble(items[10]);
                double o3   = items[11] == "NULL" ? -1 : Convert.ToDouble(items[11]);
                double so2  = items[12] == "NULL" ? -1 : Convert.ToDouble(items[12]);

                var pair = new Pair <int, Time>(sid, new Time(year, month, day, hour));
                int aqi  = 0;
                try
                {
                    aqi = aqis[pair];
                }
                catch (Exception)
                {
                    aqi = int.MaxValue;
                }
                AirQuality airQuality = new AirQuality(sid, new Time(year, month, day, hour), pm25, pm10, no2, co, o3, so2, aqi);
                int        index      = timeValue.IndexOf(items[1] + items[2] + items[3]);
                airQualities[index * Time.NUMSLOTPERDAY + hour].Add(airQuality);

                long complete = fs.Position * 10 / totalSize;
                if (complete != completeOld)
                {
                    Console.WriteLine(complete * 10 + "% completed!");
                }
                completeOld = complete;
            }

            sr.Close();

            StreamReader srStation             = new StreamReader(aqiStationFilename);
            Dictionary <int, LatLong> stations = new Dictionary <int, LatLong>();

            srStation.ReadLine();
            while (srStation.Peek() > 0)
            {
                string[] items   = srStation.ReadLine().Split(new char[] { '\t' });
                int      sid     = Convert.ToInt32(items[0]);
                LatLong  latlong = new LatLong(Convert.ToDouble(items[2]), Convert.ToDouble(items[3]));
                stations.Add(sid, latlong);
            }
            srStation.Close();

            TotalAirQuality totalAirQuality = new TotalAirQuality(airQualities, stations, timeValue);

            return(totalAirQuality);
        }
        /// <summary>
        /// read the meterology data from the file.
        /// </summary>
        /// <param name="meterologyFilename"></param>specifies the path to the meterology data file.
        /// <param name="stationLocationFilename"></param>specifies the path to the station-location correspondence file.
        /// <returns></returns>
        public static TotalMeterology ReadMeterology(string meterologyFilename, string stationLocationFilename)
        {
            //decide the number of days
            StreamReader srPre = new StreamReader(meterologyFilename);
            int          month_first = 0, month_old = 0;
            int          day_first = 0, day_old = 0;
            int          district_first = 0;

            int           countDay  = 0;
            List <string> timeValue = new List <string>();

            srPre.ReadLine();
            while (srPre.Peek() > 0)
            {
                string[] items    = srPre.ReadLine().Split(new char[] { ',', '-', ' ' });
                int      month    = Convert.ToInt32(items[2]);
                int      day      = Convert.ToInt32(items[3]);
                int      district = Convert.ToInt32(items[0]);

                if (month_first == 0)
                {
                    month_first    = month;
                    day_first      = day;
                    district_first = district;
                }
                if (month == month_first && day == day_first && district != district_first)
                {
                    break;
                }
                if (day != day_old || month != month_old)
                {
                    countDay++;
                    timeValue.Add(items[1] + items[2] + items[3]);
                }

                month_old = month;
                day_old   = day;
            }
            srPre.Close();

            Console.WriteLine("start to read the meterology data.");
            FileInfo     fRead       = new FileInfo(meterologyFilename);
            var          totalSize   = fRead.Length;
            var          fs          = fRead.OpenRead();
            StreamReader sr          = new StreamReader(fs);
            long         completeOld = 0;

            double rainFall, rainFallOld = 0.0;
            int    temperature, temperatureOld = 0;
            int    pressure, pressureOld = 0;
            int    humidity, humidityOld = 0;
            double windSpeed, windSpeedOld = 0;
            int    windDirection, windDirectionOld = 0;
            int    weatherFlag, weatherFlagOld = 0;

            List <Meterology>[] meterologies = new List <Meterology> [countDay * Time.NUMSLOTPERDAY];
            for (int i = 0; i < countDay * Time.NUMSLOTPERDAY; i++)
            {
                meterologies[i] = new List <Meterology>();
            }
            sr.ReadLine(); //read the attributes line
            while (sr.Peek() > 0)
            {
                string[] items = sr.ReadLine().Split(new char[] { ',', ':', ' ' });

                int      districtID = Convert.ToInt32(items[0]);
                string[] time       = items[1].Split(new char[] { '-' });
                int      month      = Convert.ToInt32(time[1]);
                int      day        = Convert.ToInt32(time[2]);
                int      year       = Convert.ToInt32(time[0]);
                int      hour       = Convert.ToInt32(items[2]);
                int      min        = Convert.ToInt32(items[3]);
                //int timeSlot = (min < 30) ? (hour * 2) : (hour * 2 + 1);
                int timeSlot = hour;

                rainFall      = items[5] != "NULL" ? Convert.ToDouble(items[5]) : rainFallOld;
                temperature   = items[6] != "NULL" ? (int)Convert.ToDouble(items[6]) : temperatureOld;
                pressure      = items[7] != "NULL" ? (int)Convert.ToDouble(items[7]) : pressureOld;
                humidity      = items[8] != "NULL" ? (int)Convert.ToDouble(items[8]) : humidityOld;
                windSpeed     = items[9] != "NULL" ? Convert.ToDouble(items[9]) : windSpeedOld;
                windDirection = items[10] != "-1" && items[10] != "NULL" ? Convert.ToInt32(items[10]) : windDirectionOld;
                weatherFlag   = items[11] != "NULL" ? Convert.ToInt32(items[11]) : weatherFlagOld;

                rainFallOld      = rainFall;
                temperatureOld   = temperature;
                pressureOld      = pressure;
                humidityOld      = humidity;
                windSpeedOld     = windSpeed;
                windDirectionOld = windDirection;
                weatherFlagOld   = weatherFlag;

                Meterology meterology = new Meterology(districtID, new Time(year, month, day, timeSlot), rainFall, temperature, pressure, humidity, windSpeed, windDirection, weatherFlag);
                //decide the index of the time
                int index = timeValue.IndexOf(time[0] + time[1] + time[2]);
                meterologies[index * Time.NUMSLOTPERDAY + timeSlot].Add(meterology);


                long complete = fs.Position * 10 / totalSize;
                if (complete != completeOld)
                {
                    Console.WriteLine(complete * 10 + "% completed!");
                }
                completeOld = complete;
            }
            sr.Close();

            Dictionary <LatLong, int> stationLocations = new Dictionary <LatLong, int>();
            StreamReader srStation = new StreamReader(stationLocationFilename);

            srStation.ReadLine();
            while (srStation.Peek() > 0)
            {
                string[] items   = srStation.ReadLine().Split(new char[] { ',' });
                LatLong  latlong = new LatLong(Convert.ToDouble(items[2]), Convert.ToDouble(items[3]));
                if (!stationLocations.ContainsKey(latlong))
                {
                    stationLocations.Add(latlong, Convert.ToInt32(items[4]));
                }
            }
            srStation.Close();

            TotalMeterology totalMeterology = new TotalMeterology(meterologies, stationLocations, timeValue);

            return(totalMeterology);
        }
        /// <summary>
        /// read all files regarding to the mobility of the city in all time.
        /// </summary>
        /// <param name="roadNetwork"></param>the road network that use.
        /// <param name="roadMapFilename"></param>specifies the path to the map of mobility edge to the edge of road network.
        /// <param name="speedDirectory"></param>specifies the directory to all the speed information.
        /// <param name="pickDropPointsDirectory"></param>specifies the directory to the pickup and dropoff points.
        /// <returns></returns>
        public static ToTalMobility ReadMobility(RoadNetwork roadNetwork, string roadMapFilename, string speedFilename, string pickDropPointsFilename, string dayName)
        {
            Console.WriteLine("start to read the road map file.");
            StreamReader srMap = new StreamReader(roadMapFilename);
            Dictionary <int, List <int> > edgeRecords = new Dictionary <int, List <int> >();

            while (srMap.Peek() > 0)
            {
                string[] items = srMap.ReadLine().Split(new char[] { '\t' });
                if (Convert.ToInt32(items[1]) >= 0)
                {
                    if (!edgeRecords.ContainsKey(Convert.ToInt32(items[1])))
                    {
                        List <int> oriEdges = new List <int>();
                        oriEdges.Add(Convert.ToInt32(items[0]));
                        edgeRecords.Add(Convert.ToInt32(items[1]), oriEdges);
                    }
                    else
                    {
                        edgeRecords[Convert.ToInt32(items[1])].Add(Convert.ToInt32(items[0]));
                    }
                }
            }
            srMap.Close();

            Console.WriteLine("start to read the speed file.");


            Time[] timeStamps             = new Time[Time.NUMSLOTPERDAY];
            List <PickDropPoint>[] points = new List <PickDropPoint> [Time.NUMSLOTPERDAY];
            List <MobilityEdge>[]  edges  = new List <MobilityEdge> [Time.NUMSLOTPERDAY];
            for (int i = 0; i < Time.NUMSLOTPERDAY; i++)
            {
                points[i] = new List <PickDropPoint>();
                edges[i]  = new List <MobilityEdge>();
            }

            FileInfo     fRead       = new FileInfo(speedFilename);
            var          totalSize   = fRead.Length;
            var          fs          = fRead.OpenRead();
            StreamReader srSpeed     = new StreamReader(fs);
            long         completeOld = 0;

            StreamWriter swSpeed = new StreamWriter(@"D:\Result\Temporary\SpeedDistribution\speed.txt");

            int count = 0;

            while (srSpeed.Peek() > 0)
            {
                string[] items    = srSpeed.ReadLine().Split(new char[] { ' ' });
                int      edgeID   = Convert.ToInt32(items[0]);
                int      timeSlot = Convert.ToInt32(items[1]) / 2;
                if (Convert.ToInt32(items[1]) == 1)
                {
                    ;
                }
                double avgs        = Convert.ToDouble(items[2]);
                int    numVehicles = Convert.ToInt32(items[4]);
                if (numVehicles < 0)
                {
                    numVehicles = 0;
                }
                double stds  = Convert.ToDouble(items[3]);
                int    year  = Convert.ToInt32(dayName.Substring(0, 4));
                int    month = Convert.ToInt32(dayName.Substring(4, 2));
                int    day   = Convert.ToInt32(dayName.Substring(6, 2));

                swSpeed.WriteLine(avgs);

                timeStamps[timeSlot] = new Time(year, month, day, timeSlot);
                foreach (var ind in edgeRecords[edgeID])
                {
                    Edge oriEdge = roadNetwork.Edges[ind];
                    edges[timeSlot].Add(new MobilityEdge(oriEdge.EdgeID, oriEdge.StartID, oriEdge.EndID, oriEdge.Length, oriEdge.RoadClass,
                                                         oriEdge.Direction, oriEdge.FormWay, oriEdge.Urban, oriEdge.MaxLanes, oriEdge.MaxSpeed, oriEdge.Level,
                                                         oriEdge.AttributeNum, oriEdge.Points, avgs, numVehicles, stds));
                }

                long complete = fs.Position * 10 / totalSize;
                if (complete != completeOld)
                {
                    Console.WriteLine(complete * 10 + "% completed!");
                }
                completeOld = complete;
                count++;
            }
            srSpeed.Close();
            swSpeed.Close();

            Console.WriteLine("start to read the pickdroppoint directory.");
            StreamReader srPoint = new StreamReader(pickDropPointsFilename);

            while (srPoint.Peek() > 0)
            {
                string[] items = srPoint.ReadLine().Split(new char[] { ' ', ':', '/' });
                int      hour  = Convert.ToInt32(items[3]);
                int      min   = Convert.ToInt32(items[4]);
                //int timeSlot = (min <= 30) ? (hour * 2) : (hour * 2 + 1);
                int     timeSlot = hour;
                LatLong location = new LatLong(Convert.ToDouble(items[6]), Convert.ToDouble(items[7]));
                points[timeSlot].Add(new PickDropPoint(location, Convert.ToInt32(items[8])));
            }
            srPoint.Close();


            //start to built the totalModality
            Mobility[] mobilities = new Mobility[Time.NUMSLOTPERDAY];
            for (int i = 0; i < Time.NUMSLOTPERDAY; i++)
            {
                if (timeStamps[i] != null)
                {
                    mobilities[i] = new Mobility(edges[i], points[i], timeStamps[i]);
                }
            }
            ToTalMobility totalModality = new ToTalMobility(roadNetwork.Nodes, mobilities);

            return(totalModality);
        }
        public int Flag; //flag = 1 represents "pick up" and flag = 2 represents "drop off"

        public PickDropPoint(LatLong l, int f)
        {
            this.Location = new LatLong(l.Latitude, l.Longitude);
            this.Flag     = f;
        }
 public Node(int nodeID, LatLong ll)
 {
     this.NodeID = nodeID;
     this.LatLon = new LatLong(ll.Latitude, ll.Longitude);
 }
Exemple #7
0
        public void TotalMeterologyFeature(Grid grid, string meterologyFeatureDirectory, int mode)
        {
            int gridNum  = grid.GridNumLat * grid.GridNumLon;
            int countDay = this.NumTimeSlots / Time.NUMSLOTPERDAY;

            List <LatLong> stations = new List <LatLong>();

            foreach (var s in this.StationLocations)
            {
                stations.Add(s.Key);
            }
            List <double>[] meterologyFeatureOld = new List <double> [gridNum];


            for (int i = 0; i < this.NumTimeSlots; i++)
            {
                List <Meterology> meterology        = this.Meterologies[i];
                List <double>[]   meterologyFeature = new List <double> [gridNum];
                for (int g = 0; g < gridNum; g++)
                {
                    meterologyFeatureOld[g] = new List <double>();
                }

                if (meterology.Count() > 0)
                {
                    for (int j = 0; j < gridNum; j++)
                    {
                        LatLong maxPoint, minPoint;
                        grid.RetrieveBoundary(j, out maxPoint, out minPoint);
                        LatLong central = new LatLong((maxPoint.Latitude - minPoint.Latitude) / 2 + minPoint.Latitude, (maxPoint.Longitude - minPoint.Longitude) / 2 + minPoint.Longitude);
                        LatLong nearest = Geo.FindKNN(central, stations, 1)[0];

                        int districtID = this.StationLocations[nearest];
                        meterologyFeature[j] = new List <double>();
                        int indexStation = grid.RetrieveIndex(nearest);

                        if (mode == 0 || (mode == 1 && indexStation == j))
                        {
                            int count = 0;
                            //feature 1: average rainfall
                            double avgRainFall = 0.0;
                            //feature 2: average temperature
                            int avgTemperature = 0;
                            //feature 3: average pressure
                            int avgPressure = 0;
                            //feature 4: average humidity
                            int avgHumidity = 0;
                            //feature 5: average windspeed
                            double avgWindSpeed = 0.0;
                            //feautre 6: average wind direction
                            int avgWindDirection = 0;
                            //feature 7: the majority of weather flag
                            Dictionary <int, int> weatherFlag = new Dictionary <int, int>();

                            foreach (var mtrl in meterology)
                            {
                                if (mtrl.DistrictID == districtID) //infer the meterology record that the region has
                                {
                                    avgRainFall      += mtrl.RainFall;
                                    avgTemperature   += mtrl.Temperature;
                                    avgPressure      += mtrl.Pressure;
                                    avgHumidity      += mtrl.Humidity;
                                    avgWindSpeed     += mtrl.WindSpeed;
                                    avgWindDirection += mtrl.WindDirection;
                                    if (!weatherFlag.ContainsKey(mtrl.WeatherFlag))
                                    {
                                        weatherFlag.Add(mtrl.WeatherFlag, 1);
                                    }
                                    else
                                    {
                                        weatherFlag[mtrl.WeatherFlag]++;
                                    }
                                    count++;
                                }
                            }

                            if (count > 0)
                            {
                                meterologyFeature[j].Add(avgRainFall / count);
                                meterologyFeature[j].Add(((double)avgTemperature) / count);
                                meterologyFeature[j].Add(((double)avgPressure) / count);
                                meterologyFeature[j].Add(((double)avgHumidity) / count);
                                meterologyFeature[j].Add(avgWindSpeed / count);
                                meterologyFeature[j].Add(((double)avgWindDirection) / count);

                                var flagSort = (from w in weatherFlag
                                                orderby w.Value
                                                descending
                                                select w.Key).ToList();
                                meterologyFeature[j].Add((double)flagSort[0]);
                            }
                            else
                            {
                                meterologyFeature[j].Add(Double.MaxValue);
                            }
                        }
                        else
                        {
                            meterologyFeature[j].Add(Double.MaxValue);
                        }
                    }
                    for (int j = 0; j < gridNum; j++)
                    {
                        meterologyFeatureOld[j] = new List <double>();
                        foreach (var mf in meterologyFeature[j])
                        {
                            meterologyFeatureOld[j].Add(mf);
                        }
                    }
                }
                else
                {
                    meterologyFeature = meterologyFeatureOld;
                }


                string directory = Path.Combine(meterologyFeatureDirectory, this.Days[i / Time.NUMSLOTPERDAY]);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                string filename = Path.Combine(directory, (i % Time.NUMSLOTPERDAY).ToString("D2"));
                IO.WriteFeature(meterologyFeature, filename);
            }
        }