public int AddRawFact(Fact fact)
        {
            string sql = @"INSERT INTO gpsfact(carid, tripid, timeid, dateid, point)
                                               VALUES (@carid, @tripid, @timeid, @dateid,
                                                ST_SetSrid(ST_MakePoint(@pointlng, @pointlat), 4326))";

            NpgsqlCommand command = new NpgsqlCommand(sql, Connection);
            command.Parameters.AddWithValue("@carid", fact.CarId);
            command.Parameters.AddWithValue("@tripid", fact.TripId);

            //TemporalInformation
            command.Parameters.AddWithValue("@timeid", Convert.ToInt32(fact.Temporal.Timestamp.ToString("HHmmss")));
            command.Parameters.AddWithValue("@dateid", Convert.ToInt32(fact.Temporal.Timestamp.ToString("yyyyMMdd")));

            //SpatialInformation
            //point
            command.Parameters.AddWithValue("@pointlat", fact.Spatial.Point.Latitude);
            command.Parameters.AddWithValue("@pointlng", fact.Spatial.Point.Longitude);

            try {
                return NonQuery(command, "gpsfact");
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }

            return 0;
        }
        public int AddFact(Fact fact)
        {
            string sql = @"INSERT INTO gpsfact(carid, tripid, segmentid, qualityid, timeid, dateid, secondstolag, point, mpoint, distancetolag,
                                                speed, maxspeed, acceleration, jerk, speeding, accelerating, braking, jerking)
                                               VALUES (@carid, @tripid, @segmentid, @qualityid, @timeid, @dateid, @secondstolag,
                                                ST_SetSrid(ST_MakePoint(@pointlng, @pointlat), 4326), ST_SetSrid(ST_MakePoint(@mpointlng, @mpointlat), 4326),
                                                @distancetolag, @speed, @maxspeed, @acceleration, @jerk, @speeding, @accelerating, @braking, @jerking)";

            NpgsqlCommand command = new NpgsqlCommand(sql, Connection);
            command.Parameters.AddWithValue("@carid", fact.CarId);
            command.Parameters.AddWithValue("@tripid", fact.TripId);
            //command.Parameters.AddWithValue("@localtripid", fact.LocalTripId);

            //SegmentInformation
            if (fact.Segment != null) {
                command.Parameters.AddWithValue("@segmentid", fact.Segment.SegmentId);
            } else {
                command.Parameters.AddWithValue("@segmentid", DBNull.Value);
            }

            //QualityInformation
            if (fact.Quality != null) {
                command.Parameters.AddWithValue("@qualityid", fact.Quality.QualityId);
            } else {

                command.Parameters.AddWithValue("@qualityid", DBNull.Value);
            }

            //TemporalInformation

            //command.Parameters.AddWithValue("@timeid", Convert.ToInt32(fact.Temporal.Timestamp.ToString("HHmmss")));
            //command.Parameters.AddWithValue("@dateid", 20000101);

            command.Parameters.AddWithValue("@timeid", Convert.ToInt32(fact.Temporal.Timestamp.ToString("HHmmss")));
            command.Parameters.AddWithValue("@dateid", Convert.ToInt32(fact.Temporal.Timestamp.ToString("yyyyMMdd")));
            command.Parameters.AddWithValue("@secondstolag", fact.Temporal.SecondsToLag.TotalSeconds);

            //SpatialInformation
            //point
            command.Parameters.AddWithValue("@pointlat", fact.Spatial.Point.Latitude);
            command.Parameters.AddWithValue("@pointlng", fact.Spatial.Point.Longitude);

            //mpoint
            command.Parameters.AddWithValue("@mpointlat", 0);
            command.Parameters.AddWithValue("@mpointlng", 0);
            command.Parameters.AddWithValue("@distancetolag", fact.Spatial.DistanceToLag);

            //MeasureInformation
            command.Parameters.AddWithValue("@speed", fact.Measure.Speed);
            // ATTENTION
            //////// NO MAXSPEED ///////////
            // ATTENTION
            command.Parameters.AddWithValue("@maxspeed", 0);
            command.Parameters.AddWithValue("@acceleration", fact.Measure.Acceleration);
            command.Parameters.AddWithValue("@jerk", fact.Measure.Jerk);

            //FlagInformation
            command.Parameters.AddWithValue("@speeding", fact.Flag.Speeding);
            command.Parameters.AddWithValue("@accelerating", fact.Flag.Accelerating);
            command.Parameters.AddWithValue("@braking", fact.Flag.Braking);
            command.Parameters.AddWithValue("@jerking", fact.Flag.Jerking);

            try {
                return NonQuery(command, "gpsfact");
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }

            return 0;
        }