Skip to content

Commit

Permalink
Merge pull request #2336 from Mapsui/feature/remove-dispose-of-feature
Browse files Browse the repository at this point in the history
Remove RenderedGeometry and IDisposable from IFeature
  • Loading branch information
pauldendulk committed Dec 11, 2023
2 parents 7a47260 + 0546b22 commit c9414d5
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 109 deletions.
10 changes: 1 addition & 9 deletions Mapsui.Nts/GeometryFeature.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Diagnostics;
using Mapsui.Layers;
using Mapsui.Nts.Extensions;
using NetTopologySuite.Geometries;

namespace Mapsui.Nts;

public class GeometryFeature : BaseFeature, IFeature, IDisposable
public class GeometryFeature : BaseFeature, IFeature
{
private bool _disposed;

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / linuxBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / linuxBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / linuxBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / build-and-publish-website

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / winBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / winBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / winBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / macBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / macBuild

The field 'GeometryFeature._disposed' is never used

Check warning on line 10 in Mapsui.Nts/GeometryFeature.cs

View workflow job for this annotation

GitHub Actions / macBuild

The field 'GeometryFeature._disposed' is never used

Expand Down Expand Up @@ -37,13 +36,6 @@ public GeometryFeature(Geometry? geometry)

public MRect? Extent => Geometry?.EnvelopeInternal.ToMRect();

public override void Dispose()
{
if (_disposed) return;
base.Dispose();
_disposed = true;
}

public void CoordinateVisitor(Action<double, double, CoordinateSetter> visit)
{
if (Geometry is null) return;
Expand Down
3 changes: 1 addition & 2 deletions Mapsui.Nts/Providers/Shapefile/ShapeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ private Collection<uint> GetObjectIDsInViewPrivate(MRect? bbox)
{
if (FilterDelegate != null) //Apply filtering
{
using var fdr = GetFeature(oid);
return fdr?.Geometry;
return GetFeature(oid)?.Geometry;
}

return ReadGeometry(oid);
Expand Down
7 changes: 1 addition & 6 deletions Mapsui.UI.MapView/Objects/Callout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Mapsui.UI.Maui;

public class Callout : IFeatureProvider, IDisposable, INotifyPropertyChanged
public class Callout : IFeatureProvider, INotifyPropertyChanged
{
private readonly Pin _pin;

Expand Down Expand Up @@ -602,11 +602,6 @@ internal void Update()
UpdateCalloutStyle();
}

public virtual void Dispose()
{
Feature.Dispose();
}

public event PropertyChangedEventHandler? PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
Expand Down
12 changes: 1 addition & 11 deletions Mapsui.UI.MapView/Objects/MyLocationLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Mapsui.UI.Objects;
/// A layer to display a symbol for own location
/// </summary>
/// <remarks>
/// There are two different symbols for own loaction: one is used when there isn't a change in position (still),
/// There are two different symbols for own location: one is used when there isn't a change in position (still),
/// and one is used, if the position changes (moving).
/// </remarks>
public class MyLocationLayer : BaseLayer
Expand Down Expand Up @@ -485,16 +485,6 @@ public void UpdateMyViewDirection(double newDirection, double newViewportRotatio
}
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_feature.Dispose();
}

base.Dispose(disposing);
}

private bool InternalUpdateMyLocation(Position newLocation)
{
var modified = false;
Expand Down
9 changes: 1 addition & 8 deletions Mapsui.UI.MapView/Objects/Pin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Mapsui.UI.Maui;

public class Pin : IFeatureProvider, IDisposable, INotifyPropertyChanged
public class Pin : IFeatureProvider, INotifyPropertyChanged
{
// Cache for used bitmaps
private static readonly Dictionary<string, int> _bitmapIds = new Dictionary<string, int>();
Expand Down Expand Up @@ -57,7 +57,6 @@ public Pin()
_mapView?.RemoveCallout(_callout);
}

Feature?.Dispose();
Feature = null;
_mapView = value;

Expand Down Expand Up @@ -603,12 +602,6 @@ private void CreateFeature()
}
}

public virtual void Dispose()
{
_callout?.Dispose();
Feature?.Dispose();
}

public event PropertyChangedEventHandler? PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
Expand Down
35 changes: 2 additions & 33 deletions Mapsui/Features/BaseFeature.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Collections.Concurrent;
using Mapsui.Styles;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using Mapsui.Styles;

namespace Mapsui.Layers;

public abstract class BaseFeature : IDisposable
public abstract class BaseFeature
{
// last used feature id
private static long _currentFeatureId;
Expand Down Expand Up @@ -48,7 +46,6 @@ private void Copy(BaseFeature baseFeature)
}

private readonly Dictionary<string, object?> _dictionary = new();
private IDictionary<IStyle, object>? _renderedGeometry;

public ICollection<IStyle> Styles { get; set; } = new Collection<IStyle>();
public IEnumerable<string> Fields => _dictionary.Keys;
Expand All @@ -59,37 +56,9 @@ private void Copy(BaseFeature baseFeature)
set => _dictionary[key] = value;
}

public IDictionary<IStyle, object> RenderedGeometry
{
get => _renderedGeometry ??= new ConcurrentDictionary<IStyle, object>();
set => _renderedGeometry = value;
}

public void Modified()
{
// is modified needs a new id.
Id = NextId();
ClearRenderedGeometry();
}

public virtual void Dispose()
{
ClearRenderedGeometry();
}

