Example #1
0
def create_style(style_name,
                 style_title,
                 is_default,
                 sld,
                 layer,
                 gs,
                 is_preview=False):
    if is_preview:
        style_name = style_name + '__tmp'
    is_default = utils.set_default_style(layer,
                                         gs,
                                         is_preview=is_preview,
                                         is_default=is_default)

    style = Style(name=style_name,
                  title=style_title,
                  is_default=is_default,
                  minscale=-1,
                  maxscale=-1,
                  type='CS',
                  sld=sld)
    style.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    sld_body = sld

    if is_preview:
        if gs.createOverwrittenStyle(style.name, sld_body, True):
            return style
    else:
        if gs.createStyle(style.name, sld_body):
            if not is_preview:
                gs.setLayerStyle(layer, style.name, style.is_default)
            return style
Example #2
0
def create_style(style_name,
                 style_title,
                 is_default,
                 sld,
                 layer_id,
                 is_preview=False):

    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)
    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

    if is_preview:
        style_name = style_name + '__tmp'
        is_default = False

    style = Style(name=style_name,
                  title=style_title,
                  is_default=is_default,
                  minscale=-1,
                  maxscale=-1,
                  type='CS',
                  sld=sld)
    style.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    sld_body = sld

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

        else:
            return False
Example #3
0
def clone_layer_styles(mapservice, source_layer, target_layer):
    for style_layer in StyleLayer.objects.filter(
            layer=source_layer).order_by("-style__is_default"):
        style = style_layer.style
        # TODO: decide the new style name
        source_layer.datastore.workspace.name
        base_name = style.name.replace(
            source_layer.datastore.workspace.name + "_", "")
        new_style_name = target_layer.datastore.workspace.name + "_" + base_name
        salt = ''
        i = 0
        while mapservice.getStyle(new_style_name) is not None:
            new_style_name = target_layer.datastore.workspace.name + "_" + base_name + "_" + str(
                i) + salt
            i = i + 1
            if (i % 1000) == 0:
                salt = '_' + get_random_string(3)
        new_style = clone_layer_style(style,
                                      target_layer,
                                      new_style_name=new_style_name)
        new_style_layer = StyleLayer()
        new_style_layer.layer = target_layer
        new_style_layer.style = new_style
        new_style_layer.save()
        sld_body = sld_builder.build_sld(target_layer, new_style)
        if mapservice.createStyle(new_style.name, sld_body):
            mapservice.setLayerStyle(target_layer, new_style.name,
                                     new_style.is_default)
        else:
            # TODO: manage errors
            print "DEBUG: Problem cloning style .." + new_style.name
