Beispiel #1
0
def layer_tiles(request):
    """Ajax request to get layer's url to show in the map.
    """
    if request.method != 'GET':
        return HttpResponseBadRequest()
    layer_id = request.GET.get('layer_id')
    if request.GET.get('target_srid'):
        target_srid = request.GET.get('target_srid')
    else:
        target_srid = 'EPSG:4326'
    if not layer_id:
        return HttpResponseBadRequest()
    try:
        layer = Layer.objects.get(id=layer_id)
        # Use QGIS Server bbox conventions
        bbox_x0, bbox_y0, bbox_x1, bbox_y1 = transform_layer_bbox(
            layer, target_srid)
        context = {
            'layer_tiles_url': layer.get_tiles_url(),
            'layer_bbox_x0': float(bbox_x0),
            'layer_bbox_x1': float(bbox_x1),
            'layer_bbox_y0': float(bbox_y0),
            'layer_bbox_y1': float(bbox_y1),
            'layer_name': layer.title,
            'legend_url': layer.get_legend_url()
        }

        return HttpResponse(json.dumps(context),
                            content_type="application/json")
    except Exception as e:
        LOGGER.exception(e)
        return HttpResponseServerError()
    def test_transform_layer_bbox(self):
        """Test bbox CRS conversion"""
        filename = os.path.join(gisdata.GOOD_DATA, 'raster/test_grid.tif')
        uploaded = file_upload(filename)

        new_bbox = transform_layer_bbox(uploaded, 3857)

        expected_bbox = [
            10793092.549352637, -615294.6893182276,
            10810202.947307253, -591232.8900397373]

        self.assertEqual(new_bbox, expected_bbox)

        new_bbox = transform_layer_bbox(uploaded, 4326)

        expected_bbox = [96.956, -5.518733, 97.10970532, -5.303545552]

        self.assertEqual(new_bbox, expected_bbox)

        uploaded.delete()