User:Edamame/PatchPhyghtmapHgtDir

From OpenStreetMap Wiki
Jump to: navigation, search

A patch to phyghtmap 1.43-1 for support of hgt cache directory by command line option

Original source: http://katze.tfiu.de/projects/phyghtmap/

Changes

  • Add command line option --hgtdir to specify hgt file cache directory
  • To achieve this use a configuration object NASASRTMUtilConfig inside NASASRTMUtil.py

Patch created with git format-patch

From cadefaef78334a1f531962c2cac1ad11b8ee20b2 Mon Sep 17 00:00:00 2001
From: Edamame
Date: Sun, 26 Aug 2012 15:34:46 +0200
Subject: [PATCH 1/2] Add command line option --hgtdir to specify hgt file
 cache directory

To achieve this use a configuration object NASASRTMUtilConfig
insie NASASRTMUtil.py
---
 usr/share/pyshared/phyghtmap/NASASRTMUtil.py |  104 +++++++++++++++-----------
 usr/share/pyshared/phyghtmap/main.py         |   22 ++++--
 2 files changed, 76 insertions(+), 50 deletions(-)

diff --git a/usr/share/pyshared/phyghtmap/NASASRTMUtil.py b/usr/share/pyshared/phyghtmap/NASASRTMUtil.py
index d1bb4c4..d4ad8b6 100644
--- a/usr/share/pyshared/phyghtmap/NASASRTMUtil.py
+++ b/usr/share/pyshared/phyghtmap/NASASRTMUtil.py
@@ -5,32 +5,50 @@ import zipfile
 from matplotlib.nxutils import points_inside_poly
 import numpy
 
-############################################################
-### general vriables #######################################
-############################################################
 
-hgtSaveDir = "hgt"
-
-############################################################
-### NASA SRTM specific variables ###########################
-############################################################
-
-NASAhgtFileServerRe = "http://dds.cr.usgs.gov/srtm/version2_1/SRTM%s"
-NASAhgtFileDirs = {3: ["Africa", "Australia", "Eurasia", "Islands", "North_America",
-               "South_America"],
-               1: ["Region_0%i"%i for i in range(1, 8)]}
-
-NASAhgtSaveSubDirRe = "SRTM%i"
-NASAhgtIndexFileRe = os.path.join(hgtSaveDir, "hgtIndex_%i.txt")
-
-############################################################
-### www.vierfinderpanoramas.org specific variables #########
-############################################################
-
-VIEWfileDictPageRe = "http://www.viewfinderpanoramas.org/Coverage%%20map%%20viewfinderpanoramas_org%i.htm"
-
-VIEWhgtSaveSubDirRe = "VIEW%i"
-VIEWhgtIndexFileRe = os.path.join(hgtSaveDir, "viewfinderHgtIndex_%i.txt")
+class NASASRTMUtilConfigClass():
+   """The config is stored in a class, to be configurable from outside
+
+   Don't change configuration during usage, only at the beginning!
+   You can use the member call CustomHgtSaveDir for configuration from the outside:
+   NASASRTMUtil.NASASRTMUtilConfig.CustomHgtSaveDir(custom_hgt_directory)
+   """
+
+   # C'Tor setting the defaults
+   def __init__(self):
+       # Set the default ght directory
+       self.CustomHgtSaveDir("hgt")
+       # Other config
+       ############################################################
+       ### NASA SRTM specific variables ###########################
+       ############################################################
+       self.NASAhgtFileServerRe = "http://dds.cr.usgs.gov/srtm/version2_1/SRTM%s"
+       self.NASAhgtFileDirs = {3: ["Africa", "Australia", "Eurasia", "Islands", "North_America",
+                              "South_America"],
+                              1: ["Region_0%i"%i for i in range(1, 8)]}
+       self.NASAhgtSaveSubDirRe = "SRTM%i"
+       ############################################################
+       ### www.vierfinderpanoramas.org specific variables #########
+       ############################################################
+       self.VIEWfileDictPageRe = "http://www.viewfinderpanoramas.org/Coverage%%20map%%20viewfinderpanoramas_org%i.htm"
+       self.VIEWhgtSaveSubDirRe = "VIEW%i"
+
+
+   def CustomHgtSaveDir(self, dir):
+       """Set a custom directory to store the hgt files
+
+       Argument dir: Directory to use
+       """
+       ############################################################
+       ### general config variables ###############################
+       ############################################################
+       # Default value
+       self.hgtSaveDir = dir
+       self.NASAhgtIndexFileRe = os.path.join(self.hgtSaveDir, "hgtIndex_%i.txt")
+       self.VIEWhgtIndexFileRe = os.path.join(self.hgtSaveDir, "viewfinderHgtIndex_%i.txt")
+
+# Create the config object
+NASASRTMUtilConfig = NASASRTMUtilConfigClass()
 
 texAreas = []
 
