Beispiel #1
0
def sld_import(name, is_default, layer_id, file, mapservice):

    layer = Layer.objects.get(id=int(layer_id))

    ### write the data to a temp file
    tup = tempfile.mkstemp()  # make a tmp file
    f = os.fdopen(tup[0], 'w')  # open the tmp file for writing
    f.write(file.read())  # write the tmp file
    f.close()

    filepath = tup[1]
    tmp_sld = open(filepath, 'r')

    sld = sld_builder.parse_sld(tmp_sld)

    style = Style(name=name,
                  title=sld.NamedLayer[0].UserStyle[0].Title,
                  is_default=is_default,
                  type="EX")
    style.save()

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

    rules = sld.NamedLayer[0].UserStyle[0].FeatureTypeStyle[0].Rule
    for r in rules:
        filter = utils.filter_to_json(r.Filter)
        rule = Rule(style=style,
                    name=r.Name,
                    title=r.Title,
                    abstract='',
                    filter=json.dumps(filter),
                    minscale=-1 if r.MinScaleDenominator is None else
                    r.MinScaleDenominator,
                    maxscale=-1 if r.MaxScaleDenominator is None else
                    r.MaxScaleDenominator,
                    order=0)
        rule.save()

        scount = 0
        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]

                    fill = '#383838'
                    fill_opacity = 0.5
                    if len(mark.Fill.CssParameter) > 0:
                        for css_parameter in mark.Fill.CssParameter:
                            if css_parameter.name == 'fill':
                                fill = css_parameter.valueOf_
                            if css_parameter.name == 'fill-opacity':
                                fill_opacity = css_parameter.valueOf_

                    stroke = '#ffffff'
                    stroke_width = 1
                    stroke_opacity = 0.0
                    stroke_dash_array = 'none'
                    if len(mark.Stroke.CssParameter) > 0:
                        for css_parameter in mark.Stroke.CssParameter:
                            if css_parameter.name == 'stroke':
                                stroke = css_parameter.valueOf_
                            if css_parameter.name == 'stroke-width':
                                stroke_width = css_parameter.valueOf_
                            if css_parameter.name == 'stroke-opacity':
                                stroke_opacity = css_parameter.valueOf_
                            if css_parameter.name == 'stroke-dasharray':
                                stroke_dash_array = css_parameter.valueOf_

                    symbolizer = MarkSymbolizer(
                        rule=rule,
                        order=scount,
                        opacity=opacity,
                        size=size,
                        rotation=rotation,
                        well_known_name=mark.WellKnownName,
                        fill=fill,
                        fill_opacity=fill_opacity,
                        stroke=stroke,
                        stroke_width=stroke_width,
                        stroke_opacity=stroke_opacity,
                        stroke_dash_array=stroke_dash_array)
                    symbolizer.save()

                if len(s.Graphic.ExternalGraphic) >= 1:
                    print 'ExternalGraphic'

            elif s.original_tagname_ == 'LineSymbolizer':
                stroke = '#ffffff'
                stroke_width = 1
                stroke_opacity = 0.0
                stroke_dash_array = 'none'
                if len(s.Stroke.CssParameter) > 0:
                    for css_parameter in s.Stroke.CssParameter:
                        if css_parameter.name == 'stroke':
                            stroke = css_parameter.valueOf_
                        if css_parameter.name == 'stroke-width':
                            stroke_width = css_parameter.valueOf_
                        if css_parameter.name == 'stroke-opacity':
                            stroke_opacity = css_parameter.valueOf_
                        if css_parameter.name == 'stroke-dasharray':
                            stroke_dash_array = css_parameter.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':

                fill = '#383838'
                fill_opacity = 0.5
                if len(s.Fill.CssParameter) > 0:
                    for css_parameter in s.Fill.CssParameter:
                        if css_parameter.name == 'fill':
                            fill = css_parameter.valueOf_
                        if css_parameter.name == 'fill-opacity':
                            fill_opacity = css_parameter.valueOf_

                stroke = '#ffffff'
                stroke_width = 1
                stroke_opacity = 0.0
                stroke_dash_array = 'none'
                if len(s.Stroke.CssParameter) > 0:
                    for css_parameter in s.Stroke.CssParameter:
                        if css_parameter.name == 'stroke':
                            stroke = css_parameter.valueOf_
                        if css_parameter.name == 'stroke-width':
                            stroke_width = css_parameter.valueOf_
                        if css_parameter.name == 'stroke-opacity':
                            stroke_opacity = css_parameter.valueOf_
                        if css_parameter.name == 'stroke-dasharray':
                            stroke_dash_array = css_parameter.valueOf_

                symbolizer = PolygonSymbolizer(
                    rule=rule,
                    order=scount,
                    fill=fill,
                    fill_opacity=fill_opacity,
                    stroke=stroke,
                    stroke_width=stroke_width,
                    stroke_opacity=stroke_opacity,
                    stroke_dash_array=stroke_dash_array)
                symbolizer.save()

            scount += 1

    sld_body = sld_builder.build_sld(layer, style)
    if mapservice.createStyle(style.name, sld_body):
        mapservice.setLayerStyle(layer, style.name, style.is_default)
        utils.__delete_temporaries(filepath)
        return True

    else:
        utils.__delete_temporaries(filepath)
        return False
Beispiel #2
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)