Beispiel #1
0
        public static void MatchTrip(Int16 carId, Int64 tripId)
        {
            DBController dbc = new DBController();
            List<Fact> facts = dbc.GetFactsForMapMatchingByCarIdAndTripId(carId, tripId);
            dbc.Close();
            string postData = "";

            //CSV FORMAT
            foreach (Fact f in facts) {
                postData += f.EntryId + "," + f.Spatial.Point.Longitude.ToString().Replace(",",".") + "," + f.Spatial.Point.Latitude.ToString().Replace(",", ".") + ",\"" + f.Temporal.Timestamp.ToString("yyyy-MM-dd") + "T" + f.Temporal.Timestamp.TimeOfDay + "\"" + System.Environment.NewLine;
            }

            //postData = ConvertToXML(facts);

            // Create a request using a URL that can receive a post.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://test.roadmatching.com/rest/mapmatch/?app_id=" + APPID + "&app_key=" + APPKEY + "&output.groupByWays=false&output.linkGeometries=false&output.osmProjection=false&output.linkMatchingError=false&output.waypoints=true&output.waypointsIds=true");

            // Set the Method property of the request to POST.
            request.Method = "POST";
            request.ContentType = "text/csv";
            //request.ContentType = "application/gpx+xml";

            request.Accept = "application/json";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine("Server response: " + responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

            var dbc2 = new DBController();
            try {
                Console.WriteLine(" Saving mapmatches in DB");
                SaveMapMatchingToDB(responseFromServer);
                Console.WriteLine(" Saving complete.");
                Console.WriteLine(" Updating unmatched MPoints with Point.");
                dbc2.UpdateMpointsWithPoint(carId, tripId);
                Console.WriteLine(" Update of MPoint complete.");
            } catch(Exception e) {
                Console.WriteLine(e.ToString());
            }
            dbc2.Close();
        }