MCPcopy Create free account
hub / github.com/Apress/python-for-matlab-development / fix_geos

Function fix_geos

code/bin/patch_cartopy.py:45–99  ·  view source on GitHub ↗

On Windows, modify shapely/geos.py when the CONDA_PREFIX string is empty so that sys.prefix is hardcoded to the correct virtual environment path to load the geos_c.dll.

()

Source from the content-addressed store, hash-verified

43import shapely
44
45def fix_geos(): # {{{
46 """
47 On Windows, modify shapely/geos.py when the CONDA_PREFIX
48 string is empty so that
49 sys.prefix
50 is hardcoded to the correct virtual environment path
51 to load the geos_c.dll.
52 """
53 if sys.platform != 'win32':
54 print('fix_geos() exit since this is not Windows')
55 return
56
57 shapely_dir = shapely.__path__
58
59 if not shapely_dir:
60 print(f"Unable to determine shapely's location, exit")
61 return
62
63 P = pathlib.Path(shapely_dir[0]) / 'geos.py'
64 if not P.exists():
65 print(f"{str(P)} doesn't exist, exit")
66 return
67
68 lines_in = P.read_text().split('\n')
69 found_win = False
70 for L in lines_in:
71 if re.search(r'_lgeos\s*=.*?sys\.prefix,.*?geos_c\.dll', L):
72 found_win = True
73 if not found_win:
74 print(f"{str(P)} does not use sys.prefix to determine "
75 f"geos_c.dll path, nothing done.")
76 return
77
78 shutil.copy(str(P), f'{str(P)}.bak') # backup the original version
79 print(f'Wrote backup {str(P)}.bak')
80
81 lines_out = []
82 found_win = False
83 did_fix = False
84 for L in lines_in:
85 if did_fix:
86 lines_out.append( L )
87 continue
88 if re.search(r"\s*==\s*'win32':", L):
89 found_win = True
90 lines_out.append( L )
91 elif found_win and L.endswith("'geos_c.dll'))"):
92 did_fix = True
93 lines_out.append( f'#{L}' )
94 lines_out.append( L.replace('sys.prefix', f"r'{sys.prefix}'") )
95 else:
96 lines_out.append(L)
97
98 P.write_text('\n'.join(lines_out))
99 print(f'Wrote updated {str(P)}')
100# }}}
101def fix_img_tiles(): # {{{
102 """

Callers 1

mainFunction · 0.85

Calls 1

copyMethod · 0.80

Tested by

no test coverage detected