def test_write_map_data_to_db_results_with_loss_map(self):
        """
        The write_map_data_to_db() return values are correct for loss maps.
        """
        expected_loss_data = [([-118.229726, 34.050622], 16.04934554846202),
                              ([-118.241243, 34.061557], 629.323267954),
                              ([-118.245388, 34.055984], 245.9928520658)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_loss_data[idx][0])

        def value(idx):
            """Access the loss value."""
            return view_utils.round_float(expected_loss_data[idx][1])

        loss_map = self.setup_output(output_type="loss_map")
        self.job_to_tear_down = loss_map.oq_job
        config = {
            "key": "%s" % loss_map.id,
            "layer": "77-lossmap-0.01-quantile-0.25",
            "output": "tests/77",
            "path": "tests/data/loss-map-0fcfdbc7.xml",
            "type": "loss"
        }
        db_key, minimum, maximum = write_map_data_to_db(config)
        self.assertEqual(config["key"], db_key)
        self.assertEqual(
            min(value(idx) for idx in range(len(expected_loss_data))),
            view_utils.round_float(minimum))
        self.assertEqual(
            max(value(idx) for idx in range(len(expected_loss_data))),
            view_utils.round_float(maximum))
示例#2
0
def prepare_map_result(output):
    """Prepare a json fragment for a single hazard/loss map.

    The desired json fragment should look as follows:

        {"id": 77, "name": "loss-map-0fcfdbc7.xml", "type": "loss map",
         "min": 2.718281, "max": 3.141593,
         "layer": {
            "ows": "http://gemsun02.ethz.ch/geoserver-geonode-dev/ows"
            "layer": "geonode:77-loss-map-0fcfdbc7"}}

    :param output: the :py:class:`geonode.mtapi.models.Output` instance
        in question
    :returns: a dictionary with data needed to produce the json above.
    """
    map_type = dict(output.OUTPUT_TYPE_CHOICES)[output.output_type].lower()
    if output.output_type == "hazard_map":
        layer_name = "hazard_map_data"
    else:
        layer_name = "loss_map_data"
    ows = urljoin(settings.GEOSERVER_BASE_URL, "ows")
    result = dict(
        name="%s-%s" % (output.oq_job.id, output.display_name),
        type=map_type, min=view_utils.round_float(output.min_value),
        id=output.id, max=view_utils.round_float(output.max_value),
        layer=dict(ows=ows, layer="geonode:%s" % layer_name,
                   filter="output_id=%s" % output.id))
    return result
    def test_write_map_data_to_db_results_with_loss_map(self):
        """
        The write_map_data_to_db() return values are correct for loss maps.
        """
        expected_loss_data = [
            ([-118.229726, 34.050622], 16.04934554846202),
            ([-118.241243, 34.061557], 629.323267954),
            ([-118.245388, 34.055984], 245.9928520658)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_loss_data[idx][0])

        def value(idx):
            """Access the loss value."""
            return view_utils.round_float(expected_loss_data[idx][1])

        loss_map = self.setup_output(output_type="loss_map")
        self.job_to_tear_down = loss_map.oq_job
        config = {
            "key": "%s" % loss_map.id,
            "layer": "77-lossmap-0.01-quantile-0.25",
            "output": "tests/77", "path": "tests/data/loss-map-0fcfdbc7.xml",
            "type": "loss"}
        db_key, minimum, maximum = write_map_data_to_db(config)
        self.assertEqual(config["key"], db_key)
        self.assertEqual(
            min(value(idx) for idx in range(len(expected_loss_data))),
            view_utils.round_float(minimum))
        self.assertEqual(
            max(value(idx) for idx in range(len(expected_loss_data))),
            view_utils.round_float(maximum))
    def test_write_map_data_to_db_results_with_hazard_map(self):
        """
        The write_map_data_to_db() return values are correct for hazard maps.
        """
        expected_hazard_data = [([-121.8, 37.9], 1.23518683436),
                                ([-122.0, 37.5], 1.19244541041),
                                ([-122.1, 38.0], 1.1905288226)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_hazard_data[idx][0])

        def value(idx):
            """Access the hazard value."""
            return view_utils.round_float(expected_hazard_data[idx][1])

        hazard_map = self.setup_output(output_type="hazard_map")
        self.job_to_tear_down = hazard_map.oq_job
        config = {
            "key": "%s" % hazard_map.id,
            "layer": "78-hazardmap-0.01-quantile-0.25",
            "output": "tests/78",
            "path": "tests/data/hazardmap-0.1-quantile-0.25.xml",
            "type": "hazard"
        }
        db_key, minimum, maximum = write_map_data_to_db(config)
        self.assertEqual(config["key"], db_key)
        self.assertEqual(
            min(value(idx) for idx in range(len(expected_hazard_data))),
            view_utils.round_float(minimum))
        self.assertEqual(
            max(value(idx) for idx in range(len(expected_hazard_data))),
            view_utils.round_float(maximum))
    def test_write_map_data_to_db_results_with_hazard_map(self):
        """
        The write_map_data_to_db() return values are correct for hazard maps.
        """
        expected_hazard_data = [
            ([-121.8, 37.9], 1.23518683436),
            ([-122.0, 37.5], 1.19244541041),
            ([-122.1, 38.0], 1.1905288226)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_hazard_data[idx][0])

        def value(idx):
            """Access the hazard value."""
            return view_utils.round_float(expected_hazard_data[idx][1])

        hazard_map = self.setup_output(output_type="hazard_map")
        self.job_to_tear_down = hazard_map.oq_job
        config = {
            "key": "%s" % hazard_map.id,
            "layer": "78-hazardmap-0.01-quantile-0.25",
            "output": "tests/78",
            "path": "tests/data/hazardmap-0.1-quantile-0.25.xml",
            "type": "hazard"}
        db_key, minimum, maximum = write_map_data_to_db(config)
        self.assertEqual(config["key"], db_key)
        self.assertEqual(
            min(value(idx) for idx in range(len(expected_hazard_data))),
            view_utils.round_float(minimum))
        self.assertEqual(
            max(value(idx) for idx in range(len(expected_hazard_data))),
            view_utils.round_float(maximum))
    def test_write_map_data_to_db_with_hazard_map(self):
        """
        Writing hazard map data works.
        """
        expected_hazard_data = [([-122.1, 38.0], 1.1905288226),
                                ([-122.0, 37.5], 1.19244541041),
                                ([-121.8, 37.9], 1.23518683436)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_hazard_data[idx][0])

        def value(idx):
            """Access the hazard value."""
            return view_utils.round_float(expected_hazard_data[idx][1])

        hazard_map = self.setup_output(output_type="hazard_map")
        self.job_to_tear_down = hazard_map.oq_job
        config = {
            "key": "%s" % hazard_map.id,
            "layer": "78-hazardmap-0.01-quantile-0.25",
            "output": "tests/78",
            "path": "tests/data/hazardmap-0.1-quantile-0.25.xml",
            "type": "hazard"
        }
        write_map_data_to_db(config)
        self.assertEqual(0, len(hazard_map.lossmapdata_set.all()))
        self.assertEqual(3, len(hazard_map.hazardmapdata_set.all()))
        # don't depend on sort order
        all_hazard = sorted([(h.location.coords, h.value)
                             for h in hazard_map.hazardmapdata_set.all()])

        for idx, hazard in enumerate(all_hazard):
            self.assertEqual(coords(idx), hazard[0])
            self.assertEqual(value(idx), view_utils.round_float(hazard[1]))
    def test_write_map_data_to_db_with_hazard_map(self):
        """
        Writing hazard map data works.
        """
        expected_hazard_data = [
            ([-121.8, 37.9], 1.23518683436),
            ([-122.0, 37.5], 1.19244541041),
            ([-122.1, 38.0], 1.1905288226)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_hazard_data[idx][0])

        def value(idx):
            """Access the hazard value."""
            return view_utils.round_float(expected_hazard_data[idx][1])

        hazard_map = self.setup_output(output_type="hazard_map")
        self.job_to_tear_down = hazard_map.oq_job
        config = {
            "key": "%s" % hazard_map.id,
            "layer": "78-hazardmap-0.01-quantile-0.25",
            "output": "tests/78",
            "path": "tests/data/hazardmap-0.1-quantile-0.25.xml",
            "type": "hazard"}
        write_map_data_to_db(config)
        self.assertEqual(0, len(hazard_map.lossmapdata_set.all()))
        self.assertEqual(3, len(hazard_map.hazardmapdata_set.all()))
        for idx, hazard in enumerate(hazard_map.hazardmapdata_set.all()):
            self.assertEqual(coords(idx), hazard.location.coords)
            self.assertEqual(value(idx), view_utils.round_float(hazard.value))
    def test_write_map_data_to_db_with_loss_map(self):
        """
        Writing hazard loss data works.
        """
        expected_loss_data = [([-118.229726, 34.050622], 16.04934554846202),
                              ([-118.241243, 34.061557], 629.323267954),
                              ([-118.245388, 34.055984], 245.9928520658)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_loss_data[idx][0])

        def value(idx):
            """Access the loss value."""
            return view_utils.round_float(expected_loss_data[idx][1])

        loss_map = self.setup_output(output_type="loss_map")
        self.job_to_tear_down = loss_map.oq_job
        config = {
            "key": "%s" % loss_map.id,
            "layer": "77-lossmap-0.01-quantile-0.25",
            "output": "tests/77",
            "path": "tests/data/loss-map-0fcfdbc7.xml",
            "type": "loss"
        }
        write_map_data_to_db(config)
        self.assertEqual(0, len(loss_map.hazardmapdata_set.all()))
        self.assertEqual(3, len(loss_map.lossmapdata_set.all()))
        for idx, loss in enumerate(loss_map.lossmapdata_set.all()):
            self.assertEqual(coords(idx), loss.location.coords)
            self.assertEqual(value(idx), view_utils.round_float(loss.value))
    def test_write_map_data_to_db_with_loss_map(self):
        """
        Writing hazard loss data works.
        """
        expected_loss_data = [
            ([-118.229726, 34.050622], 16.04934554846202),
            ([-118.241243, 34.061557], 629.323267954),
            ([-118.245388, 34.055984], 245.9928520658)]

        def coords(idx):
            """Access the point coordinates."""
            return tuple(expected_loss_data[idx][0])

        def value(idx):
            """Access the loss value."""
            return view_utils.round_float(expected_loss_data[idx][1])

        loss_map = self.setup_output(output_type="loss_map")
        self.job_to_tear_down = loss_map.oq_job
        config = {
            "key": "%s" % loss_map.id,
            "layer": "77-lossmap-0.01-quantile-0.25",
            "output": "tests/77", "path": "tests/data/loss-map-0fcfdbc7.xml",
            "type": "loss"}
        write_map_data_to_db(config)
        self.assertEqual(0, len(loss_map.hazardmapdata_set.all()))
        self.assertEqual(3, len(loss_map.lossmapdata_set.all()))
        for idx, loss in enumerate(loss_map.lossmapdata_set.all()):
            self.assertEqual(coords(idx), loss.location.coords)
            self.assertEqual(value(idx), view_utils.round_float(loss.value))
