Пример #1
0
    def test_clip_vector_by_vector_polygons(self):
        """WaveEnergy: testing clipping polygons from polygons."""
        from natcap.invest import wave_energy

        aoi_path = os.path.join(REGRESSION_DATA, 'aoi_proj_to_extract.shp')
        extract_path = os.path.join(SAMPLE_DATA, 'WaveData',
                                    'Global_extract.shp')

        result_path = os.path.join(self.workspace_dir, 'aoi_proj_clipped.shp')
        target_projection = pygeoprocessing.get_vector_info(
            extract_path)['projection']
        wave_energy._clip_vector_by_vector(aoi_path, extract_path, result_path,
                                           target_projection,
                                           self.workspace_dir)

        expected_path = os.path.join(REGRESSION_DATA, 'aoi_proj_clipped.shp')
        WaveEnergyRegressionTests._assert_point_vectors_equal(
            result_path, expected_path)
Пример #2
0
    def test_clip_vector_by_vector_points(self):
        """WaveEnergy: testing clipping points from polygons."""
        from natcap.invest import wave_energy

        srs = sampledata.SRS_WILLAMETTE

        pos_x = srs.origin[0]
        pos_y = srs.origin[1]
        fields_pt = {'id': 'int', 'myattr': 'string'}
        attrs_one = [{
            'id': 1,
            'myattr': 'hello'
        }, {
            'id': 2,
            'myattr': 'bye'
        }, {
            'id': 3,
            'myattr': 'highbye'
        }]

        fields_poly = {'id': 'int'}
        attrs_poly = [{'id': 1}]
        # Create geometry for the points, which will get clipped
        geom_one = [
            Point(pos_x + 20, pos_y - 20),
            Point(pos_x + 40, pos_y - 20),
            Point(pos_x + 100, pos_y - 20)
        ]
        # Create geometry for the polygons, which will be used to clip
        geom_two = [
            Polygon([(pos_x, pos_y), (pos_x + 60, pos_y),
                     (pos_x + 60, pos_y - 60), (pos_x, pos_y - 60),
                     (pos_x, pos_y)])
        ]

        shape_to_clip_path = os.path.join(self.workspace_dir,
                                          'shape_to_clip.shp')
        # Create the point shapefile
        shape_to_clip_path = pygeoprocessing.testing.create_vector_on_disk(
            geom_one,
            srs.projection,
            fields_pt,
            attrs_one,
            vector_format='ESRI Shapefile',
            filename=shape_to_clip_path)

        binding_shape_path = os.path.join(self.workspace_dir,
                                          'binding_shape.shp')
        # Create the polygon shapefile
        binding_shape_path = pygeoprocessing.testing.create_vector_on_disk(
            geom_two,
            srs.projection,
            fields_poly,
            attrs_poly,
            vector_format='ESRI Shapefile',
            filename=binding_shape_path)

        output_path = os.path.join(self.workspace_dir, 'vector.shp')
        # Call the function to test
        wave_energy._clip_vector_by_vector(shape_to_clip_path,
                                           binding_shape_path, output_path,
                                           srs.projection, self.workspace_dir)

        # Create the expected point shapefile
        fields_pt = {'id': 'int', 'myattr': 'string'}
        attrs_one = [{'id': 1, 'myattr': 'hello'}, {'id': 2, 'myattr': 'bye'}]
        geom_three = [
            Point(pos_x + 20, pos_y - 20),
            Point(pos_x + 40, pos_y - 20)
        ]
        # Need to save the expected shapefile in a sub folder since it must
        # have the same layer name / filename as what it will be compared
        # against.
        if not os.path.isdir(os.path.join(self.workspace_dir, 'exp_vector')):
            os.mkdir(os.path.join(self.workspace_dir, 'exp_vector'))

        expected_path = os.path.join(self.workspace_dir, 'exp_vector',
                                     'vector.shp')
        expected_shape = pygeoprocessing.testing.create_vector_on_disk(
            geom_three,
            srs.projection,
            fields_pt,
            attrs_one,
            vector_format='ESRI Shapefile',
            filename=expected_path)

        WaveEnergyRegressionTests._assert_point_vectors_equal(
            output_path, expected_shape)