@@ -236,15 +254,15 @@ def makeFileNames(bbox, polygon, corrx, corry, resolution, viewfinder):
 def makeNasaHgtIndex(resolution):
 	"""generates an index file for the NASA SRTM server.
 	"""
-	hgtIndexFile = NASAhgtIndexFileRe%resolution
-	hgtFileServer = NASAhgtFileServerRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.NASAhgtIndexFileRe%resolution
+	hgtFileServer = NASASRTMUtilConfig.NASAhgtFileServerRe%resolution
 	print "generating index in %s ..."%hgtIndexFile, 
 	try:
 		index = open(hgtIndexFile, 'w')
 	except:
 		print ""
 		raise IOError("could not open %s for writing"%hgtIndexFile)
-	for continent in NASAhgtFileDirs[resolution]:
+	for continent in NASASRTMUtilConfig.NASAhgtFileDirs[resolution]:
 		index.write("[%s]\n"%continent)
 		url = "/".join([hgtFileServer, continent])
 		continentHtml = urllib.urlopen(url)
@@ -257,7 +275,7 @@ def makeNasaHgtIndex(resolution):
 	print "DONE"
 
 def writeViewIndex(resolution, zipFileDict):
-	hgtIndexFile = VIEWhgtIndexFileRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.VIEWhgtIndexFileRe%resolution
 	try:
 		index = open(hgtIndexFile, 'w')
 	except:
@@ -271,7 +289,7 @@ def writeViewIndex(resolution, zipFileDict):
 	print "DONE"
 
 def inViewIndex(resolution, areaName):
-	hgtIndexFile = VIEWhgtIndexFileRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.VIEWhgtIndexFileRe%resolution
 	index = [line.strip() for line in open(hgtIndexFile, 'r').read().split("\n")
 		if line.strip()]
 	areaNames = [a for a in index if not a.startswith("[")]
@@ -305,9 +323,9 @@ def makeViewHgtIndex(resolution):
 				names.append(name)
 		return names
 
-	hgtIndexFile = VIEWhgtIndexFileRe%resolution
-	hgtFileServer = NASAhgtFileServerRe%resolution
-	hgtDictUrl = VIEWfileDictPageRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.VIEWhgtIndexFileRe%resolution
+	hgtFileServer = NASASRTMUtilConfig.NASAhgtFileServerRe%resolution
+	hgtDictUrl = NASASRTMUtilConfig.VIEWfileDictPageRe%resolution
 	areaDict = {}
 	for a in BeautifulSoup(urllib.urlopen(hgtDictUrl).read()).findAll("area"):
 		areaNames = calcAreaNames(a["coords"])
@@ -324,7 +342,7 @@ def makeViewHgtIndex(resolution):
 def updateViewIndex(resolution, zipFileUrl, areaList):
 	"""cleans up the viewfinder index.
 	"""
-	hgtIndexFile = VIEWhgtIndexFileRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.VIEWhgtIndexFileRe%resolution
 	try:
 		os.stat(hgtIndexFile)
 	except:
@@ -352,8 +370,8 @@ def getNASAUrl(area, resolution):
 	"""determines the NASA download url for a given area.
 	"""
 	file = "%s.hgt.zip"%area
-	hgtIndexFile = NASAhgtIndexFileRe%resolution
-	hgtFileServer = NASAhgtFileServerRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.NASAhgtIndexFileRe%resolution
+	hgtFileServer = NASASRTMUtilConfig.NASAhgtFileServerRe%resolution
 	try:
 		os.stat(hgtIndexFile)
 	except:
@@ -374,7 +392,7 @@ def getNASAUrl(area, resolution):
 def getViewUrl(area, resolution):
 	"""determines the viewfinder download url for a given area.
 	"""
-	hgtIndexFile = VIEWhgtIndexFileRe%resolution
+	hgtIndexFile = NASASRTMUtilConfig.VIEWhgtIndexFileRe%resolution
 	try:
 		os.stat(hgtIndexFile)
 	except:
@@ -436,20 +454,20 @@ def mkdir(dirName):
 def getDirNames(source):
 	resolution = int(source[-1])
 	if source.startswith("srtm"):
-		hgtSaveSubDir = os.path.join(hgtSaveDir, NASAhgtSaveSubDirRe%resolution)
+		hgtSaveSubDir = os.path.join(NASASRTMUtilConfig.hgtSaveDir, NASASRTMUtilConfig.NASAhgtSaveSubDirRe%resolution)
 	elif source.startswith("view"):
-		hgtSaveSubDir = os.path.join(hgtSaveDir, VIEWhgtSaveSubDirRe%resolution)
-	return hgtSaveDir, hgtSaveSubDir
+		hgtSaveSubDir = os.path.join(NASASRTMUtilConfig.hgtSaveDir, NASASRTMUtilConfig.VIEWhgtSaveSubDirRe%resolution)
+	return NASASRTMUtilConfig.hgtSaveDir, hgtSaveSubDir
 
 def initDirs(sources):
