Пример #1
0
 def set_table(self, arch):
     attrs = OrderedDict({
         '__tablename__':
         'stats',
         'ocgid':
         Column(Integer, primary_key=True),
         'gid':
         Column(Integer, ForeignKey(self.db.Geometry.gid)),
         'level':
         Column(Integer, nullable=False, index=True)
     })
     for key, value in arch.iteritems():
         if key in ['gid', 'level']: continue
         if key in ['day', 'month', 'year']:
             index = True
         else:
             index = False
         attrs.update({
             key:
             Column(self.__types[type(value[0])],
                    nullable=False,
                    index=index)
         })
     try:
         self.db.Stat = type('Stat', (self.db.AbstractValue, self.db.Base),
                             attrs)
     except InvalidRequestError:
         self.db.metadata.remove(self.db.Stat.__table__)
         self.db.Stat = type('Stat', (self.db.AbstractValue, self.db.Base),
                             attrs)
     try:
         self.db.Stat.__table__.create()
     except OperationalError:
         self.db.Stat.__table__.drop()
         self.db.Stat.__table__.create()
Пример #2
0
 def set_table(self,arch):
     attrs = OrderedDict({'__tablename__':'stats',
                          'ocgid':Column(Integer,primary_key=True),
                          'gid':Column(Integer,ForeignKey(self.db.Geometry.gid)),
                          'level':Column(Integer,nullable=False,index=True)})
     for key,value in arch.iteritems():
         if key in ['gid','level']: continue
         if key in ['day','month','year']:
             index = True
         else:
             index = False
         attrs.update({key:Column(self.__types[type(value[0])],
                                        nullable=False,
                                        index=index)})
     try:
         self.db.Stat = type('Stat',
                             (self.db.AbstractValue,self.db.Base),
                             attrs)
     except InvalidRequestError:
         self.db.metadata.remove(self.db.Stat.__table__)
         self.db.Stat = type('Stat',
                             (self.db.AbstractValue,self.db.Base),
                             attrs)
     try:
         self.db.Stat.__table__.create()
     except OperationalError:
         self.db.Stat.__table__.drop()
         self.db.Stat.__table__.create()
Пример #3
0
 def iter_time(self,expand=True):
     for dt in self.dim_time:
         d = OrderedDict(zip(['TID','TIME'],[int(self.tid[dt]),self.timevec[dt]]))
         if expand:
             attrs = ['YEAR','MONTH','DAY']
             for attr in attrs:
                 d.update({attr:getattr(d['TIME'],attr.lower())})
         yield(d)
Пример #4
0
    def f_calculate(all_attrs, sub, groups, funcs, time_conv, time_grouping,
                    grouping):
        """
        funcs -- dict[] {'function':sum,'name':'foo','kwds':{}} - kwds optional
        """
        ## construct the base variable dictionary
        keys = list(grouping) + [f.get('name') for f in funcs]
        pvars = OrderedDict([[key, list()] for key in keys])
        ## loop through the groups adding the data
        for group in groups:
            ## append the grouping information
            for grp in grouping:
                pvars[grp].append(getattr(group, grp))
            ## extract the time indices of the values
            cmp = [getattr(group, grp) for grp in time_grouping]
            ## loop through time vector selecting the time indices
            tids = [ii for ii, time in enumerate(time_conv) if cmp == time]
            ## get the index for the particular geometry
            gid_idx = sub.gid == group.gid
            ## extract the values
            values = sub.value[tids, group.level - 1, gid_idx].tolist()
            ## calculate function value and update variables
            for f in funcs:
                kwds = f.get('kwds', {})
                args = [values] + f.get('args', [])
                ivalue = f['function'](*args, **kwds)
                if type(ivalue) == np.float_:
                    ivalue = float(ivalue)
                elif type(ivalue) == np.int_:
                    ivalue = int(ivalue)
                pvars[f.get('name')].append(ivalue)

        all_attrs.append(pvars)
Пример #5
0
 def iter_stats(self,caps=True,keep_geom=True,wkt=False,wkb=False):
     if caps:
         keys = [k.upper() for k in self.stats.keys()]
     else:
         keys = self.stats.keys()
     if not keep_geom:
         keys.remove('GEOMETRY')
     keys.insert(0,'OCGID')
     for ii in range(0,len(self.stats['gid'])):
         d = OrderedDict(zip(keys,
                         [ii+1] + [self.stats[k.lower()][ii] for k in keys[1:]]))
         if wkt:
             d.update({'WKT':self.stats['geometry'][ii][0].wkt})
         if wkb:
             d.update({'WKT':self.stats['geometry'][ii][0].wkb})
         yield(d)
Пример #6
0
 def iter_geom_with_area(self,area_srid=3005,keep_geom=True,wkt=False,wkb=False):
     sr_orig = get_sr(4326)
     sr_dest = get_sr(area_srid)
     for gid,geom in zip(self.gid,self.geometry):
         if keep_geom:
             d = OrderedDict(zip(
                 ['GID','AREA_M2','geometry'],
                 [int(gid),get_area(geom,sr_orig,sr_dest),geom]))
         else:
             d = OrderedDict(zip(
                 ['GID','AREA_M2'],
                 [int(gid),get_area(geom,sr_orig,sr_dest)]))
         if wkt:
             d.update(dict(WKT=geom.wkt))
         if wkb:
             d.update(dict(WKB=geom.wkb))
         yield(d)
Пример #7
0
    def load(self, db):
        self.set_table(
            OrderedDict(
                zip(self.stats.keys(),
                    [self.stats[key][0] for key in self.stats.keys()])), db)

        def f(idx, stats={}):
            d = dict(
                zip(stats.keys(), [stats[key][idx] for key in stats.keys()]))
            return (d)

        fkwds = dict(stats=self.stats)
        gen = ParallelGenerator(db.Stat,
                                range(0, len(self.stats['gid'])),
                                f,
                                fkwds=fkwds,
                                procs=settings.MAXPROCESSES,
                                use_lock=True)
        gen.load()
Пример #8
0
 def iter_stats(self, caps=True, keep_geom=True, wkt=False, wkb=False):
     if caps:
         keys = [k.upper() for k in self.stats.keys()]
     else:
         keys = self.stats.keys()
     if not keep_geom:
         keys.remove('GEOMETRY')
     keys.insert(0, 'OCGID')
     for ii in range(0, len(self.stats['gid'])):
         d = OrderedDict(
             zip(keys,
                 [ii + 1] + [self.stats[k.lower()][ii] for k in keys[1:]]))
         if wkt:
             d.update({'WKT': self.stats['geometry'][ii][0].wkt})
         if wkb:
             d.update({'WKT': self.stats['geometry'][ii][0].wkb})
         yield (d)
Пример #9
0
    def __iter__(self):
        """
        Default object iterator returning a dictionary representation of each
            object "record".
        """

        ocgid = 0
        
        for dt in self.dim_time:
            tid = int(self.tid[dt])
            for dl in self.dim_level:
                for dd in self.dim_data:
                    ocgid += 1
                    keys = ['OCGID','GID','TID','LEVEL','TIME','VALUE','geometry']
                    values = [ocgid,
                              int(self.gid[dd]),
                              tid,
                              int(self.levelvec[dl]),
                              self.timevec[dt],
                              float(self.value[dt,dl,dd]),
                              self.geometry[dd]]
                    d = OrderedDict(zip(keys,values))
                    yield(d)