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 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 mapservice.createOverwrittenStyle(style.name, sld_body, True): return True else: return False else: if mapservice.createOverwrittenStyle(style.name, sld_body, True): if not is_preview: mapservice.setLayerStyle(layer, style.name, style.is_default) return True else: return False
def survey_section_update_project_operation(request, survey, section, lyorder): if section.layer_id != None: layer_delete_operation(request, section.layer_id) try: mapservice_backend.deleteTable(survey.datastore, section.name) mapservice_backend.deleteResource(survey.datastore.workspace, survey.datastore, section.name) except: pass geom_type = 'Point' fields = [] field_defs = [] field_definitions = SURVEY_FUNCTIONS if section.definition: definitions = json.loads(section.definition) for definition in definitions: form_name = definition["formname"] for item in definition['formitems']: item_type = item['type'] for db_item in field_definitions: for key in db_item: if key == item_type: db_type = db_item[key]['db_type'] if db_type != None and db_type.__len__() > 0: aux = { 'id': str(section.id) + '_' + form_name + '_' + item['key'], 'name': form_name + '_' + item['key'], 'type': db_type } fields.append(aux) field_def = {} field_def[ 'name'] = form_name + '_' + item['key'] for id, language in LANGUAGES: field_def['title-' + id] = item['title'] field_def['visible'] = True field_def['editableactive'] = True field_def['editable'] = True for control_field in settings.CONTROL_FIELDS: if field_def['name'] == control_field[ 'name']: field_def['editableactive'] = False field_def['editable'] = False field_def['infovisible'] = False field_defs.append(field_def) section.name = prepare_string(section.name) mapservice_backend.createTableFromFields(survey.datastore, section.name, geom_type, section.srs, fields) # first create the resource on the backend try: mapservice_backend.createResource(survey.datastore.workspace, survey.datastore, section.name, section.title) except: pass try: mapservice_backend.setQueryable(survey.datastore.workspace.name, survey.datastore.name, survey.datastore.type, section.name, True) except: pass layer = Layer( datastore=survey.datastore, layer_group=survey.layer_group, name=section.name, title=section.title, abstract=section.title, type='v_PostGIS', visible=True, queryable=True, cached=False, single_image=False, time_enabled=False, highlight=False, order=lyorder, created_by=request.user.username, ) layer.save() style_name = survey.datastore.workspace.name + '_' + layer.name + '_default' mapservice_backend.createDefaultStyle(layer, style_name) mapservice_backend.setLayerStyle(layer, style_name, True) layer = mapservice_backend.updateThumbnail(layer, 'create') layer.save() section.layer_id = layer.id section.save() layer_conf = {'fields': field_defs} layer.conf = layer_conf layer.save() core_utils.toc_add_layer(layer) mapservice_backend.createOrUpdateGeoserverLayerGroup(survey.layer_group) mapservice_backend.reload_nodes() permissionsr = SurveyReadGroup.objects.filter(survey=survey) permissionsw = SurveyWriteGroup.objects.filter(survey=survey) groupsr = [] groupsw = [] for permission in permissionsr: groupsr.append(permission.user_group) try: lwr = LayerReadGroup() lwr.layer = section.layer lwr.group = permission.user_group lwr.save() except: pass for permission in permissionsw: groupsw.append(permission.user_group) try: lwg = LayerWriteGroup() lwg.layer = section.layer lwg.group = permission.user_group lwg.save() except: pass mapservice_backend.setLayerDataRules(layer, groupsr, groupsw) mapservice_backend.reload_nodes()
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 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='UV') 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 mapservice.createOverwrittenStyle(style.name, sld_body, True): return True else: return False else: if mapservice.createStyle(style.name, sld_body): if not is_preview: mapservice.setLayerStyle(layer, style.name, style.is_default) return True else: return False