Skip to content

Commit

Permalink
Merge pull request #3036 from GeoNode/ISSUE_3035
Browse files Browse the repository at this point in the history
 - Fix for Issue #3035: Upload Layer: the check on default style is w…
  • Loading branch information
Alessio Fabiani committed May 10, 2017
2 parents a15a85f + 232865a commit 6c9f006
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
40 changes: 33 additions & 7 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,34 @@ def _style_name(resource):
return _punc.sub("_", resource.store.workspace.name + ":" + resource.name)


def get_sld_for(layer):
# FIXME: GeoServer sometimes fails to associate a style with the data, so
def get_sld_for(gs_catalog, layer):
# GeoServer sometimes fails to associate a style with the data, so
# for now we default to using a point style.(it works for lines and
# polygons, hope this doesn't happen for rasters though)
name = layer.default_style.name if layer.default_style is not None else "point"
if layer.default_style is None:
gs_catalog._cache.clear()
layer = gs_catalog.get_layer(layer.name)
name = layer.default_style.name if layer.default_style is not None else "raster"

# Detect geometry type if it is a FeatureType
if layer.resource.resource_type == 'featureType':
res = layer.resource
res.fetch()
ft = res.store.get_resources(res.name)
ft.fetch()
for attr in ft.dom.find("attributes").getchildren():
attr_binding = attr.find("binding")
if "jts.geom" in attr_binding.text:
if "Polygon" in attr_binding.text:
name = "polygon"
elif "Line" in attr_binding.text:
name = "line"
else:
name = "point"

# FIXME: When gsconfig.py exposes the default geometry type for vector
# layers we should use that rather than guessing based on the auto-detected
# style.

if name in _style_templates:
fg, bg, mark = _style_contexts.next()
return _style_templates[name] % dict(
Expand All @@ -213,7 +231,7 @@ def fixup_style(cat, resource, style):
logger.info("%s uses a default style, generating a new one", lyr)
name = _style_name(resource)
if style is None:
sld = get_sld_for(lyr)
sld = get_sld_for(cat, lyr)
else:
sld = style.read()
logger.info("Creating style [%s]", name)
Expand Down Expand Up @@ -746,7 +764,15 @@ def set_attributes_from_geoserver(layer, overwrite=False):
def set_styles(layer, gs_catalog):
style_set = []
gs_layer = gs_catalog.get_layer(layer.name)
default_style = gs_layer.default_style
if gs_layer.default_style:
default_style = gs_layer.default_style
else:
default_style = gs_catalog.get_style(layer.name)
try:
gs_layer.default_style = default_style
gs_catalog.save(gs_layer)
except Exception as e:
logger.exception("GeoServer Layer Default Style issues!")
layer.default_style = save_style(default_style)
# FIXME: This should remove styles that are no longer valid
style_set.append(layer.default_style)
Expand Down Expand Up @@ -1168,7 +1194,7 @@ def geoserver_upload(
sld = f.read()
f.close()
else:
sld = get_sld_for(publishing)
sld = get_sld_for(cat, publishing)

style = None
if sld is not None:
Expand Down
32 changes: 18 additions & 14 deletions geonode/upload/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,12 @@ def final_step(upload_session, user):
sld = f.read()
f.close()
else:
sld = get_sld_for(publishing)
sld = get_sld_for(cat, publishing)

style = None
# print " **************************************** "
if sld is not None:
try:
cat.create_style(name, sld)
style = cat.get_style(name)
except geoserver.catalog.ConflictingDataError as e:
msg = 'There was already a style named %s in GeoServer, try using another name: "%s"' % (
name, str(e))
Expand All @@ -625,19 +623,25 @@ def final_step(upload_session, user):
logger.error(msg)
e.args = (msg,)

# what are we doing with this var?
msg = 'No style could be created for the layer, falling back to POINT default one'
if style is None:
try:
style = cat.get_style(name + '_layer')
style = cat.get_style(name)
except:
style = cat.get_style('point')
logger.warn(msg)
e.args = (msg,)

# FIXME: Should we use the fully qualified typename?
publishing.default_style = style
_log('default style set to %s', name)
cat.save(publishing)
logger.warn('Could not retreive the Layer default Style name')
# what are we doing with this var?
msg = 'No style could be created for the layer, falling back to POINT default one'
try:
style = cat.get_style(name + '_layer')
except:
style = cat.get_style('point')
logger.warn(msg)
e.args = (msg,)

if style:
# FIXME: Should we use the fully qualified typename?
publishing.default_style = style
_log('default style set to %s', name)
cat.save(publishing)

_log('Creating Django record for [%s]', name)
target = task.target
Expand Down

0 comments on commit 6c9f006

Please sign in to comment.