示例#10
0
    def test_prepare_job_result_with_succeeded_and_maps(self):
        """
        The result dictionary for succeeded OpenQuake jobs (w/o hazard/loss
        maps) is prepared correctly.
        """
        hazard_map = self.setup_output()
        self.job_to_teardown = job = hazard_map.oq_job
        self.add_shapefile_data(hazard_map)
        hazard_layer, _ = os.path.splitext(
            os.path.basename(hazard_map.shapefile_path))
        hazard_file = os.path.basename(hazard_map.path)

        loss_map = self.setup_output(job_to_use=job, output_type="loss_map")
        self.add_shapefile_data(loss_map)
        loss_layer, _ = os.path.splitext(
            os.path.basename(loss_map.shapefile_path))
        loss_file = os.path.basename(loss_map.path)
        job.status = "succeeded"
        expected = {
            "id": job.id,
            "msg": "Calculation succeeded",
            "status": "success",
            "files": [
                {"id": hazard_map.id,
                 "layer": {
                    "layer": "geonode:hazard_map_data",
                    "filter": "output_id=%s" % hazard_map.id,
                    "ows": "http://gemsun02.ethz.ch/geoserver-geonode-dev/ows"},
                "min": view_utils.round_float(hazard_map.min_value),
                "max": view_utils.round_float(hazard_map.max_value),
                "name": "%s-%s" % (job.id, hazard_file),
                "type": "hazard map"},
                {"id": loss_map.id,
                 "layer": {
                    "layer": "geonode:loss_map_data",
                    "filter": "output_id=%s" % loss_map.id,
                    "ows": "http://gemsun02.ethz.ch/geoserver-geonode-dev/ows"},
                "min": view_utils.round_float(loss_map.min_value),
                "max": view_utils.round_float(loss_map.max_value),
                "name": "%s-%s" % (job.id, loss_file),
                "type": "loss map"}]}

        actual = prepare_job_result(job)
        self.assertEqual(expected, actual)
