Example #1
0
def create_style(request,
                 json_data,
                 layer_id,
                 is_preview=False,
                 has_custom_legend=None):

    layer = Layer.objects.get(id=int(layer_id))
    datastore = layer.datastore
    workspace = datastore.workspace
    gs = geographic_servers.get_instance().get_server_by_id(
        workspace.server.id)

    layer_styles = StyleLayer.objects.filter(layer=layer)
    is_default = False
    if not is_preview:
        is_default = json_data.get('is_default')
    if is_default:
        for ls in layer_styles:
            s = Style.objects.get(id=ls.style.id)
            s.is_default = False
            s.save()
    else:
        has_default_style = False
        for ls in layer_styles:
            s = Style.objects.get(id=ls.style.id)
            if s.is_default:
                has_default_style = True
        if not has_default_style:
            is_default = True

    name = json_data.get('name')
    if is_preview:
        name = name + '__tmp'
        is_default = False

    style = Style(name=name,
                  title=json_data.get('title'),
                  is_default=is_default,
                  type='CT')

    if has_custom_legend == 'true':
        style.has_custom_legend = True
        legend_name = 'legend_' + ''.join(
            random.choice(string.ascii_uppercase) for i in range(6)) + '.png'
        legend_path = utils.check_custom_legend_path()
        style.custom_legend_url = utils.save_custom_legend(
            legend_path, request.FILES['file'], legend_name)

    else:
        style.has_custom_legend = False

    style.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    json_rule = json_data.get('rule')

    filter_text = ""
    if json_rule.get('filter').__len__() != 0:
        filter_text = str(json.dumps(json_rule.get('filter')))

    rule = Rule(style=style,
                name=json_rule.get('name'),
                title=json_rule.get('title'),
                abstract="",
                filter=filter_text,
                minscale=json_rule.get('minscale'),
                maxscale=json_rule.get('maxscale'),
                order=json_rule.get('order'))
    rule.save()

    color_map = ColorMap(type='ramp', extended=False)
    color_map.save()

    symbolizer = RasterSymbolizer(rule=rule,
                                  color_map=color_map,
                                  order=0,
                                  opacity=1.0)
    symbolizer.save()

    color_map_entry_list = []
    for entry in json_data.get('entries'):
        json_entry = json.loads(entry.get('json'))
        color_map_entry = ColorMapEntry(color_map=color_map,
                                        order=int(json_entry.get('order')),
                                        color=json_entry.get('color'),
                                        quantity=json_entry.get('quantity'),
                                        label=json_entry.get('label'),
                                        opacity=json_entry.get('opacity'))
        color_map_entry_list.append(color_map_entry)

    order = 0
    color_map_entry_list = sorted(
        color_map_entry_list,
        key=lambda color_map_entry: float(color_map_entry.quantity))
    for cme in color_map_entry_list:
        cme.order = order
        cme.save()
        order = order + 1

    sld_body = sld_builder.build_sld(layer, style)

    if is_preview:
        if gs.createOverwrittenStyle(style.name, sld_body, True):
            return True
        else:
            return False
    else:
        if gs.createOverwrittenStyle(style.name, sld_body, True):
            if not is_preview:
                gs.setLayerStyle(layer, style.name, style.is_default)
            return True

        else:
            return False
Example #2
0
def update_style(request,
                 json_data,
                 layer,
                 gs,
                 style,
                 is_preview=False,
                 has_custom_legend=None):
    is_default = json_data.get('is_default', False)
    is_default = utils.set_default_style(layer,
                                         gs,
                                         style=style,
                                         is_preview=is_preview,
                                         is_default=is_default)

    if has_custom_legend == 'true':
        style.has_custom_legend = True
        legend_name = 'legend_' + ''.join(
            random.choice(string.ascii_uppercase) for i in range(6)) + '.png'
        legend_path = utils.check_custom_legend_path()
        if 'file' in request.FILES:
            style.custom_legend_url = utils.save_custom_legend(
                legend_path, request.FILES['file'], legend_name)

    else:
        style.has_custom_legend = False

    style.title = json_data.get('title')
    if json_data.get('minscale') != '':
        style.minscale = json_data.get('minscale')
    else:
        style.minscale = -1
    if json_data.get('maxscale') != '':
        style.maxscale = json_data.get('maxscale')
    else:
        style.maxscale = -1
    style.is_default = is_default
    style.save()

    json_rule = json_data.get('rule')

    filter_text = ""
    if json_rule.get('filter').__len__() != 0:
        filter_text = str(json.dumps(json_rule.get('filter')))

    if json_data.get('minscale') != '':
        minscale = json_rule.get('minscale')
    else:
        minscale = -1
    if json_data.get('maxscale') != '':
        maxscale = json_rule.get('maxscale')
    else:
        maxscale = -1

    rule_old = Rule.objects.get(style=style)
    rule_old.delete()

    rule = Rule(style=style,
                name=json_rule.get('name'),
                title=json_rule.get('title'),
                abstract='',
                filter=filter_text,
                minscale=minscale,
                maxscale=maxscale,
                order=json_rule.get('order'))
    rule.save()

    for s in Symbolizer.objects.filter(rule=rule):
        s.rastersymbolizer.color_map.delete()
        s.delete()

    color_map = ColorMap(type='ramp', extended=False)
    color_map.save()

    symbolizer = RasterSymbolizer(rule=rule,
                                  color_map=color_map,
                                  order=0,
                                  opacity=1.0)
    symbolizer.save()

    color_map_entry_list = []
    for entry in json_data.get('entries'):
        json_entry = json.loads(entry.get('json'))
        color_map_entry = ColorMapEntry(color_map=color_map,
                                        order=int(json_entry.get('order')),
                                        color=json_entry.get('color'),
                                        quantity=json_entry.get('quantity'),
                                        label=json_entry.get('label'),
                                        opacity=json_entry.get('opacity'))

        color_map_entry_list.append(color_map_entry)

    order = 0
    color_map_entry_list = sorted(
        color_map_entry_list,
        key=lambda color_map_entry: float(color_map_entry.quantity))
    for cme in color_map_entry_list:
        cme.order = order
        cme.save()
        order = order + 1

    sld_body = sld_builder.build_sld(layer, style)

    if is_preview:
        if gs.createOverwrittenStyle(style.name, sld_body, True):
            return style
    else:
        if gs.updateStyle(layer, style.name, sld_body):
            gs.setLayerStyle(layer, style.name, style.is_default)
            return style