Skip to content

Commit

Permalink
add qml and xml files when downloading a layer in zip and geotiff for…
Browse files Browse the repository at this point in the history
…mats (#540)

* add qml when downloading a layer in zip and geotiff formats

fix #539
  • Loading branch information
boney-bun authored Jan 11, 2019
1 parent ba48295 commit 8c6ba48
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions geonode/qgis_server/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def test_ogc_specific_layer(self):
response = self.client.get(
reverse('qgis_server:geotiff', kwargs=params))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get('Content-Type'), 'image/tiff')
self.assertEqual(what('', h=response.content), 'tiff')
self.assertEqual(response.get('Content-Type'), 'application/x-zip-compressed')
self.assertTrue("zip" in response['Content-Disposition'].split("filename")[1])

# Layer is already on the database
# checking the Link
Expand Down
45 changes: 43 additions & 2 deletions geonode/qgis_server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ def download_zip(request, layername):
qgis_layer = get_object_or_404(QGISServerLayer, layer=layer)
# Files (local path) to put in the .zip
filenames = qgis_layer.files

# generate qml then add to zip
qgis_layer.extract_default_style_to_qml()
filenames.append(qgis_layer.qml_path)

# Exclude qgis project files, because it contains server specific path
filenames = [f for f in filenames if not f.endswith('.qgs')]

Expand Down Expand Up @@ -433,8 +438,44 @@ def geotiff(request, layername):
logger.debug(msg)
raise Http404(msg)

with open(filename, 'rb') as f:
return HttpResponse(f.read(), content_type='image/tiff')
zip_subdir = layer.name
# removes dash, dot, brackets, and space from the name
zip_subdir = re.sub('[()-. ]', '', zip_subdir)
zip_filename = "%s.zip" % zip_subdir

# Open StringIO to grab in-memory ZIP contents
s = StringIO.StringIO()

# The zip compressor
zf = zipfile.ZipFile(s, "w")

filenames = [filename]

# generate qml first
qgis_layer.extract_default_style_to_qml()
# then add qml to zip
filenames.append(qgis_layer.qml_path)
# also add xml to zip
if os.path.exists(qgis_layer.xml_path):
filenames.append(qgis_layer.xml_path)

for fpath in filenames:
# Calculate path for file in zip
fdir, fname = os.path.split(fpath)

zip_path = os.path.join(zip_subdir, fname)

# Add file, at correct path
zf.write(fpath, zip_path)
zf.close()

# Grab ZIP file from in-memory, make response with correct MIME-type
resp = HttpResponse(
s.getvalue(), content_type="application/x-zip-compressed")
# ..and correct content-disposition
resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename

return resp


def qgis_server_request(request):
Expand Down

0 comments on commit 8c6ba48

Please sign in to comment.