Skip to content

Commit

Permalink
Fix compilation of addon manuals for Windows (#524)
Browse files Browse the repository at this point in the history
* fix indent error

* lazy import gdal

* lazy import gdal

* lazy import requests and pandas

* use fatal/warning

* lazy import addon libs

* Py3 fix for UserDict

* lazy import gdal

* lazy import gdal

* lazy import requests and pandas

* use fatal/warning

* lazy import addon libs

* Py3 fix for UserDict

* rebase

* rebase

* flake8

* black

* black

Co-authored-by: Markus Neteler <neteler@gmail.com>
  • Loading branch information
ninsbl and neteler committed Jun 24, 2021
1 parent fdf8c90 commit 54010b4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 54 deletions.
13 changes: 10 additions & 3 deletions grass7/general/g.isis3mt/g.isis3mt.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@
import sys
import os
import platform
import re
import string
import grass.script as grass
from grass.lib import gis as grasslib
from grass.lib import proj as grassproj
from UserDict import *
import re
import string

# UserDict is merged into collections http://python3porting.com/problems.html
if sys.version_info > (3,):
from collections import UserDict

IterableUserDict = UserDict
else:
from UserDict import UserDict, IterableUserDict

if "GISBASE" not in os.environ:
print("You must be in GRASS GIS to run this program.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@
from grass.script.utils import get_lib_path
import grass.script.array as garray

from osgeo import gdal, osr

path = get_lib_path(modname="maskrcnn", libname="model")
if path is None:
gscript.fatal("Not able to find the maskrcnn library directory.")
Expand All @@ -99,6 +97,12 @@

def main(options, flags):

# Lazy import GDAL python bindings
try:
from osgeo import gdal, osr
except ImportError as e:
gscript.fatal(_("Module requires GDAL python bindings: {}").format(e))

import model as modellib
from config import ModelConfig

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@
import fnmatch
import hashlib
import os
import requests
import xml.etree.ElementTree as ET
import shutil
import sys
Expand All @@ -174,16 +173,6 @@

import grass.script as gs

try:
import pandas
except ImportError as e:
gs.fatal(_("Module requires pandas library: {}").format(e))

try:
from tqdm import tqdm
except ImportError as e:
gs.fatal(_("Module requires tqdm library: {}").format(e))


def create_dir(dir):
if not os.path.isdir(dir):
Expand Down Expand Up @@ -425,6 +414,12 @@ def download_gcs_file(url, destination, checksum_function, checksum):

def download_gcs(scene, output):
"""Downloads a single S2 scene from Google Cloud Storage."""
# Lazy import tqdm
try:
from tqdm import tqdm
except ImportError as e:
gs.fatal(_("Module requires tqdm library: {}").format(e))

final_scene_dir = os.path.join(output, "{}.SAFE".format(scene))
create_dir(final_scene_dir)
level = scene.split("_")[1]
Expand Down Expand Up @@ -1027,6 +1022,18 @@ def filter_USGS(


def main():

# Lazy import nonstandard modules
try:
import pandas
except ImportError as e:
gs.fatal(_("Module requires pandas library: {}").format(e))

try:
import requests
except ImportError as e:
gs.fatal(_("Module requires requests library: {}").format(e))

user = password = None
if options["datasource"] == "ESA_COAH" or options["datasource"] == "GCS":
api_url = "https://apihub.copernicus.eu/apihub"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@
from grass.pygrass.vector.geometry import Centroid
from grass.pygrass.vector.geometry import Point
from grass.pygrass.vector.geometry import Line
from osgeo import ogr

# check if GRASS is running or not
if "GISBASE" not in os.environ:
Expand Down Expand Up @@ -265,6 +264,12 @@ def cleanup():
def main():
"""Do the main processing"""

# Lazy import GDAL python bindings
try:
from osgeo import gdal, osr
except ImportError as e:
grass.fatal(_("Module requires GDAL python bindings: {}").format(e))

# Parse input options:
patch_map = options["input"]
patches = patch_map.split("@")[0]
Expand Down
49 changes: 22 additions & 27 deletions grass7/raster/r.flowfill/r.flowfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ def main():
try:
from netCDF4 import Dataset
except:
g.message(
flags="e",
message=(
g.fatal(
_(
"netCDF4 not detected. Install pip3 and "
+ "then type at the command prompt: "
+ '"pip3 install netCDF4".'
),
"then type at the command prompt: "
'"pip3 install netCDF4".'
)
)

options, flags = gscript.parser()
Expand Down Expand Up @@ -167,31 +166,26 @@ def main():
_rasters = np.array(gscript.parse_command("g.list", type="raster").keys())
if (_rasters == _output).any() or (_water == _output).any():
if gscript.overwrite() is False:
g.message(flags="e", message="output would overwrite " + _output)
g.fatal(_("output would overwrite " + _output))

# Check for proper number of processors
try:
_np = int(_np)
except:
g.message(flags="e", message="Number of processors must be an integer.")
g.fatal(_("Number of processors must be an integer."))

if _np < 3:
g.message(flags="e", message="FlowFill requires 3 or more processors.")
g.fatal(_("FlowFill requires 3 or more processors."))

# Check for proper option set
if _h_runoff is not "": # ????? possible ?????
if _h_runoff_raster is not "":
g.message(
flags="e",
message='Only one of "h_runoff" and ' + '"h_runoff_raster" may be set',
)
g.fatal(_('Only one of "h_runoff" and "h_runoff_raster" may be set'))
elif _h_runoff_raster is "":
g.message(
flags="e", message='Either "h_runoff" or ' + '"h_runoff_raster" must be set'
)
g.fatal(_('Either "h_runoff" or "h_runoff_raster" must be set'))

if _output is "" and _water is "":
g.message(flags="w", message="No output is set.")
g.warning(_("No output is set."))

# Set up runoff options
if _h_runoff_raster is not "":
Expand Down Expand Up @@ -282,16 +276,17 @@ def main():
popen.stdout.close()
if _mpirun_error_flag:
print("")
g.message(
flags="e",
message="FlowFill executable not found.\n"
+ "If you have not installed FlowFill, please download it "
+ "from https://github.com/KCallaghan/FlowFill, "
+ "and follow the directions in the README to compile and "
+ "install it on your system.\n"
+ 'This should then work with the default "ffpath". '
+ "Otherwise, you may have simply have typed in an incorrect "
+ '"ffpath".',
g.fatal(
_(
"FlowFill executable not found.\n"
"If you have not installed FlowFill, please download it "
"from https://github.com/KCallaghan/FlowFill, "
"and follow the directions in the README to compile and "
"install it on your system.\n"
'This should then work with the default "ffpath". '
"Otherwise, you may have simply have typed in an incorrect "
'"ffpath".'
)
)

# _stdout = subprocess.Popen(mpirunstr, shell=True, stdout=subprocess.PIPE)
Expand Down
22 changes: 12 additions & 10 deletions grass7/raster/r.learn.ml2/r.learn.train/r.learn.train.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,6 @@

gs.utils.set_path(modulename="r.learn.ml2", dirname="rlearnlib", path="..")

from rlearnlib.utils import (
predefined_estimators,
load_training_data,
save_training_data,
option_to_list,
scoring_metrics,
check_class_weights,
)
from rlearnlib.raster import RasterStack

tmp_rast = []


Expand Down Expand Up @@ -440,6 +430,18 @@ def process_param_grid(hyperparams):


def main():

# Lazy import libraries
from rlearnlib.utils import (
predefined_estimators,
load_training_data,
save_training_data,
option_to_list,
scoring_metrics,
check_class_weights,
)
from rlearnlib.raster import RasterStack

try:
import sklearn

Expand Down

0 comments on commit 54010b4

Please sign in to comment.