forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformation.aspx.cs
More file actions
264 lines (232 loc) · 13.3 KB
/
Transformation.aspx.cs
File metadata and controls
264 lines (232 loc) · 13.3 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web;
using System.Web.UI;
using SharpMap;
using GeoAPI.CoordinateSystems;
using GeoAPI.CoordinateSystems.Transformations;
using SharpMap.Data.Providers;
using GeoAPI.Geometries;
using SharpMap.Layers;
using SharpMap.Web;
using Point=GeoAPI.Geometries.Coordinate;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
public partial class Transformation : Page
{
private ICoordinateSystem datacoordsys;
private Map myMap;
protected void Page_Load(object sender, EventArgs e)
{
//Set up the map. We use the method in the App_Code folder for initializing the map
myMap = InitializeMap(new Size((int) imgMap.Width.Value, (int) imgMap.Height.Value));
if (Page.IsPostBack)
{
//Page is post back. Restore center and zoom-values from viewstate
myMap.Center = (Point) ViewState["mapCenter"];
myMap.Zoom = (double) ViewState["mapZoom"];
}
else
{
//This is the initial view of the map. Zoom to the extents of the map:
myMap.Zoom = 80;
myMap.Center = new Point(-95, 37);
//Create the map
GenerateMap();
}
}
protected void imgMap_Click(object sender, ImageClickEventArgs e)
{
//Set center of the map to where the client clicked
myMap.Center = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
//Set zoom value if any of the zoom tools were selected
if (rblMapTools.SelectedValue == "0") //Zoom in
myMap.Zoom = myMap.Zoom*0.5;
else if (rblMapTools.SelectedValue == "1") //Zoom out
myMap.Zoom = myMap.Zoom*2;
//Create the map
GenerateMap();
}
/// <summary>
/// Creates the map, inserts it into the cache and sets the ImageButton Url
/// </summary>
private void GenerateMap()
{
//Save the current mapcenter and zoom in the viewstate
ViewState.Add("mapCenter", myMap.Center);
ViewState.Add("mapZoom", myMap.Zoom);
ViewState.Add("currentProj", ddlProjection.SelectedValue);
//Render the map
Image img = myMap.GetMap();
string imgID = Caching.InsertIntoCache(1, img);
imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
litEnvelope.Text = myMap.Envelope.MinX.ToString("#.##") + "," + myMap.Envelope.MinY.ToString("#.##") + " -> " +
myMap.Envelope.MaxX.ToString("#.##") + "," + myMap.Envelope.MaxY.ToString("#.##") +
" (Projected coordinate system)";
}
protected void ddlProjection_SelectedIndexChanged(object sender, EventArgs e)
{
//Transform current view to new coordinate system and zoom to the transformed box
string PreviousProj = ViewState["currentProj"].ToString();
string SelectedProj = ddlProjection.SelectedValue;
//Points defining the current view
Point left = new Point(myMap.Envelope.MinX, myMap.Center.Y);
Point right = new Point(myMap.Envelope.MaxX, myMap.Center.Y);
Point center = myMap.Center;
if (PreviousProj != "Pseudo")
{
//Transform current view back to geographic coordinates
ICoordinateTransformation trans = GetTransform(PreviousProj);
left = GeometryTransform.TransformCoordinate(new Point(myMap.Envelope.MinX, myMap.Center.Y),
trans.MathTransform.Inverse());
right = GeometryTransform.TransformCoordinate(new Point(myMap.Envelope.MaxX, myMap.Center.Y),
trans.MathTransform.Inverse());
center = GeometryTransform.TransformCoordinate(myMap.Center, trans.MathTransform.Inverse());
}
//If both PreviousSRID and SelectedSRID are projected coordsys, first transform to geographic
if (SelectedProj == "Pseudo")
{
myMap.Center = center;
myMap.Zoom = Math.Abs(right.X - left.X);
}
else //Project coordinates to new projection
{
//Transform back to geographic and over to new projection
ICoordinateTransformation trans = GetTransform(SelectedProj);
left = GeometryTransform.TransformCoordinate(left, trans.MathTransform);
right = GeometryTransform.TransformCoordinate(right, trans.MathTransform);
center = GeometryTransform.TransformCoordinate(center, trans.MathTransform);
myMap.Center = center;
myMap.Zoom = Math.Abs(right.X - left.X);
var envelopeGcs = GeometryTransform.TransformBox(myMap.Envelope, trans.MathTransform.Inverse());
litEnvelopeLatLong.Text = envelopeGcs.ToString();
}
GenerateMap();
}
public Map InitializeMap(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
//Initialize a new map of size 'imagesize'
Map map = new Map(size);
//Set up the countries layer
VectorLayer layCountries = new VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
ShapeFile datasource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\USA\states.shp"), true);
layCountries.DataSource = datasource;
datacoordsys = datasource.CoordinateSystem;
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = Pens.Black;
layCountries.Style.EnableOutline = true;
layCountries.CoordinateTransformation = GetTransform(ddlProjection.SelectedValue);
if (layCountries.CoordinateTransformation != null)
{
litInputCoordsys.Text = layCountries.CoordinateTransformation.TargetCS.WKT;
litCoordsys.Text = layCountries.CoordinateTransformation.SourceCS.WKT;
litTransform.Text = layCountries.CoordinateTransformation.MathTransform.WKT;
}
else
{
litInputCoordsys.Text = datasource.CoordinateSystem.WKT;
litCoordsys.Text = "None";
litTransform.Text = "None";
}
VectorLayer layGrid = new VectorLayer("Grid");
//Set the datasource to a shapefile in the App_data folder
layGrid.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\USA\latlong.shp"), true);
layGrid.CoordinateTransformation = layCountries.CoordinateTransformation;
layGrid.Style.Line = new Pen(Color.FromArgb(127, 255, 0, 0), 1);
//Add the layers to the map object.
map.Layers.Add(layCountries);
map.Layers.Add(layGrid);
map.BackColor = Color.LightBlue;
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public ICoordinateTransformation GetTransform(string name)
{
switch (name)
{
case "Mercator":
return Transform2Mercator(datacoordsys);
case "Albers":
return Transform2Albers(datacoordsys);
case "Lambert":
return Transform2Lambert(datacoordsys);
default:
return null;
}
}
public static ICoordinateTransformation Transform2Albers(ICoordinateSystem source)
{
if (source == null)
throw new ArgumentException("Source coordinate system is null");
if (!(source is IGeographicCoordinateSystem))
throw new ArgumentException("Source coordinate system must be geographic");
CoordinateSystemFactory cFac = new CoordinateSystemFactory();
List<ProjectionParameter> parameters = new List<ProjectionParameter>();
parameters.Add(new ProjectionParameter("central_meridian", -95));
parameters.Add(new ProjectionParameter("latitude_of_origin", 50));
parameters.Add(new ProjectionParameter("standard_parallel_1", 29.5));
parameters.Add(new ProjectionParameter("standard_parallel_2", 45.5));
parameters.Add(new ProjectionParameter("false_easting", 0));
parameters.Add(new ProjectionParameter("false_northing", 0));
IProjection projection = cFac.CreateProjection("Albers_Conic_Equal_Area", "albers", parameters);
IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Albers_Conic_Equal_Area",
source as IGeographicCoordinateSystem,
projection, ProjNet.CoordinateSystems.LinearUnit.Metre,
new AxisInfo("East",
AxisOrientationEnum.East),
new AxisInfo("North",
AxisOrientationEnum.
North));
return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
}
public static ICoordinateTransformation Transform2Mercator(ICoordinateSystem source)
{
CoordinateSystemFactory cFac = new CoordinateSystemFactory();
List<ProjectionParameter> parameters = new List<ProjectionParameter>();
parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
parameters.Add(new ProjectionParameter("central_meridian", 0));
parameters.Add(new ProjectionParameter("false_easting", 0));
parameters.Add(new ProjectionParameter("false_northing", 0));
IProjection projection = cFac.CreateProjection("Mercator", "Mercator_2SP", parameters);
IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Mercator",
source as IGeographicCoordinateSystem,
projection, ProjNet.CoordinateSystems.LinearUnit.Metre,
new AxisInfo("East",
AxisOrientationEnum.East),
new AxisInfo("North",
AxisOrientationEnum.
North));
return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
}
public static ICoordinateTransformation Transform2Lambert(ICoordinateSystem source)
{
if (source == null)
throw new ArgumentException("Source coordinate system is null");
if (!(source is IGeographicCoordinateSystem))
throw new ArgumentException("Source coordinate system must be geographic");
CoordinateSystemFactory cFac = new CoordinateSystemFactory();
List<ProjectionParameter> parameters = new List<ProjectionParameter>();
parameters.Add(new ProjectionParameter("latitude_of_origin", 50));
parameters.Add(new ProjectionParameter("central_meridian", -95));
parameters.Add(new ProjectionParameter("standard_parallel_1", 33));
parameters.Add(new ProjectionParameter("standard_parallel_2", 45));
parameters.Add(new ProjectionParameter("false_easting", 0));
parameters.Add(new ProjectionParameter("false_northing", 0));
IProjection projection = cFac.CreateProjection("Lambert Conformal Conic 2SP", "lambert_conformal_conic_2sp",
parameters);
IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Lambert Conformal Conic 2SP",
source as IGeographicCoordinateSystem,
projection, ProjNet.CoordinateSystems.LinearUnit.Metre,
new AxisInfo("East",
AxisOrientationEnum.East),
new AxisInfo("North",
AxisOrientationEnum.
North));
return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
}
}