Skip to content

Commit

Permalink
No-op render requests if map is not in a valid state for rendering (i…
Browse files Browse the repository at this point in the history
…e. One or more display parameters are <= 0)

Fixes #50
  • Loading branch information
jumpinjackie committed Oct 25, 2017
1 parent 29edc76 commit 671e618
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
71 changes: 36 additions & 35 deletions Maestro.MapViewer/MapViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,9 +1191,7 @@ private void InitViewerFromMap(double? initialScale)
_viewHistoryIndex = -1;
OnPropertyChanged(nameof(ViewHistoryIndex));

var handler = this.MapLoaded;
if (handler != null)
handler(this, EventArgs.Empty);
this.MapLoaded?.Invoke(this, EventArgs.Empty);

if (initialScale.HasValue)
ZoomToScale(initialScale.Value);
Expand Down Expand Up @@ -1318,9 +1316,7 @@ public void ClearSelection()
_selectionImage = null;
}

var handler = this.SelectionChanged;
if (handler != null)
handler(this, EventArgs.Empty);
this.SelectionChanged?.Invoke(this, EventArgs.Empty);

this.Refresh();
}
Expand Down Expand Up @@ -1408,6 +1404,8 @@ private class RenderResult
public bool RaiseEvents { get; set; }

public bool InvalidateRegardless { get; set; }

public bool IsNoop { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -1437,9 +1435,7 @@ public void UpdateSelection(bool raise)
RenderSelection();
if (raise)
{
var handler = this.SelectionChanged;
if (handler != null)
handler(this, EventArgs.Empty);
this.SelectionChanged?.Invoke(this, EventArgs.Empty);
}
}

Expand Down Expand Up @@ -1491,9 +1487,7 @@ internal void RenderSelection(bool invalidateRegardless)

internal void RefreshMap(bool raiseEvents)
{
var h = this.MapRefreshing;
if (h != null)
h(this, EventArgs.Empty);
this.MapRefreshing?.Invoke(this, EventArgs.Empty);

//This is our refresh action
RefreshAction action = new RefreshAction(() =>
Expand Down Expand Up @@ -1793,9 +1787,7 @@ internal void ZoomToView(double x, double y, double scale, bool refresh, bool ra

if (Math.Abs(oldScale - _map.ViewScale) > double.Epsilon)
{
var handler = this.MapScaleChanged;
if (handler != null)
handler(this, EventArgs.Empty);
this.MapScaleChanged?.Invoke(this, EventArgs.Empty);
}

UpdateExtents();
Expand Down Expand Up @@ -1858,23 +1850,29 @@ private void renderWorker_DoWork(object sender, DoWorkEventArgs e)
{
_map.Save(); //We must sync up runtime map state before rendering

var args = (RenderWorkArgs)e.Argument;
var res = new RenderResult() { RaiseEvents = args.RaiseEvents, InvalidateRegardless = args.InvalidateRegardless };
if (args.MapRenderingOptions != null)
if (!_map.IsValidForRendering)
{
if (args.UseRenderMap)
res.Image = Image.FromStream(_map.Render(args.MapRenderingOptions.Format));
else
res.Image = Image.FromStream(_map.RenderDynamicOverlay(null, args.MapRenderingOptions.Format, args.MapRenderingOptions.Color, args.MapRenderingOptions.Behavior));
e.Result = new RenderResult { IsNoop = true };
}
if (args.SelectionRenderingOptions != null)
else
{
//HACK: HTTP provider is stateless, so passing the selection is not only redundant, but will probably break on large selections.
var sel = (_map.CurrentConnection.ProviderName.ToUpper().Equals("MAESTRO.HTTP")) ? null : _map.Selection;
res.SelectionImage = Image.FromStream(_map.RenderDynamicOverlay(sel, args.SelectionRenderingOptions.Format, args.SelectionRenderingOptions.Color, args.SelectionRenderingOptions.Behavior));
var args = (RenderWorkArgs)e.Argument;
var res = new RenderResult() { RaiseEvents = args.RaiseEvents, InvalidateRegardless = args.InvalidateRegardless };
if (args.MapRenderingOptions != null)
{
if (args.UseRenderMap)
res.Image = Image.FromStream(_map.Render(args.MapRenderingOptions.Format));
else
res.Image = Image.FromStream(_map.RenderDynamicOverlay(null, args.MapRenderingOptions.Format, args.MapRenderingOptions.Color, args.MapRenderingOptions.Behavior));
}
if (args.SelectionRenderingOptions != null)
{
//HACK: HTTP provider is stateless, so passing the selection is not only redundant, but will probably break on large selections.
var sel = (_map.CurrentConnection.ProviderName.ToUpper().Equals("MAESTRO.HTTP")) ? null : _map.Selection;
res.SelectionImage = Image.FromStream(_map.RenderDynamicOverlay(sel, args.SelectionRenderingOptions.Format, args.SelectionRenderingOptions.Color, args.SelectionRenderingOptions.Behavior));
}
e.Result = res;
}

e.Result = res;
}

private void renderWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
Expand All @@ -1887,6 +1885,9 @@ private void renderWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEv
else
{
var res = (RenderResult)e.Result;
if (res.IsNoop)
return;

//reset translation
translate = new System.Drawing.Point();

Expand Down Expand Up @@ -1942,9 +1943,7 @@ private void renderWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEv
*/
if (res.RaiseEvents)
{
var handler = this.MapRefreshed;
if (handler != null)
handler(this, EventArgs.Empty);
this.MapRefreshed?.Invoke(this, EventArgs.Empty);
}
}
}
Expand Down Expand Up @@ -2073,7 +2072,11 @@ private string QueryFirstVisibleTooltip(int x, int y)
{
//No intialized map
if (_map == null)
return "";
return string.Empty;

//QueryMapFeatures *is* a rendering operation, so it must be in valid state for rendering
if (!_map.IsValidForRendering)
return string.Empty;

//No change in position
if (_lastTooltipX == x && _lastTooltipY == y && !string.IsNullOrEmpty(_activeTooltipText))
Expand Down Expand Up @@ -2182,9 +2185,7 @@ public void SelectByWkt(string wkt, int maxFeatures)
#endif

RenderSelection(true); //This is either async or queued up. Either way do this before firing off selection changed
var handler = this.SelectionChanged;
if (handler != null)
handler(this, EventArgs.Empty);
this.SelectionChanged?.Invoke(this, EventArgs.Empty);
}

private QueryMapOptions CreateQueryOptionsForSelection()
Expand Down
5 changes: 5 additions & 0 deletions OSGeo.MapGuide.MaestroAPI/Mapping/RuntimeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ public virtual int DisplayWidth
}
}

/// <summary>
/// Gets whether this map is valid for rendering
/// </summary>
public bool IsValidForRendering => this.DisplayWidth > 0 && this.DisplayHeight > 0 && this.DisplayDpi > 0 && this.ViewScale > 0 && this.MetersPerUnit > 0;

/// <summary>
/// Gets or sets the tile set definition resource id. Only applicable
/// if the Map Definition used to create this map contains a reference
Expand Down

0 comments on commit 671e618

Please sign in to comment.