Exemplo n.º 1
0
def export_library(library, library_rules):
    dir_path = tempfile.mkdtemp(suffix='', prefix='tmp-library-')
    resource_path = dir_path + "/resources/"
    os.makedirs(resource_path)
    i = 0
    for library_rule in library_rules:
        try:
            file = open(
                dir_path + "/symbol-" + library_rule.rule.name + "-" + str(i) +
                ".sld", 'w+')
            file.write(sld_builder.build_library_symbol(library_rule.rule))
            file.close()

            sld_utils.copy_resources(library_rule.rule, resource_path)

        except Exception as e:
            raise e

        i = i + 1

    buffer = StringIO.StringIO()
    z = zipfile.ZipFile(buffer, "w")
    relroot = dir_path
    for root, dirs, files in os.walk(dir_path):
        rel_path = os.path.relpath(root, relroot)
        if rel_path != ".":
            z.write(root, os.path.relpath(root, relroot))
        for file in files:
            filename = os.path.join(root, file)
            if os.path.isfile(filename):
                arcname = os.path.join(os.path.relpath(root, relroot), file)
                z.write(filename, arcname)
    z.close()
    buffer.seek(0)
    response = HttpResponse(content_type='application/zip; charset=utf-8')
    response[
        'Content-Disposition'] = 'attachment; filename=' + library.name + '.zip'
    response.write(buffer.read())

    utils.__delete_temporaries(dir_path)

    return response
Exemplo n.º 2
0
def clone_style(mapservice, layer, original_style_name, cloned_style_name):
    exists_cloned_style = False
    try:
        original_style = Style.objects.filter(
            name__exact=original_style_name)[0]
    except Exception as e:
        print str(e)
        return False

    try:
        style = Style.objects.filter(name__exact=cloned_style_name)[0]
        exists_cloned_style = True
    except Exception as e:
        print "DEBUG: Problem getting style .." + cloned_style_name
        print str(e)

    if exists_cloned_style:
        print "DEBUG: Exists cloned style .." + cloned_style_name
        rule = Rule.objects.filter(style=style)[0]
        symbolizers_to_delete = Symbolizer.objects.filter(rule=rule)
        for i in symbolizers_to_delete:
            i.delete()
    else:
        print "DEBUG: Not existe cloned style .." + cloned_style_name
        style = Style(name=cloned_style_name,
                      title=original_style_name,
                      is_default=True,
                      type='US')
        style.save()

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

        rule = Rule(style=style,
                    name='Default symbol',
                    title=original_style_name,
                    abstract='',
                    filter=str(""),
                    minscale=-1,
                    maxscale=-1,
                    order=0)
        rule.save()

    original_rules = Rule.objects.filter(
        style=original_style).order_by('order')
    for original_rule in original_rules:
        original_symbolizers = Symbolizer.objects.filter(rule=original_rule)
        for original_symbolizer in original_symbolizers:
            if hasattr(original_symbolizer, 'externalgraphicsymbolizer'):
                symbolizer = ExternalGraphicSymbolizer(
                    rule=rule,
                    order=original_symbolizer.externalgraphicsymbolizer.order,
                    opacity=original_symbolizer.externalgraphicsymbolizer.
                    opacity,
                    size=original_symbolizer.externalgraphicsymbolizer.size,
                    rotation=original_symbolizer.externalgraphicsymbolizer.
                    rotation,
                    online_resource=original_symbolizer.
                    externalgraphicsymbolizer.online_resource,
                    format=original_symbolizer.externalgraphicsymbolizer.
                    format,
                )
                symbolizer.save()

            elif hasattr(original_symbolizer, 'polygonsymbolizer'):
                symbolizer = PolygonSymbolizer(
                    rule=rule,
                    order=original_symbolizer.polygonsymbolizer.order,
                    fill=original_symbolizer.polygonsymbolizer.fill,
                    fill_opacity=original_symbolizer.polygonsymbolizer.
                    fill_opacity,
                    stroke=original_symbolizer.polygonsymbolizer.stroke,
                    stroke_width=original_symbolizer.polygonsymbolizer.
                    stroke_width,
                    stroke_opacity=original_symbolizer.polygonsymbolizer.
                    stroke_opacity,
                    stroke_dash_array=original_symbolizer.polygonsymbolizer.
                    stroke_dash_array)
                symbolizer.save()

            elif hasattr(original_symbolizer, 'linesymbolizer'):
                symbolizer = LineSymbolizer(
                    rule=rule,
                    order=original_symbolizer.linesymbolizer.order,
                    stroke=original_symbolizer.linesymbolizer.stroke,
                    stroke_width=original_symbolizer.linesymbolizer.
                    stroke_width,
                    stroke_opacity=original_symbolizer.linesymbolizer.
                    stroke_opacity,
                    stroke_dash_array=original_symbolizer.linesymbolizer.
                    stroke_dash_array)
                symbolizer.save()

            elif hasattr(original_symbolizer, 'marksymbolizer'):
                symbolizer = MarkSymbolizer(
                    rule=rule,
                    order=original_symbolizer.marksymbolizer.order,
                    opacity=original_symbolizer.marksymbolizer.opacity,
                    size=original_symbolizer.marksymbolizer.size,
                    rotation=original_symbolizer.marksymbolizer.rotation,
                    well_known_name=original_symbolizer.marksymbolizer.
                    well_known_name,
                    fill=original_symbolizer.marksymbolizer.fill,
                    fill_opacity=original_symbolizer.marksymbolizer.
                    fill_opacity,
                    stroke=original_symbolizer.marksymbolizer.stroke,
                    stroke_width=original_symbolizer.marksymbolizer.
                    stroke_width,
                    stroke_opacity=original_symbolizer.marksymbolizer.
                    stroke_opacity,
                    stroke_dash_array=original_symbolizer.marksymbolizer.
                    stroke_dash_array)
                symbolizer.save()

    sld_body = sld_builder.build_library_symbol(rule)
    s = mapservice.getStyle(style.name)
    if s is None:
        print "DEBUG: style not exists in Geoserver .. " + style.name
        if mapservice.createStyle(style.name, sld_body):
            mapservice.setLayerStyle(layer, cloned_style_name,
                                     style.is_default)
        else:
            "DEBUG: problem creating style !!!!!" + style.name
    else:
        print "DEBUG: Style exists in Geoserver .. " + style.name
        if not mapservice.createStyle(cloned_style_name, sld_body):
            mapservice.updateStyle(layer, cloned_style_name, sld_body)
        mapservice.setLayerStyle(layer, cloned_style_name, style.is_default)

    return True