示例#11
0
    def test_prepare_map_result_with_loss(self):
        """
        prepare_map_result() returns a correct result dictionary for a
        hazard map.
        """
        self.output = self.setup_output(output_type="loss_map")
        self.output.min_value, self.output.max_value = (30.0, 40.0)

        name = os.path.basename(self.output.path)
        map_type = ("loss map" if self.output.output_type == "loss_map"
                             else "loss map")
        expected = {
            "layer": {
                "layer": "geonode:loss_map_data",
                "filter": "output_id=%s" % self.output.id,
                "ows": "http://gemsun02.ethz.ch/geoserver-geonode-dev/ows"},
            "name": "%s-%s" % (self.output.oq_job.id, name),
            "min": view_utils.round_float(self.output.min_value),
            "max": view_utils.round_float(self.output.max_value),
            "type": map_type,
            "id": self.output.id}

        actual = prepare_map_result(self.output)
        self.assertEqual(expected, actual)
def _upper_bound(iml_n, iml_n_1):
    """
    Calculate an upper bound given the last (n) and second-to-last (n-1) values
    in a vulnerability IML set.

    :type iml_n: float
    :type iml_n_1: float

    :py:function:`view_utils.round_float` is used with the calculated bound
    values to maintain reasonable limits on precision.
    """
    upper_bound = view_utils.round_float(iml_n + ((iml_n - iml_n_1) / 2))

    assert upper_bound > 0.0, \
        "Invalid upper bound '%s': must be > 0.0" % upper_bound

    return upper_bound