Example #4
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 #5
0
def create_style(request, json_data, layer, gs, is_preview=False):
    name = json_data.get('name')
    if is_preview:
        name = name + '__tmp'
    is_default = json_data.get('is_default', False)
    is_default = utils.set_default_style(layer,
                                         gs,
                                         is_preview=is_preview,
                                         is_default=is_default)

    style = Style(name=name,
                  title=json_data.get('title'),
                  is_default=is_default,
                  minscale=json_data.get('minscale'),
                  maxscale=json_data.get('maxscale'),
                  type='US')
    style.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    for r in json_data.get('rules'):
        json_rule = r.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()

        for sym in r.get('symbolizers'):
            if sym.get('type') == 'PolygonSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = PolygonSymbolizer(
                    rule=rule,
                    order=int(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 sym.get('type') == 'LineSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = LineSymbolizer(
                    rule=rule,
                    order=int(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 sym.get('type') == 'MarkSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = MarkSymbolizer(
                    rule=rule,
                    order=int(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()

            elif sym.get('type') == 'ExternalGraphicSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = ExternalGraphicSymbolizer(
                    rule=rule,
                    order=int(sym.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()

            elif sym.get('type') == 'TextSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = TextSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    label=json_sym.get('label'),
                    font_family=json_sym.get('font_family'),
                    font_size=json_sym.get('font_size'),
                    font_weight=json_sym.get('font_weight'),
                    font_style=json_sym.get('font_style'),
                    halo_fill=json_sym.get('halo_fill'),
                    halo_fill_opacity=json_sym.get('halo_fill_opacity'),
                    halo_radius=json_sym.get('halo_radius'),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                )
                symbolizer.save()

    sld_body = sld_builder.build_sld(layer, style)

    if is_preview:
        if gs.createOverwrittenStyle(style.name, sld_body, True):
            return style
    else:
        if gs.createStyle(style.name, sld_body):
            if not is_preview:
                gs.setLayerStyle(layer, style.name, style.is_default)
            return style
def create_style(request, json_data, layer_id, is_preview=False):

    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='IN')
    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.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    for r in json_data.get('rules'):
        json_rule = r.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()

        for sym in r.get('symbolizers'):
            if sym.get('type') == 'PolygonSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = PolygonSymbolizer(
                    rule=rule,
                    order=int(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 sym.get('type') == 'LineSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = LineSymbolizer(
                    rule=rule,
                    order=int(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 sym.get('type') == 'MarkSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = MarkSymbolizer(
                    rule=rule,
                    order=int(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()

            elif sym.get('type') == 'ExternalGraphicSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = ExternalGraphicSymbolizer(
                    rule=rule,
                    order=int(sym.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()

            elif sym.get('type') == 'TextSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = TextSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    label=json_sym.get('label'),
                    font_family=json_sym.get('font_family'),
                    font_size=json_sym.get('font_size'),
                    font_weight=json_sym.get('font_weight'),
                    font_style=json_sym.get('font_style'),
                    halo_fill=json_sym.get('halo_fill'),
                    halo_fill_opacity=json_sym.get('halo_fill_opacity'),
                    halo_radius=json_sym.get('halo_radius'),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                )
                symbolizer.save()

    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.createStyle(style.name, sld_body):
            if not is_preview:
                gs.setLayerStyle(layer, style.name, style.is_default)
            return True

        else:
            return False
Example #7
0
def create_default_style(layer_id, style_name, style_type, geom_type, count):
    layer = Layer.objects.get(id=int(layer_id))

    minscaledenominator = -1
    maxscaledenominator = -1
    if count and count > 200000:
        minscaledenominator = 0
        maxscaledenominator = 50000

    style = Style(name=style_name,
                  title=_('Default style for: ') + layer.title,
                  is_default=True,
                  type=style_type,
                  minscale=minscaledenominator,
                  maxscale=maxscaledenominator)
    style.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    if style.is_default:
        layer_styles = StyleLayer.objects.filter(layer=layer)
        for ls in layer_styles:
            s = Style.objects.get(id=ls.style.id)
            s.is_default = False

    symbol_type = None
    if geom.isPoint(geom_type):
        symbol_type = 'MarkSymbolizer'
    elif geom.isLine(geom_type):
        symbol_type = 'LineSymbolizer'
    elif geom.isPolygon(geom_type):
        symbol_type = 'PolygonSymbolizer'
    elif geom.isRaster(geom_type):
        symbol_type = 'RasterSymbolizer'

    rule = Rule(style=style,
                name='Default symbol',
                title=_('Default symbol'),
                abstract='',
                filter=str(""),
                minscale=minscaledenominator,
                maxscale=maxscaledenominator,
                order=0)
    rule.save()

    if symbol_type == 'PolygonSymbolizer':
        symbolizer = PolygonSymbolizer(rule=rule,
                                       order=0,
                                       fill='#383838',
                                       fill_opacity=0.6,
                                       stroke='#000000',
                                       stroke_width=1,
                                       stroke_opacity=1.0,
                                       stroke_dash_array='none')
        symbolizer.save()

    elif symbol_type == 'LineSymbolizer':
        symbolizer = LineSymbolizer(rule=rule,
                                    order=0,
                                    stroke='#000000',
                                    stroke_width=1,
                                    stroke_opacity=1.0,
                                    stroke_dash_array='none')
        symbolizer.save()

    elif symbol_type == 'MarkSymbolizer':
        symbolizer = MarkSymbolizer(rule=rule,
                                    order=0,
                                    opacity=1.0,
                                    size=8,
                                    rotation=0,
                                    well_known_name='circle',
                                    fill='#383838',
                                    fill_opacity=0.6,
                                    stroke='#000000',
                                    stroke_width=1,
                                    stroke_opacity=1.0,
                                    stroke_dash_array='none')
        symbolizer.save()

    elif symbol_type == 'RasterSymbolizer':
        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()

    sld_body = sld_builder.build_sld(layer, style)
    return sld_body
Example #8
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
Example #9
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