Skip to content

Commit 2259097

Browse files
committed
Be more flexible in looking for python-poly2tri
Now the p2t.so file can be searched for in the current directory, any higher-level directory, or a python-poly2tri directory attached to any of the above. This will make the software a little easier to install.
1 parent 8ceacde commit 2259097

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/dataconvert.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,53 @@
1717

1818
from shapely.geometry import Point, LineString, Polygon
1919
import itertools
20-
import sys
21-
sys.path.append('../../python-poly2tri')
22-
import p2t
23-
sys.path.remove('../../python-poly2tri')
20+
import sys, os
2421
from generalfuncs import pairwise
2522

23+
def import_p2t():
24+
"""Find and import the p2t module, which might be in several possible locations:
25+
* The current directory, or any ancestor directory
26+
* A "python-poly2tri" directory based of the current dir or an ancestor
27+
"""
28+
try:
29+
import p2t
30+
except ImportError:
31+
pass # Rest of function will hunt for it
32+
else:
33+
# Found it already, so just return it
34+
return p2t
35+
fname = 'p2t.so'
36+
curdir = os.getcwd()
37+
def check(path):
38+
return os.path.exists(os.path.join(path, fname))
39+
while True:
40+
# Check both current dir and python-poly2tri folder
41+
found = check(curdir)
42+
if found:
43+
sys.path.append(curdir)
44+
import p2t
45+
sys.path.remove(curdir)
46+
return p2t
47+
poly2tri_dir = os.path.join(curdir, 'python-poly2tri')
48+
found = check(poly2tri_dir)
49+
if found:
50+
sys.path.append(poly2tri_dir)
51+
import p2t
52+
sys.path.remove(poly2tri_dir)
53+
return p2t
54+
parent = os.path.abspath(os.path.join(curdir, '..'))
55+
if parent == curdir:
56+
sys.stderr.write("ERROR: python-poly2tri library not found. Please install it from\n")
57+
sys.stderr.write("https://github.com/hansent/python-poly2tri and follow its build\n")
58+
sys.stderr.write("instructions to produce the p2t.so file, then copy p2t.so into\n")
59+
sys.stderr.write("the same folder that contains extractpoints.py.\n")
60+
raise ImportError, "Could not find python-poly2tri module"
61+
curdir = parent
62+
continue
63+
return p2t # Should never reach here, but just in case
64+
65+
p2t = import_p2t()
66+
2667
def any_to_polyline(pointlist):
2768
"""Given a point list in any format, convert it to a polyline."""
2869
if hasattr(pointlist, 'exterior'):

src/extractpoints.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import warnings
1616
import math
1717
from shapely.geometry import Polygon, LineString, Point
18-
sys.path.append('../../python-poly2tri')
19-
import p2t
20-
sys.path.remove('../../python-poly2tri')
18+
19+
from dataconvert import import_p2t
20+
p2t = import_p2t()
2121

2222
from dataconvert import (
2323
any_to_linestring, any_to_polygon, any_to_polyline, any_to_closedpolyline,

0 commit comments

Comments
 (0)