コード例 #1
0
ファイル: models.py プロジェクト: lizardsystem/lizard-map
    def mapnik_style(self, value_field=None):
        """Return a Mapnik style from Legend object. """

        mapnik_style = mapnik.Style()
        if value_field is None:
            value_field = "value"

        # Default color: red.
        mapnik_rule = point_rule(
            self.icon, self.mask, self.default_color)
        mapnik_style.rules.append(mapnik_rule)

        # < min
        mapnik_filter = "[%s] <= %f" % (value_field, self.min_value)
        logger.debug('adding mapnik_filter: %s' % mapnik_filter)
        mapnik_rule = point_rule(
            self.icon, self.mask, self.too_low_color,
            mapnik_filter=mapnik_filter)
        mapnik_style.rules.append(mapnik_rule)

        # in boundaries
        for legend_value in self.legend_values():
            mapnik_filter = "[%s] > %f and [%s] <= %f" % (
                value_field, legend_value['low_value'],
                value_field, legend_value['high_value'])
            logger.debug('adding mapnik_filter: %s' % mapnik_filter)
            mapnik_rule = point_rule(
                self.icon, self.mask, legend_value['color'],
                mapnik_filter=mapnik_filter)
            mapnik_style.rules.append(mapnik_rule)

        # > max
        mapnik_filter = "[%s] > %f" % (value_field, self.max_value)
        logger.debug('adding mapnik_filter: %s' % mapnik_filter)
        mapnik_rule = point_rule(
            self.icon, self.mask, self.too_high_color,
            mapnik_filter=mapnik_filter)
        mapnik_style.rules.append(mapnik_rule)

        return mapnik_style
コード例 #2
0
ファイル: models.py プロジェクト: pombredanne/lizard-shape
    def mapnik_style(self, value_field=None):
        """
        Generates mapnik style from this object.
        """
        if value_field is None:
            value_field = 'value'
        style = mapnik.Style()
        classes = self.shapelegendsingleclass_set.all()

        # Add a rule for each class
        for c in classes:
            layout_rule = mapnik.Rule()
            if c.color_inside:
                area_looks = mapnik.PolygonSymbolizer(
                    mapnik.Color('#' + c.color_inside))
                area_looks.fill_opacity = 0.5
                layout_rule.symbols.append(area_looks)
                logger.debug('adding polygon symbolizer')
            if c.color:
                line_looks = mapnik.LineSymbolizer(
                    mapnik.Color('#' + c.color), c.size)
                layout_rule.symbols.append(line_looks)
                logger.debug('adding line symbolizer')
            mapnik_filter = None
            if c.is_exact or c.min_value and c.min_value == c.max_value:
                # Check if c.min_value is parsable as float. Yes:
                # compare like float.
                try:
                    float(c.min_value)
                    mapnik_filter = str("[%s] = %s" % (
                            value_field, c.min_value))
                except ValueError:
                    mapnik_filter = str("[%s] = '%s'" % (
                            value_field, c.min_value))
            else:
                if c.min_value and c.max_value:
                    mapnik_filter = str("[%s] >= %s and [%s] < %s"
                                        % (value_field,
                                           c.min_value,
                                           value_field,
                                           c.max_value))
                elif c.min_value and not c.max_value:
                    mapnik_filter = str("[%s] >= %s"
                                        % (value_field,
                                           c.min_value))
                elif not c.min_value and c.max_value:
                    mapnik_filter = str("[%s] < %s"
                                        % (value_field,
                                           c.max_value))
            if mapnik_filter is not None:
                logger.debug('adding mapnik_filter: %s' % mapnik_filter)
                layout_rule.filter = mapnik.Filter(mapnik_filter)

            style.rules.append(layout_rule)

            # Add icon rule, if applicable.
            if c.icon:
                style.rules.append(
                    point_rule(c.icon, c.mask,
                               c.color, mapnik_filter))

        return style