Esempio n. 1
0
 def addTopo(self, blxfile):
     if self._topo == None:
         self._topo = Topo(self.mapdir)
     self._topo.addblx(blxfile, bigendian=self._bigendian)
Esempio n. 2
0
class MapImage(object):
    """Map image suitable for use with a Magellan GPS. A MapImage object may contain several maps (Map objects).

    >>> mi = MapImage("./test/images/canarias.imi")
    >>> mi.open()
    >>> len(mi.maps)
    1

    """
    def __init__(self, filename = None, mapdirobj = None, bigendian = False, maps = []):
        self._filename = filename

        if mapdirobj != None and not isinstance(mapdirobj, mapdir.MapDirectory):
            raise ValueError("mapdirobj should be a MapDirectory object")

        self.mapdir = mapdirobj
            

        self._maps = list(maps)
        self._bigendian = bigendian

        self._topo = None

        self.mode = None

    @property
    def topo(self): return self._topo
    
    def addTopo(self, blxfile):
        if self._topo == None:
            self._topo = Topo(self.mapdir)
        self._topo.addblx(blxfile, bigendian=self._bigendian)

    def createMap(self, *args, **kvargs):
        """Create a new map and return the new Map object"""

        if self.mode in ('a', 'w'):
            m = Map(self.mapdir, mapnumber = len(self._maps), bigendian=self._bigendian, **kvargs)
            m.open('w')
            self._maps.append(m)
            return self._maps[-1]

    @property
    def maps(self):
        return tuple(self._maps)

    def open(self, mode='r'):
        self.mode = mode

        if self.mapdir == None:
            self.mapdir = mapdir.Image(self._filename, mode = mode)

        if mode == 'w':
            ## Copy data files
            for file in ["bmp2bit.ics", "bmp4bit.ics"]:
                self.mapdir.copyfile(os.path.join(datadir, file))

        elif mode in ('r', 'a'):
            if self.mapdir.exists('add_maps.cfg'):
                addmapscfg = self.mapdir.open('add_maps.cfg')
            
                columns = re.split('\s+', addmapscfg.readline())
                n = int(columns[0])
                inifiles = columns[1:n+1]
                hasmultiplemaps = True
            else:
                hasmultiplemaps = False
                if self.mapdir.exists('lay_info.ini'):
                    inifiles = ['lay_info.ini']
                else:
                    raise Exception('Could not find a layer ini file')

            for inifile in inifiles:
                m = Map(self.mapdir, bigendian=self._bigendian, inifile=inifile, hasmultiplemaps=hasmultiplemaps)
                m.open(mode)
                self._maps.append(m)

    def writeconfig(self):
        ## Create add_maps.cfg
        addmapscfg = self.mapdir.open('add_maps.cfg', 'w')
        addmapscfg.write(cfg_writelist([m.inifilename for m in self._maps]) + '\n')
        addmapscfg.close()

    def close(self):
        self.writeconfig()
        
        ## Close maps
        for m in self._maps:
            m.close()

        ## Write topo
        if self._topo:
            self._topo.write()

        ## Write image
        self.mapdir.write()