-	mkdir(hgtSaveDir)
+	mkdir(NASASRTMUtilConfig.hgtSaveDir)
 	for source in sources:
 		sourceType, sourceResolution = source[:4], int(source[-1])
 		if sourceType == "srtm":
-			NASAhgtSaveSubDir = os.path.join(hgtSaveDir, NASAhgtSaveSubDirRe%sourceResolution)
+			NASAhgtSaveSubDir = os.path.join(NASASRTMUtilConfig.hgtSaveDir, NASASRTMUtilConfig.NASAhgtSaveSubDirRe%sourceResolution)
 			mkdir(NASAhgtSaveSubDir)
 		elif sourceType == "view":
-			VIEWhgtSaveSubDir = os.path.join(hgtSaveDir, VIEWhgtSaveSubDirRe%sourceResolution)
+			VIEWhgtSaveSubDir = os.path.join(NASASRTMUtilConfig.hgtSaveDir, NASASRTMUtilConfig.VIEWhgtSaveSubDirRe%sourceResolution)
 			mkdir(VIEWhgtSaveSubDir)
 
 def downloadAndUnzip(url, area, source):
diff --git a/usr/share/pyshared/phyghtmap/main.py b/usr/share/pyshared/phyghtmap/main.py
index be56ca6..b27be6b 100644
--- a/usr/share/pyshared/phyghtmap/main.py
+++ b/usr/share/pyshared/phyghtmap/main.py
@@ -27,12 +27,12 @@ def parseCommandLine():
 	parser = OptionParser(usage="%prog [options] [<hgt file>] [<hgt files>]"
     "\nphyghtmap generates contour lines from NASA SRTM data."
 		"\nIt takes at least an area definition as input.  It then looks for a"
-		"\ncache directory (./hgt/) and the needed SRTM files.  If no cache"
-		"\ndirectory is found, it will be created.  It then downloads all the"
-		"\nneeded NASA SRTM data files automatically if they are not cached yet."
-		"\nThere is also the possibility of masking the NASA SRTM data with data"
-		"\nfrom www.viewfinderpanoramas.org which fills voids and other data"
-		"\nlacking in the NASA data set.")
+		"\ncache directory (default ./hgt/) and the needed SRTM files.  If no"
+		"\ncache directory is found, it will be created.  It then downloads all"
+		"\nthe needed NASA SRTM data files automatically if they are not cached"
+		"\nyet. There is also the possibility of masking the NASA SRTM data with"
+		"\ndata from www.viewfinderpanoramas.org which fills voids and other"
+		"\ndata lacking in the NASA data set.")
 	parser.add_option("-a", "--area", help="choses the area to generate osm SRTM"
 		"\ndata for by bounding box. If necessary, files are downloaded from"
 		"\nthe NASA server (%s)."
@@ -40,7 +40,7 @@ def parseCommandLine():
 		"\nand longitude, respectively. Latitudes south of the equator and"
 		"\nlongitudes west of Greenwich may be given as negative decimal numbers."
 		"\nIf this option is given, specified hgt"
-		"\nfiles will be omitted."%NASASRTMUtil.NASAhgtFileServerRe%"[1|3]",
+		"\nfiles will be omitted."%NASASRTMUtil.NASASRTMUtilConfig.NASAhgtFileServerRe%"[1|3]",
 	  dest="area", metavar="LEFT:BOTTOM:RIGHT:TOP", action="store", default=None)
 	parser.add_option("--polygon", help="use polygon FILENAME as downloaded from"
 		"\nhttp://download.geofabrik.de/clipbounds/ as bounds for the output contour"
@@ -96,6 +96,11 @@ def parseCommandLine():
 		"\nnumber of nodes per way.  It defaults to 2000, which is the maximum value"
 		"\nfor OSM api version 0.6.  Say 0 here, if you want unsplitted ways.",
 		dest="maxNodesPerWay", type="int", default=2000, action="store")
+	parser.add_option("--hgtdir", help="Cache directory for hgt files."
+		"\nThe downloaded SRTM files are stored in a cache directory for later use."
+		"\nThe default directory for this is hgt/ in the current directory. You can"
+		"\nspecify another caching directory with this option.",
+		dest="hgtdir", action="store", default=None, metavar="DIRECTORY")
 	parser.add_option("--gzip", help="turn on gzip compression of output files."
 		"\nThis reduces the needed disk space but results in higher computation"
 		"\ntimes.  Specifiy an integer between 1 and 9.  1 means low compression and"
@@ -169,6 +174,9 @@ def parseCommandLine():
 		sys.exit(1)
 	if opts.polygon:
 		opts.area, opts.polygon = hgt.parsePolygon(opts.polygon)
+	if opts.hgtdir:  # Set custom ./hgt/ directory
+		NASASRTMUtil.NASASRTMUtilConfig.CustomHgtSaveDir(opts.hgtdir)
+
 	return opts, args
 
 def makeOsmFilename(borders, opts, srcName):
-- 
1.7.10.4