Skip to content

Commit

Permalink
Fixed real/double conversion issue since S111 doesn't exacly specify …
Browse files Browse the repository at this point in the history
…32bit vs 64bit floats. Conversion issues arise from this lack of definiton. This is now solved by checking the inner type
  • Loading branch information
Richard Flapper committed Mar 15, 2023
1 parent 27a9900 commit de9ae25
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
22 changes: 20 additions & 2 deletions S1XViewer.HDF/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class Extensions
/// <returns></returns>
public static bool Contains(this List<Hdf5AttributeElement> elements, string label)
{
foreach(var element in elements)
foreach (var element in elements)
{
if (element.Name == label) return true;
}
Expand Down Expand Up @@ -54,10 +54,28 @@ public static bool Contains(this List<Hdf5AttributeElement> elements, string lab
{
if (element != null && element.Values != null)
{
return (T)((Array)element.Values).GetValue(0);
var value = ((Array)element.Values).GetValue(0) ?? "";

if (typeof(T).ToString().ToUpper().Contains("DOUBLE"))
{
if (double.TryParse(value.ToString(), out double doubleValue))
{
return (T)(object)doubleValue;
}
}
else if (typeof(T).ToString().ToUpper().Contains("FLOAT"))
{
if (float.TryParse(value.ToString(), out float floatValue))
{
return (T)(object)floatValue;
}
}

return (T)value;
}

return defaultValue;
}
}
}

18 changes: 9 additions & 9 deletions S1XViewer.Model/S111DCF2DataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public override async Task<IS1xxDataPackage> ParseAsync(string hdf5FileName, Dat

// retrieve boundingbox
var eastBoundLongitudeAttribute = hdf5S111Root.Attributes.Find("eastBoundLongitude");
var eastBoundLongitude = eastBoundLongitudeAttribute?.Value<float>(0f) ?? 0.0;
var eastBoundLongitude = eastBoundLongitudeAttribute?.Value<double>(0f) ?? 0.0;
var northBoundLatitudeAttribute = hdf5S111Root.Attributes.Find("northBoundLatitude");
var northBoundLatitude = northBoundLatitudeAttribute?.Value<float>(0f) ?? 0f;
var northBoundLatitude = northBoundLatitudeAttribute?.Value<double>(0f) ?? 0f;
var southBoundLatitudeAttribute = hdf5S111Root.Attributes.Find("southBoundLatitude");
var southBoundLatitude = southBoundLatitudeAttribute?.Value<float>(0f) ?? 0f;
var southBoundLatitude = southBoundLatitudeAttribute?.Value<double>(0f) ?? 0f;
var westBoundLongitudeAttribute = hdf5S111Root.Attributes.Find("westBoundLongitude");
var westBoundLongitude = westBoundLongitudeAttribute?.Value<float>(0f) ?? 0f;
var westBoundLongitude = westBoundLongitudeAttribute?.Value<double>(0f) ?? 0f;

var horizontalCRSAttribute = hdf5S111Root.Attributes.Find("horizontalCRS");
var horizontalCRS = horizontalCRSAttribute?.Value<long>(4326) ?? 4326;
Expand All @@ -96,13 +96,13 @@ await Task.Run(() =>
//we've found the relevant group. Use this group to create features on by calculating its position
var minGroupParentNode = (Hdf5Element)minGroup.Parent;
var gridOriginLatitudeElement = minGroupParentNode.Attributes.Find("gridOriginLatitude");
var gridOriginLatitude = gridOriginLatitudeElement?.Value<float>();
var gridOriginLatitude = gridOriginLatitudeElement?.Value<double>();
var gridOriginLongitudeElement = minGroupParentNode.Attributes.Find("gridOriginLongitude");
var gridOriginLongitude = gridOriginLongitudeElement?.Value<float>();
var gridOriginLongitude = gridOriginLongitudeElement?.Value<double>();
var gridSpacingLatitudinalElement = minGroupParentNode.Attributes.Find("gridSpacingLatitudinal");
var gridSpacingLatitudinal = gridSpacingLatitudinalElement?.Value<float>();
var gridSpacingLatitudinal = gridSpacingLatitudinalElement?.Value<double>();
var gridSpacingLongitudinalElement = minGroupParentNode.Attributes.Find("gridSpacingLongitudinal");
var gridSpacingLongitudinal = gridSpacingLongitudinalElement?.Value<float>();
var gridSpacingLongitudinal = gridSpacingLongitudinalElement?.Value<double>();

var numPointsLatitudinalElement = minGroupParentNode.Attributes.Find("numPointsLatitudinal");
int numPointsLatitude = numPointsLatitudinalElement?.Value<int>() ?? -1;
Expand Down Expand Up @@ -145,7 +145,7 @@ await Task.Run(() =>
}
}

Progress?.Invoke(50 + ((50 / numPointsLatitude) * latIdx));
Progress?.Invoke(50 + (int)((50.0 / (double) numPointsLatitude) * (double) latIdx));
}