def _lower_bound(iml_1, iml_2):
    """
    Calculate a lower bound given the first and second values in a
    vulnerability IML set.

    :type iml_1: float
    :type iml_2: float

    :py:function:`view_utils.round_float` is used with the calculated bound
    values to maintain reasonable limits on precision.
    """
    lower_bound = view_utils.round_float(iml_1 - ((iml_2 - iml_1) / 2))

    assert lower_bound > 0.0, \
        "Invalid lower bound '%s': must be > 0.0" % lower_bound

    return lower_bound
示例#14
0
def _upper_bound(iml_n, iml_n_1):
    """
    Calculate an upper bound given the last (n) and second-to-last (n-1) values
    in a vulnerability IML set.

    :type iml_n: float
    :type iml_n_1: float

    :py:function:`view_utils.round_float` is used with the calculated bound
    values to maintain reasonable limits on precision.
    """
    upper_bound = view_utils.round_float(iml_n + ((iml_n - iml_n_1) / 2))

    assert upper_bound > 0.0, \
        "Invalid upper bound '%s': must be > 0.0" % upper_bound

    return upper_bound
示例#15
0
def _lower_bound(iml_1, iml_2):
    """
    Calculate a lower bound given the first and second values in a
    vulnerability IML set.

    :type iml_1: float
    :type iml_2: float

    :py:function:`view_utils.round_float` is used with the calculated bound
    values to maintain reasonable limits on precision.
    """
    lower_bound = view_utils.round_float(iml_1 - ((iml_2 - iml_1) / 2))

    assert lower_bound > 0.0, \
        "Invalid lower bound '%s': must be > 0.0" % lower_bound

    return lower_bound
 def value(idx):
     """Access the hazard value."""
     return view_utils.round_float(expected_hazard_data[idx][1])
 def value(idx):
     """Access the loss value."""
     return view_utils.round_float(expected_loss_data[idx][1])
 def value(idx):
     """Access the loss value."""
     return view_utils.round_float(expected_loss_data[idx][1])
 def value(idx):
     """Access the hazard value."""
     return view_utils.round_float(expected_hazard_data[idx][1])