|
17 | 17 |
|
18 | 18 | from shapely.geometry import Point, LineString, Polygon
|
19 | 19 | import itertools
|
20 |
| -import sys |
21 |
| -sys.path.append('../../python-poly2tri') |
22 |
| -import p2t |
23 |
| -sys.path.remove('../../python-poly2tri') |
| 20 | +import sys, os |
24 | 21 | from generalfuncs import pairwise
|
25 | 22 |
|
| 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 | + |
26 | 67 | def any_to_polyline(pointlist):
|
27 | 68 | """Given a point list in any format, convert it to a polyline."""
|
28 | 69 | if hasattr(pointlist, 'exterior'):
|
|
0 commit comments