Skip to content

Commit

Permalink
- ImageMosaics refactoring: first step - support ZIP archives with g…
Browse files Browse the repository at this point in the history
…ranules and .properties files
  • Loading branch information
afabiani committed May 23, 2018
1 parent 4a346bd commit e4774bf
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 219 deletions.
30 changes: 26 additions & 4 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,12 @@ def set_time_info(layer, attribute, end_attribute, presentation,
layer = gs_catalog.get_layer(layer.name)
if layer is None:
raise ValueError('no such layer: %s' % layer.name)
resource = layer.resource
resource = layer.resource if layer else None
if not resource:
resources = gs_catalog.get_resources(store=layer.name)
if resources:
resource = resources[0]

resolution = None
if precision_value and precision_step:
resolution = '%s %s' % (precision_value, precision_step)
Expand All @@ -1632,6 +1637,7 @@ def set_time_info(layer, attribute, end_attribute, presentation,
else:
metadata = dict({})
metadata['time'] = info

if resource and resource.metadata:
resource.metadata = metadata
if resource:
Expand All @@ -1648,7 +1654,12 @@ def get_time_info(layer):
layer = gs_catalog.get_layer(layer.name)
if layer is None:
raise ValueError('no such layer: %s' % layer.name)
resource = layer.resource
resource = layer.resource if layer else None
if not resource:
resources = gs_catalog.get_resources(store=layer.name)
if resources:
resource = resources[0]

info = resource.metadata.get('time', None) if resource.metadata else None
vals = None
if info:
Expand Down Expand Up @@ -1777,10 +1788,11 @@ def mosaic_delete_first_granule(cat, layer):
cat.mosaic_delete_granule(coverages['coverages']['coverage'][0]['name'], store, granule_id)


def set_time_dimension(cat, layer, time_presentation, time_presentation_res, time_presentation_default_value,
def set_time_dimension(cat, name, workspace, time_presentation, time_presentation_res, time_presentation_default_value,
time_presentation_reference_value):
# configure the layer time dimension as LIST
cat._cache.clear()
# cat.reload()

presentation = time_presentation
if not presentation:
Expand All @@ -1797,7 +1809,17 @@ def set_time_dimension(cat, layer, time_presentation, time_presentation_res, tim
timeInfo = DimensionInfo("time", "true", presentation, resolution, "ISO8601", None, attribute="time",
strategy=strategy, reference_value=time_presentation_reference_value)

resource = cat.get_layer(layer).resource
layer = cat.get_layer(name)
resource = layer.resource if layer else None
if not resource:
resources = cat.get_resources(store=name) or cat.get_resources(store=name, workspace=workspace)
if resources:
resource = resources[0]

if not resource:
logger.exception("No resource could be found on GeoServer with name %s" % name)
raise Exception("No resource could be found on GeoServer with name %s" % name)

resource.metadata = {'time': timeInfo}
cat.save(resource)

Expand Down
15 changes: 15 additions & 0 deletions geonode/upload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################


class UploadException(Exception):

'''A handled exception meant to be presented to the user'''

@staticmethod
def from_exc(msg, ex):
args = [msg]
args.extend(ex.args)
return UploadException(*args)


class LayerNotReady(Exception):
pass
28 changes: 17 additions & 11 deletions geonode/upload/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from geoserver.resource import FeatureType
from geoserver.resource import Coverage

from django.utils.translation import ugettext as _

from UserList import UserList
import zipfile
import os
Expand Down Expand Up @@ -117,8 +119,9 @@ def __repr__(self):

TYPE_UNKNOWN = FileType("unknown", None, None)

_keep_original_data = ('kmz', 'zip-mosaic')
_tif_extensions = ("tif", "tiff", "geotif", "geotiff")
_mosaics_extensions = ("properties", "shp")
_mosaics_extensions = ("properties", "shp", "aux")

types = [
FileType("Shapefile", "shp", vector,
Expand All @@ -127,8 +130,8 @@ def __repr__(self):
aliases=_tif_extensions[1:]),
FileType(
"ImageMosaic", "zip-mosaic", raster,
aliases=_tif_extensions[1:],
auxillary_file_exts=("properties", "aux") + _tif_extensions[1:]
aliases=_tif_extensions,
auxillary_file_exts=_mosaics_extensions + _tif_extensions
),
FileType("ASCII Text File", "asc", raster,
auxillary_file_exts=('prj')),
Expand Down Expand Up @@ -246,17 +249,21 @@ def get_scan_hint(valid_extensions):
result = "kml-overlay"
elif "kmz" in valid_extensions:
result = "kmz"
elif "zip-mosaic" in valid_extensions:
result = "zip-mosaic"
else:
result = None
return result


def scan_file(file_name, scan_hint=None):
'''get a list of SpatialFiles for the provided file'''
if not os.path.exists(file_name):
raise Exception(_("Could not access to uploaded data."))

dirname = os.path.dirname(file_name)
if zipfile.is_zipfile(file_name):
paths, kept_zip = _process_zip(file_name, dirname)
paths, kept_zip = _process_zip(file_name, dirname, scan_hint=scan_hint)
archive = file_name if kept_zip else None
else:
paths = [os.path.join(dirname, p) for p in os.listdir(dirname)]
Expand Down Expand Up @@ -284,21 +291,21 @@ def scan_file(file_name, scan_hint=None):
if len(found) == 1:
found[0].xml_files = xml_files
else:
raise Exception("One or more XML files was provided, but no " +
"matching files were found for them.")
raise Exception(_("One or more XML files was provided, but no " +
"matching files were found for them."))

# detect slds and assign if a single upload is found
sld_files = _find_file_type(safe_paths, extension='.sld')
if sld_files:
if len(found) == 1:
found[0].sld_files = sld_files
else:
raise Exception("One or more SLD files was provided, but no " +
"matching files were found for them.")
raise Exception(_("One or more SLD files was provided, but no " +
"matching files were found for them."))
return SpatialFiles(dirname, found, archive=archive)


def _process_zip(zip_path, destination_dir):
def _process_zip(zip_path, destination_dir, scan_hint=None):
"""Perform sanity checks on uploaded zip file
This function will check if the zip file's contents have legal names.
Expand All @@ -308,10 +315,9 @@ def _process_zip(zip_path, destination_dir):
It will also check if an .sld file exists inside the zip and extract it
"""

safe_zip_path = _rename_files([zip_path])[0]
with zipfile.ZipFile(safe_zip_path, "r") as zip_handler:
if safe_zip_path.endswith(".kmz"):
if scan_hint in _keep_original_data:
extracted_paths = _extract_zip(zip_handler, destination_dir)
else:
extracted_paths = _sanitize_zip_contents(
Expand Down

0 comments on commit e4774bf

Please sign in to comment.