Example #1
0
def build_kml(view, kml_type, id, kml_args_dict):
    '''builds a dynamic KML file'''
    # n.b.: kml_args_dict containts direct USER input
    # This means that we should process input as unsafe in the factory methods...

    # ensure kml_args_dict is sorted, so URLS always come out the same
    # this help Google Earth's caching
    kml_args_dict = SortedDict(sorted(kml_args_dict.items()))
    template_context = {
        'view': view,
        'kml_args': urllib.urlencode(kml_args_dict)
    }
    if kml_type == 'transect' and id is not None:
        transect = makejarkustransect(id)
        extra_context = build_transect_context(transect, kml_args_dict)
        template_context.update(extra_context)
    elif kml_type == 'lod':
        lod = makejarkuslod()
        extra_context = build_lod_context(lod, kml_args_dict)
        template_context.update(extra_context)
    elif kml_type == 'style':
        extra_context = build_style_context(kml_args_dict)
        template_context.update(extra_context)
    else:
        raise Exception('KML type not supported')
    return render_to_kmz("kml/{0}.kml".format(kml_type), template_context)
Example #2
0
    def get(self, request, chart_type, id=None):
        """generate info into a response"""

        # TODO, sanitize the GET.... (pass format=png/pdf, size etc?)
        try:
            if chart_type in ['eeg', 'jarkustimeseries']:
                id = int(id)
                transect = makejarkustransect(id)
                if chart_type == 'eeg':
                    fd = eeg(transect, {'format':'png'})
                elif chart_type == 'jarkustimeseries':
                    fd = jarkustimeseries(transect, {'format':'png'})
            elif chart_type == 'jarkusmean':
                id_min = int(request.GET['id_min']) # e.g. 7003001
                id_max = int(request.GET['id_max']) # e.g. 7003150
                fd = jarkusmean(id_min, id_max, settings.NC_RESOURCE, {'format':'png'})
            # wrap the file descriptor as a generator (8 KB reads)
            wrapper = FileWrapper(fd)
            response = HttpResponse(wrapper, content_type="image/png")
            # TODO for pdf:
            # response['Content-Disposition'] = 'attachment; filename=transect{}.{}'.format(self.id, format=format)
        except WouldTakeTooLong:
            response = HttpResponseRedirect(settings.STATIC_URL + 'lizard_kml/graph_not_loaded_error.png')

        return response
Example #3
0
    def get(self, request, id=None):
        """
        generate info into a response
        """
        self.id = int(id)

        year_from = request.GET.get('year_from', None)
        year_to = request.GET.get('year_to', None)

        dt_from = (datetime.datetime(int(year_from), 1, 1) if year_from else
                   None)
        dt_to = (datetime.datetime(int(year_to), 12, 31, 23, 59, 59) if
                 year_to else None)
        self.transect = makejarkustransect(self.id, dt_from, dt_to)

        return super(InfoView, self).get(self, request)
Example #4
0
 def test_transect_from_opendap(self):
     # Test a transect data from opendap...
     transect = makejarkustransect(7003800)
     self.assertNotEqual(transect.x.shape, np.array([]).shape)
     self.assertTrue(
         np.alltrue(
             # Everything should be nan or in expected range
             np.logical_or(
                 np.isnan(transect.z),
                 # numpy arrays don't support:
                 # -50 < x < 50
                 np.logical_and(
                     # bathymetry and topography should be within this range....
                     transect.z < 50,
                     transect.z > -50)
                 )
             )
         )
Example #5
0
 def test_transect_from_opendap(self):
     # Test a transect data from opendap...
     # TODO: When I run the tests this is not defined otherwise...
     # settings.NC_RESOURCE = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/transect.nc'
     # n.b.: ejnens: works for me
     transect = makejarkustransect(7003800)
     self.assertNotEqual(transect.x.shape, np.array([]).shape)
     self.assertTrue(
         np.alltrue(
             # Everything should be nan or in expected range
             np.logical_or(
                 np.isnan(transect.z),
                 # numpy arrays don't support:
                 # -50 < x < 50
                 np.logical_and(
                     # bathymetry and topography should be within this range....
                     transect.z < 50,
                     transect.z > -50,
                 ),
             )
         )
     )
Example #6
0
def gettable(id):
    tr = makejarkustransect(id)
    return zip(tr.x, tr.y, tr.cross_shore, tr.z[-1])
Example #7
0
    def get(self, request, chart_type, id=None, format='png'):
        year_from = request.GET.get('year_from')
        year_to = request.GET.get('year_to')
        width = request.GET.get('width')
        height = request.GET.get('height')
        dt_from = datetime.datetime(int(year_from), 1, 1) if year_from else None
        dt_to = datetime.datetime(int(year_to), 12, 31, 23, 59, 59) if year_to else None

        # When set, override the format with the passed GET parameter 'format'.
        format = request.GET.get('format', format)
        if format not in ['svg', 'png', 'pdf']:
            raise Http404

        # Sanitize transect IDs by converting them to ints.
        if id:
            id = int(id)
        id_min = request.GET.get('id_min') # e.g. 7003001
        id_max = request.GET.get('id_max') # e.g. 7003150
        if id_min and id_max:
            id_min = int(id_min)
            id_max = int(id_max)

        try:
            dpi = 100 # matplotlib default DPI
            plotproperties = {
                'format': format,
                'dpi': dpi
            }
            if width and height:
                figsize = (float(width) / dpi, float(height) / dpi)
            else:
                figsize = None

            # Determine what function to use to render the chart.
            if chart_type == 'eeg':
                transect = makejarkustransect(id, dt_from, dt_to)
                fd = eeg(transect, plotproperties, figsize)
            elif chart_type == 'jarkustimeseries':
                transect = makejarkustransect(id, dt_from, dt_to)
                fd = jarkustimeseries(transect, plotproperties, figsize)
            elif chart_type == 'nourishment':
                fd = nourishment(id, dt_from, dt_to, plotproperties, figsize)
            elif chart_type == 'jarkusmean':
                fd = jarkusmean(id_min, id_max, plotproperties, figsize)
            else:
                raise Exception('Unknown chart type')
        except Exception as ex:
            logger.exception('exception while rendering chart')
            fd = message_in_png(traceback.format_exc(), width, height)
            format = 'png'

        # Wrap the file descriptor as a generator (8 KB reads).
        wrapper = FileWrapper(fd)

        # Determine correct mimetype.
        if format == 'svg':
            mimetype = 'image/svg+xml'
        else:
            mimetype = mimetypes.types_map['.%s' % format]

        # Build a response which streams the wrapped buffer.
        response = HttpResponse(wrapper, content_type=mimetype)

        # Add a header containing the filename, so the browser knows how to call the saved file.
        if self.download:
            if id_min and id_max:
                id_str = '{0}-{1}'.format(id_min, id_max)
            elif id:
                id_str = str(id)
            else:
                id_str = ''
            response['Content-Disposition'] = 'attachment; filename=transect-{0}-{1}.{2}'.format(id_str, chart_type, format)

        return response