public void TestGeoCoordinates() { var service = new GPSService(); var result = service.GetCartesianPoint(new SmartPlayerAPI.ViewModels.Pitch.PitchCornersPoints() { LeftUpPoint = new GPSPoint(54.370416, 18.630052), LeftDownPoint = new GPSPoint(54.370014, 18.629365), RightUpPoint = new GPSPoint(54.369786, 18.631127), RightDownPoint = new GPSPoint(54.369383, 18.630432) }, new GPSPoint(54.369568, 18.630247)); }
public async Task <IActionResult> SaveGPSBatch([FromBody] GPSBatch <GeoPointsInTime> locationBatch) { try { var playerInGame = await _playerInGameRepository .FindWithInclude(i => i.PlayerId == locationBatch.PlayerId && i.GameId == locationBatch.GameId, i => i.Game); if (playerInGame == null) { return(BadRequest("Bad playerId or gameId")); } var pitchId = playerInGame.Game.PitchId; if (pitchId == null) { return(BadRequest("Pitch is not recognized or null")); } var pitch = await _pitchRepository.FindById(pitchId.GetValueOrDefault()); if (pitch == null) { return(BadRequest("Pitch is not recognized or null")); } var pitchCornersPoints = _mapper.Map <PitchCornersPoints>(pitch); if (pitchCornersPoints == null) { return(BadRequest("error in mapping")); } var game = await _gameRepository.FindById(locationBatch.GameId); if (game == null) { return(BadRequest("Bad game id")); } GPSBatch <CartesianPointsInTime> result = new GPSBatch <CartesianPointsInTime>(); result.PlayerId = locationBatch.PlayerId; result.GameId = locationBatch.GameId; foreach (var p in locationBatch.ListOfPositions) { var time = game.TimeOfStart.AddMilliseconds(p.TimeOfOccurLong); if (p.Lat == null || p.Lng == null) { return(BadRequest("lat or lng is null")); } var xy = _gpsService.GetCartesianPoint(pitchCornersPoints, new GPSPoint(p.Lat, p.Lng)); if (xy == null || double.IsNaN(xy.X) || double.IsNaN(xy.Y) || double.IsInfinity(xy.X) || double.IsInfinity(xy.Y)) { return(BadRequest("cannot calculate distance between points. Value is Nan or infinity")); } var location = await GPSLocationRepository.AddAsync(new GPSLocation() { Lat = p.Lat, Lng = p.Lng, TimeOfOccur = game.TimeOfStart.AddMilliseconds(p.TimeOfOccurLong), PlayerInGameId = playerInGame.Id, X = xy.X, Y = xy.Y }); if (location == null) { return(BadRequest("Error during saving location coordinates in database")); } } return(Ok(result)); } catch (Exception e) { return(BadRequest()); } }