Commit 70b04780 authored by Rick Gruber-Riemer's avatar Rick Gruber-Riemer

Added reading parameters from file

parent 1083d9e8
......@@ -84,13 +84,14 @@ import stg_io
import tools
import calc_tile
import parameters
from parameters import Parameters
# -- defaults
no_elev = False # -- skip elevation interpolation; FIXME: remove from here and module -> Parameters
check_overlap = True # -- check for overlap with static models; FIXME: remove from here and module -> Parameters
if '-e' in sys.argv: #FIXME: remove from here and module -> Parameters
if '-e' in sys.argv: #FIXME: remove from here and module -> Parameters and argumentparser in main()
no_elev = True
if '-c' in sys.argv:
check_overlap = False
......@@ -439,12 +440,30 @@ def write_xml(fname, LOD_lists, LM_dict, buildings):
# -----------------------------------------------------------------------------
if __name__ == "__main__":
#Parse arguments and eventually override Parameters
import argparse
parser = argparse.ArgumentParser(description="osm2city is a script to read OSM data and create buildings in Flightgear")
parser.add_argument("-f", "--file", dest="filename",
help="read parameters from FILE (e.g. params.ini)", metavar="FILE")
parser.add_argument("-e", dest="e", action="store_true", help="skip elevation interpolation")
parser.add_argument("-c", dest="c", action="store_true", help="do not check for overlapping with static objects")
args = parser.parse_args()
if args.filename is None:
params = Parameters()
else:
params = parameters.readFromFile(args.filename)
if args.e:
params.no_elev = True
if args.c:
params.check_overlap = False
print params.printParams()
#initialize modules
tools.init()
tex.init()
params = Parameters()
# prepare translation to local coordinates
cmin = vec2d(params.boundary_west, params.boundary_south)
cmax = vec2d(params.boundary_east, params.boundary_north)
......
......@@ -5,13 +5,15 @@ Created on May 27, 2013
@author: vanosten
'''
import sys
import types
class Parameters(object):
'''
Central place to store parameters / settings / variables in osm2city.
TODO: load from file and write to file
'''
def __init__(self):
'''
Constructor. For the time being replace the default values with your own before running the modules in osm2city.
......@@ -41,4 +43,137 @@ class Parameters(object):
"Frauenkirche", "Coselpalais", "Palais im Großen Garten",
"Residenzschloss Dresden", "Fernsehturm", "Fernsehturm Dresden"] # the buildings in OSM to skip
def printParams(self):
'''
Prints all parameters as key = value
'''
print '--- Using the following parameters: ---'
for k in sorted(self.__dict__.iterkeys()):
if isinstance(self.__dict__[k], types.ListType):
value = ', '.join(self.__dict__[k])
print k, '=', value
else:
print k, '=', self.__dict__[k]
print '------'
def setParameters(self, paramDict):
'''
Sets the parameter values from a dictionary read by function readFromFile.
If a parameter is not in the dictionary or cannot be parsed, then a default is chosen.
'''
if 'boundary_west' in paramDict:
floatValue = parseFloat('boundary_west', paramDict['boundary_west'])
if None is not floatValue:
self.boundary_west = floatValue
if 'boundary_south' in paramDict:
floatValue = parseFloat('boundary_south', paramDict['boundary_south'])
if None is not floatValue:
self.boundary_south = floatValue
if 'boundary_east' in paramDict:
floatValue = parseFloat('boundary_east', paramDict['boundary_east'])
if None is not floatValue:
self.boundary_east = floatValue
if 'boundary_north' in paramDict:
floatValue = parseFloat('boundary_north', paramDict['boundary_north'])
if None is not floatValue:
self.boundary_north = floatValue
if 'elev_raster_x' in paramDict:
intValue = parseInt('elev_raster_x', paramDict['elev_raster_x'])
if None is not intValue:
self.elev_raster_x = intValue
if 'elev_raster_y' in paramDict:
intValue = parseInt('elev_raster_y', paramDict['elev_raster_y'])
if None is not intValue:
self.elev_raster_y = intValue
if 'prefix' in paramDict:
if None is not paramDict['prefix']:
self.prefix = paramDict['prefix']
if 'path_to_scenery' in paramDict:
if None is not paramDict['path_to_scenery']:
self.path_to_scenery = paramDict['path_to_scenery']
if 'no_elev' in paramDict:
self.no_elev = parseBool(paramDict['no_elev'])
if 'check_overlap' in paramDict:
self.check_overlap = parseBool(paramDict['check_overlap'])
if 'use_pkl' in paramDict:
self.use_pkl = parseBool(paramDict['use_pkl'])
if 'tile_size' in paramDict:
intValue = parseInt('tile_size', paramDict['tile_size'])
if None is not intValue:
self.tile_size = intValue
if 'total_objects' in paramDict:
intValue = parseInt('total_objects', paramDict['total_objects'])
if None is not intValue:
self.total_objects = intValue
if 'osmfile' in paramDict:
if None is not paramDict['osmfile']:
self.osmfile = paramDict['osmfile']
if 'skiplist' in paramDict:
self.skiplist = parseList(paramDict['skiplist'])
def parseList(stringValue):
'''
Tries to parse a string containing comma separated values and returns a list
'''
myList = []
if None is not stringValue:
myList = stringValue.split(',')
for index in range(len(myList)):
myList[index] = myList[index].strip()
return myList
def parseFloat(key, stringValue):
'''
Tries to parse a string and get a float. If it is not possible, then None is returned.
On parse exception the key and the value are printed to console
'''
floatValue = None
try:
floatValue = float(stringValue)
except ValueError:
print 'Unable to convert', stringValue, 'to decimal number. Relates to key', key
return floatValue
def parseInt(key, stringValue):
'''
Tries to parse a string and get an int. If it is not possible, then None is returned.
On parse exception the key and the value are printed to console
'''
intValue = None
try:
intValue = int(stringValue)
except ValueError:
print 'Unable to convert', stringValue, 'to number. Relates to key', key
return intValue
def parseBool(stringValue):
'''
Tries to parse a string and get a boolean. If it is not possible, then False is returned.
'''
if stringValue.lower() in ("yes", "true"):
return True
return False
def readFromFile(filename):
params = Parameters()
print 'Reading parameters from file:', filename
try:
file_object = open(filename, 'r')
paramDict = {}
for line in file_object:
if 0 == len(line.strip()) or line.startswith('#'): # lines starting with # are treated as comments
continue
else:
pair = line.split('=',1)
key = pair[0].strip()
value = None
if 2 == len(pair):
value = pair[1].strip()
paramDict[key] = value
params.setParameters(paramDict)
file_object.close()
except IOError, reason:
print "Error processing file with parameters:", reason
sys.exit(1)
return params
# List of parameters for osm2city
boundary_east = 9.58
boundary_north = 47.5
boundary_south = 47.48
boundary_west = 9.54
check_overlap = False
elev_raster_x = 10
elev_raster_y = 10
no_elev = False
osmfile = lszr.osm
path_to_scenery = /home/vanosten/bin/fgfs_scenery/customscenery/LSZR
prefix = LSZR
skiplist = Skip1, Skip2
tile_size = 1000
total_objects = 50000
use_pkl = False
......@@ -16,6 +16,7 @@ stats = None
import vec2d
import coordinates
import parameters
from parameters import Parameters
class Interpolator(object):
......@@ -234,6 +235,17 @@ def init():
print "tools: init", stats
if __name__ == "__main__":
params = Parameters()
#Parse arguments and eventually override Parameters
import argparse
parser = argparse.ArgumentParser(description="tools prepares an elevation grid for Nasal script and then osm2city")
parser.add_argument("-f", "--file", dest="filename",
help="read parameters from FILE (e.g. params.ini)", metavar="FILE")
args = parser.parse_args()
if args.filename is None:
params = Parameters()
else:
params = parameters.readFromFile(args.filename)
print params.printParams()
raster_glob(params)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment