-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathGdalSample.cs
More file actions
187 lines (159 loc) · 7.45 KB
/
GdalSample.cs
File metadata and controls
187 lines (159 loc) · 7.45 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
using System;
using System.Linq;
using System.Reflection;
namespace WinFormSamples.Samples
{
public static class GdalSample
{
private static int _num;
private static string _gdalSampleDataset;
public static string GdalSampleDataSet
{
get { return _gdalSampleDataset; }
}
public static SharpMap.Map InitializeMap(float angle)
{
_num %= 9;
int num = _num;
_num++;
switch (num)
{
case 0:
case 1:
case 2:
case 3:
return InitializeGeoTiff(num, angle);
default:
return InitializeVRT(num, angle);
}
}
private static SharpMap.Map InitializeGeoTiff(int index, float angle)
{
try
{
//Sample provided by Dan Brecht and Joel Wilson
var map = new SharpMap.Map();
map.BackColor = System.Drawing.Color.White;
const string relativePath = "GeoData/GeoTiff/";
SharpMap.Layers.GdalRasterLayer layer;
switch (index)
{
case 0:
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiff", relativePath + "utm.tif");
map.Layers.Add(layer);
break;
case 1:
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiff", relativePath + "utm.jp2");
map.Layers.Add(layer);
break;
case 2:
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiff", relativePath + "world_raster_mod.tif");
map.Layers.Add(layer);
break;
default:
if (!System.IO.File.Exists(relativePath + "format01-image_a.tif"))
{
throw new System.Exception("Make sure the data is in the relative directory: " + relativePath);
}
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiffA", relativePath + "format01-image_a.tif");
map.Layers.Add(layer);
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiffB", relativePath + "format01-image_b.tif");
map.Layers.Add(layer);
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiffC", relativePath + "format01-image_c.tif");
map.Layers.Add(layer);
layer = new SharpMap.Layers.GdalRasterLayer("GeoTiffD", relativePath + "format01-image_d.tif");
map.Layers.Add(layer);
SharpMap.Layers.VectorLayer shapeLayer;
if (!System.IO.File.Exists(relativePath + "outline.shp"))
{
throw new System.Exception("Make sure the data is in the relative directory: " + relativePath);
}
shapeLayer = new SharpMap.Layers.VectorLayer("outline", new SharpMap.Data.Providers.ShapeFile(relativePath + "outline.shp"));
shapeLayer.Style.Fill = System.Drawing.Brushes.Transparent;
shapeLayer.Style.Outline = System.Drawing.Pens.Black;
shapeLayer.Style.EnableOutline = true;
shapeLayer.Style.Enabled = true;
map.Layers.Add(shapeLayer);
break;
}
//if (layer != null)
// layer.UseRotation = true;
map.ZoomToExtents();
System.Drawing.Drawing2D.Matrix mat = new System.Drawing.Drawing2D.Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
_gdalSampleDataset = "GeoTiff" + _num;
return map;
}
catch (System.TypeInitializationException ex)
{
if (ex.Message == "The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an exception.")
{
var asm = Assembly.GetAssembly(typeof(OSGeo.GDAL.Gdal)).GetName();
throw new System.Exception(
string.Format(
"The application threw a PINVOKE exception. You probably need to copy the unmanaged dll's to your bin directory. " +
"They are a part of the GDAL NuGet package v{0}.",
asm.Version.ToString(3)));
}
throw;
}
}
private static readonly string[] Vrts =
{
@"..\DEM\Golden_CO.dem",
"contours_sample_polyline_play_polyline.asc",
"contours_sample_polyline_play1_polyline.vrt",
"contours_sample_polyline_play2_polyline.vrt",
"contours_sample_polyline_play3_polyline.vrt"
};
private const string RelativePath = "GeoData/VRT/";
private static SharpMap.Map InitializeVRT(int index, float angle)
{
SharpMap.Map map = new SharpMap.Map();
int ind = index - 4;
if (ind >= Vrts.Length) ind = 0;
if (!System.IO.File.Exists(RelativePath + Vrts[ind]))
{
throw new System.Exception("Make sure the data is in the relative directory: " + RelativePath);
}
SharpMap.Layers.GdalRasterLayer layer = new SharpMap.Layers.GdalRasterLayer("VirtualRasterTable", RelativePath + Vrts[ind]);
var ext = System.IO.Path.GetExtension(layer.Filename);
map.Layers.Add(layer);
_gdalSampleDataset = string.Format("'{0}'", ext != null ? ext.ToUpper() : string.Empty);
map.ZoomToExtents();
System.Drawing.Drawing2D.Matrix mat = new System.Drawing.Drawing2D.Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
return map;
}
public static SharpMap.Map InitializeMap(int angle, string[] filenames)
{
if (filenames == null || filenames.Length == 0) return null;
var map = new SharpMap.Map();
for (int i = 0; i < filenames.Length; i++)
{
if (filenames[i].StartsWith("PG:"))
{
SharpMap.Layers.ILayer lyr = null;
try
{
lyr = new SharpMap.Layers.GdalRasterLayer($"PG_RASTER{i}", filenames[i]);
}
catch (Exception)
{
return null;
}
map.Layers.Add(lyr);
}
else
map.Layers.Add(new SharpMap.Layers.GdalRasterLayer(System.IO.Path.GetFileName(filenames[i]), filenames[i]));
}
var mat = new System.Drawing.Drawing2D.Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
map.ZoomToExtents();
return map;
}
}
}