From 43b0f63893867f92ea837429a581b9584cdfd607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Grenotton?= Date: Mon, 6 Nov 2023 12:19:19 +0100 Subject: [PATCH] Allow polygon filtering of manual input Use manually fed input files in priority, still applying any provided polygon. Closes #28 --- pyhgtmap/NASASRTMUtil.py | 11 +- pyhgtmap/hgt/file.py | 2 +- pyhgtmap/main.py | 53 ++-- tests/data/france.poly | 497 ++++++++++++++++++++++++++++++++++++ tests/hgt/test_processor.py | 4 +- tests/test_main.py | 108 ++++++++ 6 files changed, 642 insertions(+), 33 deletions(-) create mode 100644 tests/data/france.poly create mode 100644 tests/test_main.py diff --git a/pyhgtmap/NASASRTMUtil.py b/pyhgtmap/NASASRTMUtil.py index 36ec62c..d00cce4 100644 --- a/pyhgtmap/NASASRTMUtil.py +++ b/pyhgtmap/NASASRTMUtil.py @@ -3,6 +3,7 @@ import base64 import os import sys +from typing import List, Optional, Tuple import urllib import zipfile from http import cookiejar as cookielib @@ -876,7 +877,13 @@ def getFile(opener, area, source): return downloadAndUnzip(opener, url, area, source) -def getFiles(area, polygon, corrx, corry, sources): +def getFiles( + area: str, + polygon: Optional[List[List[Tuple[float, float]]]], + corrx: float, + corry: float, + sources: List[str], +) -> List[Tuple[str, bool]]: initDirs(sources) bbox = calcBbox(area, corrx, corry) areaPrefixes = makeFileNamePrefixes(bbox, polygon, corrx, corry) @@ -898,7 +905,7 @@ def getFiles(area, polygon, corrx, corry, sources): return files -def anySRTMsources(sources): +def anySRTMsources(sources: List[str]) -> bool: """ Returns True if any of the given sources start with 'srtm', False otherwise. diff --git a/pyhgtmap/hgt/file.py b/pyhgtmap/hgt/file.py index 441fcaa..d28f349 100644 --- a/pyhgtmap/hgt/file.py +++ b/pyhgtmap/hgt/file.py @@ -282,7 +282,7 @@ def polygon_mask( maskArray = numpy.ma.array(numpy.empty((len(x_data) * len(y_data), 1))) for p in clipped_polygons: # run through all polygons and combine masks - mask = PolygonPath(p).contains_points(xyPoints) + mask = PolygonPath(p).contains_points(xyPoints) # type: ignore maskArray = numpy.ma.array(maskArray, mask=mask, keep_mask=True) return numpy.invert(maskArray.mask.reshape(len(y_data), len(x_data))) diff --git a/pyhgtmap/main.py b/pyhgtmap/main.py index 71ece17..72f24e6 100644 --- a/pyhgtmap/main.py +++ b/pyhgtmap/main.py @@ -4,7 +4,8 @@ import logging import os import sys -from optparse import OptionParser +from optparse import OptionParser, Values +from typing import List, Tuple from pyhgtmap import NASASRTMUtil, __version__, configUtil from pyhgtmap.hgt.file import calcHgtArea, parsePolygon @@ -16,7 +17,7 @@ logger = logging.getLogger(__name__) -def parseCommandLine(): +def parseCommandLine(sys_args: List[str]) -> Tuple[Values, List[str]]: """parses the command line.""" parser = OptionParser( usage="%prog [options] [] []" @@ -439,7 +440,7 @@ def parseCommandLine(): default="WARNING", help="Set this tool's debug logging level", ) - opts, args = parser.parse_args() + opts, args = parser.parse_args(sys_args) if opts.version: print("pyhgtmap {0:s}".format(__version__)) sys.exit(0) @@ -553,39 +554,35 @@ def parseCommandLine(): return opts, args -def main() -> None: - opts, args = parseCommandLine() +def main(sys_args: List[str]) -> None: + opts, args = parseCommandLine(sys_args) configure_logging(opts.logLevel) - if opts.area: + + hgtDataFiles: List[Tuple[str, bool]] + if args: + # Prefer using any manually provided source file + use_poly_flag = opts.polygon is not None + hgtDataFiles = [ + (arg, use_poly_flag) + for arg in args + if os.path.splitext(arg)[1].lower() in (".hgt", ".tif", ".tiff", ".vrt") + ] + opts.area = ":".join( + [str(i) for i in calcHgtArea(hgtDataFiles, opts.srtmCorrx, opts.srtmCorry)] + ) + else: + # Download from area or polygon logger.debug(f"Downloading HGT files for area {opts.area}") hgtDataFiles = NASASRTMUtil.getFiles( opts.area, opts.polygon, opts.srtmCorrx, opts.srtmCorry, opts.dataSource ) if len(hgtDataFiles) == 0: - if len(opts.dataSource) == 1: - print( - "No files for this area {0:s} from desired source.".format( - opts.area - ) - ) - else: - print( - "No files for this area {0:s} from desired sources.".format( - opts.area - ) - ) + print( + "No files for this area {0:s} from desired source(s).".format(opts.area) + ) sys.exit(0) elif opts.downloadOnly: sys.exit(0) - else: - hgtDataFiles = [ - (arg, False) - for arg in args - if os.path.splitext(arg)[1].lower() in (".hgt", ".tif", ".tiff", ".vrt") - ] - opts.area = ":".join( - [str(i) for i in calcHgtArea(hgtDataFiles, opts.srtmCorrx, opts.srtmCorry)] - ) HgtFilesProcessor(opts.nJobs, opts.startId, opts.startWayId, opts).process_files( hgtDataFiles @@ -593,4 +590,4 @@ def main() -> None: if __name__ == "__main__": - main() + main(sys.argv[1:]) diff --git a/tests/data/france.poly b/tests/data/france.poly new file mode 100644 index 0000000..7e04159 --- /dev/null +++ b/tests/data/france.poly @@ -0,0 +1,497 @@ +none +1 + 9.900000E+00 4.243788E+01 + 9.900000E+00 4.141346E+01 + 9.328765E+00 4.132062E+01 + 9.286847E+00 4.128319E+01 + 8.798805E+00 4.123866E+01 + 3.176237E+00 4.243371E+01 + 3.083413E+00 4.242015E+01 + 3.026463E+00 4.246648E+01 + 2.837709E+00 4.244490E+01 + 2.788530E+00 4.240826E+01 + 2.708677E+00 4.241289E+01 + 2.663866E+00 4.237779E+01 + 2.691517E+00 4.233398E+01 + 2.559084E+00 4.234963E+01 + 2.548566E+00 4.232755E+01 + 2.480866E+00 4.233811E+01 + 2.402161E+00 4.239066E+01 + 2.254490E+00 4.242562E+01 + 2.204567E+00 4.241458E+01 + 2.131985E+00 4.240938E+01 + 2.103023E+00 4.235347E+01 + 2.015824E+00 4.234351E+01 + 1.967745E+00 4.236930E+01 + 1.930762E+00 4.244482E+01 + 1.897025E+00 4.244444E+01 + 1.715368E+00 4.249186E+01 + 1.721935E+00 4.251842E+01 + 1.733308E+00 4.254018E+01 + 1.733994E+00 4.254989E+01 + 1.739960E+00 4.258620E+01 + 1.726074E+00 4.258690E+01 + 1.705100E+00 4.261127E+01 + 1.596991E+00 4.262443E+01 + 1.537782E+00 4.264797E+01 + 1.482483E+00 4.264068E+01 + 1.480280E+00 4.260966E+01 + 1.458178E+00 4.259845E+01 + 1.436179E+00 4.259784E+01 + 1.333942E+00 4.271582E+01 + 1.278561E+00 4.271122E+01 + 1.157990E+00 4.270307E+01 + 1.063964E+00 4.277432E+01 + 9.418060E-01 4.278718E+01 + 9.298620E-01 4.278272E+01 + 8.518980E-01 4.282326E+01 + 6.854070E-01 4.284880E+01 + 6.644810E-01 4.275647E+01 + 6.891700E-01 4.271239E+01 + 6.816990E-01 4.268734E+01 + 4.130770E-01 4.268797E+01 + 3.554180E-01 4.271399E+01 + 3.053230E-01 4.266539E+01 + 1.920030E-01 4.272941E+01 + 9.936000E-03 4.267894E+01 + -6.451100E-02 4.269136E+01 + -1.104370E-01 4.271887E+01 + -2.040790E-01 4.279020E+01 + -3.422410E-01 4.283365E+01 + -3.814320E-01 4.279723E+01 + -4.414800E-01 4.279259E+01 + -5.063150E-01 4.281662E+01 + -5.370930E-01 4.277334E+01 + -5.696100E-01 4.277920E+01 + -6.868150E-01 4.287147E+01 + -7.323500E-01 4.288842E+01 + -7.560540E-01 4.296415E+01 + -8.094670E-01 4.294895E+01 + -9.512650E-01 4.294996E+01 + -1.120291E+00 4.301295E+01 + -1.139513E+00 4.300476E+01 + -1.227202E+00 4.305215E+01 + -1.250002E+00 4.303704E+01 + -1.314324E+00 4.306835E+01 + -1.296915E+00 4.311313E+01 + -1.328943E+00 4.310377E+01 + -1.346643E+00 4.302447E+01 + -1.447267E+00 4.304550E+01 + -1.479677E+00 4.308436E+01 + -1.415645E+00 4.315543E+01 + -1.388447E+00 4.324896E+01 + -1.547573E+00 4.328599E+01 + -1.571828E+00 4.324545E+01 + -1.617394E+00 4.324945E+01 + -1.634897E+00 4.328298E+01 + -1.633187E+00 4.330369E+01 + -1.692238E+00 4.330744E+01 + -1.731377E+00 4.328945E+01 + -1.745110E+00 4.331444E+01 + -1.751805E+00 4.332992E+01 + -1.774807E+00 4.334128E+01 + -1.791630E+00 4.335299E+01 + -1.782704E+00 4.336300E+01 + -1.791115E+00 4.336756E+01 + -1.792231E+00 4.337667E+01 + -1.777023E+00 4.339261E+01 + -6.937207E+00 4.850183E+01 + -2.790533E+00 4.940025E+01 + -2.021490E+00 4.895137E+01 + -1.873172E+00 4.926781E+01 + -2.191778E+00 4.947170E+01 + -2.062684E+00 4.995917E+01 + 1.263308E+00 5.092504E+01 + 2.338938E+00 5.142880E+01 + 2.565332E+00 5.106589E+01 + 2.583292E+00 5.100078E+01 + 2.612142E+00 5.099230E+01 + 2.637097E+00 5.094448E+01 + 2.610007E+00 5.091976E+01 + 2.631189E+00 5.083253E+01 + 2.724426E+00 5.081543E+01 + 2.785034E+00 5.075253E+01 + 2.811623E+00 5.072165E+01 + 2.848185E+00 5.072487E+01 + 2.900805E+00 5.070449E+01 + 2.936352E+00 5.074634E+01 + 3.004094E+00 5.077965E+01 + 3.056090E+00 5.078272E+01 + 3.112746E+00 5.079655E+01 + 3.151858E+00 5.079197E+01 + 3.201828E+00 5.073580E+01 + 3.212998E+00 5.071590E+01 + 3.247236E+00 5.071366E+01 + 3.265700E+00 5.070288E+01 + 3.270985E+00 5.067452E+01 + 3.247681E+00 5.066492E+01 + 3.285786E+00 5.059875E+01 + 3.293582E+00 5.053136E+01 + 3.383281E+00 5.050011E+01 + 3.477201E+00 5.053878E+01 + 3.527813E+00 5.052325E+01 + 3.502235E+00 5.049618E+01 + 3.570041E+00 5.050262E+01 + 3.611669E+00 5.049798E+01 + 3.672405E+00 5.045270E+01 + 3.675957E+00 5.040370E+01 + 3.680763E+00 5.033713E+01 + 3.702368E+00 5.031903E+01 + 3.745572E+00 5.035710E+01 + 3.847269E+00 5.035704E+01 + 3.907037E+00 5.033462E+01 + 4.019507E+00 5.036707E+01 + 4.099844E+00 5.031431E+01 + 4.208602E+00 5.027597E+01 + 4.229065E+00 5.025562E+01 + 4.167066E+00 5.020974E+01 + 4.150909E+00 5.013764E+01 + 4.203708E+00 5.013668E+01 + 4.236622E+00 5.006639E+01 + 4.149156E+00 5.002673E+01 + 4.205988E+00 4.996177E+01 + 4.309461E+00 4.997151E+01 + 4.430317E+00 4.994678E+01 + 4.671075E+00 5.000730E+01 + 4.675420E+00 5.006663E+01 + 4.695282E+00 5.009427E+01 + 4.758854E+00 5.013708E+01 + 4.824017E+00 5.017007E+01 + 4.880676E+00 5.015491E+01 + 4.901517E+00 5.013981E+01 + 4.875612E+00 5.008635E+01 + 4.843484E+00 5.007472E+01 + 4.846397E+00 5.004192E+01 + 4.815237E+00 4.998043E+01 + 4.899263E+00 4.990950E+01 + 4.864919E+00 4.986129E+01 + 4.886018E+00 4.980282E+01 + 4.998090E+00 4.980340E+01 + 5.010602E+00 4.978463E+01 + 5.095318E+00 4.976647E+01 + 5.130979E+00 4.972719E+01 + 5.166264E+00 4.971575E+01 + 5.172503E+00 4.969729E+01 + 5.269212E+00 4.969903E+01 + 5.330195E+00 4.966086E+01 + 5.342210E+00 4.963741E+01 + 5.418881E+00 4.961136E+01 + 5.464153E+00 4.955985E+01 + 5.473374E+00 4.950552E+01 + 5.557300E+00 4.953101E+01 + 5.603620E+00 4.952331E+01 + 5.648722E+00 4.955716E+01 + 5.699646E+00 4.954849E+01 + 5.771656E+00 4.956579E+01 + 5.838375E+00 4.954381E+01 + 5.867901E+00 4.950504E+01 + 5.941887E+00 4.950169E+01 + 5.973558E+00 4.949230E+01 + 5.976176E+00 4.947978E+01 + 5.977464E+00 4.946416E+01 + 5.989265E+00 4.946394E+01 + 5.988972E+00 4.945838E+01 + 6.043339E+00 4.945518E+01 + 6.055183E+00 4.946723E+01 + 6.096210E+00 4.946628E+01 + 6.101789E+00 4.947248E+01 + 6.119814E+00 4.947560E+01 + 6.126766E+00 4.949444E+01 + 6.168823E+00 4.951145E+01 + 6.242466E+00 4.951585E+01 + 6.269760E+00 4.950565E+01 + 6.280489E+00 4.950381E+01 + 6.282334E+00 4.950074E+01 + 6.289630E+00 4.949458E+01 + 6.300230E+00 4.948853E+01 + 6.353059E+00 4.946768E+01 + 6.364946E+00 4.946087E+01 + 6.367092E+00 4.947069E+01 + 6.391210E+00 4.946600E+01 + 6.420994E+00 4.947794E+01 + 6.430521E+00 4.947761E+01 + 6.455069E+00 4.946595E+01 + 6.466141E+00 4.946818E+01 + 6.486826E+00 4.945395E+01 + 6.499958E+00 4.945133E+01 + 6.538238E+00 4.943654E+01 + 6.562271E+00 4.941957E+01 + 6.540213E+00 4.941136E+01 + 6.552915E+00 4.940025E+01 + 6.565189E+00 4.939109E+01 + 6.581840E+00 4.939249E+01 + 6.589565E+00 4.938416E+01 + 6.587505E+00 4.937729E+01 + 6.604156E+00 4.936717E+01 + 6.590595E+00 4.934923E+01 + 6.573257E+00 4.934386E+01 + 6.592398E+00 4.933704E+01 + 6.596861E+00 4.932982E+01 + 6.591625E+00 4.932205E+01 + 6.625700E+00 4.930408E+01 + 6.651449E+00 4.928925E+01 + 6.671533E+00 4.928102E+01 + 6.669989E+00 4.926988E+01 + 6.666298E+00 4.926450E+01 + 6.694021E+00 4.924865E+01 + 6.689300E+00 4.924366E+01 + 6.695652E+00 4.921709E+01 + 6.720886E+00 4.922365E+01 + 6.734762E+00 4.920596E+01 + 6.713848E+00 4.918799E+01 + 6.724834E+00 4.917564E+01 + 6.739940E+00 4.916700E+01 + 6.782341E+00 4.917037E+01 + 6.835384E+00 4.915398E+01 + 6.844997E+00 4.915712E+01 + 6.843195E+00 4.916335E+01 + 6.843023E+00 4.917323E+01 + 6.859252E+00 4.917885E+01 + 6.856842E+00 4.918490E+01 + 6.848688E+00 4.919309E+01 + 6.847143E+00 4.920218E+01 + 6.833668E+00 4.921266E+01 + 6.856204E+00 4.922345E+01 + 6.939411E+00 4.922399E+01 + 6.958809E+00 4.920526E+01 + 6.973743E+00 4.921143E+01 + 7.014341E+00 4.919477E+01 + 7.037086E+00 4.919225E+01 + 7.035284E+00 4.918030E+01 + 7.029447E+00 4.917205E+01 + 7.034855E+00 4.916431E+01 + 7.048931E+00 4.913719E+01 + 7.045841E+00 4.912180E+01 + 7.048588E+00 4.911624E+01 + 7.055712E+00 4.911326E+01 + 7.065668E+00 4.911557E+01 + 7.067470E+00 4.912220E+01 + 7.082062E+00 4.913175E+01 + 7.078714E+00 4.914854E+01 + 7.081032E+00 4.915381E+01 + 7.101116E+00 4.915684E+01 + 7.107639E+00 4.915606E+01 + 7.113819E+00 4.915280E+01 + 7.128067E+00 4.914314E+01 + 7.137337E+00 4.912933E+01 + 7.166519E+00 4.913051E+01 + 7.183170E+00 4.913231E+01 + 7.206087E+00 4.912534E+01 + 7.245312E+00 4.913124E+01 + 7.300157E+00 4.912355E+01 + 7.312603E+00 4.913893E+01 + 7.329855E+00 4.914663E+01 + 7.359896E+00 4.914522E+01 + 7.360496E+00 4.917323E+01 + 7.447786E+00 4.918653E+01 + 7.448387E+00 4.918350E+01 + 7.439632E+00 4.917783E+01 + 7.447100E+00 4.917048E+01 + 7.496624E+00 4.917082E+01 + 7.510271E+00 4.915297E+01 + 7.497911E+00 4.913759E+01 + 7.507439E+00 4.912529E+01 + 7.518511E+00 4.912332E+01 + 7.533102E+00 4.909882E+01 + 7.603397E+00 4.908466E+01 + 7.629604E+00 4.907796E+01 + 7.641334E+00 4.905576E+01 + 7.696609E+00 4.905992E+01 + 7.762356E+00 4.905452E+01 + 7.794199E+00 4.906746E+01 + 7.812567E+00 4.906189E+01 + 7.863636E+00 4.903967E+01 + 7.887840E+00 4.905058E+01 + 7.933797E+00 4.905955E+01 + 7.946634E+00 4.905604E+01 + 7.951140E+00 4.904769E+01 + 7.967405E+00 4.904338E+01 + 7.979164E+00 4.902982E+01 + 7.999206E+00 4.902937E+01 + 8.049717E+00 4.901536E+01 + 8.064694E+00 4.900635E+01 + 8.071046E+00 4.899776E+01 + 8.123660E+00 4.898712E+01 + 8.135805E+00 4.898154E+01 + 8.138165E+00 4.897993E+01 + 8.169966E+00 4.897968E+01 + 8.184986E+00 4.897879E+01 + 8.196405E+00 4.897762E+01 + 8.226226E+00 4.897706E+01 + 8.235465E+00 4.896555E+01 + 8.195738E+00 4.894824E+01 + 8.137092E+00 4.888063E+01 + 8.124046E+00 4.881851E+01 + 8.074952E+00 4.878890E+01 + 8.017317E+00 4.875802E+01 + 7.977616E+00 4.873705E+01 + 7.966976E+00 4.871237E+01 + 7.907307E+00 4.865820E+01 + 7.860343E+00 4.864026E+01 + 7.805786E+00 4.857558E+01 + 7.813511E+00 4.852479E+01 + 7.808018E+00 4.850762E+01 + 7.783813E+00 4.848601E+01 + 7.780554E+00 4.847652E+01 + 7.746219E+00 4.838795E+01 + 7.752914E+00 4.832179E+01 + 7.730419E+00 4.830298E+01 + 7.699444E+00 4.823911E+01 + 7.609234E+00 4.815406E+01 + 7.602402E+00 4.813405E+01 + 7.581696E+00 4.811462E+01 + 7.595516E+00 4.809649E+01 + 7.575647E+00 4.803242E+01 + 7.616648E+00 4.799853E+01 + 7.628959E+00 4.797058E+01 + 7.584643E+00 4.789773E+01 + 7.567523E+00 4.784391E+01 + 7.533792E+00 4.777697E+01 + 7.551910E+00 4.774231E+01 + 7.554834E+00 4.772587E+01 + 7.522382E+00 4.770202E+01 + 7.522266E+00 4.767038E+01 + 7.568069E+00 4.763648E+01 + 7.599707E+00 4.760269E+01 + 7.586918E+00 4.757479E+01 + 7.568657E+00 4.757537E+01 + 7.559186E+00 4.756450E+01 + 7.537093E+00 4.755492E+01 + 7.518425E+00 4.754467E+01 + 7.499885E+00 4.753772E+01 + 7.503405E+00 4.753070E+01 + 7.525206E+00 4.753743E+01 + 7.533359E+00 4.752856E+01 + 7.524090E+00 4.751361E+01 + 7.512760E+00 4.750990E+01 + 7.512245E+00 4.749633E+01 + 7.490101E+00 4.748136E+01 + 7.458000E+00 4.746802E+01 + 7.399463E+00 4.742727E+01 + 7.324276E+00 4.743761E+01 + 7.244453E+00 4.741798E+01 + 7.166175E+00 4.744156E+01 + 7.180611E+00 4.748424E+01 + 7.123379E+00 4.749519E+01 + 7.073582E+00 4.748623E+01 + 7.017036E+00 4.750156E+01 + 6.990120E+00 4.749177E+01 + 7.008271E+00 4.745709E+01 + 6.996815E+00 4.744629E+01 + 6.923513E+00 4.739414E+01 + 6.886725E+00 4.735942E+01 + 7.045077E+00 4.737892E+01 + 7.071103E+00 4.733632E+01 + 6.955017E+00 4.726808E+01 + 6.959622E+00 4.724318E+01 + 6.859946E+00 4.716892E+01 + 6.863693E+00 4.715944E+01 + 6.762511E+00 4.710584E+01 + 6.724583E+00 4.705046E+01 + 6.636254E+00 4.699539E+01 + 6.477084E+00 4.694731E+01 + 6.450383E+00 4.692278E+01 + 6.477536E+00 4.687973E+01 + 6.447514E+00 4.680017E+01 + 6.480897E+00 4.678110E+01 + 6.122464E+00 4.657550E+01 + 6.166862E+00 4.654304E+01 + 6.075330E+00 4.641927E+01 + 6.177486E+00 4.637396E+01 + 6.126079E+00 4.631516E+01 + 6.133713E+00 4.630227E+01 + 6.133461E+00 4.625050E+01 + 6.109857E+00 4.622902E+01 + 6.097746E+00 4.622131E+01 + 6.059406E+00 4.623123E+01 + 6.052694E+00 4.622646E+01 + 6.034452E+00 4.623721E+01 + 5.994110E+00 4.621453E+01 + 5.965748E+00 4.619712E+01 + 5.992552E+00 4.618791E+01 + 5.997395E+00 4.617999E+01 + 5.967121E+00 4.614517E+01 + 5.987793E+00 4.614547E+01 + 6.044961E+00 4.614189E+01 + 6.050575E+00 4.615224E+01 + 6.091467E+00 4.615266E+01 + 6.111574E+00 4.614398E+01 + 6.148910E+00 4.615088E+01 + 6.187448E+00 4.616634E+01 + 6.183071E+00 4.617846E+01 + 6.205730E+00 4.619278E+01 + 6.233110E+00 4.620752E+01 + 6.294291E+00 4.622640E+01 + 6.307440E+00 4.624599E+01 + 6.291475E+00 4.625745E+01 + 6.266756E+00 4.624356E+01 + 6.234226E+00 4.627501E+01 + 6.245384E+00 4.630206E+01 + 6.211309E+00 4.631398E+01 + 6.264181E+00 4.638458E+01 + 6.624293E+00 4.649704E+01 + 6.836175E+00 4.645196E+01 + 6.804248E+00 4.638968E+01 + 6.808963E+00 4.638216E+01 + 6.793157E+00 4.634152E+01 + 6.869543E+00 4.628553E+01 + 6.858857E+00 4.625292E+01 + 6.810446E+00 4.620361E+01 + 6.822936E+00 4.613691E+01 + 6.904849E+00 4.612709E+01 + 6.940736E+00 4.606656E+01 + 7.094688E+00 4.591969E+01 + 6.940965E+00 4.583551E+01 + 6.820030E+00 4.581591E+01 + 6.829337E+00 4.571123E+01 + 7.012493E+00 4.564465E+01 + 7.009821E+00 4.552013E+01 + 7.110975E+00 4.547131E+01 + 7.192060E+00 4.540674E+01 + 7.139578E+00 4.530455E+01 + 7.144923E+00 4.525531E+01 + 7.080046E+00 4.520884E+01 + 6.980034E+00 4.520287E+01 + 6.951246E+00 4.516805E+01 + 6.895852E+00 4.513346E+01 + 6.847371E+00 4.512460E+01 + 6.786350E+00 4.515017E+01 + 6.639546E+00 4.509804E+01 + 6.664861E+00 4.507298E+01 + 6.681253E+00 4.502691E+01 + 6.753322E+00 4.502224E+01 + 6.787871E+00 4.489831E+01 + 6.909210E+00 4.485059E+01 + 6.930832E+00 4.486853E+01 + 7.027325E+00 4.483719E+01 + 7.043637E+00 4.472277E+01 + 7.069489E+00 4.471579E+01 + 7.082193E+00 4.467768E+01 + 6.984578E+00 4.468155E+01 + 6.954428E+00 4.465258E+01 + 6.976129E+00 4.462322E+01 + 6.929081E+00 4.456146E+01 + 6.859932E+00 4.452980E+01 + 6.954603E+00 4.442810E+01 + 6.901817E+00 4.437246E+01 + 7.001151E+00 4.428406E+01 + 7.009620E+00 4.424643E+01 + 7.100105E+00 4.422949E+01 + 7.188148E+00 4.420436E+01 + 7.296657E+00 4.414937E+01 + 7.347362E+00 4.414755E+01 + 7.360254E+00 4.412652E+01 + 7.623408E+00 4.416728E+01 + 7.628561E+00 4.417919E+01 + 7.689757E+00 4.418558E+01 + 7.680372E+00 4.412350E+01 + 7.731295E+00 4.407925E+01 + 7.651235E+00 4.396522E+01 + 7.580754E+00 4.394251E+01 + 7.565829E+00 4.389753E+01 + 7.503786E+00 4.387040E+01 + 7.534675E+00 4.377821E+01 + 9.810986E+00 4.320419E+01 + 9.900000E+00 4.243788E+01 +END +END diff --git a/tests/hgt/test_processor.py b/tests/hgt/test_processor.py index 7bce145..9616956 100644 --- a/tests/hgt/test_processor.py +++ b/tests/hgt/test_processor.py @@ -332,7 +332,7 @@ def test_way_id_overflow(default_options: SimpleNamespace) -> None: @staticmethod def test_process_tile_internal_empty_contour( - default_options: SimpleNamespace, caplog + default_options: SimpleNamespace, caplog: pytest.LogCaptureFixture ) -> None: """Ensure no empty output file is generated when there's no contour.""" processor = HgtFilesProcessor( @@ -345,7 +345,7 @@ def test_process_tile_internal_empty_contour( tile_mock.__str__.return_value = "Tile (28.00, 42.50, 29.00, 43.00)" # type: ignore with tempfile.TemporaryDirectory() as tempdir_name: with cwd(tempdir_name): - caplog.set_level(logging.INFO) + caplog.set_level(logging.INFO, logger="pyhgtmap.hgt.processor") processor.process_tile_internal("empty.pbf", tile_mock) # NO file must be generated assert not os.path.exists("empty.pbf") diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..92c7a42 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,108 @@ +import optparse +import os +from unittest.mock import patch +from pyhgtmap import main + +from . import TEST_DATA_PATH + + +@patch("pyhgtmap.main.NASASRTMUtil") +@patch("pyhgtmap.main.HgtFilesProcessor") +def test_main_download_from_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None: + """Only polygon option is used, without files; download tiles.""" + # Prepare + sys_args = [ + "--pbf", + "--source=view1", + f"--polygon={os.path.join(TEST_DATA_PATH, 'france.poly')}", + ] + NASASRTMUtil_mock.getFiles.return_value = [ + ("hgt/VIEW1/N45E006.hgt", True), + ("hgt/VIEW1/N46E006.hgt", True), + ] + + # Test + main.main(sys_args) + + # Check + NASASRTMUtil_mock.getFiles.assert_called_once() + assert ( + NASASRTMUtil_mock.getFiles.call_args[0][0] + == "-6.9372070:41.2386600:9.9000000:51.4288000" + ) + assert NASASRTMUtil_mock.getFiles.call_args[0][1][0][0:5] == [ + (9.9, 42.43788), + (9.9, 41.41346), + (9.328765, 41.32062), + (9.286847, 41.28319), + (8.798805, 41.23866), + ] + assert NASASRTMUtil_mock.getFiles.call_args[0][2] == 0 + assert NASASRTMUtil_mock.getFiles.call_args[0][3] == 0 + assert NASASRTMUtil_mock.getFiles.call_args[0][4] == ["view1"] + + HgtFilesProcessor_mock.assert_called_once() + parsed_options: optparse.Values = HgtFilesProcessor_mock.call_args.args[3] + assert parsed_options.area == "-6.9372070:41.2386600:9.9000000:51.4288000" + + HgtFilesProcessor_mock.return_value.process_files.assert_called_once_with( + [("hgt/VIEW1/N45E006.hgt", True), ("hgt/VIEW1/N46E006.hgt", True)] + ) + + +@patch("pyhgtmap.main.NASASRTMUtil") +@patch("pyhgtmap.main.HgtFilesProcessor") +def test_main_manual_input_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None: + """Polygon option is used, with manual files; polygon must be applied to files.""" + # Prepare + sys_args = [ + "--pbf", + "--source=view1", + f"--polygon={os.path.join(TEST_DATA_PATH, 'france.poly')}", + "N45E007.hgt", + "N46E007.hgt", + "N47E007.hgt", + ] + + # Test + main.main(sys_args) + + # Check + NASASRTMUtil_mock.getFiles.assert_not_called() + + HgtFilesProcessor_mock.assert_called_once() + parsed_options: optparse.Values = HgtFilesProcessor_mock.call_args.args[3] + # area must be properly computed from files names + assert parsed_options.area == "7:45:8:48" + # Polygon check must be enabled for all files + HgtFilesProcessor_mock.return_value.process_files.assert_called_once_with( + [("N45E007.hgt", True), ("N46E007.hgt", True), ("N47E007.hgt", True)] + ) + + +@patch("pyhgtmap.main.NASASRTMUtil") +@patch("pyhgtmap.main.HgtFilesProcessor") +def test_main_manual_input_no_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None: + """Polygon option is NOT used, with manual files.""" + # Prepare + sys_args = [ + "--pbf", + "N45E007.hgt", + "N46E007.hgt", + "N47E007.hgt", + ] + + # Test + main.main(sys_args) + + # Check + NASASRTMUtil_mock.getFiles.assert_not_called() + + HgtFilesProcessor_mock.assert_called_once() + parsed_options: optparse.Values = HgtFilesProcessor_mock.call_args.args[3] + # area must be properly computed from files names + assert parsed_options.area == "7:45:8:48" + # Polygon check must NOT be enabled for any files + HgtFilesProcessor_mock.return_value.process_files.assert_called_once_with( + [("N45E007.hgt", False), ("N46E007.hgt", False), ("N47E007.hgt", False)] + )