Skip to content

Commit

Permalink
Merge branch 'master-qgis_server' into download-leaflet-page-kartoza#151
Browse files Browse the repository at this point in the history
  • Loading branch information
boney-bun committed Oct 13, 2017
2 parents 4707cb9 + 7c1aff3 commit d4e467f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 14 deletions.
7 changes: 4 additions & 3 deletions geonode/maps/qgis_server_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,11 @@ def perm_filter(layer):
+ '?SERVICE=LAYERDEFINITIONS&LAYERS=' + json_layers
fwd_request = requests.get(url_server)
response = HttpResponse(
fwd_request.content, content_type="application/xml",
fwd_request.content,
content_type="application/x-qgis-layer-definition",
status=fwd_request.status_code)
response['Content-Disposition'] = 'attachment; filename=%s' \
% map_obj.title + '.qlr'
response['Content-Disposition'] = 'attachment; filename=%s.qlr' \
% map_obj.title

return response

Expand Down
2 changes: 1 addition & 1 deletion geonode/maps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ def map_download_leaflet(request, mapid):
return HttpResponseBadRequest('Sorry, leaflet page has not supported yet')


def map_download_qlr(request):
def map_download_qlr(request, mapid):
"""
This function serves as geoserver compatibility.
We might need to use QLR on geoserver in the future.
Expand Down
108 changes: 100 additions & 8 deletions geonode/qgis_server/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,41 @@ def test_ogc_specific_layer(self):
self.assertEqual(response.get('Content-Type'), 'image/tiff')
self.assertEqual(what('', h=response.content), 'tiff')

# Layer is already on the database
# checking the Link
links = uploaded.link_set.download().filter(
name__in=settings.DOWNLOAD_FORMATS_RASTER)

# checks signals.py for the hardcoded names in QLR and QGS
qlr_link = links.get(name='QGIS layer file (.qlr)')
self.assertIn("download-qlr", qlr_link.url)
qgs_link = links.get(name='QGIS project file (.qgs)')
self.assertIn("download-qgs", qgs_link.url)

# QLR
response = self.client.get(
reverse('qgis_server:download-qlr', kwargs=params))
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get('Content-Type'),
'application/x-qgis-layer-definition')
# check file name's extension
file_name = response.get('Content-Disposition').split('filename=')
file_ext = file_name[1].split('.')
self.assertEqual(file_ext[1], "qlr")

# QGS
response = self.client.get(
reverse('qgis_server:download-qgs', kwargs=params))
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get('Content-Type'),
'application/x-qgis-project')
# check file name's extension
file_name = response.get('Content-Disposition').split('filename=')
file_ext = file_name[1].split('.')
self.assertEqual(file_ext[1], "qgs")

response = self.client.get(
reverse('qgis_server:geotiff', kwargs={
'layername': vector_layer.name
Expand Down Expand Up @@ -286,8 +321,64 @@ def test_ogc_specific_layer(self):
vector_layer.delete()

@on_ogc_backend(qgis_server.BACKEND_PACKAGE)
def test_download_map_qlr(self):
"""Test download QLR file for a map"""
# 2 layers to be added to the map
filename = os.path.join(
gisdata.GOOD_DATA, 'raster/relief_san_andres.tif')
layer1 = file_upload(filename)

filename = os.path.join(
gisdata.GOOD_DATA,
'vector/san_andres_y_providencia_administrative.shp')
layer2 = file_upload(filename)

# construct json request for new map
json_payload = InitialSetup.generate_initial_map(layer1, layer2)

self.client.login(username='admin', password='admin')

response = self.client.post(
reverse('new_map_json'),
json.dumps(json_payload),
content_type='application/json')
# map is successfull saved
self.assertEqual(response.status_code, 200)

map_id = json.loads(response.content).get('id')

map = Map.objects.get(id=map_id)

# check that QLR is added to the links
links = map.link_set.download()
map_qlr_link = links.get(name='Download QLR Layer file')
self.assertIn('qlr', map_qlr_link.url)

# QLR
response = self.client.get(
reverse('map_download_qlr', kwargs={'mapid': map_id}))
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get('Content-Type'),
'application/x-qgis-layer-definition')

# cleanup
map.delete()
layer1.delete()
layer2.delete()

def test_map_json(self):
json_payload = InitialSetup.generate_initial_map()
# 2 layers to be added to the map
filename = os.path.join(
gisdata.GOOD_DATA, 'raster/relief_san_andres.tif')
layer1 = file_upload(filename)

filename = os.path.join(
gisdata.GOOD_DATA,
'vector/san_andres_y_providencia_administrative.shp')
layer2 = file_upload(filename)

json_payload = InitialSetup.generate_initial_map(layer1, layer2)
# First, create a map with two layers
# Need to log in for saving a map
self.client.login(username='admin', password='admin')
Expand Down Expand Up @@ -322,6 +413,8 @@ def test_map_json(self):
# clean up
map = Map.objects.get(id=map_id)
map.delete()
layer1.delete()
layer2.delete()


class QGISServerStyleManagerTest(LiveServerTestCase):
Expand Down Expand Up @@ -508,7 +601,7 @@ def test_map_thumbnail(self):
""":type: geonode.layers.models.Layer"""

# construct json request for new map
json_payload = InitialSetup.generate_initial_map()
json_payload = InitialSetup.generate_initial_map(layer1, layer2)

self.client.login(username='admin', password='admin')

Expand Down Expand Up @@ -572,7 +665,7 @@ def test_map_thumbnail(self):
class InitialSetup():

@classmethod
def generate_initial_map(cls):
def generate_initial_map(cls, layer1, layer2):
# construct json request for new map
json_payload = {
"sources": {
Expand Down Expand Up @@ -620,9 +713,8 @@ def generate_initial_map(cls):
"source": "source_OpenStreetMap"
},
{
"name": "geonode:"
"san_andres_y_providencia_administrative",
"title": "san_andres_y_providencia_administrative",
"name": layer2.typename,
"title": layer2.name,
"visibility": True,
"url": "http://geonode.dev/qgis-server/tiles"
"/san_andres_y_providencia_administrative/"
Expand All @@ -631,8 +723,8 @@ def generate_initial_map(cls):
"san_andres_y_providencia_administrative"
},
{
"name": "geonode:relief_san_andres",
"title": "relief_san_andres",
"name": layer1.typename,
"title": layer1.name,
"visibility": True,
"url": "http://geonode.dev/qgis-server/tiles"
"/relief_san_andres/{z}/{x}/{y}.png",
Expand Down
4 changes: 2 additions & 2 deletions geonode/qgis_server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def download_qgs(request, layername):
layer_title = layer.name

response = HttpResponse(
result.content, content_type="application/xml",
result.content, content_type="application/x-qgis-project",
status=result.status_code)
response['Content-Disposition'] = \
'attachment; filename=%s.qgs' % layer_title
Expand Down Expand Up @@ -847,7 +847,7 @@ def download_qlr(request, layername):
result = requests.get(url)
response = HttpResponse(
result.content,
content_type="application/xml",
content_type="application/x-qgis-layer-definition",
status=result.status_code)
response['Content-Disposition'] = \
'attachment; filename=%s.qlr' % layer_title
Expand Down

0 comments on commit d4e467f

Please sign in to comment.