Exemple #1
0
    def createKML(self):
        '''Reuse STOQS utils/Viz code to build some simple KML. Use 'position' for Parameter Name.
        Fudge data value to distinguish platforms by color, use 0.0 for depth except for adcp data.
        '''
        request = HttpRequest()
        qs = None
        qparams = {}
        stoqs_object_name = None
        kml = KML(request,
                  qs,
                  qparams,
                  stoqs_object_name,
                  withTimeStamps=True,
                  withLineStrings=True,
                  withFullIconURL=True)

        # Put data into form that KML() expects - use different datavalues (-1, 1) to color the platforms
        dataHash = defaultdict(lambda: [])
        colors = {}
        values = np.linspace(-1, 1, len(list(self.trackDrift.keys())))
        for i, k in enumerate(self.trackDrift.keys()):
            colors[k] = values[i]

        for platform, drift in list(self.trackDrift.items()):
            for es, lo, la in zip(drift['es'], drift['lon'], drift['lat']):
                dataHash[platform].append([
                    datetime.utcfromtimestamp(es), lo, la, 0.0, 'position',
                    colors[platform], platform
                ])

        for depth, drift in list(self.adcpDrift.items()):
            for es, lo, la in zip(drift['es'], drift['lon'], drift['lat']):
                dataHash[depth].append([
                    datetime.utcfromtimestamp(es), lo, la,
                    float(depth), 'position', 0.0, 'adcp'
                ])

        for key, drift in list(self.stoqsDrift.items()):
            parm, plat = key.split(',')[:2]
            for es, lo, la, de, dv in zip(drift['es'], drift['lon'],
                                          drift['lat'], drift['depth'],
                                          drift['datavalue']):
                dataHash[parm].append([
                    datetime.utcfromtimestamp(es), lo, la, de, parm, dv, plat
                ])

        try:
            title = self.title
        except AttributeError:
            title = 'Product of STOQS drift_data.py'
        description = self.commandline.replace('&', '&')
        kml = kml.makeKML(self.args.database, dataHash, 'position', title,
                          description, 0.0, 0.0)

        fh = open(self.args.kmlFileName, 'w')
        fh.write(kml)
        fh.close()
        print('Wrote file', self.args.kmlFileName)
Exemple #2
0
    def createKML(self):
        """Reuse STOQS utils/Viz code to build some simple KML. Use 'position' for Parameter Name.
        Fudge data value to distinguish platforms by color, use 0.0 for depth except for adcp data.
        """
        request = HttpRequest()
        qs = None
        qparams = {}
        stoqs_object_name = None
        kml = KML(
            request, qs, qparams, stoqs_object_name, withTimeStamps=True, withLineStrings=True, withFullIconURL=True
        )

        # Put data into form that KML() expects - use different datavalues (-1, 1) to color the platforms
        dataHash = defaultdict(lambda: [])
        colors = {}
        values = np.linspace(-1, 1, len(self.trackDrift.keys()))
        for i, k in enumerate(self.trackDrift.keys()):
            colors[k] = values[i]

        for platform, drift in self.trackDrift.iteritems():
            for es, lo, la in zip(drift["es"], drift["lon"], drift["lat"]):
                dataHash[platform].append(
                    [datetime.utcfromtimestamp(es), lo, la, 0.0, "position", colors[platform], platform]
                )

        for depth, drift in self.adcpDrift.iteritems():
            for es, lo, la in zip(drift["es"], drift["lon"], drift["lat"]):
                dataHash[depth].append([datetime.utcfromtimestamp(es), lo, la, float(depth), "position", 0.0, "adcp"])

        for key, drift in self.stoqsDrift.iteritems():
            parm, plat = key.split(",")[:2]
            for es, lo, la, de, dv in zip(drift["es"], drift["lon"], drift["lat"], drift["depth"], drift["datavalue"]):
                dataHash[parm].append([datetime.utcfromtimestamp(es), lo, la, de, parm, dv, plat])

        try:
            title = self.title
        except AttributeError:
            title = "Product of STOQS drift_data.py"
        description = self.commandline.replace("&", "&")
        kml = kml.makeKML(self.args.database, dataHash, "position", title, description, 0.0, 0.0)

        fh = open(self.args.kmlFileName, "w")
        fh.write(kml)
        fh.close()
        print "Wrote file", self.args.kmlFileName
