Skip to content

Commit

Permalink
[Fixes #6596] Incorrect Legend displayed in the layer detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani committed Oct 30, 2020
1 parent 5cf83ad commit 0aa6902
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 60 deletions.
120 changes: 70 additions & 50 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from collections import namedtuple, defaultdict
from os.path import basename, splitext, isfile
from threading import local
from urllib.parse import urlencode, urlsplit, urljoin
from urllib.parse import urlparse, urlencode, urlsplit, urljoin
from pinax.ratings.models import OverallRating
from bs4 import BeautifulSoup
from dialogos.models import Comment
Expand Down Expand Up @@ -368,22 +368,20 @@ def set_layer_style(saved_layer, title, sld, base_file=None):
style = None
if match is None:
try:
cat.create_style(
saved_layer.name, sld, overwrite=False, raw=True, workspace=saved_layer.workspace)
cat.reset()
style = cat.get_style(saved_layer.name, workspace=saved_layer.workspace) or \
cat.get_style(saved_layer.name)
if not style:
style = cat.create_style(
saved_layer.name, sld, overwrite=False, raw=True, workspace=saved_layer.workspace)
except Exception as e:
logger.exception(e)
else:
style = cat.get_style(saved_layer.name, workspace=saved_layer.workspace) or \
cat.get_style(saved_layer.name)
# style.update_body(sld)
try:
cat.create_style(saved_layer.name, sld, overwrite=True, raw=True,
workspace=saved_layer.workspace)
cat.reset()
style = cat.get_style(saved_layer.name, workspace=saved_layer.workspace)
# style.update_body(sld)
style = cat.create_style(saved_layer.name, sld, overwrite=True, raw=True,
workspace=saved_layer.workspace)
except Exception as e:
logger.exception(e)

Expand Down Expand Up @@ -1047,71 +1045,65 @@ def set_styles(layer, gs_catalog):
gs_layer = None
try:
gs_layer = gs_catalog.get_layer(layer.name)
gs_layer.fetch()
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
logger.exception(tb)
if not gs_layer:
try:
gs_layer = gs_catalog.get_layer(layer.alternate or layer.typename)
except Exception:
tb = traceback.format_exc()
logger.debug(tb)

if gs_layer:
default_style = None
try:
default_style = gs_catalog.get_style(
gs_layer.default_style.name, workspace=layer.workspace)
except Exception as e:
raise e
tb = traceback.format_exc()
logger.debug(tb)
logger.exception(tb)
raise e

if gs_layer:
default_style = gs_layer.default_style
if not default_style:
try:
default_style = gs_catalog.get_style(layer.name, workspace=layer.workspace) \
or gs_catalog.get_style(layer.name)
gs_layer.default_style = default_style
gs_catalog.save(gs_layer)
except Exception:
except Exception as e:
tb = traceback.format_exc()
logger.debug(tb)
logger.exception("GeoServer Layer Default Style issues!")
raise e

if default_style:
# make sure we are not using a default SLD (which won't be editable)
if not default_style.workspace or default_style.workspace != layer.workspace:
if not default_style.workspace:
sld_name = default_style.sld_name
sld_body = default_style.sld_body
try:
gs_catalog.create_style(layer.name, sld_body, raw=True, workspace=layer.workspace)
style = gs_catalog.get_style(layer.name, workspace=layer.workspace)
except Exception:
style = gs_catalog.get_style(sld_name, workspace=layer.workspace)
if not style:
style = gs_catalog.create_style(sld_name, sld_body, raw=True, workspace=layer.workspace)
except Exception as e:
tb = traceback.format_exc()
logger.debug(tb)
raise e
else:
style = default_style

if style:
try:
gs_layer.default_style = style
gs_catalog.save(gs_layer)
layer.default_style = save_style(style, layer)
if layer.default_style not in style_set:
style_set.append(layer.default_style)
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
gs_layer.default_style = style
gs_catalog.save(gs_layer)
layer.default_style = save_style(style, layer)

try:
if gs_layer.styles:
alt_styles = gs_layer.styles
for alt_style in alt_styles:
if alt_style:
style_set.append(save_style(alt_style, layer))
except Exception:
if alt_style and alt_style:
_s = save_style(alt_style, layer)
if _s != layer.default_style:
style_set.append(_s)
except Exception as e:
tb = traceback.format_exc()
logger.debug(tb)
raise e
else:
raise Exception("No valid layer found!")

# Remove duplicates
style_set = list(dict.fromkeys(style_set))
Expand All @@ -1125,31 +1117,59 @@ def set_styles(layer, gs_catalog):
Layer.objects.filter(id=layer.id).update(**to_update)
layer.refresh_from_db()

# Legend links
logger.debug(" -- Resource Links[Legend link]...")
from geonode.base.models import Link
layer_legends = Link.objects.filter(resource=layer.resourcebase_ptr, name='Legend')
for style in set(list(layer.styles.all()) + [layer.default_style, ]):
style_name = os.path.basename(
urlparse(style.sld_url).path).split('.')[0]
legend_url = ogc_server_settings.PUBLIC_LOCATION + \
'ows?service=WMS&request=GetLegendGraphic&format=image/png&WIDTH=20&HEIGHT=20&LAYER=' + \
layer.alternate + '&STYLE=' + style_name + \
'&legend_options=fontAntiAliasing:true;fontSize:12;forceLabels:on'

if layer_legends.filter(url=legend_url).count() == 0:
Link.objects.update_or_create(
resource=layer.resourcebase_ptr,
name='Legend',
url=legend_url,
defaults=dict(
extension='png',
url=legend_url,
mime='image/png',
link_type='image',
)
)
logger.debug(" -- Resource Links[Legend link]...done!")

try:
set_geowebcache_invalidate_cache(layer.alternate or layer.typename)
except Exception as e:
logger.exception(e)


def save_style(gs_style, layer):

if not gs_style.workspace or gs_style.workspace != layer.workspace:
sld_body = gs_style.sld_body
style_name = os.path.basename(
urlparse(gs_style.body_href).path).split('.')[0]
sld_name = gs_style.sld_name
sld_body = gs_style.sld_body
if not gs_style.workspace:
try:
gs_catalog.create_style(gs_style.name, sld_body, raw=True, workspace=layer.workspace)
gs_catalog.reset()
gs_style = gs_catalog.get_style(gs_style.name, workspace=layer.workspace)
except Exception:
gs_style = gs_catalog.get_style(style_name, workspace=layer.workspace)
if not gs_style:
gs_style = gs_catalog.create_style(style_name, sld_body, raw=True, workspace=layer.workspace)
except Exception as e:
tb = traceback.format_exc()
logger.debug(tb)

raise e
style = None
try:
style, created = Style.objects.get_or_create(name=gs_style.name)
style, created = Style.objects.get_or_create(name=style_name)
if style:
if not style.workspace and gs_style.workspace:
style.workspace = layer.workspace
style.sld_title = gs_style.sld_title or gs_style.sld_name
style.sld_title = gs_style.sld_title or sld_name
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
Expand Down
5 changes: 2 additions & 3 deletions geonode/geoserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def layer_style_manage(request, layername):
except Exception:
tb = traceback.format_exc()
logger.debug(tb)

default_style = (def_sld_name, def_sld_title)
default_style = (def_sld_name, def_sld_title)
layer_styles.append(default_style)

return render(
request,
Expand Down Expand Up @@ -272,7 +272,6 @@ def layer_style_manage(request, layername):
elif request.method in ('POST', 'PUT', 'DELETE'):
try:
selected_styles = request.POST.getlist('style-select')

default_style = request.POST['default_style']

# Save to GeoServer
Expand Down
10 changes: 7 additions & 3 deletions geonode/layers/templates/layers/layer_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{% load guardian_tags %}
{% load client_lib_tags %}
{% load proxy_lib_tags %}
{% load layer_tags %}

{% block title %}{{ resource.title|default:resource.alternate }} — {{ block.super }}{% endblock %}

Expand Down Expand Up @@ -586,10 +587,12 @@ <h5 class="modal-title" id="myModalLabel">{% trans "Attribute Information" %}</h
<li class="list-group-item">
<h4 class="list-group-item-heading">{%trans "Legend" %}</h4>
{% if resource.default_style %}
{% for style in resource.styles.all %}
{% get_all_resource_styles resource as resource_styles_all%}
{% for style in resource_styles_all %}
{% if resource.default_style == style or resource.default_style.name == style.name or resource.default_style.sld_title == style.sld_title %}
{% for legend in resource.get_legend %}
{% with "STYLE="|add:style.name as style_name %}
{% get_sld_name_from_url style.sld_url as sld_name %}
{% with "STYLE="|add:sld_name as style_name %}
{% if legend.link_type == 'image' and style_name in legend.url %}
<p id="legend_sld_title" style="font-size:10px">{{ style.sld_title|default:style.name }}</p>
{% if request.user.is_authenticated and access_token %}
Expand Down Expand Up @@ -666,7 +669,8 @@ <h4>{% trans "Styles" %}</h4>
<p>{% trans "The following styles are associated with this layer. Choose a style to view it in the preview map." %}</p>
<ul class="list-unstyled">
{% if OGC_SERVER.default.BACKEND == 'geonode.geoserver' %}
{% for style in resource.styles.all %}
{% get_all_resource_styles resource as resource_styles_all%}
{% for style in resource_styles_all %}
<li>
{% if resource.default_style == style %}
<input type="radio" checked name="style" id="{{ style.name }}" value="{{ style.sld_title|default_if_none:style.name }}"/>
Expand Down
14 changes: 13 additions & 1 deletion geonode/layers/templatetags/layer_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################

import os
from django import template
from django.conf import settings
from urllib.parse import urlparse
from inflection import camelize as do_camelize

register = template.Library()
Expand All @@ -46,3 +47,14 @@ def crs_labels(value): # Only one argument.
if value in settings.EPSG_CODE_MATCHES.keys():
return settings.EPSG_CODE_MATCHES[value]
return value


@register.simple_tag
def get_all_resource_styles(resource):
return set(list(resource.styles.all()) + [resource.default_style, ])


@register.simple_tag
def get_sld_name_from_url(sld_url):
return os.path.basename(
urlparse(sld_url).path).split('.')[0]
4 changes: 3 additions & 1 deletion geonode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,9 +1787,11 @@ def set_resource_default_links(instance, layer, prune=False, **kwargs):
# Legend link
logger.debug(" -- Resource Links[Legend link]...")
for style in instance.styles.all():
style_name = os.path.basename(
urlparse(style.sld_url).path).split('.')[0]
legend_url = ogc_server_settings.PUBLIC_LOCATION + \
'ows?service=WMS&request=GetLegendGraphic&format=image/png&WIDTH=20&HEIGHT=20&LAYER=' + \
instance.alternate + '&STYLE=' + style.name + \
instance.alternate + '&STYLE=' + style_name + \
'&legend_options=fontAntiAliasing:true;fontSize:12;forceLabels:on'

if Link.objects.filter(resource=instance.resourcebase_ptr, url=legend_url).count() < 2:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ geonode-announcements==2.0.2
geonode-django-activity-stream==0.9.0
gn-arcrest==10.5.3
geonode-dialogos==3.0.1
geoserver-restconfig==2.0.4.6
geoserver-restconfig==2.0.4.7
gn-gsimporter==2.0.1
gisdata==0.5.4

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ install_requires =
geonode-django-activity-stream==0.9.0
gn-arcrest==10.5.3
geonode-dialogos==3.0.1
geoserver-restconfig==2.0.4.6
geoserver-restconfig==2.0.4.7
gn-gsimporter==2.0.1
gisdata==0.5.4

Expand Down

0 comments on commit 0aa6902

Please sign in to comment.