BoundingBox now immutable

This commit is contained in:
ClemensFischer 2024-04-11 14:57:54 +02:00
parent dc9932a0cf
commit 2685792758
8 changed files with 61 additions and 105 deletions

View file

@ -10,26 +10,21 @@ namespace MapControl
/// <summary>
/// A geographic bounding box with south and north latitude and west and east longitude values in degrees.
/// </summary>
#if !UWP
#if WINUI || UWP
[Windows.Foundation.Metadata.CreateFromString(MethodName = "MapControl.BoundingBox.Parse")]
#else
[System.ComponentModel.TypeConverter(typeof(BoundingBoxConverter))]
#endif
public class BoundingBox
{
private double south;
private double north;
public BoundingBox()
{
south = double.NaN;
north = double.NaN;
West = double.NaN;
East = double.NaN;
}
public BoundingBox(double latitude1, double longitude1, double latitude2, double longitude2)
{
South = Math.Min(latitude1, latitude2);
North = Math.Max(latitude1, latitude2);
South = Math.Min(Math.Max(Math.Min(latitude1, latitude2), -90d), 90d);
North = Math.Min(Math.Max(Math.Max(latitude1, latitude2), -90d), 90d);
West = Math.Min(longitude1, longitude2);
East = Math.Max(longitude1, longitude2);
}
@ -39,28 +34,15 @@ namespace MapControl
{
}
public double South
{
get => south;
set => south = Math.Min(Math.Max(value, -90d), 90d);
}
public double North
{
get => north;
set => north = Math.Min(Math.Max(value, -90d), 90d);
}
public double West { get; set; }
public double East { get; set; }
public double South { get; }
public double North { get; }
public double West { get; }
public double East { get; }
public virtual double Width => East - West;
public virtual double Height => North - South;
public virtual Location Center =>
HasValidBounds ? new Location((South + North) / 2d, (West + East) / 2d) : null;
public virtual bool HasValidBounds => South < North && West < East;
public virtual Location Center => new Location((South + North) / 2d, (West + East) / 2d);
public static BoundingBox Parse(string boundingBox)
{

View file

@ -19,7 +19,6 @@ namespace MapControl
height = Math.Max(h, 0d);
}
public override bool HasValidBounds => false;
public override Location Center => center;
public override double Width => width;
public override double Height => height;

View file

@ -12,7 +12,9 @@ namespace MapControl
/// A collection of Locations with support for string parsing
/// and calculation of great circle and rhumb line locations.
/// </summary>
#if !UWP
#if WINUI || UWP
[Windows.Foundation.Metadata.CreateFromString(MethodName = "MapControl.LocationCollection.Parse")]
#else
[System.ComponentModel.TypeConverter(typeof(LocationCollectionConverter))]
#endif
public class LocationCollection : List<Location>

View file

@ -71,7 +71,7 @@ namespace MapControl
{
MapRect mapRect = null;
if (boundingBox.HasValidBounds)
if (boundingBox.South < boundingBox.North && boundingBox.West < boundingBox.East)
{
var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West));
var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East));

View file

@ -17,7 +17,9 @@ namespace MapControl
/// <summary>
/// Provides the download Uri or ImageSource of map tiles.
/// </summary>
#if !UWP
#if WINUI || UWP
[Windows.Foundation.Metadata.CreateFromString(MethodName = "MapControl.TileSource.Parse")]
#else
[System.ComponentModel.TypeConverter(typeof(TileSourceConverter))]
#endif
public class TileSource
@ -81,6 +83,11 @@ namespace MapControl
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
}
public static TileSource Parse(string uriTemplate)
{
return new TileSource { UriTemplate = uriTemplate };
}
}
public class TmsTileSource : TileSource

View file

@ -56,7 +56,7 @@ namespace MapControl
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return new TileSource { UriTemplate = (string)value };
return TileSource.Parse((string)value);
}
}
}

View file