Пример #3
0
    def test_clip_vector_by_vector_points(self):
        """WaveEnergy: testing clipping points from polygons."""
        from natcap.invest import wave_energy

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

        fields_pt = {'id': ogr.OFTInteger, 'myattr': ogr.OFTString}
        attrs_one = [{
            'id': 1,
            'myattr': 'hello'
        }, {
            'id': 2,
            'myattr': 'bye'
        }, {
            'id': 3,
            'myattr': 'highbye'
        }]

        fields_poly = {'id': ogr.OFTInteger}
        attrs_poly = [{'id': 1}]
        # Create geometry for the points, which will get clipped
        geom_one = [
            Point(pos_x + 20, pos_y - 20),
            Point(pos_x + 40, pos_y - 20),
            Point(pos_x + 100, pos_y - 20)
        ]
        # Create geometry for the polygons, which will be used to clip
        geom_two = [
            Polygon([(pos_x, pos_y), (pos_x + 60, pos_y),
                     (pos_x + 60, pos_y - 60), (pos_x, pos_y - 60),
                     (pos_x, pos_y)])
        ]

        shape_to_clip_path = os.path.join(self.workspace_dir,
                                          'shape_to_clip.shp')
        # Create the point shapefile
        pygeoprocessing.shapely_geometry_to_vector(geom_one,
                                                   shape_to_clip_path,
                                                   projection_wkt,
                                                   'ESRI Shapefile',
                                                   fields=fields_pt,
                                                   attribute_list=attrs_one,
                                                   ogr_geom_type=ogr.wkbPoint)

        binding_shape_path = os.path.join(self.workspace_dir,
                                          'binding_shape.shp')
        # Create the polygon shapefile
        pygeoprocessing.shapely_geometry_to_vector(geom_two,
                                                   binding_shape_path,
                                                   projection_wkt,
                                                   'ESRI Shapefile',
                                                   fields=fields_poly,
                                                   attribute_list=attrs_poly)

        output_path = os.path.join(self.workspace_dir, 'vector.shp')
        # Call the function to test
        wave_energy._clip_vector_by_vector(shape_to_clip_path,
                                           binding_shape_path, output_path,
                                           projection_wkt, self.workspace_dir)

        # Create the expected point shapefile
        fields_pt = {'id': ogr.OFTInteger, 'myattr': ogr.OFTString}
        attrs_one = [{'id': 1, 'myattr': 'hello'}, {'id': 2, 'myattr': 'bye'}]
        geom_three = [
            Point(pos_x + 20, pos_y - 20),
            Point(pos_x + 40, pos_y - 20)
        ]
        # Need to save the expected shapefile in a sub folder since it must
        # have the same layer name / filename as what it will be compared
        # against.
        if not os.path.isdir(os.path.join(self.workspace_dir, 'exp_vector')):
            os.mkdir(os.path.join(self.workspace_dir, 'exp_vector'))

        expected_path = os.path.join(self.workspace_dir, 'exp_vector',
                                     'vector.shp')
        pygeoprocessing.shapely_geometry_to_vector(geom_three,
                                                   expected_path,
                                                   projection_wkt,
                                                   'ESRI Shapefile',
                                                   fields=fields_pt,
                                                   attribute_list=attrs_one,
                                                   ogr_geom_type=ogr.wkbPoint)

        WaveEnergyRegressionTests._assert_point_vectors_equal(
            output_path, expected_path)
Пример #4
0
    def test_clip_vector_by_vector_polygons(self):
        """WaveEnergy: testing clipping polygons from polygons."""
        from natcap.invest import wave_energy
        from natcap.invest.utils import _assert_vectors_equal

        projection_wkt = osr.SRS_WKT_WGS84_LAT_LONG
        origin = (-62.00, 44.00)
        pos_x = origin[0]
        pos_y = origin[1]

        fields_aoi = {'id': ogr.OFTInteger}
        attrs_aoi = [{'id': 1}]
        # Create polygon for the aoi
        aoi_polygon = [
            Polygon([(pos_x, pos_y),
                     (pos_x + 2, pos_y), (pos_x + 2, pos_y - 2),
                     (pos_x, pos_y - 2), (pos_x, pos_y)])
        ]

        aoi_path = os.path.join(self.workspace_dir, 'aoi.shp')
        # Create the polygon shapefile
        pygeoprocessing.shapely_geometry_to_vector(aoi_polygon,
                                                   aoi_path,
                                                   projection_wkt,
                                                   'ESRI Shapefile',
                                                   fields=fields_aoi,
                                                   attribute_list=attrs_aoi)

        fields_data = {'id': ogr.OFTInteger, 'myattr': ogr.OFTString}
        attrs_data = [{'id': 1, 'myattr': 'hello'}]
        # Create polygon to clip with the aoi
        data_polygon = [
            Polygon([(pos_x - 2, pos_y + 2), (pos_x + 6, pos_y - 2),
                     (pos_x + 6, pos_y - 4), (pos_x - 2, pos_y - 6),
                     (pos_x - 2, pos_y + 2)])
        ]

        data_path = os.path.join(self.workspace_dir, 'data.shp')
        # Create the polygon shapefile
        pygeoprocessing.shapely_geometry_to_vector(data_polygon,
                                                   data_path,
                                                   projection_wkt,
                                                   'ESRI Shapefile',
                                                   fields=fields_data,
                                                   attribute_list=attrs_data)

        result_path = os.path.join(self.workspace_dir, 'aoi_clipped.shp')
        wave_energy._clip_vector_by_vector(data_path, aoi_path, result_path,
                                           projection_wkt, self.workspace_dir)

        fields_expected = {'id': ogr.OFTInteger, 'myattr': ogr.OFTString}
        attrs_expected = [{'id': 1, 'myattr': 'hello'}]
        # Create polygon to clip with the aoi
        expected_polygon = aoi_polygon
        expected_path = os.path.join(self.workspace_dir, 'expected.shp')
        # Create the polygon shapefile
        pygeoprocessing.shapely_geometry_to_vector(
            expected_polygon,
            expected_path,
            projection_wkt,
            'ESRI Shapefile',
            fields=fields_expected,
            attribute_list=attrs_expected)

        _assert_vectors_equal(expected_path, result_path)