Exemple #1
0
    def test_wind_data_to_point_vector_360(self):
        """WindEnergy: testing 'wind_data_to_point_vector' function.

        This test is to test that when Longitude values range from -360 to 0,
            instead of the normal -180 to 180, they are handled properly.
        """
        from natcap.invest import wind_energy

        # Set up a coordinate with a longitude in the range of -360 to 0.
        wind_data = {
            (31.79, -200.0): {
                'LONG': -200.0,
                'LATI': 31.79,
                'Ram-080m': 7.98,
                'K-010m': 1.90
            }
        }
        wind_data_pickle_path = os.path.join(self.workspace_dir,
                                             'wind_data.pickle')
        pickle.dump(wind_data, open(wind_data_pickle_path, 'wb'))

        layer_name = "datatopoint"
        out_path = os.path.join(self.workspace_dir, 'datatopoint.shp')

        wind_energy._wind_data_to_point_vector(wind_data_pickle_path,
                                               layer_name, out_path)

        field_names = ['LONG', 'LATI', 'Ram-080m', 'K-010m']
        ogr_point = ogr.Geometry(ogr.wkbPoint)
        # Point geometry should have been converted to the WSG84 norm of
        # -180 to 180
        ogr_point.AddPoint_2D(160.00, 31.79)

        shape = ogr.Open(out_path)
        layer = shape.GetLayer()

        feat = layer.GetNextFeature()
        while feat is not None:

            geom = feat.GetGeometryRef()
            if bool(geom.Equals(ogr_point)) is False:
                raise AssertionError(
                    'Geometries are not equal. Expected is: %s, '
                    'but current is %s' % (ogr_point, geom))

            for field in field_names:
                try:
                    field_val = feat.GetField(field)
                    self.assertEqual(wind_data[(31.79, -200.0)][field],
                                     field_val)
                except ValueError:
                    raise AssertionError('Could not find field %s' % field)

            feat = layer.GetNextFeature()
Exemple #2
0
    def test_wind_data_to_point_vector(self):
        """WindEnergy: testing 'wind_data_to_point_vector' function."""
        from natcap.invest import wind_energy

        wind_data = {
            (31.79, 123.76): {
                'LONG': 123.76,
                'LATI': 31.79,
                'Ram-080m': 7.98,
                'K-010m': 1.90
            }
        }
        wind_data_pickle_path = os.path.join(self.workspace_dir,
                                             'wind_data.pickle')
        pickle.dump(wind_data, open(wind_data_pickle_path, 'wb'))

        layer_name = "datatopoint"
        out_path = os.path.join(self.workspace_dir, 'datatopoint.shp')

        wind_energy._wind_data_to_point_vector(wind_data_pickle_path,
                                               layer_name, out_path)

        field_names = ['LONG', 'LATI', 'Ram-080m', 'K-010m']
        ogr_point = ogr.Geometry(ogr.wkbPoint)
        ogr_point.AddPoint_2D(123.76, 31.79)

        shape = ogr.Open(out_path)
        layer = shape.GetLayer()

        feat = layer.GetNextFeature()
        while feat is not None:

            geom = feat.GetGeometryRef()
            if bool(geom.Equals(ogr_point)) is False:
                raise AssertionError(
                    'Geometries are not equal. Expected is: %s, '
                    'but current is %s' % (ogr_point, geom))

            for field in field_names:
                try:
                    field_val = feat.GetField(field)
                    self.assertEqual(wind_data[(31.79, 123.76)][field],
                                     field_val)
                except ValueError:
                    raise AssertionError('Could not find field %s' % field)

            feat = layer.GetNextFeature()