class Test_OsmosisCommand(unittest.TestCase):
    def setUp(self):
        self.__toolsDir = os.path.abspath("../../tools") + "/"
        self.__osmosisTool = os.path.join(self.__toolsDir, "osmosis/bin/osmosis")
        self.__osmosisCmd = OsmosisCommand()
        logging.basicConfig(level=logging.DEBUG)

    def tearDown(self):
        pass

    def testCutMapWithPolygon_Default(self):
        cmdstr = self.__osmosisCmd.cutMapWithPolygon()
        expectedStr = (
            self.__osmosisTool
            + ' --read-xml file="germany.osm" --bounding-polygon file="germany.poly" completeWays=no --write-xml file="temp.osm"'
        )
        logging.debug("cmdstr   = %s" % cmdstr)
        logging.debug("expected = %s" % expectedStr)
        self.assertEqual(expectedStr, cmdstr, "unexpected cmdstr, \nexpected: %s,\nbut was: %s" % (expectedStr, cmdstr))

    def testCutMapWithPolygon_Parameters(self):
        cmdstr = self.__osmosisCmd.cutMapWithPolygon(infile="input.osm", outfile="output.osm", poly="borders.poly")
        expectedStr = (
            self.__osmosisTool
            + ' --read-xml file="input.osm" --bounding-polygon file="borders.poly" completeWays=no --write-xml file="output.osm"'
        )
        logging.debug("cmdstr   = %s" % cmdstr)
        logging.debug("expected = %s" % expectedStr)
        self.assertEqual(expectedStr, cmdstr, "unexpected cmdstr, \nexpected: %s,\nbut was: %s" % (expectedStr, cmdstr))

    def testCutMapWithBoundingBox_Default(self):
        cmdstr = self.__osmosisCmd.cutMapWithBoundingBox()
        expectedStr = (
            self.__osmosisTool
            + ' --read-xml file="germany.osm" --bounding-box top=55.2 left=5.7 bottom=47.2 right=15.1 completeWays=no --write-xml file="temp.osm"'
        )
        self.assertEqual(expectedStr, cmdstr, "unexpected cmdstr, \nexpected: %s,\nbut was: %s" % (expectedStr, cmdstr))

    def testCutMapWithBoundingBox_Parameters(self):
        cmdstr = self.__osmosisCmd.cutMapWithBoundingBox(top=49.5138, left=10.9351, bottom=49.3866, right=11.201)
        expectedStr = (
            self.__osmosisTool
            + ' --read-xml file="germany.osm" --bounding-box top=49.5138 left=10.9351 bottom=49.3866 right=11.201 completeWays=no --write-xml file="temp.osm"'
        )
        logging.debug("cmdstr   = %s" % cmdstr)
        logging.debug("expected = %s" % expectedStr)
        self.assertEqual(expectedStr, cmdstr, "unexpected cmdstr, \nexpected: %s,\nbut was: %s" % (expectedStr, cmdstr))
Example #2
0
 def __init__(self, options, test = False):
     '''
     Constructor
     '''
     self.__cmdArgs = self.parseCmdLine(options)
     self.__toolsDir = os.path.abspath("../../tools") + "/"
     self.__dataDir = os.path.abspath("../../data") + "/"
     self.__osmosisCmd = OsmosisCommand()
     self.__executor = ShellCommand(test)
     logging.basicConfig(level=logging.INFO)
     logging.debug('MapCreator created with testmode = %s' % test)
 def setUp(self):
     self.__toolsDir = os.path.abspath("../../tools") + "/"
     self.__osmosisTool = os.path.join(self.__toolsDir, "osmosis/bin/osmosis")
     self.__osmosisCmd = OsmosisCommand()
     logging.basicConfig(level=logging.DEBUG)
