Esempio n. 1
0
    def test_calculate_distances_land_grid(self):
        """WindEnergy: testing 'calculate_distances_land_grid' function."""
        from natcap.invest import wind_energy

        # Setup parameters for creating point shapefile
        fields = {'id': 'real', 'L2G': 'real'}
        attrs = [{'id': 1, 'L2G': 10}, {'id': 2, 'L2G': 20}]
        srs = sampledata.SRS_WILLAMETTE
        pos_x = srs.origin[0]
        pos_y = srs.origin[1]
        geometries = [
            Point(pos_x + 50, pos_y - 50),
            Point(pos_x + 50, pos_y - 150)
        ]
        shape_path = os.path.join(self.workspace_dir, 'temp_shape.shp')
        # Create point shapefile to use for testing input
        land_shape_path = pygeoprocessing.testing.create_vector_on_disk(
            geometries,
            srs.projection,
            fields,
            attrs,
            vector_format='ESRI Shapefile',
            filename=shape_path)

        # Setup parameters for create raster
        matrix = numpy.array([[1, 1, 1, 1], [1, 1, 1, 1]])
        raster_path = os.path.join(self.workspace_dir, 'temp_raster.tif')
        # Create raster to use for testing input
        harvested_masked_path = pygeoprocessing.testing.create_raster_on_disk(
            [matrix],
            srs.origin,
            srs.projection,
            -1,
            srs.pixel_size(100),
            datatype=gdal.GDT_Int32,
            filename=raster_path)

        tmp_dist_final_path = os.path.join(self.workspace_dir,
                                           'dist_final.tif')
        # Call function to test given testing inputs
        wind_energy._calculate_distances_land_grid(land_shape_path,
                                                   harvested_masked_path,
                                                   tmp_dist_final_path, '')

        # Compare the results
        result = gdal.Open(tmp_dist_final_path)
        res_band = result.GetRasterBand(1)
        res_array = res_band.ReadAsArray()
        exp_array = numpy.array([[10, 110, 210, 310], [20, 120, 220, 320]])
        numpy.testing.assert_array_equal(res_array, exp_array)
Esempio n. 2
0
    def test_calculate_distances_land_grid(self):
        """WindEnergy: testing 'calculate_distances_land_grid' function."""
        from natcap.invest import wind_energy

        srs = osr.SpatialReference()
        srs.ImportFromEPSG(3157)
        projection_wkt = srs.ExportToWkt()
        origin = (443723.127327877911739, 4956546.905980412848294)
        pos_x = origin[0]
        pos_y = origin[1]

        # Setup parameters for creating point shapefile
        fields = {'id': ogr.OFTReal, 'L2G': ogr.OFTReal}
        attrs = [{'id': 1, 'L2G': 10}, {'id': 2, 'L2G': 20}]

        geometries = [
            Point(pos_x + 50, pos_y - 50),
            Point(pos_x + 50, pos_y - 150)
        ]
        land_shape_path = os.path.join(self.workspace_dir, 'temp_shape.shp')
        # Create point shapefile to use for testing input
        pygeoprocessing.shapely_geometry_to_vector(geometries,
                                                   land_shape_path,
                                                   projection_wkt,
                                                   'ESRI Shapefile',
                                                   fields=fields,
                                                   attribute_list=attrs,
                                                   ogr_geom_type=ogr.wkbPoint)

        # Setup parameters for create raster
        matrix = numpy.array([[1, 1, 1, 1], [1, 1, 1, 1]], dtype=numpy.int32)
        harvested_masked_path = os.path.join(self.workspace_dir,
                                             'temp_raster.tif')
        # Create raster to use for testing input
        pygeoprocessing.numpy_array_to_raster(matrix, -1, (100, -100), origin,
                                              projection_wkt,
                                              harvested_masked_path)

        tmp_dist_final_path = os.path.join(self.workspace_dir,
                                           'dist_final.tif')
        # Call function to test given testing inputs
        wind_energy._calculate_distances_land_grid(land_shape_path,
                                                   harvested_masked_path,
                                                   tmp_dist_final_path, '')

        # Compare the results
        res_array = pygeoprocessing.raster_to_numpy_array(tmp_dist_final_path)
        exp_array = numpy.array([[10, 110, 210, 310], [20, 120, 220, 320]],
                                dtype=numpy.int32)
        numpy.testing.assert_allclose(res_array, exp_array)