public void UploadCsv_ShouldConvertItAndAddMissingFields() { IFormFile file = Substitute.For <IFormFile>(); file.OpenReadStream() .Returns(new MemoryStream(Encoding.UTF8.GetBytes( "Title,Description,Website,ImageUrl,FileUrl\r\ntitle,description,website?id=42,image,file"))); var fetcher = Substitute.For <IRemoteFileFetcherGateway>(); fetcher.GetFileContent("file").Returns(new RemoteFileFetcherGatewayResponse()); _httpGatewayFactory.CreateRemoteFileFetcherGateway(null).Returns(fetcher); var featureCollection = new FeatureCollection(new Collection <IFeature> { new Feature(new Point(new Coordinate(11, 12)), new AttributesTable()) }); _dataContainerConverterService.Convert(Arg.Any <byte[]>(), Arg.Any <string>(), FlowFormats.GEOJSON) .Returns(featureCollection.ToBytes()); var resutls = _controller.UploadCsv(file, "\\?id=(.*)", "http://sourceImageUrl/1.png", "icon", "icon-color", Categories.ROUTE_HIKE).Result as FileStreamResult; Assert.IsNotNull(resutls); var memoryStream = new MemoryStream(); resutls.FileStream.CopyTo(memoryStream); var resutlsString = Encoding.UTF8.GetString(memoryStream.ToArray()); Assert.IsTrue(resutlsString.Contains("42")); Assert.IsTrue(resutlsString.Contains("11")); Assert.IsTrue(resutlsString.Contains("12")); Assert.IsTrue(resutlsString.Contains("icon")); Assert.IsTrue(resutlsString.Contains("icon-color")); Assert.IsTrue(resutlsString.Contains("http://sourceImageUrl/1.png")); Assert.IsTrue(resutlsString.Contains("Hiking")); }
public async Task <IActionResult> PostGpsTrace([FromQuery] string url = "", [FromForm] IFormFile file = null) { var fileFetcherGatewayResponse = await GetFile(url, file); if (fileFetcherGatewayResponse == null) { return(BadRequest("Url is not provided or the file is empty... " + url)); } var gpxBytes = await _dataContainerConverterService.Convert(fileFetcherGatewayResponse.Content, fileFetcherGatewayResponse.FileName, DataContainerConverterService.GPX); var gpx = gpxBytes.ToGpx().UpdateBounds(); var highwayType = GetHighwayType(gpx); var gpxItmLines = GpxToItmLineStrings(gpx); if (!gpxItmLines.Any()) { return(BadRequest("File does not contain any traces...")); } var manipulatedItmLines = await _addibleGpxLinesFinderService.GetLines(gpxItmLines); var attributesTable = new AttributesTable { { "highway", highwayType } }; if (string.IsNullOrEmpty(url) == false) { attributesTable.Add("source", url); } var features = manipulatedItmLines.Select(l => new Feature(ToWgs84LineString(l.Coordinates), attributesTable) as IFeature).ToList(); return(Ok(new FeatureCollection(new Collection <IFeature>(features)))); }
public async Task <IActionResult> PostFindUnmappedPartsFromGpsTrace([FromQuery] int traceId) { var file = await CreateOsmGateway().GetTraceData(traceId); if (file == null) { return(BadRequest("Invalid trace id: " + traceId)); } using (var memoryStream = new MemoryStream()) { await file.Stream.CopyToAsync(memoryStream); var gpxBytes = await _dataContainerConverterService.Convert(memoryStream.ToArray(), file.FileName, DataContainerConverterService.GPX); var gpx = gpxBytes.ToGpx().UpdateBounds(); var highwayType = GetHighwayType(gpx); var gpxItmLines = GpxToItmLineStrings(gpx); if (!gpxItmLines.Any()) { return(BadRequest("File does not contain any traces...")); } var manipulatedItmLines = await _addibleGpxLinesFinderService.GetLines(gpxItmLines); var attributesTable = new AttributesTable { { "highway", highwayType } }; attributesTable.Add("source", "trace id: " + traceId); var featureCollection = new FeatureCollection(); foreach (var line in manipulatedItmLines) { featureCollection.Add(new Feature(ToWgs84LineString(line.Coordinates), attributesTable)); } return(Ok(featureCollection)); } }
private string SetupGpxUrl(gpxType gpx, List<LineString> addibleLines = null) { var url = "url"; var fetcher = Substitute.For<IRemoteFileFetcherGateway>(); var fileResponse = new RemoteFileFetcherGatewayResponse { FileName = url, Content = new byte[0] }; fetcher.GetFileContent(url).Returns(fileResponse); _dataContainerConverterService.Convert(Arg.Any<byte[]>(), Arg.Any<string>(), Arg.Any<string>()) .Returns(gpx.ToBytes()); _httpGatewayFactory.CreateRemoteFileFetcherGateway(Arg.Any<TokenAndSecret>()).Returns(fetcher); _addibleGpxLinesFinderService.GetLines(Arg.Any<List<ILineString>>()).Returns( addibleLines ?? new List <LineString> { new LineString(new[] {new Coordinate(0, 0), new Coordinate(1, 1)}) }.AsEnumerable() ); return url; }
public async Task <IActionResult> UploadCsv([FromForm] IFormFile file, [FromQuery] string idRegExPattern, [FromQuery] string sourceImageUrl, [FromQuery] string icon = "icon-bike", [FromQuery] string iconColor = "black", [FromQuery] string category = Categories.ROUTE_BIKE) { var reader = new StreamReader(file.OpenReadStream()); var csvReader = new CsvReader(reader); csvReader.Configuration.HeaderValidated = null; csvReader.Configuration.MissingFieldFound = null; var pointsOfInterest = csvReader.GetRecords <CsvPointOfInterestRow>().ToList(); var stream = new MemoryStream(); using (TextWriter writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) { var csvWriter = new CsvWriter(writer); csvWriter.Configuration.HasHeaderRecord = true; csvWriter.WriteHeader <CsvPointOfInterestRow>(); csvWriter.NextRecord(); var fetcher = _httpGatewayFactory.CreateRemoteFileFetcherGateway(null); foreach (var csvRow in pointsOfInterest) { if (!string.IsNullOrWhiteSpace(csvRow.FileUrl)) { var response = await fetcher.GetFileContent(csvRow.FileUrl); var geojsonBytes = await _dataContainerConverterService.Convert(response.Content, response.FileName, FlowFormats.GEOJSON); var geoJson = geojsonBytes.ToFeatureCollection(); var coordinate = geoJson.Features.First().Geometry.Coordinate; csvRow.Latitude = coordinate.Y; csvRow.Longitude = coordinate.X; } csvRow.SourceImageUrl = sourceImageUrl; csvRow.Website = csvRow.Website; csvRow.Icon = icon; csvRow.IconColor = iconColor; csvRow.Category = category; csvRow.Id = Regex.Match(csvRow.Website, idRegExPattern).Groups[1].Value; csvWriter.WriteRecord(csvRow); csvWriter.NextRecord(); } csvWriter.Flush(); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); return(File(stream, "text/csv")); } }
public void ConvertKmzToGeoJson_ShouldConvert() { var zipfileStream = new MemoryStream(); using (var zipOutputStream = new ZipOutputStream(zipfileStream)) { ZipEntry entry = new ZipEntry("file.kml"); zipOutputStream.PutNextEntry(entry); new MemoryStream(_randomBytes).CopyTo(zipOutputStream); zipOutputStream.CloseEntry(); } _gpsBabelGateway.ConvertFileFromat(Arg.Any <byte[]>(), FlowFormats.KML_BABEL_FORMAT, FlowFormats.GPX_BABEL_FORMAT).Returns(_simpleGpx.ToBytes()); var converterd = _converterService.Convert(zipfileStream.ToArray(), "file.kmz", FlowFormats.GEOJSON).Result; Assert.AreNotEqual(0, converterd.Length); }
private int SetupGpxUrl(GpxFile gpx, List <LineString> addibleLines = null) { int traceId = 1; var fetcher = Substitute.For <IAuthClient>(); var fileResponse = new TypedStream { FileName = "file.gpx", Stream = new MemoryStream(new byte[0]) }; fetcher.GetTraceData(traceId).Returns(fileResponse); _dataContainerConverterService.Convert(Arg.Any <byte[]>(), Arg.Any <string>(), Arg.Any <string>()) .Returns(gpx.ToBytes()); _clientsFactory.CreateOAuthClient(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>()).Returns(fetcher); _addibleGpxLinesFinderService.GetLines(Arg.Any <List <LineString> >()).Returns( addibleLines ?? new List <LineString> { new LineString(new[] { new Coordinate(0, 0), new Coordinate(1, 1) }) }.AsEnumerable() ); return(traceId); }