Skip to content

Commit e49d006

Browse files
committed
Merge pull request #10 from rguerra/bug-libmgrs
Fix how libmgrs.so is imported if installing with Python 3.5.
2 parents b59f356 + 1375709 commit e49d006

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

mgrs/core.py

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import atexit, os, re, sys
22
import ctypes
33
from ctypes.util import find_library
4-
5-
import ctypes
4+
import sysconfig
65
import math
76

87
class RTreeError(Exception):
98
"RTree exception, indicates a RTree-related error."
109
pass
11-
10+
1211
if os.name == 'nt':
1312
try:
1413
local_dlls = sys.path
@@ -29,7 +28,11 @@ def free(m):
2928
raise
3029
elif os.name == 'posix':
3130
platform = os.uname()[0]
32-
lib_name = 'libmgrs.so'
31+
soabi = sysconfig.get_config_var('SOABI')
32+
if soabi:
33+
lib_name = 'libmgrs.{}.so'.format(soabi)
34+
else:
35+
lib_name = 'libmgrs.so'
3336
local_library_path = os.path.abspath(os.path.dirname(__file__) + "/..")
3437
free = ctypes.CDLL(find_library('c')).free
3538
rt = ctypes.CDLL(os.path.join(local_library_path, lib_name))
@@ -54,7 +57,7 @@ def get_errors(value):
5457
if key & value:
5558
output += errors[key] + " & "
5659
return output[:-2]
57-
60+
5861
def TO_RADIANS(degrees):
5962
return (float(degrees) * math.pi/180.0)
6063

@@ -76,33 +79,33 @@ def check_error(result, func, cargs):
7679
# char *MGRS);
7780
# /*
7881
# * The function Convert_Geodetic_To_MGRS converts geodetic (latitude and
79-
# * longitude) coordinates to an MGRS coordinate string, according to the
80-
# * current ellipsoid parameters. If any errors occur, the error code(s)
82+
# * longitude) coordinates to an MGRS coordinate string, according to the
83+
# * current ellipsoid parameters. If any errors occur, the error code(s)
8184
# * are returned by the function, otherwise MGRS_NO_ERROR is returned.
8285
# *
8386
# * Latitude : Latitude in radians (input)
8487
# * Longitude : Longitude in radians (input)
8588
# * Precision : Precision level of MGRS string (input)
8689
# * MGRS : MGRS coordinate string (output)
87-
# *
90+
# *
8891
# */
8992

90-
rt.Convert_Geodetic_To_MGRS.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_long, ctypes.c_char_p]
93+
rt.Convert_Geodetic_To_MGRS.argtypes = [ctypes.c_double, ctypes.c_double, ctypes.c_long, ctypes.c_char_p]
9194
rt.Convert_Geodetic_To_MGRS.restype = ctypes.c_long
9295
rt.Convert_Geodetic_To_MGRS.errcheck = check_error
9396

94-
#
97+
#
9598
# /*
9699
# * This function converts an MGRS coordinate string to Geodetic (latitude
97-
# * and longitude in radians) coordinates. If any errors occur, the error
98-
# * code(s) are returned by the function, otherwise MGRS_NO_ERROR is returned.
100+
# * and longitude in radians) coordinates. If any errors occur, the error
101+
# * code(s) are returned by the function, otherwise MGRS_NO_ERROR is returned.
99102
# *
100103
# * MGRS : MGRS coordinate string (input)
101104
# * Latitude : Latitude in radians (output)
102105
# * Longitude : Longitude in radians (output)
103-
# *
106+
# *
104107
# */
105-
#
108+
#
106109

107110
rt.Convert_MGRS_To_Geodetic.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double)]
108111
rt.Convert_MGRS_To_Geodetic.restype = ctypes.c_long
@@ -111,8 +114,8 @@ def check_error(result, func, cargs):
111114

112115
# /*
113116
# * The function Convert_UTM_To_MGRS converts UTM (zone, easting, and
114-
# * northing) coordinates to an MGRS coordinate string, according to the
115-
# * current ellipsoid parameters. If any errors occur, the error code(s)
117+
# * northing) coordinates to an MGRS coordinate string, according to the
118+
# * current ellipsoid parameters. If any errors occur, the error code(s)
116119
# * are returned by the function, otherwise MGRS_NO_ERROR is returned.
117120
# *
118121
# * Zone : UTM zone (input)
@@ -129,9 +132,9 @@ def check_error(result, func, cargs):
129132

130133
# /*
131134
# * The function Convert_MGRS_To_UTM converts an MGRS coordinate string
132-
# * to UTM projection (zone, hemisphere, easting and northing) coordinates
133-
# * according to the current ellipsoid parameters. If any errors occur,
134-
# * the error code(s) are returned by the function, otherwise UTM_NO_ERROR
135+
# * to UTM projection (zone, hemisphere, easting and northing) coordinates
136+
# * according to the current ellipsoid parameters. If any errors occur,
137+
# * the error code(s) are returned by the function, otherwise UTM_NO_ERROR
135138
# * is returned.
136139
# *
137140
# * MGRS : MGRS coordinate string (input)
@@ -143,4 +146,4 @@ def check_error(result, func, cargs):
143146

144147
rt.Convert_MGRS_To_UTM.argtype = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_long), ctypes.POINTER(ctypes.c_char), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double)]
145148
rt.Convert_MGRS_To_UTM.restype = ctypes.c_long
146-
rt.Convert_MGRS_To_UTM.errcheck = check_error
149+
rt.Convert_MGRS_To_UTM.errcheck = check_error

0 commit comments

Comments
 (0)