Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions MergerCli/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public void Start(IData baseData, IData newData, BatchStatusManager batchStatusM
}

this._logger.LogInformation($"[{MethodBase.GetCurrentMethod()?.Name}] Total amount of tiles to merge: {totalTileCount - tileProgressCount}");

ParallelRun(baseData, newData, batchStatusManager,
tileProgressCount, totalTileCount, resumeBatchIdentifier, resumeMode, pollForBatch);

batchStatusManager.CompleteLayer(newData.Path);
newData.Reset();
// base data wrap up is in program as the same base data object is used in multiple calls
Expand All @@ -79,7 +79,7 @@ public void Start(IData baseData, IData newData, BatchStatusManager batchStatusM
newData.setBatchIdentifier(resumeBatchIdentifier);
}
}

var incompleteBatch = batchStatusManager.GetFirstIncompleteBatch(newData.Path);
if (incompleteBatch is not null)
{
Expand All @@ -88,21 +88,21 @@ public void Start(IData baseData, IData newData, BatchStatusManager batchStatusM
}

List<Tile> newTiles = newData.GetNextBatch(out string? currentBatchIdentifier, out string? nextBatchIdentifier, totalTileCount);

if (!resumeMode && newTiles.Count != 0)
{
batchStatusManager.AssignBatch(newData.Path, currentBatchIdentifier);
batchStatusManager.SetCurrentBatch(newData.Path, nextBatchIdentifier);
}

return (newTiles, currentBatchIdentifier);
}
}
private void ProcessBatch(IData baseData, List<Tile> newTiles, ref long tileProgressCount, long totalTileCount,ref bool pollForBatch)

private void ProcessBatch(IData baseData, List<Tile> newTiles, ref long tileProgressCount, long totalTileCount, ref bool pollForBatch)
{
ConcurrentBag<Tile> tiles = new ConcurrentBag<Tile>();

if (newTiles.Count == 0)
{
pollForBatch = false;
Expand Down Expand Up @@ -137,7 +137,7 @@ private void ProcessBatch(IData baseData, List<Tile> newTiles, ref long tileProg
}

private void ParallelRun(IData baseData, IData newData,
BatchStatusManager batchStatusManager, long tileProgressCount, long totalTileCount, string? resumeBatchIdentifier, bool resumeMode,bool pollForBatch)
BatchStatusManager batchStatusManager, long tileProgressCount, long totalTileCount, string? resumeBatchIdentifier, bool resumeMode, bool pollForBatch)
{
var numOfThreads = this._configManager.GetConfiguration<int>("GENERAL", "parallel", "numOfThreads");
Parallel.For(0, numOfThreads, new ParallelOptions { MaxDegreeOfParallelism = -1 }, _ =>
Expand All @@ -151,7 +151,7 @@ private void ParallelRun(IData baseData, IData newData,
}
});
}

