Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow polygon filtering of manual input #29

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pyhgtmap/NASASRTMUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pyhgtmap/hgt/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
53 changes: 25 additions & 28 deletions pyhgtmap/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] [<hgt or GeoTiff file>] [<hgt or GeoTiff files>]"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -553,44 +554,40 @@ 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
)


if __name__ == "__main__":
main()
main(sys.argv[1:])
Loading