Exemplo n.º 3
0
def add_symbol(request, json_rule, library_id, symbol_type):
    gs = geographic_servers.get_instance().get_default_server()

    name = json_rule.get('name')
    title = json_rule.get('title')

    if _valid_name_regex.search(name) == None:
        raise InvalidValue(
            -1,
            _("Invalid name: '{value}'. Name should not contain any special characters"
              ).format(value=name))

    #if _valid_title_regex.search(title) == None:
    #    raise InvalidValue(-1, _("Invalid title: '{value}'. Title should not contain any special characters").format(value=title))

    style = Style(name=name, title=title, is_default=False, type="US")
    style.save()

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

    library = Library.objects.get(id=int(library_id))

    library_rule = LibraryRule(library=library, rule=rule)
    library_rule.save()

    symbs = json_rule.get('symbolizers')
    for sym in symbs:
        json_sym = json.loads(sym.get('json'))
        if symbol_type == 'ExternalGraphicSymbolizer':
            library_path = utils.check_library_path(library)
            file_name = name + '.png'
            if utils.save_external_graphic(library_path,
                                           request.FILES['eg-file'],
                                           file_name):
                online_resource = utils.get_online_resource(library, file_name)
                json_sym['online_resource'] = json_sym[
                    'online_resource'].replace("online_resource_replace",
                                               online_resource)

                symbolizer = ExternalGraphicSymbolizer(
                    rule=rule,
                    order=json_rule.get('order'),
                    opacity=json_sym.get('opacity'),
                    size=json_sym.get('size'),
                    rotation=json_sym.get('rotation'),
                    online_resource=json_sym.get('online_resource'),
                    format=json_sym.get('format'))
                symbolizer.save()

        if symbol_type == 'PolygonSymbolizer':
            symbolizer = PolygonSymbolizer(
                rule=rule,
                order=json_sym.get('order'),
                fill=json_sym.get('fill'),
                fill_opacity=json_sym.get('fill_opacity'),
                stroke=json_sym.get('stroke'),
                stroke_width=json_sym.get('stroke_width'),
                stroke_opacity=json_sym.get('stroke_opacity'),
                stroke_dash_array=json_sym.get('stroke_dash_array'))
            symbolizer.save()

        elif symbol_type == 'LineSymbolizer':
            symbolizer = LineSymbolizer(
                rule=rule,
                order=json_sym.get('order'),
                stroke=json_sym.get('stroke'),
                stroke_width=json_sym.get('stroke_width'),
                stroke_opacity=json_sym.get('stroke_opacity'),
                stroke_dash_array=json_sym.get('stroke_dash_array'))
            symbolizer.save()

        elif symbol_type == 'MarkSymbolizer' or symbol_type == 'PointSymbolizer':
            symbolizer = MarkSymbolizer(
                rule=rule,
                order=json_sym.get('order'),
                opacity=json_sym.get('opacity'),
                size=json_sym.get('size'),
                rotation=json_sym.get('rotation'),
                well_known_name=json_sym.get('well_known_name'),
                fill=json_sym.get('fill'),
                fill_opacity=json_sym.get('fill_opacity'),
                stroke=json_sym.get('stroke'),
                stroke_width=json_sym.get('stroke_width'),
                stroke_opacity=json_sym.get('stroke_opacity'),
                stroke_dash_array=json_sym.get('stroke_dash_array'))
            symbolizer.save()

    sld_body = sld_builder.build_library_symbol(rule)

    if gs.createStyle(style.name, sld_body):
        return True

    else:
        gs.updateStyle(None, style.name, sld_body)
        return True