Exemple #3
0
    def process_request(self):
        '''
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        '''
        fields = self.getFields()
        geomFields = self.getGeomFields()
        self.assign_qs()

        if self.stoqs_object_name == 'measured_parameter':
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                mpq = MPQuery(self.request)
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(
                    sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(sql, MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(None,
                                     MPQuerySet.rest_columns,
                                     qs_mp=self.qs)

        # Process request based on format requested
        if self.format == 'csv' or self.format == 'tsv':
            response = HttpResponse()
            if self.format == 'tsv':
                response['Content-type'] = 'text/tab-separated-values'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.tsv' % self.stoqs_object_name
                writer = csv.writer(response, delimiter='\t')
            else:
                response['Content-type'] = 'text/csv'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.csv' % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow([obj[f] for f in fields])

            return response
        elif self.format == 'xml':
            return HttpResponse(serializers.serialize('xml', self.query_set),
                                'application/xml')

        elif self.format == 'json':
            return HttpResponse(
                simplejson.dumps(self.qs, cls=encoders.STOQSJSONEncoder),
                'application/json')

        elif self.format == 'kml':
            kml = KML(self.request, self.qs, self.qparams,
                      self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == 'kmln':
            kml = KML(self.request,
                      self.qs,
                      self.qparams,
                      self.stoqs_object_name,
                      withTimeStamps=False,
                      withLineStrings=False,
                      withFullIconURL=False)
            return kml.kmlResponse()

        elif self.format == 'count':
            count = self.qs.count()
            logger.debug('count = %d', count)
            return HttpResponse('%d' % count, mimetype='text/plain')

        elif self.format == 'help':
            helpText = 'Fields: %s\n\nField Lookups: %s' % (fields,
                                                            self.fieldLookups)
            if geomFields:
                helpText += '\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s' % (
                    geomFields, self.distanceLookups, self.spatialLookups)
            helpText += '\n\nResponses: %s' % (self.responses, )
            response = HttpResponse(helpText, mimetype="text/plain")
            return response

        else:
            self.build_html_template()
            response = render_to_response(
                self.html_tmpl_file, {
                    'cols': fields,
                    'google_analytics_code': settings.GOOGLE_ANALYTICS_CODE
                },
                context_instance=RequestContext(self.request))
            fh = open(self.html_tmpl_path, 'w')
            for line in response:
                fh.write(line)
            fh.close()
            return render_to_response(self.html_tmpl_path, {'list': self.qs})
Exemple #4
0
    def process_request(self):
        '''
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        '''
        fields = self.getFields()
        geomFields = self.getGeomFields()
        try:
            self.assign_qs()
        except EmptyQuerySetException:
            raise Http404

        if self.stoqs_object_name == 'measured_parameter':
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(self.request.META['dbAlias'], sql, MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(self.request.META['dbAlias'], None, MPQuerySet.rest_columns, qs_mp=self.qs)

        # Process request based on format requested
        if self.format == 'csv' or self.format == 'tsv':
            response = HttpResponse()
            if self.format == 'tsv':
                response['Content-type'] = 'text/tab-separated-values'
                response['Content-Disposition'] = 'attachment; filename=%s.tsv' % self.stoqs_object_name
                writer = csv.writer(response, delimiter='\t')
            else:
                response['Content-type'] = 'text/csv'
                response['Content-Disposition'] = 'attachment; filename=%s.csv' % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow([obj[f] for f in fields])

            return response
        elif self.format == 'xml':
            return HttpResponse(serializers.serialize('xml', self.query_set), 'application/xml')

        elif self.format == 'json':
            return HttpResponse(json.dumps(self.qs, cls=encoders.STOQSJSONEncoder), 'application/json')

        elif self.format == 'kml':
            kml = KML(self.request, self.qs, self.qparams, self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == 'kmln':
            kml = KML(self.request, self.qs, self.qparams, self.stoqs_object_name, withTimeStamps=False, withLineStrings=False, withFullIconURL=False)
            return kml.kmlResponse()

        elif self.format == 'count':
            count = self.qs.count()
            logger.debug('count = %d', count)
            return HttpResponse('%d' % count, content_type='text/plain')

        elif self.format == 'help':
            helpText = 'Fields: %s\n\nField Lookups: %s' % (fields, self.fieldLookups)
            if geomFields:
                helpText += '\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s' % (geomFields, 
                            self.distanceLookups, self.spatialLookups)
            helpText += '\n\nResponses: %s' % (self.responses,)
            response = HttpResponse(helpText, content_type="text/plain")
            return response

        else:
            self.build_html_template()
            try:
                response = render_to_response(self.html_tmpl_file, {'cols': fields, 
                                                'google_analytics_code': settings.GOOGLE_ANALYTICS_CODE },
                                                context_instance = RequestContext(self.request))
            except AttributeError:
                response = render_to_response(self.html_tmpl_file, {'cols': fields}, 
                                                context_instance = RequestContext(self.request))
            fh = open(self.html_tmpl_path, 'w')
            for line in response:
                fh.write(line)
            fh.close()
            return render_to_response(self.html_tmpl_path, {'list': self.qs})
Exemple #5
0
    def process_request(self):
        """
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        """
        fields = self.getFields()
        geomFields = self.getGeomFields()
        try:
            self.assign_qs()
        except EmptyQuerySetException:
            raise Http404

        if self.stoqs_object_name == "measured_parameter":
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                mpq = MPQuery(self.request)
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(self.request.META["dbAlias"], sql, MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(self.request.META["dbAlias"], None, MPQuerySet.rest_columns, qs_mp=self.qs)

        # Process request based on format requested
        if self.format == "csv" or self.format == "tsv":
            response = HttpResponse()
            if self.format == "tsv":
                response["Content-type"] = "text/tab-separated-values"
                response["Content-Disposition"] = "attachment; filename=%s.tsv" % self.stoqs_object_name
                writer = csv.writer(response, delimiter="\t")
            else:
                response["Content-type"] = "text/csv"
                response["Content-Disposition"] = "attachment; filename=%s.csv" % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow([obj[f] for f in fields])

            return response
        elif self.format == "xml":
            return HttpResponse(serializers.serialize("xml", self.query_set), "application/xml")

        elif self.format == "json":
            return HttpResponse(json.dumps(self.qs, cls=encoders.STOQSJSONEncoder), "application/json")

        elif self.format == "kml":
            kml = KML(self.request, self.qs, self.qparams, self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == "kmln":
            kml = KML(
                self.request,
                self.qs,
                self.qparams,
                self.stoqs_object_name,
                withTimeStamps=False,
                withLineStrings=False,
                withFullIconURL=False,
            )
            return kml.kmlResponse()

        elif self.format == "count":
            count = self.qs.count()
            logger.debug("count = %d", count)
            return HttpResponse("%d" % count, content_type="text/plain")

        elif self.format == "help":
            helpText = "Fields: %s\n\nField Lookups: %s" % (fields, self.fieldLookups)
            if geomFields:
                helpText += "\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s" % (
                    geomFields,
                    self.distanceLookups,
                    self.spatialLookups,
                )
            helpText += "\n\nResponses: %s" % (self.responses,)
            response = HttpResponse(helpText, content_type="text/plain")
            return response

        else:
            self.build_html_template()
            response = render_to_response(
                self.html_tmpl_file,
                {"cols": fields, "google_analytics_code": settings.GOOGLE_ANALYTICS_CODE},
                context_instance=RequestContext(self.request),
            )
            fh = open(self.html_tmpl_path, "w")
            for line in response:
                fh.write(line)
            fh.close()
            return render_to_response(self.html_tmpl_path, {"list": self.qs})
Exemple #6
0
    def process_request(self):
        '''
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        '''
        fields = self.getFields()
        geomFields = self.getGeomFields()
        try:
            self.assign_qs()
        except EmptyQuerySetException:
            raise Http404

        # A terrible hack to add latitude and longitude columns to the response - must call following assign_qs()
        fields = self.add_lon_lat_cols()

        if self.stoqs_object_name == 'measured_parameter':
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(
                    sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(self.request.META['dbAlias'], sql,
                                     MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(self.request.META['dbAlias'],
                                     None,
                                     MPQuerySet.rest_columns,
                                     qs_mp=self.qs)

        # Process request based on format requested
        if self.format == 'csv' or self.format == 'tsv':
            response = HttpResponse()
            if self.format == 'tsv':
                response['Content-type'] = 'text/tab-separated-values'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.tsv' % self.stoqs_object_name
                writer = csv.writer(response, delimiter='\t')
            else:
                response['Content-type'] = 'text/csv'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.csv' % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow(self.row_of_fields(obj))

            return response
        elif self.format == 'xml':
            return HttpResponse(serializers.serialize('xml', self.query_set),
                                'application/xml')

        elif self.format == 'json':
            return HttpResponse(
                json.dumps(self.qs, cls=encoders.STOQSJSONEncoder),
                'application/json')

        elif self.format == 'kml':
            kml = KML(self.request, self.qs, self.qparams,
                      self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == 'kmln':
            kml = KML(self.request,
                      self.qs,
                      self.qparams,
                      self.stoqs_object_name,
                      withTimeStamps=False,
                      withLineStrings=False,
                      withFullIconURL=False)
            return kml.kmlResponse()

        elif self.format == 'count':
            count = self.qs.count()
            logger.debug('count = %d', count)
            return HttpResponse('%d' % count, content_type='text/plain')

        elif self.format == 'help':
            helpText = 'Fields: %s\n\nField Lookups: %s' % (fields,
                                                            self.fieldLookups)
            if geomFields:
                helpText += '\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s' % (
                    geomFields, self.distanceLookups, self.spatialLookups)
            helpText += '\n\nResponses: %s' % (self.responses, )
            response = HttpResponse(helpText, content_type="text/plain")
            return response

        else:
            self.build_html_template()
            try:
                response = render(self.request,
                                  self.html_tmpl_file,
                                  context={
                                      'cols':
                                      fields,
                                      'google_analytics_code':
                                      settings.GOOGLE_ANALYTICS_CODE
                                  })
            except AttributeError:
                response = render(self.request,
                                  self.html_tmpl_file,
                                  context={'cols': fields})
            fh = open(self.html_tmpl_path, 'w')
            for line in response:
                # Override Django's default datetime formatting with ISO 8601 format that includes seconds
                # STOQS model field names ending in 'timevalue' and 'date' are convertable datetimes
                # See: https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#date
                line = line.decode("utf-8").replace('timevalue }',
                                                    'timevalue|date:"c" }')
                line = line.replace('date }', 'date|date:"c" }')
                fh.write(line)
            fh.close()
            return render(self.request,
                          self.html_tmpl_path,
                          context={'list': self.qs})