def _generate(self): self.values = [] n = 12 if "single" in self.name.lower(): for i in range(0, n): self.values.append(QColor(self._color)) elif "elevation" in self.name.lower(): path = os.path.dirname(os.path.realpath(__file__)) ramp = os.path.join(path, "ramps", "dem.png") img = QImage(ramp) step = img.size().height() / n middle_x = img.size().width() / 2 for i in range(0, n): y = i * step self.values.append(img.pixelColor(middle_x, y)) self.values.reverse()
def _check_red(): req = QgsBufferServerRequest('http://my_server/' + qs) res = QgsBufferServerResponse() self._server.handleRequest(req, res, self.test_project) self.assertEqual(res.statusCode(), 200) result_path = os.path.join(self.temp_dir.path(), 'red.png') with open(result_path, 'wb+') as f: f.write(res.body()) # A full red image is expected image = QImage(result_path) self.assertFalse(image.isGrayscale()) color = image.pixelColor(100, 100) self.assertEqual(color.red(), 255) self.assertEqual(color.green(), 0) self.assertEqual(color.blue(), 0)
def test_atlas(self): """Test atlas""" qs = "?" + "&".join([ "%s=%s" % i for i in list({ 'SERVICE': "WMS", 'VERSION': "1.3.0", 'REQUEST': "GetPrint", 'CRS': 'EPSG:4326', 'FORMAT': 'png', 'LAYERS': 'multiple_pks', 'DPI': 72, 'TEMPLATE': "print1", }.items()) ]) req = QgsBufferServerRequest('http://my_server/' + qs + '&ATLAS_PK=1,2') res = QgsBufferServerResponse() self._server.handleRequest(req, res, self.test_project) self.assertEqual(res.statusCode(), 200) result_path = os.path.join(self.temp_dir.path(), 'atlas_1_2.png') with open(result_path, 'wb+') as f: f.write(res.body()) # A full red image is expected image = QImage(result_path) self.assertFalse(image.isGrayscale()) color = image.pixelColor(100, 100) self.assertEqual(color.red(), 255) self.assertEqual(color.green(), 0) self.assertEqual(color.blue(), 0) # Forbid 1-1 self._accesscontrol.active['layerFilterSubsetString'] = True self._check_exception(qs + '&ATLAS_PK=1,2', "Atlas error: empty atlas.") self._accesscontrol.active['layerFilterSubsetString'] = False self._accesscontrol.active['layerFilterExpression'] = True self._check_exception(qs + '&ATLAS_PK=1,2', "Atlas error: empty atlas.") # Remove all constraints self._clear_constraints() req = QgsBufferServerRequest('http://my_server/' + qs + '&ATLAS_PK=1,2') res = QgsBufferServerResponse() self._server.handleRequest(req, res, self.test_project) self.assertEqual(res.statusCode(), 200) result_path = os.path.join(self.temp_dir.path(), 'atlas_1_2.png') with open(result_path, 'wb+') as f: f.write(res.body()) # A full red image is expected image = QImage(result_path) self.assertFalse(image.isGrayscale()) color = image.pixelColor(100, 100) self.assertEqual(color.red(), 255) self.assertEqual(color.green(), 0) self.assertEqual(color.blue(), 0)
def test_getPrint(self): """Test getprint, requires https://github.com/qgis/QGIS/pull/41823""" c = Client() self.assertTrue(c.login(username='******', password='******')) ows_url = reverse('OWS:ows', kwargs={ 'group_slug': self.qdjango_project.group.slug, 'project_type': 'qdjango', 'project_id': self.qdjango_project.pk }) # Extent for feature where pk1 = 1, pk2 = 2 args = { 'SERVICE': "WMS", 'VERSION': "1.3.0", 'REQUEST': "GetPrint", 'CRS': 'EPSG:4326', 'FORMAT': 'png', 'LAYERS': 'multiple_pks', 'DPI': 72, 'TEMPLATE': "print1", 'map0:EXTENT': '45.70487804878048621,7.67926829268292099,46.22987804878049189,8.42479674796748235', } response = c.get(ows_url, args) self.assertEqual(response.status_code, 200) result_path = os.path.join(self.temp_dir.path(), 'red.png') with open(result_path, 'wb+') as f: f.write(response.content) image = QImage(result_path) self.assertFalse(image.isGrayscale()) color = image.pixelColor(100, 100) self.assertEqual(color.red(), 255) self.assertEqual(color.green(), 0) self.assertEqual(color.blue(), 0) admin01 = self.test_user1 qgs_expr = "pk1 = '1' AND pk2 = '1'" session_token, _ = SessionTokenFilter.objects.get_or_create( user=admin01) SessionTokenFilterLayer.objects.filter(layer=self.qdjango_layer, qgs_expr=qgs_expr).delete() session_filter = session_token.stf_layers.create( layer=self.qdjango_layer, qgs_expr=qgs_expr) session_filter.save() args.update({'filtertoken': session_token.token}) response = c.get(ows_url, args) # Test self.assertEqual(response.status_code, 200) result_path = os.path.join(self.temp_dir.path(), 'white.png') with open(result_path, 'wb+') as f: f.write(response.content) image = QImage(result_path) color = image.pixelColor(100, 100) self.assertTrue(image.isGrayscale()) self.assertEqual(color.red(), 255) self.assertEqual(color.green(), 255) self.assertEqual(color.blue(), 255) response = self._testApiCall('core-vector-api', [ 'data', 'qdjango', str(self.qdjango_project.pk), 'multiple_pks_67787984_68b5_423c_bc5e_ce92d8d74d70' ], {'filtertoken': session_token.token}) jcontent = json.loads(response.content) features = jcontent['vector']['data']['features'] self.assertEqual(len(features), 1) self.assertEqual(features[0]['properties']['name'], '1-1') self.assertEqual(features[0]['id'], '1@@1') session_filter.delete()