public void ClearRenderedGeometry()
{
if (_renderedGeometry != null)
{
var values = _renderedGeometry.Values.ToArray();
_renderedGeometry = null;
foreach (var value in values)
{
if (value is IDisposable disposable)
{
disposable.Dispose();
}
}
}
}
}
3 changes: 1 addition & 2 deletions Mapsui/IFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ namespace Mapsui;

public delegate void CoordinateSetter(double x, double y);

public interface IFeature : IDisposable
public interface IFeature
{
ICollection<IStyle> Styles { get; }
object? this[string key] { get; set; }
IEnumerable<string> Fields { get; }
MRect? Extent { get; }
public IDictionary<IStyle, object> RenderedGeometry { get; }
void CoordinateVisitor(Action<double, double, CoordinateSetter> visit);
long Id => 0;
void Modified() { } // default implementation
Expand Down
10 changes: 0 additions & 10 deletions Mapsui/Layers/MyLocationLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,6 @@ public override IEnumerable<IFeature> GetFeatures(MRect box, double resolution)
return _features;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_feature.Dispose();
}

base.Dispose(disposing);
}

private bool InternalUpdateMyLocation(MPoint newLocation)
{
var modified = false;
Expand Down
21 changes: 0 additions & 21 deletions Mapsui/Layers/RasterizingLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class RasterizingLayer : BaseLayer, IAsyncDataFetcher, ISourceLayer
private bool _busy;
private MSection? _currentSection;
private bool _modified;
private IEnumerable<IFeature>? _previousFeatures;
private readonly IRenderer _rasterizer = DefaultRendererFactory.Create();
private FetchInfo? _fetchInfo;
public Delayer Delayer { get; } = new();
Expand Down Expand Up @@ -95,8 +94,6 @@ private void Rasterize()
using var bitmapStream = _rasterizer.RenderToBitmapStream(ToViewport(_currentSection),
new[] { _layer }, pixelDensity: _pixelDensity, renderFormat: _renderFormat);

RemoveExistingFeatures();

_cache.Clear();
var features = new RasterFeature[1];
features[0] = new RasterFeature(new MRaster(bitmapStream.ToArray(), _currentSection.Extent));
Expand All @@ -116,24 +113,6 @@ private void Rasterize()
}
}

private void RemoveExistingFeatures()
{
var features = _cache.ToArray();
_cache.Clear(); // clear before dispose to prevent possible null disposed exception on render

// Disposing previous and storing current in the previous field to prevent dispose during rendering.
if (_previousFeatures != null) DisposeRenderedGeometries(_previousFeatures);
_previousFeatures = features;
}

private static void DisposeRenderedGeometries(IEnumerable<IFeature> features)
{
foreach (var feature in features.Cast<RasterFeature>())
{
feature.ClearRenderedGeometry();
}
}

public static double SymbolSize { get; set; } = 64;

public override IEnumerable<IFeature> GetFeatures(MRect box, double resolution)
Expand Down
10 changes: 5 additions & 5 deletions Tests/Mapsui.Rendering.Skia.Tests/LabelStyleFeatureSizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void DefaultSizeFeatureSize()
LabelColumn = "test",
};

using var feature = new PointFeature(new MPoint(0, 0));
var feature = new PointFeature(new MPoint(0, 0));
feature["test"] = "Mapsui";

using var skPaint = new SKPaint();
Expand All @@ -44,7 +44,7 @@ public void DefaultSizeFeatureSize_Font()

labelStyle.Font.Size *= 2;

using var feature = new PointFeature(new MPoint(0, 0));
var feature = new PointFeature(new MPoint(0, 0));
feature["test"] = "Mapsui";

using var skPaint = new SKPaint();
Expand All @@ -70,7 +70,7 @@ public void DefaultSizeFeatureSize_Offset_x()
Offset = new Offset(2, 0),
};

using var feature = new PointFeature(new MPoint(0, 0));
var feature = new PointFeature(new MPoint(0, 0));
feature["test"] = "Mapsui";

using var skPaint = new SKPaint();
Expand All @@ -88,7 +88,7 @@ public void DefaultSizeFeatureSize_Offset_y()
Offset = new Offset(0, 2),
};

using var feature = new PointFeature(new MPoint(0, 0));
var feature = new PointFeature(new MPoint(0, 0));
feature["test"] = "Mapsui";

using var skPaint = new SKPaint();
Expand All @@ -106,7 +106,7 @@ public void DefaultSizeFeatureSize_Offset_x_y()
Offset = new Offset(2, 2),
};

using var feature = new PointFeature(new MPoint(0, 0));
var feature = new PointFeature(new MPoint(0, 0));
feature["test"] = "Mapsui";

using var skPaint = new SKPaint();
Expand Down
4 changes: 2 additions & 2 deletions Tests/Mapsui.Tests/Projections/ProjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void CoordinateProjectionTest()
// arrange
var multiPolygon = (MultiPolygon)_wktReader.Read("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))");
var projectedMultiPolygon = multiPolygon.Copy();
using var feature = new GeometryFeature(projectedMultiPolygon);
var feature = new GeometryFeature(projectedMultiPolygon);
var projection = new Projection();

// act
Expand All @@ -72,7 +72,7 @@ public void CoordinateNtsProjectionTest()
// arrange
var multiPolygon = (MultiPolygon)_wktReader.Read("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))");
var projectedMultiPolygon = multiPolygon.Copy();
using var feature = new GeometryFeature(projectedMultiPolygon);
var feature = new GeometryFeature(projectedMultiPolygon);
var projection = new DotSpatialProjection();

// act
Expand Down

0 comments on commit c9414d5

Please sign in to comment.