-
Notifications
You must be signed in to change notification settings - Fork 1
/
GdalConfiguration.vb
218 lines (189 loc) · 8.48 KB
/
GdalConfiguration.vb
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
'******************************************************************************
'*
'* Name: GdalConfiguration.vb.pp
'* Project: GDAL VB.NET Interface
'* Purpose: A static configuration utility class to enable GDAL/OGR.
'* Author: Felix Obermaier
'*
'******************************************************************************
'* Copyright (c) 2012-2018, Felix Obermaier
'*
'* Permission is hereby granted, free of charge, to any person obtaining a
'* copy of this software and associated documentation files (the "Software"),
'* to deal in the Software without restriction, including without limitation
'* the rights to use, copy, modify, merge, publish, distribute, sublicense,
'* and/or sell copies of the Software, and to permit persons to whom the
'* Software is furnished to do so, subject to the following conditions:
'*
'* The above copyright notice and this permission notice shall be included
'* in all copies or substantial portions of the Software.
'*
'* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
'* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
'* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
'* DEALINGS IN THE SOFTWARE.
'*****************************************************************************/
Option Infer On
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports Gdal = OSGeo.GDAL.Gdal
Imports Ogr = OSGeo.OGR.Ogr
Namespace GeoTIFFConverter
''' <summary>
''' Configuration class for GDAL/OGR
''' </summary>
Public Partial Class GdalConfiguration
Private Shared _configuredOgr As Boolean
Private Shared _configuredGdal As Boolean
Private Shared _usable As Boolean
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function SetDefaultDllDirectories(ByVal directoryFlags As UInteger) As Boolean
End Function
' LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_LIBRARY_SEARCH_SYSTEM32
Private Const DllSearchFlags As UInteger = &H400 Or &H800
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)>
Public Shared Function AddDllDirectory(ByVal lpPathName As String) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
''' <summary>
''' Construction of Gdal/Ogr
''' </summary>
Shared Sub New()
Dim nativePath As String = Nothing
Dim executingDirectory As String = Nothing
Dim gdalPath As String = Nothing
Try
If Not IsWindows Then
Const notSet As String = "_Not_set_"
Dim tmp As String = Gdal.GetConfigOption("GDAL_DATA", notSet)
_usable = (tmp <> notSet)
Return
End If
Dim executingAssemblyFile As String = New Uri(Assembly.GetExecutingAssembly.GetName.CodeBase).LocalPath
executingDirectory = Path.GetDirectoryName(executingAssemblyFile)
If String.IsNullOrEmpty(executingDirectory) Then
Throw New InvalidOperationException("cannot get executing directory")
End If
SetDefaultDllDirectories(DllSearchFlags)
gdalPath = Path.Combine(executingDirectory, "gdal")
nativePath = Path.Combine(gdalPath, GetPlatform())
If Not Directory.Exists(nativePath) Then
Throw New DirectoryNotFoundException($"GDAL native directory not found at '{nativePath}'")
End If
If Not File.Exists(Path.Combine(nativePath, "gdal_wrap.dll")) Then
Throw New FileNotFoundException($"GDAL native wrapper file not found at '{Path.Combine(nativePath, ", gdal_wrap.dll, ")}'")
End If
AddDllDirectory(nativePath)
AddDllDirectory(Path.Combine(nativePath, "plugins"))
' Set the additional GDAL environment variables.
Dim gdalData As String = Path.Combine(gdalPath, "data")
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData)
Gdal.SetConfigOption("GDAL_DATA", gdalData)
Dim driverPath As String = Path.Combine(nativePath, "plugins")
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath)
Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath)
Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData)
Gdal.SetConfigOption("GEOTIFF_CSV", gdalData)
Dim projSharePath As String = Path.Combine(gdalPath, "share")
Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath)
Gdal.SetConfigOption("PROJ_LIB", projSharePath)
_usable = True
Catch e As Exception
_usable = False
Trace.WriteLine(e, "error")
Trace.WriteLine($"Executing directory: {executingDirectory}", "error")
Trace.WriteLine($"gdal directory: {gdalPath}", "error")
Trace.WriteLine($"native directory: {nativePath}", "error")
'throw;
End Try
End Sub
''' <summary>
''' Gets a value indicating if the GDAL package is set up properly.
''' </summary>
Public Shared ReadOnly Property Usable As Boolean
Get
Return _usable
End Get
End Property
''' <summary>
''' Method to ensure the static constructor is being called.
''' </summary>
''' <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
Public Shared Sub ConfigureOgr()
If Not _usable Then
Return
End If
If _configuredOgr Then
Return
End If
' Register drivers
Ogr.RegisterAll
_configuredOgr = True
PrintDriversOgr()
End Sub
''' <summary>
''' Method to ensure the static constructor is being called.
''' </summary>
''' <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
Public Shared Sub ConfigureGdal()
If Not _usable Then
Return
End If
If _configuredGdal Then
Return
End If
' Register drivers
Gdal.AllRegister
_configuredGdal = True
PrintDriversGdal()
End Sub
''' <summary>
''' Function to determine which platform we're on
''' </summary>
Private Shared Function GetPlatform() As String
If (Environment.Is64BitProcess) Then Return "x64"
return "x86"
End Function
''' <summary>
''' Gets a value indicating if we are on a windows platform
''' </summary>
Private Shared ReadOnly Property IsWindows As Boolean
Get
Dim res = Not ((Environment.OSVersion.Platform = PlatformID.Unix) _
OrElse (Environment.OSVersion.Platform = PlatformID.MacOSX))
Return res
End Get
End Property
Private Shared Sub PrintDriversOgr()
#if (DEBUG)
If _usable Then
Dim num = Ogr.GetDriverCount
Dim i = 0
Do While (i < num)
Dim driver = Ogr.GetDriver(i)
Trace.WriteLine($"OGR {i}: {driver.GetName()}", "Debug")
i = (i + 1)
Loop
End If
#End If
End Sub
Private Shared Sub PrintDriversGdal()
#if (DEBUG)
If _usable Then
Dim num = Gdal.GetDriverCount
Dim i = 0
Do While (i < num)
Dim driver = Gdal.GetDriver(i)
Trace.WriteLine($"GDAL {i}: {driver.ShortName}-{driver.LongName}")
i = (i + 1)
Loop
End If
#End If
End Sub
End Class
End Namespace