forked from ubarsc/pylidar
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
380 lines (327 loc) · 14.6 KB
/
setup.py
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python
"""
Install script for PyLidar
"""
# This file is part of PyLidar
# Copyright (C) 2015 John Armston, Pete Bunting, Neil Flood, Sam Gillingham
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import os
import sys
import ctypes
from numpy.distutils.core import setup, Extension
# don't build extensions if we are in readthedocs
withExtensions = os.getenv('READTHEDOCS', default='False') != 'True'
import pylidar
# use the latest numpy API
NUMPY_MACROS = ('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')
# Are we installing the command line scripts?
# this is an experimental option for users who are
# using the Python entry point feature of setuptools and Conda instead
NO_INSTALL_CMDLINE = int(os.getenv('PYLIDAR_NOCMDLINE', '0')) > 0
def getExtraCXXFlags():
"""
Looks at the $PYLIDAR_CXX_FLAGS environment variable.
If it exists, this function returns a list of flags
to be passed as the extra_compile_args argument to
the Extension constructor.
Otherwise None.
"""
if 'PYLIDAR_CXX_FLAGS' in os.environ:
return os.environ['PYLIDAR_CXX_FLAGS'].split()
else:
return None
def addRieglRXPDriver(extModules, cxxFlags):
"""
Decides if the Riegl RXP driver is to be built. If so
adds the Extension class to extModules.
"""
if 'RIVLIB_ROOT' in os.environ and 'RIWAVELIB_ROOT' in os.environ:
print('Building Riegl RXP Extension...')
rivlibRoot = os.environ['RIVLIB_ROOT']
riwavelibRoot = os.environ['RIWAVELIB_ROOT']
rivlibs = ['scanlib-mt', 'riboost_chrono-mt',
'riboost_date_time-mt', 'riboost_filesystem-mt',
'riboost_regex-mt', 'riboost_system-mt',
'riboost_thread-mt']
riwavelibs = ['wfmifc-mt']
# on Windows the libs do not follow the normal naming convention
# and start with 'lib'. On Linux the compiler prepends this automatically
# but on Windows we need to do it manually
if sys.platform == 'win32':
rivlibs = ['lib' + name for name in rivlibs]
# all the libs
libs = rivlibs + riwavelibs
defines = getRieglWaveLibVersion(riwavelibRoot, riwavelibs[0])
defines.extend([NUMPY_MACROS])
rieglModule = Extension(name='pylidar.lidarformats._rieglrxp',
define_macros=defines,
sources=['src/riegl_rxp.cpp', 'src/pylidar.c'],
include_dirs=[os.path.join(rivlibRoot, 'include'),
os.path.join(riwavelibRoot, 'include')],
extra_compile_args=cxxFlags,
libraries=libs,
runtime_library_dirs=[os.path.join(rivlibRoot, 'lib'),
os.path.join(riwavelibRoot, 'lib')],
library_dirs=[os.path.join(rivlibRoot, 'lib'),
os.path.join(riwavelibRoot, 'lib')])
extModules.append(rieglModule)
else:
print('Riegl RXP Libraries not found.')
print('If installed set $RIVLIB_ROOT to the install location of RiVLib')
print('and $RIWAVELIB_ROOT to the install location of the waveform extraction library (riwavelib)')
def getRieglWaveLibVersion(riwavelibRoot, libname):
"""
Because we cannot distribute the wfmifc-mt library, we need
to check that the major version at compile time matches the
version the user has at runtime. We do this by getting the
version now and setting it as a #define. The library can then
use the #define to check at runtime.
Unfortunately the headers don't give us this information.
"""
if sys.platform == 'win32':
libname = os.path.join(riwavelibRoot, 'lib', libname + '.dll')
elif sys.platform == 'darwin':
libname = os.path.join(riwavelibRoot, 'lib', 'lib' + libname + '.dylib')
else:
libname = os.path.join(riwavelibRoot, 'lib', 'lib' + libname + '.so')
wfm = ctypes.cdll.LoadLibrary(libname)
major = ctypes.c_ushort()
minor = ctypes.c_ushort()
version = ctypes.c_char_p()
tag = ctypes.c_char_p()
wfm.fwifc_get_library_version(ctypes.byref(major), ctypes.byref(minor),
ctypes.byref(version), ctypes.byref(tag))
return [("RIEGL_WFM_MAJOR", str(major.value)),
("RIEGL_WFM_MINOR", str(minor.value))]
def addRieglRDBDriver(extModules, cxxFlags):
"""
Decides if the Riegl RDB driver is to be built. If so
adds the Extension class to extModules.
"""
if 'RDBLIB_ROOT' in os.environ:
print('Building Riegl RDB Extension...')
rdblibRoot = os.environ['RDBLIB_ROOT']
if sys.platform == 'win32':
rdbLibName = 'rdblib'
else:
rdbLibName = 'rdb'
defines = getRieglRDBLibVersion(rdblibRoot, rdbLibName)
defines.extend([NUMPY_MACROS])
rieglRDBModule = Extension(name='pylidar.lidarformats._rieglrdb',
define_macros=defines,
sources=['src/riegl_rdb.cpp', 'src/pylidar.c'],
include_dirs=[os.path.join(rdblibRoot, "interface", "c")],
extra_compile_args=cxxFlags,
libraries=[rdbLibName],
runtime_library_dirs=[os.path.join(rdblibRoot, 'library')],
library_dirs=[os.path.join(rdblibRoot, 'library')])
extModules.append(rieglRDBModule)
else:
print('Riegl RDB Libraries not found.')
print('If installed set $RDBLIB_ROOT to the install location of RDBLib')
def getRieglRDBLibVersion(rdbRoot, libname):
"""
Because we cannot distribute the rdblib library, we need
to check that the major version at compile time matches the
version the user has at runtime. We do this by getting the
version now and setting it as a #define. The library can then
use the #define to check at runtime.
Unfortunately the headers don't give us this information.
"""
if sys.platform == 'win32':
libname = os.path.join(rdbRoot, 'library', libname + '.dll')
elif sys.platform == 'darwin':
libname = os.path.join(rdbRoot, 'library', 'lib' + libname + '.dylib')
else:
libname = os.path.join(rdbRoot, 'library', 'lib' + libname + '.so')
rdb = ctypes.cdll.LoadLibrary(libname)
context = ctypes.c_void_p()
logLevel = ctypes.c_char_p(b"NONE")
logPath = ctypes.c_char_p(b"")
rdb.rdb_context_new(ctypes.byref(context), logLevel, logPath)
version = ctypes.c_char_p()
rdb.rdb_library_version(context, ctypes.byref(version))
versionString = version.value
if sys.version_info[0] >= 3:
versionString = versionString.decode()
rdb.rdb_context_delete(ctypes.byref(context))
# versionString is quite specific -something like:
# 2.2.1-2094 (x86_64-linux, Jul 11 2019, 13:10:32)
# we probably don't have to have the exact same string
# so just extract major and minor version numbers
arr = versionString.split('.')
major = arr[0]
minor = arr[1]
return [("RIEGL_RDB_MAJOR", major),
("RIEGL_RDB_MINOR", minor)]
def addLasDriver(extModules, cxxFlags):
"""
Decides if the Las driver is to be built. If so
adds the Extension class to extModules.
"""
import glob
if 'LASTOOLS_ROOT' in os.environ:
print('Building Las Extension...')
lastoolsRoot = os.environ['LASTOOLS_ROOT']
lasModule = Extension(name='pylidar.lidarformats._las',
sources=['src/las.cpp', 'src/pylidar.c'],
include_dirs=[os.path.join(lastoolsRoot, 'include')],
extra_compile_args=cxxFlags,
define_macros = [NUMPY_MACROS],
libraries=['las'],
library_dirs=[os.path.join(lastoolsRoot, 'lib')])
extModules.append(lasModule)
else:
print('Las library not found.')
print('If installed set $LASTOOLS_ROOT to the install location of lastools https://github.com/LAStools/LAStools')
def addASCIIDriver(extModules, cxxFlags):
"""
Decides if the ASCII driver is to be built. If so
adds the Extension class to extModules.
"""
print('Building ASCII Extension...')
includeDirs = None
libs = None
libraryDirs = None
defineMacros = [NUMPY_MACROS]
if 'ZLIB_ROOT' in os.environ:
# build for zlib
zlibRoot = os.environ['ZLIB_ROOT']
includeDirs = [os.path.join(zlibRoot, 'include')]
libs = ['z']
libraryDirs = [os.path.join(zlibRoot, 'lib')]
defineMacros.append(("HAVE_ZLIB", "1"))
else:
print('zlib library not found.')
print('If installed set $ZLIB_ROOT to the install location of zlib')
print("gzip compressed files won't be read")
asciiModule = Extension(name='pylidar.lidarformats._ascii',
sources=['src/ascii.cpp', 'src/pylidar.c'],
include_dirs=includeDirs,
extra_compile_args=cxxFlags,
define_macros=defineMacros,
libraries=libs,
library_dirs=libraryDirs)
extModules.append(asciiModule)
def addLVISBinDriver(extModules, cxxFlags):
"""
Adds the LVIS Binary driver (which is always built).
"""
print('Building LVIS Binary Extension....')
defineMacros = [NUMPY_MACROS]
lvisModule = Extension(name='pylidar.lidarformats._lvisbin',
sources=['src/lvisbin.cpp', 'src/pylidar.c'],
extra_compile_args=cxxFlags,
define_macros=defineMacros)
extModules.append(lvisModule)
def addInsidePoly(extModules):
"""
Adds the insidepoly C toolbox module. Currently requires GDAL to be
present. Could be ignored if GDAL not available, but that sounds confusing
so left it as compulsory for now.
"""
extraargs = {}
# don't use the deprecated numpy api
extraargs['define_macros'] = [NUMPY_MACROS]
if sys.platform == 'win32':
# Windows - rely on %GDAL_HOME% being set and set
# paths appropriately
gdalhome = os.getenv('GDAL_HOME')
if gdalhome is None:
raise SystemExit("need to define %GDAL_HOME%")
extraargs['include_dirs'] = [os.path.join(gdalhome, 'include')]
extraargs['library_dirs'] = [os.path.join(gdalhome, 'lib')]
extraargs['libraries'] = ['gdal_i']
else:
# Unix - can do better with actual flags using gdal-config
import subprocess
try:
cflags = subprocess.check_output(['gdal-config', '--cflags'])
if sys.version_info[0] >= 3:
cflags = cflags.decode()
extraargs['extra_compile_args'] = cflags.strip().split()
ldflags = subprocess.check_output(['gdal-config', '--libs'])
if sys.version_info[0] >= 3:
ldflags = ldflags.decode()
extraargs['extra_link_args'] = ldflags.strip().split()
except OSError:
raise SystemExit("can't find gdal-config - GDAL development files need to be installed")
extraargs['name'] = 'pylidar.toolbox.insidepoly'
extraargs['sources'] = ['src/insidepoly.c']
print('Building InsidePoly Toolbox Extension...')
insidePolyModule = Extension(**extraargs)
extModules.append(insidePolyModule)
def addPulseWavesDriver(extModules, cxxFlags):
"""
Decides if the PulseWaves driver is to be built. If so
adds the Extension class to extModules.
"""
if 'PULSEWAVES_ROOT' in os.environ:
print('Building PulseWaves Extension...')
pulseWavesRoot = os.environ['PULSEWAVES_ROOT']
pulseWavesModule = Extension(name='pylidar.lidarformats._pulsewaves',
sources=['src/pulsewaves.cpp', 'src/pylidar.c'],
include_dirs=[os.path.join(pulseWavesRoot, 'include')],
extra_compile_args=cxxFlags,
define_macros = [NUMPY_MACROS],
libraries=['pulsewaves'],
runtime_library_dirs=[os.path.join(pulseWavesRoot, 'lib')],
library_dirs=[os.path.join(pulseWavesRoot, 'lib')])
extModules.append(pulseWavesModule)
else:
print('PulseWaves library not found.')
print('If installed set $PULSEWAVES_ROOT to the install location of pulsewaves https://github.com/PulseWaves/PulseWaves')
# get any C++ flags
cxxFlags = getExtraCXXFlags()
# work out if we need to build any of the C/C++ extension
# modules
externalModules = []
if withExtensions:
addRieglRXPDriver(externalModules, cxxFlags)
addRieglRDBDriver(externalModules, cxxFlags)
addLasDriver(externalModules, cxxFlags)
addASCIIDriver(externalModules, cxxFlags)
addLVISBinDriver(externalModules, cxxFlags)
addInsidePoly(externalModules)
addPulseWavesDriver(externalModules, cxxFlags)
if NO_INSTALL_CMDLINE:
scriptList = None
else:
scriptList = ['bin/pylidar_translate', 'bin/pylidar_info',
'bin/pylidar_index', 'bin/pylidar_tile', 'bin/pylidar_rasterize',
'bin/pylidar_test', 'bin/pylidar_canopy']
setup(name='pylidar',
version=pylidar.PYLIDAR_VERSION,
ext_modules=externalModules,
description='Tools for simplifying LiDAR data I/O and tools for processing.',
packages=['pylidar', 'pylidar/lidarformats', 'pylidar/toolbox',
'pylidar/toolbox/grdfilters', 'pylidar/toolbox/indexing',
'pylidar/toolbox/translate', 'pylidar.toolbox.cmdline',
'pylidar/testing', 'pylidar/toolbox/canopy'],
scripts=scriptList,
license='LICENSE.txt',
url='http://pylidar.org/',
classifiers=['Intended Audience :: Developers',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'])