Skip to content

Commit 258f774

Browse files
committed
[Trunk]
- ensuring Map.SRID always matches Map.Factory.SRID git-tfs-id: [https://tfs.codeplex.com/tfs/TFS01]$/SharpMap/Trunk;C102205
1 parent c95a5cf commit 258f774

2 files changed

Lines changed: 53 additions & 26 deletions

File tree

SharpMap/Map/Map.cs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
using System.Globalization;
2323
using System.IO;
2424
using GeoAPI.Geometries;
25-
using NetTopologySuite.Geometries;
2625
using SharpMap.Layers;
2726
using SharpMap.Rendering;
2827
using SharpMap.Rendering.Decoration;
@@ -42,13 +41,36 @@ namespace SharpMap
4241
[Serializable]
4342
public class Map : IDisposable
4443
{
44+
static Map()
45+
{
46+
if (GeoAPI.GeometryServiceProvider.Instance == null)
47+
GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance;
48+
}
49+
4550
readonly ILog _logger = LogManager.GetLogger(typeof(Map));
4651

4752
/// <summary>
4853
/// Used for converting numbers to/from strings
4954
/// </summary>
5055
public static NumberFormatInfo NumberFormatEnUs = new CultureInfo("en-US", false).NumberFormat;
5156

57+
#region Fields
58+
private readonly List<IMapDecoration> _decorations = new List<IMapDecoration>();
59+
60+
private Color _backgroundColor;
61+
private int _srid = -1;
62+
private double _zoom;
63+
private Point _center;
64+
private readonly LayerCollection _layers;
65+
private readonly LayerCollection _backgroundLayers;
66+
private readonly VariableLayerCollection _variableLayers;
67+
private Matrix _mapTransform;
68+
internal Matrix MapTransformInverted;
69+
70+
private readonly MapViewPortGuard _mapViewportGuard;
71+
#endregion
72+
73+
5274
/// <summary>
5375
/// Specifies whether to trigger a dispose on all layers (and their datasources) contained in this map when the map-object is disposed.
5476
/// The default behaviour is true unless the map is a result of a Map.Clone() operation in which case the value is false
@@ -57,17 +79,12 @@ public class Map : IDisposable
5779
/// </summary>
5880
public bool DisposeLayersOnDispose = true;
5981

60-
/// <summary>
61-
/// Set the SRID of the Map
62-
/// </summary>
63-
public int SRID = -1;
64-
65-
6682
/// <summary>
6783
/// Initializes a new map
6884
/// </summary>
6985
public Map() : this(new Size(640, 480))
7086
{
87+
7188
}
7289

7390
/// <summary>
@@ -78,7 +95,7 @@ public Map(Size size)
7895
{
7996
_mapViewportGuard = new MapViewPortGuard(size, 0d, Double.MaxValue);
8097

81-
Factory = new GeometryFactory();
98+
Factory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(_srid);
8299
_layers = new LayerCollection();
83100
_backgroundLayers = new LayerCollection();
84101
_variableLayers = new VariableLayerCollection(_layers);
@@ -298,16 +315,16 @@ public Metafile GetMapAsMetafile()
298315
public Metafile GetMapAsMetafile(string metafileName)
299316
{
300317
Metafile metafile;
301-
Bitmap bm = new Bitmap(1, 1);
302-
using (Graphics g = Graphics.FromImage(bm))
318+
var bm = new Bitmap(1, 1);
319+
using (var g = Graphics.FromImage(bm))
303320
{
304-
IntPtr hdc = g.GetHdc();
305-
using (MemoryStream stream = new MemoryStream())
321+
var hdc = g.GetHdc();
322+
using (var stream = new MemoryStream())
306323
{
307324
metafile = new Metafile(stream, hdc, new RectangleF(0, 0, Size.Width, Size.Height),
308325
MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
309326

310-
using (Graphics metafileGraphics = Graphics.FromImage(metafile))
327+
using (var metafileGraphics = Graphics.FromImage(metafile))
311328
{
312329
metafileGraphics.PageUnit = GraphicsUnit.Pixel;
313330
metafileGraphics.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device,
@@ -801,18 +818,20 @@ public Point ImageToWorld(PointF p, bool careAboutMapTransform)
801818

802819
#region Properties
803820

804-
private readonly List<IMapDecoration> _decorations = new List<IMapDecoration>();
805-
806-
private Color _backgroundColor;
807-
private double _zoom;
808-
private Point _center;
809-
private readonly LayerCollection _layers;
810-
private readonly LayerCollection _backgroundLayers;
811-
private readonly VariableLayerCollection _variableLayers;
812-
private Matrix _mapTransform;
813-
internal Matrix MapTransformInverted;
814-
815-
private readonly MapViewPortGuard _mapViewportGuard;
821+
/// <summary>
822+
/// Gets or sets the SRID of the map
823+
/// </summary>
824+
public int SRID
825+
{
826+
get { return _srid; }
827+
set
828+
{
829+
if (_srid == value)
830+
return;
831+
_srid = value;
832+
Factory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory(_srid);
833+
}
834+
}
816835

817836
/// <summary>
818837
/// Factory used to create geometries
@@ -1187,7 +1206,6 @@ public Int32 DisclaimerLocation
11871206

11881207

11891208
#endregion
1190-
11911209
}
11921210

11931211
/// <summary>

UnitTests/MapTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ private static IProvider CreateDatasource()
4949
return new GeometryProvider(geoms);
5050
}
5151

52+
[Test]
53+
public void MapSridEqualsFactorySrid()
54+
{
55+
var m = new Map();
56+
Assert.AreEqual(m.SRID, m.Factory.SRID);
57+
m.SRID = 10;
58+
Assert.AreEqual(m.SRID, m.Factory.SRID);
59+
}
60+
5261
[Test]
5362
public void FindLayer_ReturnEnumerable()
5463
{

0 commit comments

Comments
 (0)