def response(self):
        if self.procedure is None:
            return render_template("error.xml", parameter="procedure", value="Value missing")

        station, publisher = get_station_feature(self.procedure)

        if station is None:
            return render_template("error.xml", parameter="procedure", value="Invalid value")

        return render_template("describesensor.xml", station=station, publisher=publisher)
Example #2
0
    def response(self):
        if self.offering is None:
            return render_template("error.xml", parameter="offering", value="Value missing")
        else:
            possible_offerings = GetCapabilities.offerings.values()
            if not self.offering in possible_offerings:
                return render_template("error.xml", parameter="offering", value="Invalid value.  Possible values are: %s" % ",".join(possible_offerings))

        if self.procedure is None:
            return render_template("error.xml", parameter="procedure", value="This SOS server requires a procedure argument to GetObservation")
        if self.obs_props is None:
            return render_template("error.xml", parameter="observedProperty", value="Value missing")
        else:
            # Remove duplicates and split
            self.obs_props = list(set(self.obs_props.split(",")))
    
        provider = None
        for key, value in GetCapabilities.offerings.iteritems():
            if value == self.offering:
                provider = key
                break

        # Strip out starting and ending parameters
        starting = None
        ending = None
        if self.eventtime is not None and (isinstance(self.eventtime, unicode) or isinstance(self.eventtime, str)):
            if self.eventtime.lower() != "latest":
                starting = dateparser.parse(self.eventtime.split("/")[0])
                ending = dateparser.parse(self.eventtime.split("/")[1])


        station, publisher = get_station_feature(self.procedure, provider=provider, 
                                                                 starting=starting, 
                                                                 ending=ending,
                                                                 observedProperties=self.obs_props)

        if station is None:
            return render_template("error.xml", parameter="procedure", value="Invalid value")

        min_time = "never"
        max_time = "never"
        # At least one record was found
        if len(station.time_range) > 0:
            min_time = min(station.time_range).astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
            max_time = max(station.time_range).astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%SZ")

        # If the latest was requested, strip it out now
        if self.eventtime is not None and (isinstance(self.eventtime, unicode) or isinstance(self.eventtime, str)):
            if self.eventtime.lower() == "latest":
                station.elements = station.filter_by_time(starting=max_time, ending=max_time)
                station.calculate_bounds()
               
        rows = []
        for point in station:
            row = [(point.time.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%SZ"))]
            for ob in self.obs_props:
                m = None
                try:
                    m = point.get_member(name=ob)
                except:
                    row.append("None")
                    #row.append("None")
                    #row.append("None")
                else:
                    row.append(unicode(m.get("value", None)))
                    #row.append(m.get("method_id", None))
                    #row.append(m.get("method_name", None))

            rows.append(",".join(row))

        data_block = "\n".join(rows)

        return render_template("getobservation.xml", min_time=min_time, max_time=max_time, station=station, data_block=data_block)