mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-05-07 13:37:47 +00:00
File scoped namespaces
This commit is contained in:
parent
c14377f976
commit
65aba44af6
152 changed files with 11962 additions and 12115 deletions
|
|
@ -6,36 +6,35 @@ using Microsoft.Extensions.Logging;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl.MapsforgeTiles
|
||||
namespace MapControl.MapsforgeTiles;
|
||||
|
||||
public partial class MapsforgeTileSource
|
||||
{
|
||||
public partial class MapsforgeTileSource
|
||||
public override async Task<IImage> LoadImageAsync(int zoomLevel, int column, int row)
|
||||
{
|
||||
public override async Task<IImage> LoadImageAsync(int zoomLevel, int column, int row)
|
||||
Bitmap bitmap = null;
|
||||
|
||||
try
|
||||
{
|
||||
Bitmap bitmap = null;
|
||||
var pixels = RenderTile(zoomLevel, column, row);
|
||||
|
||||
try
|
||||
if (pixels != null)
|
||||
{
|
||||
var pixels = RenderTile(zoomLevel, column, row);
|
||||
|
||||
if (pixels != null)
|
||||
unsafe
|
||||
{
|
||||
unsafe
|
||||
fixed (int* ptr = pixels)
|
||||
{
|
||||
fixed (int* ptr = pixels)
|
||||
{
|
||||
return new Bitmap(PixelFormat.Bgra8888, AlphaFormat.Opaque, (nint)ptr,
|
||||
new PixelSize(TileSize, TileSize), new Vector(96d, 96d), TileSize * 4);
|
||||
}
|
||||
return new Bitmap(PixelFormat.Bgra8888, AlphaFormat.Opaque, (nint)ptr,
|
||||
new PixelSize(TileSize, TileSize), new Vector(96d, 96d), TileSize * 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "LoadImageAsync");
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "LoadImageAsync");
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,136 +14,135 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace MapControl.MapsforgeTiles
|
||||
namespace MapControl.MapsforgeTiles;
|
||||
|
||||
public partial class MapsforgeTileSource : TileSource
|
||||
{
|
||||
public partial class MapsforgeTileSource : TileSource
|
||||
private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger<MapsforgeTileSource>();
|
||||
|
||||
private static MapDataStore mapDataStore;
|
||||
|
||||
private readonly DisplayModel displayModel = new();
|
||||
private InMemoryTileCache tileCache;
|
||||
private DatabaseRenderer renderer;
|
||||
private RenderThemeFuture renderThemeFuture;
|
||||
|
||||
public static void LoadMaps(string mapFileOrDirectory)
|
||||
{
|
||||
private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger<MapsforgeTileSource>();
|
||||
List<string> mapFiles;
|
||||
|
||||
private static MapDataStore mapDataStore;
|
||||
|
||||
private readonly DisplayModel displayModel = new();
|
||||
private InMemoryTileCache tileCache;
|
||||
private DatabaseRenderer renderer;
|
||||
private RenderThemeFuture renderThemeFuture;
|
||||
|
||||
public static void LoadMaps(string mapFileOrDirectory)
|
||||
if (mapFileOrDirectory.EndsWith(".map"))
|
||||
{
|
||||
List<string> mapFiles;
|
||||
|
||||
if (mapFileOrDirectory.EndsWith(".map"))
|
||||
{
|
||||
mapFiles = [mapFileOrDirectory];
|
||||
}
|
||||
else
|
||||
{
|
||||
mapFiles = [.. Directory.EnumerateFiles(mapFileOrDirectory, "*.map")];
|
||||
}
|
||||
|
||||
LoadMapFiles(mapFiles);
|
||||
mapFiles = [mapFileOrDirectory];
|
||||
}
|
||||
else
|
||||
{
|
||||
mapFiles = [.. Directory.EnumerateFiles(mapFileOrDirectory, "*.map")];
|
||||
}
|
||||
|
||||
public static void LoadMapFiles(List<string> mapFiles)
|
||||
LoadMapFiles(mapFiles);
|
||||
}
|
||||
|
||||
public static void LoadMapFiles(List<string> mapFiles)
|
||||
{
|
||||
if (mapFiles.Count == 1)
|
||||
{
|
||||
if (mapFiles.Count == 1)
|
||||
{
|
||||
Logger?.LogInformation("Loading {mapFile}", mapFiles[0]);
|
||||
Logger?.LogInformation("Loading {mapFile}", mapFiles[0]);
|
||||
|
||||
mapDataStore = new MapFile(mapFiles[0]);
|
||||
mapDataStore = new MapFile(mapFiles[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
var multiMapDataStore = new MultiMapDataStore(MultiMapDataStore.DataPolicy.DEDUPLICATE);
|
||||
mapDataStore = multiMapDataStore;
|
||||
|
||||
foreach (var mapFile in mapFiles)
|
||||
{
|
||||
Logger?.LogInformation("Loading {mapFile}", mapFile);
|
||||
|
||||
multiMapDataStore.addMapDataStore(new MapFile(mapFile), false, false);
|
||||
}
|
||||
else
|
||||
}
|
||||
}
|
||||
|
||||
public string Theme { get; set; } = "Default";
|
||||
|
||||
public int CacheCapacity { get; set; } = 200;
|
||||
|
||||
public float TextScale { get; set; } = 1f;
|
||||
|
||||
public float DpiScale
|
||||
{
|
||||
get => displayModel.getUserScaleFactor();
|
||||
set => displayModel.setUserScaleFactor(value);
|
||||
}
|
||||
|
||||
public int TileSize => displayModel.getTileSize();
|
||||
|
||||
public int[] RenderTile(int zoomLevel, int column, int row)
|
||||
{
|
||||
if (renderThemeFuture == null)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
int[] imageBuffer = null;
|
||||
var tile = new org.mapsforge.core.model.Tile(column, row, (byte)zoomLevel, displayModel.getTileSize());
|
||||
var job = new RendererJob(tile, mapDataStore, renderThemeFuture, displayModel, TextScale, false, false);
|
||||
var bitmap = tileCache.get(job) ?? renderer.executeJob(job);
|
||||
|
||||
if (bitmap != null)
|
||||
{
|
||||
var image = AwtGraphicFactory.getBitmap(bitmap);
|
||||
|
||||
if (image != null)
|
||||
{
|
||||
var multiMapDataStore = new MultiMapDataStore(MultiMapDataStore.DataPolicy.DEDUPLICATE);
|
||||
mapDataStore = multiMapDataStore;
|
||||
|
||||
foreach (var mapFile in mapFiles)
|
||||
{
|
||||
Logger?.LogInformation("Loading {mapFile}", mapFile);
|
||||
|
||||
multiMapDataStore.addMapDataStore(new MapFile(mapFile), false, false);
|
||||
}
|
||||
imageBuffer = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
|
||||
}
|
||||
}
|
||||
|
||||
public string Theme { get; set; } = "Default";
|
||||
return imageBuffer;
|
||||
}
|
||||
|
||||
public int CacheCapacity { get; set; } = 200;
|
||||
|
||||
public float TextScale { get; set; } = 1f;
|
||||
|
||||
public float DpiScale
|
||||
{
|
||||
get => displayModel.getUserScaleFactor();
|
||||
set => displayModel.setUserScaleFactor(value);
|
||||
}
|
||||
|
||||
public int TileSize => displayModel.getTileSize();
|
||||
|
||||
public int[] RenderTile(int zoomLevel, int column, int row)
|
||||
private void Initialize()
|
||||
{
|
||||
lock (displayModel)
|
||||
{
|
||||
if (renderThemeFuture == null)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
int[] imageBuffer = null;
|
||||
var tile = new org.mapsforge.core.model.Tile(column, row, (byte)zoomLevel, displayModel.getTileSize());
|
||||
var job = new RendererJob(tile, mapDataStore, renderThemeFuture, displayModel, TextScale, false, false);
|
||||
var bitmap = tileCache.get(job) ?? renderer.executeJob(job);
|
||||
|
||||
if (bitmap != null)
|
||||
{
|
||||
var image = AwtGraphicFactory.getBitmap(bitmap);
|
||||
|
||||
if (image != null)
|
||||
if (mapDataStore == null)
|
||||
{
|
||||
imageBuffer = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
|
||||
throw new InvalidOperationException("No map files loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
return imageBuffer;
|
||||
}
|
||||
Logger?.LogInformation("Loading render theme \"{theme}\".", Theme);
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
lock (displayModel)
|
||||
{
|
||||
if (renderThemeFuture == null)
|
||||
ZipInputStream zipInputStream = null;
|
||||
XmlRenderTheme renderTheme;
|
||||
|
||||
if (Theme.EndsWith(".zip"))
|
||||
{
|
||||
if (mapDataStore == null)
|
||||
{
|
||||
throw new InvalidOperationException("No map files loaded.");
|
||||
}
|
||||
|
||||
Logger?.LogInformation("Loading render theme \"{theme}\".", Theme);
|
||||
|
||||
ZipInputStream zipInputStream = null;
|
||||
XmlRenderTheme renderTheme;
|
||||
|
||||
if (Theme.EndsWith(".zip"))
|
||||
{
|
||||
zipInputStream = new ZipInputStream(new FileInputStream(Theme));
|
||||
renderTheme = new ZipRenderTheme(
|
||||
Path.GetFileName(Theme).Replace(".zip", ".xml"),
|
||||
new ZipXmlThemeResourceProvider(zipInputStream));
|
||||
}
|
||||
else if (Theme.EndsWith(".xml"))
|
||||
{
|
||||
renderTheme = new ExternalRenderTheme(Theme);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderTheme = MapsforgeThemes.valueOf(Theme.ToUpper());
|
||||
}
|
||||
|
||||
tileCache = new InMemoryTileCache(CacheCapacity);
|
||||
renderer = new DatabaseRenderer(mapDataStore, AwtGraphicFactory.INSTANCE, tileCache, null, true, false, null);
|
||||
renderThemeFuture = new RenderThemeFuture(AwtGraphicFactory.INSTANCE, renderTheme, displayModel);
|
||||
renderThemeFuture.run();
|
||||
zipInputStream?.close();
|
||||
|
||||
Logger?.LogInformation("Loading render theme done.");
|
||||
zipInputStream = new ZipInputStream(new FileInputStream(Theme));
|
||||
renderTheme = new ZipRenderTheme(
|
||||
Path.GetFileName(Theme).Replace(".zip", ".xml"),
|
||||
new ZipXmlThemeResourceProvider(zipInputStream));
|
||||
}
|
||||
else if (Theme.EndsWith(".xml"))
|
||||
{
|
||||
renderTheme = new ExternalRenderTheme(Theme);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderTheme = MapsforgeThemes.valueOf(Theme.ToUpper());
|
||||
}
|
||||
|
||||
tileCache = new InMemoryTileCache(CacheCapacity);
|
||||
renderer = new DatabaseRenderer(mapDataStore, AwtGraphicFactory.INSTANCE, tileCache, null, true, false, null);
|
||||
renderThemeFuture = new RenderThemeFuture(AwtGraphicFactory.INSTANCE, renderTheme, displayModel);
|
||||
renderThemeFuture.run();
|
||||
zipInputStream?.close();
|
||||
|
||||
Logger?.LogInformation("Loading render theme done.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,30 +4,29 @@ using System.Threading.Tasks;
|
|||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace MapControl.MapsforgeTiles
|
||||
namespace MapControl.MapsforgeTiles;
|
||||
|
||||
public partial class MapsforgeTileSource
|
||||
{
|
||||
public partial class MapsforgeTileSource
|
||||
public override async Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
||||
{
|
||||
public override async Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
||||
BitmapSource bitmap = null;
|
||||
|
||||
try
|
||||
{
|
||||
BitmapSource bitmap = null;
|
||||
var pixels = RenderTile(zoomLevel, column, row);
|
||||
|
||||
try
|
||||
if (pixels != null)
|
||||
{
|
||||
var pixels = RenderTile(zoomLevel, column, row);
|
||||
|
||||
if (pixels != null)
|
||||
{
|
||||
bitmap = BitmapSource.Create(TileSize, TileSize, 96d, 96d, PixelFormats.Bgra32, null, pixels, TileSize * 4);
|
||||
bitmap.Freeze();
|
||||
}
|
||||
bitmap = BitmapSource.Create(TileSize, TileSize, 96d, 96d, PixelFormats.Bgra32, null, pixels, TileSize * 4);
|
||||
bitmap.Freeze();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "LoadImageAsync");
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "LoadImageAsync");
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,37 +12,36 @@ using Microsoft.UI.Xaml.Media;
|
|||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
#endif
|
||||
|
||||
namespace MapControl.MapsforgeTiles
|
||||
namespace MapControl.MapsforgeTiles;
|
||||
|
||||
public partial class MapsforgeTileSource
|
||||
{
|
||||
public partial class MapsforgeTileSource
|
||||
public override async Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
||||
{
|
||||
public override async Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
|
||||
ImageSource image = null;
|
||||
var bitmap = new WriteableBitmap(TileSize, TileSize);
|
||||
using var stream = bitmap.PixelBuffer.AsStream();
|
||||
|
||||
try
|
||||
{
|
||||
ImageSource image = null;
|
||||
var bitmap = new WriteableBitmap(TileSize, TileSize);
|
||||
using var stream = bitmap.PixelBuffer.AsStream();
|
||||
|
||||
try
|
||||
// Run a Task because in WinUI/UWP LoadImageAsync is called in the UI thread.
|
||||
//
|
||||
await Task.Run(() =>
|
||||
{
|
||||
// Run a Task because in WinUI/UWP LoadImageAsync is called in the UI thread.
|
||||
//
|
||||
await Task.Run(() =>
|
||||
var pixels = RenderTile(zoomLevel, column, row);
|
||||
|
||||
if (pixels != null)
|
||||
{
|
||||
var pixels = RenderTile(zoomLevel, column, row);
|
||||
|
||||
if (pixels != null)
|
||||
{
|
||||
stream.Write(MemoryMarshal.AsBytes(pixels.AsSpan()));
|
||||
image = bitmap;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "LoadImageAsync");
|
||||
}
|
||||
|
||||
return image;
|
||||
stream.Write(MemoryMarshal.AsBytes(pixels.AsSpan()));
|
||||
image = bitmap;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "LoadImageAsync");
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue