-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathScaleCalculations.cs
More file actions
82 lines (76 loc) · 3.05 KB
/
ScaleCalculations.cs
File metadata and controls
82 lines (76 loc) · 3.05 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
using System;
namespace SharpMap.Utilities
{
/// <summary>
/// Functions for calculating Scales
/// </summary>
public static class ScaleCalculations
{
/// <summary>
/// Calculates the Zoom-Level for a given Scale, DPI and MapWidth
/// </summary>
/// <param name="scale"></param>
/// <param name="mapUnitFactor"></param>
/// <param name="dpi"></param>
/// <param name="mapSizeWidth"></param>
/// <returns></returns>
public static double GetMapZoomFromScaleNonLatLong(double scale, double mapUnitFactor, int dpi, double mapSizeWidth)
{
int nPxlPerInch = dpi;
double zoom;
if (mapSizeWidth <= 0) return 0.0;
try
{
double pageWidth = mapSizeWidth/nPxlPerInch*GeoSpatialMath.MetersPerInch;
zoom = Math.Abs(scale*pageWidth)/mapUnitFactor;
}
catch
{
zoom = 0.0;
}
return zoom;
}
/// <summary>
/// Calculate the Representative Fraction Scale for non Lat/Long map.
/// </summary>
/// <param name="mapWidthMeters">The current extent width of the Map</param>
/// <param name="mapSizeWidth">The width of the display area</param>
/// <param name="mapUnitFactor">MapUnitFactor is the factor the unit used on the map</param>
/// <param name="dpi">DPI used to render the map</param>
/// <returns></returns>
public static double CalculateScaleNonLatLong(double mapWidthMeters, double mapSizeWidth, double mapUnitFactor, int dpi)
{
int nPxlPerInch = dpi;
double ratio;
if (mapSizeWidth <= 0) return 0.0;
//convert map width to meters
double mapWidth = mapWidthMeters * mapUnitFactor;
//convert page width to meters.
try
{
double pageWidth = mapSizeWidth / nPxlPerInch * GeoSpatialMath.MetersPerInch;
ratio = Math.Abs(mapWidth / pageWidth);
}
catch
{
ratio = 0.0;
}
return ratio;
}
/// <summary>
/// Calculate the Representative Fraction Scale for a Lat/Long map.
/// </summary>
/// <param name="lon1">LowerLeft Longitude</param>
/// <param name="lon2">LowerRight Longitude</param>
/// <param name="lat">LowerLeft Latitude</param>
/// <param name="widthPage">The width of the display area</param>
/// <param name="dpi">DPI used to render the map</param>
/// <returns></returns>
public static double CalculateScaleLatLong(double lon1, double lon2, double lat, double widthPage, int dpi)
{
double distance = GeoSpatialMath.GreatCircleDistanceReflex(lon1, lon2, lat);
double r = CalculateScaleNonLatLong(distance, widthPage, 1, dpi);
return r;
}
}
}