Example #4
0
class MapCreator(object):
    '''
    classdocs
    '''


    def __init__(self, options, test = False):
        '''
        Constructor
        '''
        self.__cmdArgs = self.parseCmdLine(options)
        self.__toolsDir = os.path.abspath("../../tools") + "/"
        self.__dataDir = os.path.abspath("../../data") + "/"
        self.__osmosisCmd = OsmosisCommand()
        self.__executor = ShellCommand(test)
        logging.basicConfig(level=logging.INFO)
        logging.debug('MapCreator created with testmode = %s' % test)
    
    def parseCmdLine(self, options):
        parser = ArgumentParser(description='Create a Garmin map from OpenStreetMap data')

        mandatory = parser.add_argument_group('mandatory arguments')

        # coordinate options
        helpMsg = 'bottom coordinate of the area to cut from input data'
        parser.add_argument('-b', '--bottom', action='store', type=float, dest='bottom',
                            required=False, default=47.2, help=helpMsg)
        
        helpMsg = 'top coordinate of the area to cut from input data'
        parser.add_argument('-t', '--top', action='store', type=float, dest='top',
                            required=False, default=55.2, help=helpMsg)
        
        helpMsg = 'western coordinate of the area to cut from input data'
        parser.add_argument('-l', '--left', action='store', type=float, dest='left',
                            required=False, default=5.7, help=helpMsg)
        
        helpMsg = 'eastern coordinate of the area to cut from input data'
        parser.add_argument('-r', '--right', action='store', type=float, dest='right',
                            required=False, default=15.1, help=helpMsg)
        
        helpMsg = 'path of polygon file'
        parser.add_argument('-p', '--poly', action='store', type=str, dest='poly',
                            required=False, help=helpMsg)
        
        # contourline options
        helpMsg = 'distance between minor contour lines'
        parser.add_argument('--ci', '--contour-step-min', action='store', type=int, dest='cstepmin',
                            required=False, default=10, help=helpMsg)
        
        helpMsg = 'distance between medium contourlines'
        parser.add_argument('--ce', '--contour-step-med', action='store', type=int, dest='cstepmed',
                            required=False, default=50, help=helpMsg)
        
        helpMsg = 'distance between major contourlines'
        parser.add_argument('--ca', '--contour-step-max', action='store', type=int, dest='cstepmax',
                            required=False, default=100, help=helpMsg)
        
        # mapid and mapname options
        helpMsg = 'map id (a unique 4 digit integer)'
        parser.add_argument('--mi', '--map-id', action='store', type=int, dest='mapid',
                            required=False, default=6400, help=helpMsg)
        
        helpMsg = 'mapname (a string giving a name to the map)'
        parser.add_argument('--mn', '--map-name', action='store', type=str, dest='mapname',
                            required=False, default='GER', help=helpMsg)
        
        # family ids for main map and contour maps
        helpMsg = 'family id (a unique 4 digit integer)'
        parser.add_argument('--fi', '--family-id', action='store', type=int, dest='famid',
                            required=False, default=1441, help=helpMsg)
        
        helpMsg = 'minor contourmap family id (a unique 4 digit integer)'
        parser.add_argument('--cid', '--contour-min-family-id', action='store', type=int, dest='cminid',
                            required=False, default=2441, help=helpMsg)
        
        helpMsg = 'major contourmap family id (a unique 4 digit integer)'
        parser.add_argument('--cad', '--contour-max-family-id', action='store', type=int, dest='cmaxid',
                            required=False, default=2443, help=helpMsg)
        
        # nocontours flag
        helpMsg = 'scan directories recursive'
        parser.add_argument('--nc', '--no-contours', action='store_true', dest='nocontours',
                            required=False, default=False, help=helpMsg)
        
        # inputfile option
        helpMsg = 'input file containing OpenStreetMap data'
        mandatory.add_argument('-i', '--input', action='store', type=str, dest='inputfile',
                               required=True, help=helpMsg)
        
        args = parser.parse_args(options)
        return args
    
    def getArgs(self):
        return self.__cmdArgs
    
    def cutMapDataWithPolygon(self):
        self.isDataFileOk(self.getArgs().inputfile)
        self.isPolyFileOk(self.getArgs().poly)
        cmdstr = self.__osmosisCmd.cutMapWithPolygon(infile=self.getArgs().inputfile, 
                                                     outfile=self.__dataDir + "temp.osm",
                                                     poly=self.getArgs().poly)
        logging.debug('cutMapDataWithPolygon: cmdstr = %s' % cmdstr)
        res = self.__executor.execShellCmd(cmdstr)
        return res
    
    def cutMapDataWithBoundingBox(self):
        self.isDataFileOk(self.getArgs().inputfile)
        cmdstr = self.__osmosisCmd.cutMapWithBoundingBox(infile  = self.getArgs().inputfile, 
                                                         outfile = self.__dataDir + "temp.osm",
                                                         top     = self.getArgs().top,
                                                         left    = self.getArgs().left,
                                                         bottom  = self.getArgs().bottom,
                                                         right   = self.getArgs().right)
        logging.debug('cutMapDataWithBoundingBox: cmdstr = %s' % cmdstr)
        res = self.__executor.execShellCmd(cmdstr)
        return res
    
    def isKnownDataFileExtension(self, inputfile):
        dummy, file_extension = os.path.splitext(inputfile)
        return file_extension in ('.bz2', '.osm', '.pbf')
      
    def isKnownPolyFileExtension(self, inputfile):
        dummy, file_extension = os.path.splitext(inputfile)
        return file_extension in ('.poly', '.txt')
    
    def checkFileExists(self, infile):
        # the following statement will throw an error if the input file can't be read
        with open(infile, 'r') as dummy:
            pass
        return True
    
    def isDataFileOk(self, datafile):
        self.checkFileExists(datafile)
        return self.isKnownDataFileExtension(datafile)
    
    def isPolyFileOk(self, polyfile):
        self.checkFileExists(polyfile)
        return self.isKnownPolyFileExtension(polyfile)