Exemplo n.º 4
0
def update_symbol(request, json_rule, rule, library_rule):
    try:
        gs = geographic_servers.get_instance().get_default_server()

        name = json_rule.get('name')
        title = json_rule.get('title')

        #if _valid_title_regex.search(title) == None:
        #    raise InvalidValue(-1, _("Invalid title: '{value}'. Title should not contain any special characters").format(value=title))

        rule.title = title
        rule.save()
        symbol_type = None
        eg_file = None

        if 'eg-file' in request.FILES:
            file_name = json_rule.get('file_name').split('/')[-1]
            eg_file = request.FILES['eg-file']

        for s in Symbolizer.objects.filter(rule=rule):
            if get_ftype(s) == 'ExternalGraphicSymbolizer':
                symbol_type = get_ftype(s)
                if eg_file is not None:
                    utils.delete_external_graphic_img(library_rule.library,
                                                      file_name)
            s.delete()

        for sym in json_rule.get('symbolizers'):

            json_sym = json.loads(sym.get('json'))
            if json_sym.get('type') == 'ExternalGraphicSymbolizer':
                library_path = utils.check_library_path(library_rule.library)
                file_name = name + '.png'
                if eg_file is not None:
                    if utils.save_external_graphic(library_path, eg_file,
                                                   file_name):
                        online_resource = utils.get_online_resource(
                            library_rule.library, file_name)
                        json_sym['online_resource'] = json_sym[
                            'online_resource'].replace(
                                "online_resource_replace", online_resource)

                symbolizer = ExternalGraphicSymbolizer(
                    rule=rule,
                    order=json_rule.get('order'),
                    opacity=json_sym.get('opacity'),
                    size=json_sym.get('size'),
                    rotation=json_sym.get('rotation'),
                    online_resource=json_sym.get('online_resource'),
                    format=json_sym.get('format'))
                symbolizer.save()

            if json_sym.get('type') == 'PolygonSymbolizer':
                symbolizer = PolygonSymbolizer(
                    rule=rule,
                    order=json_sym.get('order'),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                    stroke=json_sym.get('stroke'),
                    stroke_width=json_sym.get('stroke_width'),
                    stroke_opacity=json_sym.get('stroke_opacity'),
                    stroke_dash_array=json_sym.get('stroke_dash_array'))
                symbolizer.save()

            elif json_sym.get('type') == 'LineSymbolizer':
                symbolizer = LineSymbolizer(
                    rule=rule,
                    order=json_sym.get('order'),
                    stroke=json_sym.get('stroke'),
                    stroke_width=json_sym.get('stroke_width'),
                    stroke_opacity=json_sym.get('stroke_opacity'),
                    stroke_dash_array=json_sym.get('stroke_dash_array'))
                symbolizer.save()

            elif json_sym.get('type') == 'MarkSymbolizer':
                symbolizer = MarkSymbolizer(
                    rule=rule,
                    order=json_sym.get('order'),
                    opacity=json_sym.get('opacity'),
                    size=json_sym.get('size'),
                    rotation=json_sym.get('rotation'),
                    well_known_name=json_sym.get('well_known_name'),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                    stroke=json_sym.get('stroke'),
                    stroke_width=json_sym.get('stroke_width'),
                    stroke_opacity=json_sym.get('stroke_opacity'),
                    stroke_dash_array=json_sym.get('stroke_dash_array'))
                symbolizer.save()

        style = Style.objects.get(id=rule.style.id)

        if gs.deleteStyle(style.name):
            sld_body = sld_builder.build_library_symbol(rule)
            if not gs.createStyle(style.name, sld_body):
                return False

        return True

    except Exception as e:
        raise e