@ -160,55 +160,43 @@
Map="{Binding ElementName=map}">
<tools:MapLayerItem Text="OpenStreetMap">
<map:MapTileLayer
TileSource="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
SourceName="OpenStreetMap"
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenStreetMap German">
<map:MapTileLayer
TileSource="https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png"
SourceName="OpenStreetMap German"
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenStreetMap French">
<map:MapTileLayer
TileSource="http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png"
SourceName="OpenStreetMap French"
Description="© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenTopoMap">
<map:MapTileLayer
TileSource="https://tile.opentopomap.org/{z}/{x}/{y}.png"
SourceName="OpenTopoMap"
Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="https://tile.opentopomap.org/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="TopPlusOpen WMTS">
<map:WmtsTileLayer
CapabilitiesUri="https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml"
SourceName="TopPlusOpen"
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)"
CapabilitiesUri="https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml"/>
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="TopPlusOpen WMS">
<map:WmsImageLayer
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)"
ServiceUri="https://sgx.geodatenzentrum.de/wms_topplus_open"/>
ServiceUri="https://sgx.geodatenzentrum.de/wms_topplus_open"
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenStreetMap WMS">
<map:WmsImageLayer
Description="© [terrestris GmbH &amp; Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"
ServiceUri="http://ows.terrestris.de/osm/service"/>
ServiceUri="http://ows.terrestris.de/osm/service"
Description="© [terrestris GmbH &amp; Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayersMenuButton.MapOverlays>
<tools:MapLayerItem Text="Graticule">
@ -218,18 +206,13 @@
<map:MapScale HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Seamarks">
<map:MapTileLayer SourceName="Seamarks" MinZoomLevel="9" MaxZoomLevel="18">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
<map:MapTileLayer
TileSource="http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png"
SourceName="Seamarks"
MinZoomLevel="9" MaxZoomLevel="18"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Sample Image">
<Image Source="10_535_330.jpg" Stretch="Fill">
<map:MapPanel.BoundingBox>
<map:BoundingBox South="53.54031" West="8.08594" North="53.74871" East="8.43750"/>
</map:MapPanel.BoundingBox>
</Image>
<Image Source="10_535_330.jpg" Stretch="Fill" map:MapPanel.BoundingBox="53.54031,8.08594,53.74871,8.43750"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Mount Etna KML">
<map:GroundOverlay SourcePath="etna.kml"/>

View file

@ -175,55 +175,43 @@
Map="{Binding ElementName=map}">
<tools:MapLayerItem Text="OpenStreetMap">
<map:MapTileLayer
TileSource="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
SourceName="OpenStreetMap"
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="https://tile.openstreetmap.org/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenStreetMap German">
<map:MapTileLayer
TileSource="https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png"
SourceName="OpenStreetMap German"
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenStreetMap French">
<map:MapTileLayer
TileSource="http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png"
SourceName="OpenStreetMap French"
Description="© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenTopoMap">
<map:MapTileLayer
TileSource="https://tile.opentopomap.org/{z}/{x}/{y}.png"
SourceName="OpenTopoMap"
Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="https://tile.opentopomap.org/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="TopPlusOpen WMTS">
<map:WmtsTileLayer
CapabilitiesUri="https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml"
SourceName="TopPlusOpen"
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)"
CapabilitiesUri="https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml"/>
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wmts-topplusopen-wmts-topplus-open.html)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="TopPlusOpen WMS">
<map:WmsImageLayer
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)"
ServiceUri="https://sgx.geodatenzentrum.de/wms_topplus_open"/>
ServiceUri="https://sgx.geodatenzentrum.de/wms_topplus_open"
Description="© [BKG](https://gdz.bkg.bund.de/index.php/default/webdienste/topplus-produkte/wms-topplusopen-mit-layer-fur-normalausgabe-und-druck-wms-topplus-open.html)"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="OpenStreetMap WMS">
<map:WmsImageLayer
Description="© [terrestris GmbH &amp; Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"
ServiceUri="http://ows.terrestris.de/osm/service"/>
ServiceUri="http://ows.terrestris.de/osm/service"
Description="© [terrestris GmbH &amp; Co. KG](http://ows.terrestris.de/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>
</tools:MapLayerItem>
<tools:MapLayersMenuButton.MapOverlays>
<tools:MapLayerItem Text="Graticule">
@ -233,18 +221,13 @@
<map:MapScale HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Seamarks">
<map:MapTileLayer SourceName="Seamarks" MinZoomLevel="9" MaxZoomLevel="18">
<map:MapTileLayer.TileSource>
<map:TileSource UriTemplate="http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png"/>
</map:MapTileLayer.TileSource>
</map:MapTileLayer>
<map:MapTileLayer
TileSource="http://tiles.openseamap.org/seamark/{z}/{x}/{y}.png"
SourceName="Seamarks"
MinZoomLevel="9" MaxZoomLevel="18"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Sample Image">
<Image Source="10_535_330.jpg" Stretch="Fill">
<map:MapPanel.BoundingBox>
<map:BoundingBox South="53.54031" West="8.08594" North="53.74871" East="8.43750"/>
</map:MapPanel.BoundingBox>
</Image>
<Image Source="10_535_330.jpg" Stretch="Fill" map:MapPanel.BoundingBox="53.54031,8.08594,53.74871,8.43750"/>
</tools:MapLayerItem>
<tools:MapLayerItem Text="Mount Etna KML">
<map:GroundOverlay SourcePath="etna.kml"/>