// build up featutes ard wrap 'em in datapackage
Expand Down
57 changes: 24 additions & 33 deletions S1XViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,24 +615,10 @@ private async Task LoadHDF5File(string productStandard, string fileName, DateTim
{
if (txt != null)
{
labelStatus.Content = $"Load time: {txt} seconds ..";
labelStatus.Content = $"Load time: {txt} seconds. Now rendering file (this can take a while) ..";
progressBar.Value = 0;
}
}), elapsedTime);

BackgroundWorker bgw = new();
bgw.DoWork += delegate
{
Task.Delay(5000).Wait();
};
bgw.RunWorkerCompleted += delegate
{
_syncContext?.Post(new SendOrPostCallback((o) =>
{
labelStatus.Content = "";
}), "");
};
bgw.RunWorkerAsync();
}
}

Expand Down Expand Up @@ -994,63 +980,63 @@ private async void CreateFeatureCollection(IS1xxDataPackage dataPackage)
{
if (geoFeature is IVectorFeature vectorGeoFeature)
{
var secondPoint = Destination((mapPoint.Y, mapPoint.X), 75, vectorGeoFeature.Orientation.OrientationValue);
double width = 0.75;
var secondPoint = Destination((mapPoint.Y, mapPoint.X), 150, vectorGeoFeature.Orientation.OrientationValue);
double width = 1.5;
int speedBand = 1;
System.Drawing.Color color = System.Drawing.Color.FromArgb(118, 82, 226);
if (vectorGeoFeature.Speed.SpeedMaximum > 0.5 && vectorGeoFeature.Speed.SpeedMaximum <= 1.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 125, vectorGeoFeature.Orientation.OrientationValue);
width = 1.25;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 250, vectorGeoFeature.Orientation.OrientationValue);
width = 2.5;
speedBand = 2;
color = System.Drawing.Color.FromArgb(72, 152, 211);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 1.0 && vectorGeoFeature.Speed.SpeedMaximum <= 2.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 150, vectorGeoFeature.Orientation.OrientationValue);
width = 1.25;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 300, vectorGeoFeature.Orientation.OrientationValue);
width = 2.5;
speedBand = 3;
color = System.Drawing.Color.FromArgb(97, 203, 229);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 2.0 && vectorGeoFeature.Speed.SpeedMaximum <= 3.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 200, vectorGeoFeature.Orientation.OrientationValue);
width = 2;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 400, vectorGeoFeature.Orientation.OrientationValue);
width = 3;
speedBand = 4;
color = System.Drawing.Color.FromArgb(109, 188, 69);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 3.0 && vectorGeoFeature.Speed.SpeedMaximum <= 5.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 250, vectorGeoFeature.Orientation.OrientationValue);
width = 2;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 500, vectorGeoFeature.Orientation.OrientationValue);
width = 3;
speedBand = 5;
color = System.Drawing.Color.FromArgb(180, 220, 0);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 5.0 && vectorGeoFeature.Speed.SpeedMaximum <= 7.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 250, vectorGeoFeature.Orientation.OrientationValue);
width = 2;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 500, vectorGeoFeature.Orientation.OrientationValue);
width = 4;
speedBand = 6;
color = System.Drawing.Color.FromArgb(205, 193, 0);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 7.0 && vectorGeoFeature.Speed.SpeedMaximum <= 10.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 250, vectorGeoFeature.Orientation.OrientationValue);
width = 3;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 500, vectorGeoFeature.Orientation.OrientationValue);
width = 4;
speedBand = 7;
color = System.Drawing.Color.FromArgb(248, 167, 24);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 10.0 && vectorGeoFeature.Speed.SpeedMaximum <= 13.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 250, vectorGeoFeature.Orientation.OrientationValue);
width = 3;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 500, vectorGeoFeature.Orientation.OrientationValue);
width = 4;
speedBand = 8;
color = System.Drawing.Color.FromArgb(247, 162, 157);
}
else if (vectorGeoFeature.Speed.SpeedMaximum > 13.0)
{
secondPoint = Destination((mapPoint.Y, mapPoint.X), 250, vectorGeoFeature.Orientation.OrientationValue);
width = 4;
secondPoint = Destination((mapPoint.Y, mapPoint.X), 500, vectorGeoFeature.Orientation.OrientationValue);
width = 5;
speedBand = 9;
color = System.Drawing.Color.FromArgb(255, 30, 30);
}
Expand Down Expand Up @@ -1148,6 +1134,11 @@ private async void CreateFeatureCollection(IS1xxDataPackage dataPackage)
myMapView.Map.OperationalLayers.Add(collectionLayer);
myMapView.GraphicsOverlays.Add(graphicsOverlay);
myMapView.GeoViewTapped += OnMapViewTapped;

_syncContext?.Post(new SendOrPostCallback((o) =>
{
labelStatus.Content = "";
}), "");
}

/// <summary>
Expand Down

0 comments on commit de9ae25

Please sign in to comment.