def iter_query_conditions(self, geom_type): ''' Yields tags, columns and conditions for the current scale. :param geom_type: one of ``'point'``, ``'line'`` or ``'polygon'`` :yields: ``[tags,]``, ``[columns,]``, ``[sqlalchemy binary expressions,]`` ''' db_class = GEOM_TYPES[geom_type] # only one condition so it can be combined with others to get better # performance simple_conds = collections.defaultdict(list) # multiple contitions complex_conds = [] # columns which need to be fetched from database columns = collections.defaultdict(set) for style in self.stylesheet.iter_styles(self.mapobj.scale, geom_type): if len(style.tag_value) == 1: tag, value = style.tag_value.iteritems().next() simple_conds[tag].append(value) column_key = tag else: complex_conds.append(style.tag_value) column_key = utils.dict2key(style.tag_value) #: search for additional columns which need to be fetched from #: from database for attr in COLUMN_ATTRS: if style.get(attr) in db_class.__dict__: columns[column_key].add(style.get(attr)) for tag, names in simple_conds.iteritems(): yield [tag], columns[tag], [getattr(db_class, tag).in_(names)] for conds in complex_conds: tags = [] query_conds = [] for key, value in conds.iteritems(): tags.append(key) query_conds.append(getattr(db_class, key)==value) yield tags, columns[utils.dict2key(conds)], query_conds
def update(self, style): ''' Adds style or overwrites existing style with new attributes. :param style: :class:`mapython.style.Style` object ''' #: overwrite if style is already set for this level existing = self.get(self.zoomlevels[style.level][0], style.geom_type, style.tag_value) if existing is not None: existing.attrs.update(style.attrs) else: # style not set for this level self.styles[style.level][style.geom_type] \ [utils.dict2key(style.tag_value)] = style
def get(self, scale, geom_type, tag_value, default=None): ''' Returns style or default for given settings. :param scale: metres per pixel or point :param geom_type: one of ``'point'``, ``'line'`` or ``'polygon'`` :param tag_value: dict specifying the tags and values of the style, e.g. ``{'highway':'motorway', ...}`` :param default: default value if no style is found :returns: :class:`mapython.style.Style` or default ''' level = self.get_level(scale) try: return self.styles[level][geom_type][utils.dict2key(tag_value)] except KeyError: return default