Skip to content

Commit 1d87c5a

Browse files
committed
WIP: try be a bit cleaner on selecting project dir
still not working. Project Path is empty even when launched from an active project.
1 parent 126ede1 commit 1d87c5a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

gis/qgis/h3-grid-from-layer.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
debug = False
4444

45+
def log(item):
46+
return QgsMessageLog.logMessage(str(item))
47+
4548
###---------- Edit these variables ----------
4649
# Min & max h3 resolution levels, from 0 to 15 (global to sub-meter)
4750
# High resolutions over broad areas can be slow and consume a lot of storage space
@@ -58,16 +61,25 @@
5861
# --------------------------------------------
5962

6063
projectPath = os.path.dirname(QgsProject.instance().fileName())
64+
if not projectPath:
65+
# fallback if project not saved
66+
projectPath = os.environ.get("TEMP", "/tmp")
67+
else:
68+
projectPath = os.path.dirname(projectPath)
69+
6170
geo_csrs = QgsCoordinateReferenceSystem(geographic_coordsys)
6271
out_csrs = QgsCoordinateReferenceSystem(output_projection)
6372

64-
dataPath = os.path.abspath(os.path.join(projectPath, "data")) # use abspath and remove trailing slash
73+
if not projectPath:
74+
projectPath = os.environ["TEMP"]
75+
log(f"No projectPath defined, using TEMP: {projectPath}")
6576

77+
dataPath = os.path.abspath(os.path.join(projectPath, "data")) # use abspath and remove trailing slash
6678
try:
6779
if not os.path.exists(dataPath):
6880
os.makedirs(dataPath, exist_ok=True) # use makedirs instead of mkdir
6981
except PermissionError:
70-
# If we can't create in project directory, fall back to temp
82+
log(f"Can't create '{dataPath}' falling back to TEMP")
7183
dataPath = os.path.abspath(os.path.join(os.environ["TEMP"], "data"))
7284
os.makedirs(dataPath, exist_ok=True)
7385

@@ -80,10 +92,6 @@
8092
QgsProject.instance().addMapLayer(mylayer)
8193

8294

83-
def log(item):
84-
return QgsMessageLog.logMessage(str(item))
85-
86-
8795
def proj_to_geo(in_layer):
8896
"""Project to geographic coordinate system, in memory.
8997
H3 needs all coordinates in decimal degrees"""

0 commit comments

Comments
 (0)