Beispiel #1
0
def upload_library(name, description, file):
    gs = geographic_servers.get_instance().get_default_server()

    library = Library(name=name, description=description)
    library.save()

    library_path = utils.check_library_path(library)
    file_path = utils.__get_uncompressed_file_upload_path(file)
    utils.copyrecursively(file_path + "/resources/", library_path)

    file_list = os.listdir(file_path)
    for file in file_list:
        if not os.path.isdir(file_path + "/" + file):
            f = open(file_path + "/" + file, 'r')
            sld = sld_reader.parse(f)

            style_name = remove_accents(sld.NamedLayer[0].Name)
            sld.NamedLayer[0].Name = style_name
            style_title = sld.NamedLayer[0].UserStyle[0].FeatureTypeStyle[
                0].Rule[0].Title
            r = sld.NamedLayer[0].UserStyle[0].FeatureTypeStyle[0].Rule[0]

            style = Style(name=style_name,
                          title=style_title,
                          is_default=True,
                          type="US")
            style.save()

            rule = Rule(style=style,
                        name=style_name,
                        title=style_title,
                        abstract='',
                        filter=str(""),
                        minscale=-1 if r.MinScaleDenominator is None else
                        r.MinScaleDenominator,
                        maxscale=-1 if r.MaxScaleDenominator is None else
                        r.MaxScaleDenominator,
                        order=0)
            rule.save()

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

            scount = r.Symbolizer.__len__() - 1
            for s in r.Symbolizer:
                if s.original_tagname_ == 'PointSymbolizer':
                    opacity = s.Graphic.Opacity.valueOf_
                    rotation = s.Graphic.Rotation.valueOf_
                    size = s.Graphic.Size.valueOf_
                    if len(s.Graphic.Mark) >= 1:
                        mark = s.Graphic.Mark[0]

                        stroke = '#000000'
                        if mark.Stroke.CssParameter.__len__() > 0:
                            stroke = mark.Stroke.CssParameter[0].valueOf_

                        stroke_width = 1
                        if mark.Stroke.CssParameter.__len__() > 1:
                            stroke_width = mark.Stroke.CssParameter[1].valueOf_

                        stroke_opacity = 1
                        if mark.Stroke.CssParameter.__len__() > 2:
                            stroke_opacity = mark.Stroke.CssParameter[
                                2].valueOf_

                        stroke_dash_array = 'none'
                        if mark.Stroke.CssParameter.__len__() > 3:
                            stroke_dash_array = mark.Stroke.CssParameter[
                                3].valueOf_

                        symbolizer = MarkSymbolizer(
                            rule=rule,
                            order=scount,
                            opacity=opacity,
                            size=size,
                            rotation=rotation,
                            well_known_name=mark.WellKnownName,
                            fill=mark.Fill.CssParameter[0].valueOf_,
                            fill_opacity=mark.Fill.CssParameter[1].valueOf_,
                            stroke=stroke,
                            stroke_width=stroke_width,
                            stroke_opacity=stroke_opacity,
                            stroke_dash_array=stroke_dash_array)
                        symbolizer.save()

                    if len(s.Graphic.ExternalGraphic) >= 1:
                        external_graphic = s.Graphic.ExternalGraphic[0]
                        online_resource = external_graphic.OnlineResource.href.split(
                            '/')
                        online_resource[-2] = library.name
                        new_online_resource = settings.MEDIA_URL + online_resource[
                            -3] + '/' + library.name + '/' + remove_accents(
                                online_resource[-1])
                        symbolizer = ExternalGraphicSymbolizer(
                            rule=rule,
                            order=scount,
                            opacity=opacity,
                            size=size,
                            rotation=rotation,
                            online_resource=new_online_resource,
                            format=external_graphic.Format)
                        symbolizer.save()

                elif s.original_tagname_ == 'LineSymbolizer':
                    stroke = '#000000'
                    if s.Stroke:
                        if s.Stroke.CssParameter.__len__() > 0:
                            stroke = s.Stroke.CssParameter[0].valueOf_

                        stroke_width = 1
                        if s.Stroke.CssParameter.__len__() > 1:
                            stroke_width = s.Stroke.CssParameter[1].valueOf_

                        stroke_opacity = 1
                        if s.Stroke.CssParameter.__len__() > 2:
                            stroke_opacity = s.Stroke.CssParameter[2].valueOf_

                        stroke_dash_array = 'none'
                        if s.Stroke.CssParameter.__len__() > 3:
                            stroke_dash_array = s.Stroke.CssParameter[
                                3].valueOf_

                        symbolizer = LineSymbolizer(
                            rule=rule,
                            order=scount,
                            stroke=stroke,
                            stroke_width=stroke_width,
                            stroke_opacity=stroke_opacity,
                            stroke_dash_array=stroke_dash_array)
                        symbolizer.save()

                elif s.original_tagname_ == 'PolygonSymbolizer':
                    stroke = '#000000'
                    if s.Stroke.CssParameter.__len__() > 0:
                        stroke = s.Stroke.CssParameter[0].valueOf_

                    stroke_width = 1
                    if s.Stroke.CssParameter.__len__() > 1:
                        stroke_width = s.Stroke.CssParameter[1].valueOf_

                    stroke_opacity = 1
                    if s.Stroke.CssParameter.__len__() > 2:
                        stroke_opacity = s.Stroke.CssParameter[2].valueOf_

                    stroke_dash_array = 'none'
                    if s.Stroke.CssParameter.__len__() > 3:
                        stroke_dash_array = s.Stroke.CssParameter[3].valueOf_

                    symbolizer = PolygonSymbolizer(
                        rule=rule,
                        order=scount,
                        fill=s.Fill.CssParameter[0].valueOf_,
                        fill_opacity=s.Fill.CssParameter[1].valueOf_,
                        stroke=stroke,
                        stroke_width=stroke_width,
                        stroke_opacity=stroke_opacity,
                        stroke_dash_array=stroke_dash_array)
                    symbolizer.save()

                scount -= 1

            output = StringIO.StringIO()
            sld.export(output, 0)
            sld_body = output.getvalue()
            output.close()

            gs.createStyle(style.name, sld_body)
    gs.reload_nodes()

    utils.__delete_temporaries(file_path)
Beispiel #2
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
Beispiel #3
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