public void Validate(IData baseData, IData newData, string? incompleteBatchIdentifier)
{
List<Tile> newTiles;
Expand Down
46 changes: 35 additions & 11 deletions MergerLogic/Batching/Tile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ImageMagick;
using MergerLogic.DataTypes;
using MergerLogic.ImageProcessing;
using MergerLogic.Utils;
using System.ComponentModel.DataAnnotations;

namespace MergerLogic.Batching
Expand Down Expand Up @@ -33,33 +34,53 @@ public int Y

public TileFormat Format { get; internal set; }

public int Width { get; internal set; }

public int Height { get; internal set; }

private byte[] _data;

public Tile(int z, int x, int y, byte[] data)
private readonly IConfigurationManager _configManager;

public Tile(IConfigurationManager configuration, int z, int x, int y, byte[] data)
{
this.Z = z;
this.X = x;
this.Y = y;
this.Format = ImageFormatter.GetTileFormat(data) ?? throw new ValidationException($"Cannot create tile {this}, data is in invalid format");
this._data = data;
}
this._configManager = configuration;

var info = new MagickImageInfo(data);
this.Width = info.Width;
this.Height = info.Height;

public Tile(Coord cords, byte[] data)
{
this.Z = cords.Z;
this.X = cords.X;
this.Y = cords.Y;
this.Format = ImageFormatter.GetTileFormat(data) ?? throw new ValidationException($"Cannot create tile {this}, data is in invalid format");
this._data = data;

int allowedPixelSize = _configManager.GetConfiguration<int>("GENERAL", "allowedPixelSize");
if (this.Width != allowedPixelSize || this.Height != allowedPixelSize)
{
throw new ArgumentException($"The image dimensions ({this.Width}x{this.Height}) does not match the allowed size ({allowedPixelSize})");
}
}

public Tile(Coord cords, IMagickImage<byte> image)
public Tile(IConfigurationManager configuration, Coord cords, byte[] data) : this(configuration, cords.Z, cords.X, cords.Y, data) { }

public Tile(IConfigurationManager configuration, Coord cords, IMagickImage<byte> image)
{
this._configManager = configuration;
this.Z = cords.Z;
this.X = cords.X;
this.Y = cords.Y;
this.Format = ImageFormatter.GetTileFormat(image) ?? throw new ValidationException($"Cannot create tile {this}, data is in invalid format");
this.Width = image.Width;
this.Height = image.Height;
this._data = image.ToByteArray();

int allowedPixelSize = configuration.GetConfiguration<int>("GENERAL", "allowedPixelSize");
if (this.Width != allowedPixelSize || this.Height != allowedPixelSize)
{
throw new ArgumentException($"The image dimensions ({this.Width}x{this.Height}) does not match the allowed size ({allowedPixelSize})");
}
}

public bool HasCoords(int z, int x, int y)
Expand All @@ -78,6 +99,8 @@ public virtual void Print()
Console.WriteLine($"x: {this.X}");
Console.WriteLine($"y: {this.Y}");
// Console.WriteLine($"blob: {this.Blob}");
Console.WriteLine($"width: {this.Width}");
Console.WriteLine($"height: {this.Height}");
Console.WriteLine($"data Size: {this._data.Length}");
}

Expand All @@ -86,7 +109,8 @@ public virtual byte[] GetImageBytes()
return this._data;
}

