forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapViewport.cs
More file actions
337 lines (296 loc) · 14.6 KB
/
MapViewport.cs
File metadata and controls
337 lines (296 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Drawing;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries.Utilities;
using SharpMap.Utilities;
namespace SharpMap
{
/// <summary>
/// A <see cref="SharpMap.Map"/> utility class, that encapsulates all data required for rendering.
/// </summary>
/// <remarks>This is a value class</remarks>
public class MapViewport
{
private readonly Envelope _envelope;
private readonly Coordinate _center;
private Coordinate _centerOfInterest;
private readonly float[] _mapTransformElements;
private readonly float[] _mapTransformInvertedElements;
private readonly double[] _worldToMapElements;
private readonly double[] _worldToMapElementsCareAboutTransform;
private double _mapScale;
private int _lastDpi;
private readonly object _lockMapScale = new object();
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="mapId">The id of the map</param>
/// <param name="srid">The spatial reference</param>
/// <param name="zoom">current map zoom</param>
/// <param name="mapHeight">map height</param>
/// <param name="env">The envelope containing the viewport</param>
/// <param name="size">The size of the viewport</param>
/// <param name="pixelAspectRatio">A ratio between width and height</param>
/// <param name="mapTransform">An affine map transform matrix</param>
/// <param name="mapTransformInverted">The affine map transformation that inverts <paramref name="mapTransform"/></param>
/// <param name="mapTransformRotation">The rotation in degrees applied by <paramref name="mapTransform"/></param>
public MapViewport(Guid mapId, int srid, double zoom, double mapHeight, Envelope env, Size size, double pixelAspectRatio,
System.Drawing.Drawing2D.Matrix mapTransform, System.Drawing.Drawing2D.Matrix mapTransformInverted,
float mapTransformRotation)
{
ID = mapId;
SRID = srid;
Zoom = zoom;
MapHeight = mapHeight;
_envelope = env.Copy();
Size = size;
_center = env.Centre;
PixelAspectRatio = pixelAspectRatio;
PixelWidth = Zoom / size.Width;
PixelHeight = PixelWidth * pixelAspectRatio;
_mapTransformElements = mapTransform.Elements;
_mapTransformInvertedElements = mapTransformInverted.Elements;
MapTransformRotation = mapTransformRotation;
// pre-calculated for use when MapTransformRotation == 0
Left = Center.X - Zoom * 0.5;
Top = Center.Y + mapHeight * 0.5;
// pre-defined for use when MapTransformation != 0
if (!mapTransformRotation.Equals(0f))
{
_worldToMapElements = Transform.WorldToMapMatrix(
Center, PixelWidth, PixelHeight, MapTransformRotation, Size, false).MatrixEntries;
_worldToMapElementsCareAboutTransform = Transform.WorldToMapMatrix(
Center, PixelWidth, PixelHeight, MapTransformRotation, Size, true).MatrixEntries;
}
}
/// <summary>
/// Creates an instance of this class based on the provided map
/// </summary>
/// <param name="map">The Map</param>
public MapViewport(Map map)
: this(map.ID, map.SRID, map.Zoom, map.MapHeight, map.Envelope, map.Size, map.PixelAspectRatio,
map.MapTransform, map.MapTransformInverted, map.MapTransformRotation)
{
CenterOfInterest = map.CenterOfInterest;
}
/// <summary>
/// Gets a value indicating the which <see cref="Map"/> this viewport belongs to.
/// </summary>
public Guid ID { get; }
/// <summary>
/// Gets a value indicating the spatial reference id of the map
/// </summary>
public int SRID { get; /*set;*/ }
/// <summary>
/// Gets a value indicating the size of the map
/// </summary>
public Size Size { get; }
/// <summary>
/// <para>Gets the rectilinear extents of the map based on the current <see cref="Zoom"/>,
/// <see cref="Center"/>, map <see cref="Size"/>, and (optionally) the <see cref="MapTransform"/></para>
/// <para>If a <see cref="MapTransform"/> is applied, the envelope CONTAINING the rotated view
/// will be returned (used by layers to spatially select data) and the aspect ratio will NOT be the
/// same as map <see cref="Size"/>. If aspect ratio is important then refer to <see cref="Zoom"/>
/// and <see cref="MapHeight"/></para>
/// </summary>
public Envelope Envelope => _envelope.Copy();
/// <summary>
/// Gets a value indicating the transformation that has to be applied when
/// rendering the map
/// </summary>
public System.Drawing.Drawing2D.Matrix MapTransform
{
get
{
return new System.Drawing.Drawing2D.Matrix(
_mapTransformElements[0],
_mapTransformElements[1],
_mapTransformElements[2],
_mapTransformElements[3],
_mapTransformElements[4],
_mapTransformElements[5]
);
}
}
/// <summary>
/// Gets a value indicating the inverse transformation that is applied when
/// rendering the map
/// </summary>
public System.Drawing.Drawing2D.Matrix MapTransformInverted
{
get
{
return new System.Drawing.Drawing2D.Matrix(
_mapTransformInvertedElements[0],
_mapTransformInvertedElements[1],
_mapTransformInvertedElements[2],
_mapTransformInvertedElements[3],
_mapTransformInvertedElements[4],
_mapTransformInvertedElements[5]
);
}
}
/// <summary>
/// Map rotation in degrees (defined by <see cref="MapTransform"/>)
/// </summary>
public float MapTransformRotation { get; }
/// <summary>
/// Cached affine transformation used to transform world coordinates from apparent rotated coordinate frame (ie MapTransformRotation != 0)
/// to image space. Unlike MapTransform, this matrix defines a complete transformation from World to Image taking into account MapRotation.
/// 2 variants are available, depending on whether or not map rotation has already been applied.
/// </summary>
internal AffineTransformation WorldToMapTransform(bool careAboutTransform)
{
if (careAboutTransform)
return new AffineTransformation(
_worldToMapElementsCareAboutTransform[0],
_worldToMapElementsCareAboutTransform[1],
_worldToMapElementsCareAboutTransform[2],
_worldToMapElementsCareAboutTransform[3],
_worldToMapElementsCareAboutTransform[4],
_worldToMapElementsCareAboutTransform[5]
);
else
return new AffineTransformation(
_worldToMapElements[0],
_worldToMapElements[1],
_worldToMapElements[2],
_worldToMapElements[3],
_worldToMapElements[4],
_worldToMapElements[5]
);
}
/// <summary>
/// Gets a value indicating the center of the map viewport
/// </summary>
public Coordinate Center => _center.Copy();
/// <summary>
/// Gets a value indicating the center of the map viewport
/// </summary>
public Coordinate CenterOfInterest
{
get => _centerOfInterest?.Copy() ?? Center;
set => _centerOfInterest = value;
}
/// <summary>
/// Gets a value indicating the zoom of the map viewport
/// </summary>
/// <remarks>This value is identical to <see cref="MapWidth"/></remarks>
public double Zoom { get; /*set;*/ }
/// <summary>
/// Gets a value indicating the height of the map viewport in world units
/// </summary>
public double MapHeight { get; }
/// <summary>
/// Gets a value indicating the width of the map viewport in world units
/// </summary>
/// <remarks>This value is equal to <see cref="Zoom"/></remarks>
private double MapWidth => Zoom;
/// <summary>
/// Applicable to non-rotated views only, returning the minimum X value of the map viewport in world units
/// </summary>
public double Left { get; }
/// <summary>
/// Applicable to non-rotated views only, returning the maximum Y value of the map viewport in world units
/// </summary>
public double Top { get; }
/// <summary>
/// Gets or sets the aspect-ratio of the pixel scales. A value less than
/// 1 will make the map stretch upwards, and larger than 1 will make it smaller.
/// </summary>
/// <exception cref="ArgumentException">Throws an argument exception when value is 0 or less.</exception>
public double PixelAspectRatio { get; }
/// <summary>
/// Returns the width of a pixel in world coordinate units.
/// </summary>
public double PixelWidth { get; }
/// <summary>
/// Returns the height of a pixel in world coordinate units.
/// </summary>
public double PixelHeight { get; }
/// <summary>
/// Function to compute the denominator of the viewport's scale when using a given <paramref name="dpi"/> resolution.
/// </summary>
/// <param name="dpi">The resolution</param>
/// <returns>The scale's denominator</returns>
public double GetMapScale(int dpi)
{
if (_lastDpi != dpi)
{
lock (_lockMapScale)
if (_lastDpi != dpi)
{
_mapScale = ScaleCalculations.CalculateScaleNonLatLong(Zoom, Size.Width, 1, dpi);
_lastDpi = dpi;
}
}
return _mapScale;
}
/// <summary>
/// Converts an array of world coordinates to image coordinates based on the current <see cref="Zoom"/>, <see cref="Center"/>,
/// map <see cref="Size"/>, and (optionally) the <see cref="MapTransform"/>.
/// </summary>
/// <param name="coordinates">Coordinate array in world coordinates</param>
/// <param name="careAboutMapTransform">Indicates whether <see cref="MapTransform"/> should be applied. True for typical coordinate calcs,
/// False when rendering to image as the Graphics object has already applied the MapTransform</param>
/// <returns>PointF array in image coordinates</returns>
public PointF[] WorldToImage(Coordinate[] coordinates, bool careAboutMapTransform = false)
{
// see WorldToImage discussion in Map.cs. This is a slightly shortened form using cached values.
if (MapTransformRotation.Equals(0f))
return Transform.WorldToMap(coordinates, Left, Top, PixelWidth, PixelHeight);
var matrix = WorldToMapTransform(careAboutMapTransform);
return Transform.WorldToMap(coordinates, matrix);
}
/// <summary>
/// Converts a point in world coordinates to image coordinates based on the current <see cref="Zoom"/>, <see cref="Center"/>,
/// map <see cref="Size"/>, and (optionally) the <see cref="MapTransform"/>.
/// </summary>
/// <param name="p">Point in world coordinates</param>
/// <param name="careAboutMapTransform">Indicates whether <see cref="MapTransform"/> should be applied. When rendering to image,
/// the Graphics object has usually applied MapTransform</param>
/// <returns>PointF in image coordinates</returns>
public PointF WorldToImage(Coordinate p, bool careAboutMapTransform = false)
{
var points = WorldToImage(new Coordinate[] {p}, careAboutMapTransform);
return points[0];
}
/// <summary>
/// Converts a point array from image coordinates to world coordinates based on the current <see cref="Zoom"/>, <see cref="Center"/>,
/// map <see cref="Size"/>, and (optionally) the <see cref="MapTransform"/>.
/// </summary>
/// <param name="points">Point array in image coordinates. Note: if you wish to preserve the input values then
/// you must clone the point array as it will be modified if a MapTransform is applied</param>
/// <param name="careAboutMapTransform">Indicates whether <see cref="MapTransform"/> should be applied. </param>
/// <returns>Point array in world coordinates</returns>
public Coordinate[] ImageToWorld(PointF[] points, bool careAboutMapTransform = false)
{
if (careAboutMapTransform && !MapTransformRotation.Equals(0f))
using (var transformInv = MapTransformInverted)
transformInv.TransformPoints(points);
return Transform.MapToWorld(points, Center, Zoom, MapHeight, PixelWidth, PixelHeight);
}
/// <summary>
/// Converts a point from image coordinates to world coordinates based on the current <see cref="Zoom"/>, <see cref="Center"/>,
/// map <see cref="Size"/>, and (optionally) the <see cref="MapTransform"/>.
/// </summary>
/// <param name="p">Point in image coordinates. Note: if you wish to preserve the input value then
/// you must clone the point as it will be modified if a MapTransform is applied</param>
/// <param name="careAboutMapTransform">Indicates whether <see cref="MapTransform"/> should be applied. </param>
/// <returns>Point in world coordinates</returns>
public Coordinate ImageToWorld(PointF p, bool careAboutMapTransform = false)
{
var pts = ImageToWorld(new PointF[] {p}, careAboutMapTransform);
return pts[0];
}
/// <summary>
/// Creates a map viewport from a given map
/// </summary>
/// <param name="map">The map</param>
/// <returns></returns>
public static implicit operator MapViewport(Map map)
{
return new MapViewport(map);
}
}
}