public int Size() {
public int Size()
{
return this._data.Length;
}

Expand Down
6 changes: 3 additions & 3 deletions MergerLogic/Clients/FileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class FileClient : DataUtils, IFileClient
{
private readonly IFileSystem _fileSystem;

public FileClient(string path, IGeoUtils geoUtils, IFileSystem fileSystem)
: base(path, geoUtils)
public FileClient(string path, IGeoUtils geoUtils, IFileSystem fileSystem, IConfigurationManager configuration)
: base(path, geoUtils, configuration)
{
this._fileSystem = fileSystem;
}
Expand All @@ -31,7 +31,7 @@ public FileClient(string path, IGeoUtils geoUtils, IFileSystem fileSystem)

public override bool TileExists(int z, int x, int y)
{
return this.GetTilePath(z,x,y) != null;
return this.GetTilePath(z, x, y) != null;
}

private string? GetTilePath(int z, int x, int y)
Expand Down
2 changes: 1 addition & 1 deletion MergerLogic/Clients/GpkgClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class GpkgClient : DataUtils, IGpkgClient
private readonly IFileSystem _fileSystem;

public GpkgClient(string path, ITimeUtils timeUtils, ILogger<GpkgClient> logger, IFileSystem fileSystem,
IGeoUtils geoUtils) : base(path, geoUtils)
IGeoUtils geoUtils, IConfigurationManager configuration) : base(path, geoUtils, configuration)
{
this._timeUtils = timeUtils;
this._logger = logger;
Expand Down
2 changes: 1 addition & 1 deletion MergerLogic/Clients/HttpSourceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class HttpSourceClient : DataUtils, IHttpSourceClient
private IPathPatternUtils _pathPatternUtils;

public HttpSourceClient(string path, IHttpRequestUtils httpClient, IPathPatternUtils pathPatternUtils,
IGeoUtils geoUtils) : base(path, geoUtils)
IGeoUtils geoUtils, IConfigurationManager configuration) : base(path, geoUtils, configuration)
{
this._httpClient = httpClient;
this._pathPatternUtils = pathPatternUtils;
Expand Down
12 changes: 6 additions & 6 deletions MergerLogic/Clients/S3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class S3Client : DataUtils, IS3Client
private readonly IPathUtils _pathUtils;
private readonly S3StorageClass _storageClass;

public S3Client(IAmazonS3 client, IPathUtils pathUtils, IGeoUtils geoUtils, ILogger<S3Client> logger,
string storageClass, string bucket, string path) : base(path, geoUtils)
public S3Client(IAmazonS3 client, IPathUtils pathUtils, IGeoUtils geoUtils, ILogger<S3Client> logger, IConfigurationManager configuration,
string storageClass, string bucket, string path) : base(path, geoUtils, configuration)
{
this._client = client;
this._bucket = bucket;
Expand Down Expand Up @@ -105,10 +105,10 @@ public void UpdateTile(Tile tile)

var request = new PutObjectRequest()
{
BucketName = this._bucket,
CannedACL = S3CannedACL.PublicRead,
Key = String.Format(key),
StorageClass=this._storageClass
BucketName = this._bucket,
CannedACL = S3CannedACL.PublicRead,
Key = String.Format(key),
StorageClass = this._storageClass
};

byte[] buffer = tile.GetImageBytes();
Expand Down
12 changes: 8 additions & 4 deletions MergerLogic/ImageProcessing/TileMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ public class TileMerger : ITileMerger
{
private readonly ITileScaler _tileScaler;
private readonly ILogger<TileMerger> _logger;
private readonly IConfigurationManager _configurationManager;

public TileMerger(ITileScaler tileScaler, ILogger<TileMerger> logger)
public TileMerger(ITileScaler tileScaler, ILogger<TileMerger> logger, IConfigurationManager configuration)
{
this._logger = logger;
this._tileScaler = tileScaler;
this._configurationManager = configuration;
}

public Tile? MergeTiles(List<CorrespondingTileBuilder> tiles, Coord targetCoords, TileFormatStrategy strategy, bool uploadOnly = false)
{
if(uploadOnly) {
if (uploadOnly)
{
this._logger.LogDebug($"[{MethodBase.GetCurrentMethod()?.Name}] Configured to upload only mode");
// Ignore target if in upload only mode
tiles = tiles.Skip(1).ToList();

// In case there is only one source then we can just return the data as is
if(tiles.Count == 1) {
if (tiles.Count == 1)
{
this._logger.LogDebug($"[{MethodBase.GetCurrentMethod()?.Name}] Only one source was found, using raw image");
Tile? rawTile = tiles[0]();
rawTile?.ConvertToFormat(strategy.ApplyStrategy(rawTile.Format));
Expand Down Expand Up @@ -70,7 +74,7 @@ public TileMerger(ITileScaler tileScaler, ILogger<TileMerger> logger)
break;
}

Tile tile = new Tile(targetCoords, image);
Tile tile = new Tile(this._configurationManager, targetCoords, image);
image.Dispose();
tile.ConvertToFormat(strategy.ApplyStrategy(tile.Format));
return tile;
Expand Down
7 changes: 5 additions & 2 deletions MergerLogic/ImageProcessing/TileScaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ public class TileScaler : ITileScaler
private const int TILE_SIZE = 256;
private readonly IMetricsProvider _metricsProvider;
private readonly ILogger<TileScaler> _logger;
private readonly IConfigurationManager _configurationManager;

public TileScaler(IMetricsProvider metricsProvider, ILogger<TileScaler> logger)
public TileScaler(IMetricsProvider metricsProvider, ILogger<TileScaler> logger, IConfigurationManager configuration)
{
this._metricsProvider = metricsProvider;
this._logger = logger;
this._configurationManager = configuration;
}

public MagickImage? Upscale(MagickImage baseImage, Tile baseTile, Coord targetCoords)
{

string fromTileToCoordMessage = $"from tile z:{baseTile.Z}, x:{baseTile.X}, y:{baseTile.Y} to coords z:{targetCoords.Z}, x:{targetCoords.X}, y:{targetCoords.Y}";
this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] MagickImage Upscale Begin {fromTileToCoordMessage}");
var upscaleStopwatch = Stopwatch.StartNew();
Expand Down Expand Up @@ -135,7 +138,7 @@ public TileScaler(IMetricsProvider metricsProvider, ILogger<TileScaler> logger)
ImageFormatter.RemoveImageDateAttributes(upscale);
}

return upscale is null ? null : new Tile(targetCoords, upscale.ToByteArray());
return upscale is null ? null : new Tile(this._configurationManager, targetCoords, upscale.ToByteArray());
}
}
}
10 changes: 7 additions & 3 deletions MergerLogic/Utils/DataUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ public abstract class DataUtils : IDataUtils
{
protected readonly string path;
protected readonly IGeoUtils GeoUtils;
protected readonly IImageFormatter Formatter;
private readonly IConfigurationManager _configurationManager;

public DataUtils(string path, IGeoUtils geoUtils)
public DataUtils(string path, IGeoUtils geoUtils, IConfigurationManager configuration)
{
this.path = path;
this.GeoUtils = geoUtils;
this._configurationManager = configuration;
}

public virtual bool IsValidGrid(bool isOneXOne = false) {
public virtual bool IsValidGrid(bool isOneXOne = false)
{
return true;
}

Expand All @@ -35,7 +39,7 @@ public virtual bool IsValidGrid(bool isOneXOne = false) {
return null;
}

return new Tile(z, x, y, data);
return new Tile(this._configurationManager, z, x, y, data);
}

protected Tile? CreateTile(Coord coord, byte[]? data)
Expand Down
13 changes: 10 additions & 3 deletions MergerLogic/Utils/ImageUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImageMagick;
using MergerLogic.Batching;

namespace MergerLogic.Utils
{
Expand All @@ -12,14 +13,15 @@ public static bool IsTransparent(MagickImage image)
}

using var pixels = image.GetPixels();

foreach (var pixel in pixels)
{
if (pixel.ToColor()?.A != 255) {
if (pixel.ToColor()?.A != 255)
{
return true;
}
}

return false;
}

Expand All @@ -35,6 +37,11 @@ public static bool IsFullyTransparent(MagickImage image)
return pixels.Select(pixel => pixel.ToColor()).All(color => color?.A == 0);
}

public static bool IsValidImageDimensions(Tile tile, int allowedImageSize)
{
return tile.Width == allowedImageSize && tile.Height == allowedImageSize;
}

// public static bool IsEmpty(MagickImage image)
// {
// using var pixels = image.GetPixels();
Expand Down
12 changes: 8 additions & 4 deletions MergerLogic/Utils/UtilsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using System.IO.Abstractions;
using MergerLogic.Clients;
using System.ComponentModel;

namespace MergerLogic.Utils
{
Expand Down Expand Up @@ -30,19 +31,21 @@ public UtilsFactory(IPathUtils pathUtils, ITimeUtils timeUtils, IGeoUtils geoUti

public IFileClient GetFileUtils(string path)
{
return new FileClient(path, this._geoUtils, this._fileSystem);
return new FileClient(path, this._geoUtils, this._fileSystem, this._container.GetRequiredService<IConfigurationManager>());
}

public IGpkgClient GetGpkgUtils(string path)
{
var logger = this._container.GetRequiredService<ILogger<GpkgClient>>();
return new GpkgClient(path, this._timeUtils, logger, this._fileSystem, this._geoUtils);
return new GpkgClient(path, this._timeUtils, logger, this._fileSystem, this._geoUtils,
this._container.GetRequiredService<IConfigurationManager>());
}

public IHttpSourceClient GetHttpUtils(string path)
{
IPathPatternUtils pathPatternUtils = this.GetPathPatternUtils(path);
return new HttpSourceClient(path, this._httpRequestUtils, pathPatternUtils, this._geoUtils);
return new HttpSourceClient(path, this._httpRequestUtils, pathPatternUtils, this._geoUtils,
this._container.GetRequiredService<IConfigurationManager>());
}

public IS3Client GetS3Utils(string path)
Expand All @@ -55,7 +58,8 @@ public IS3Client GetS3Utils(string path)
throw new Exception("S3 Data utils requires s3 client to be configured");
}
var logger = this._container.GetRequiredService<ILogger<S3Client>>();
return new S3Client(client, this._pathUtils, this._geoUtils, logger, storageClass, bucket, path);
return new S3Client(client, this._pathUtils, this._geoUtils, logger, this._container.GetRequiredService<IConfigurationManager>(),
storageClass, bucket, path);
}

public T GetDataUtils<T>(string path) where T : IDataUtils
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading