Esempio n. 1
0
def test_intersect_overwrite_existing(monkeypatch):
    monkeypatch.setattr('pandarus.calculate.intersection_dispatcher',
                        fake_intersection)

    with tempfile.TemporaryDirectory() as dirpath:
        vector_fp, data_fp = intersect(grid,
                                       'name',
                                       square,
                                       'name',
                                       dirpath=dirpath,
                                       compress=False,
                                       cpus=None)

        with open(vector_fp, "w") as f:
            f.write("Weeeee!")
        with open(data_fp, "w") as f:
            f.write("Wooooo!")

        vector_fp, data_fp = intersect(grid,
                                       'name',
                                       square,
                                       'name',
                                       dirpath=dirpath,
                                       compress=False,
                                       cpus=None)

        data = json.load(open(data_fp))
        assert data['data'] == [['grid cell 0', 'single', 42]]

        assert len(fiona.open(vector_fp)) == 1
Esempio n. 2
0
def test_intersection_polygon_projection():
    area = 1/4 * (4e7 / 360) ** 2

    with tempfile.TemporaryDirectory() as dirpath:
        vector_fp, data_fp = intersect(
            pgrid,
            'name',
            psquare,
            'name',
            dirpath=dirpath,
            compress=False,
            log_dir=dirpath
        )
        data = json.load(open(data_fp))

        assert len(data['data']) == 4
        for x, y, z in data['data']:
            assert x in ['grid cell {}'.format(x) for x in range(4)]
            assert y == 'single'
            assert np.isclose(z, area, rtol=1e-2)

        assert data['metadata'].keys() == {'first', 'second', 'when'}
        assert data['metadata']['first'].keys() == {'field', 'filename', 'path', 'sha256'}
        assert data['metadata']['second'].keys() == {'field', 'filename', 'path', 'sha256'}

        with fiona.open(vector_fp) as src:
            meta = src.meta

            assert meta['driver'] == 'GeoJSON'
            assert meta['schema'] == {
                'geometry': 'MultiPolygon',
                'properties': dict([
                    ('measure', 'float'),
                    ('from_label', 'str'),
                    ('id', 'int'),
                    ('to_label', 'str')
                ])
            }
            assert meta['crs'] == {'init': 'epsg:4326'}

            coords = [
                [[[(0.5, 1.0), (1.0, 1.0), (1.0, 0.5), (0.5, 0.5), (0.5, 1.0)]]],
                [[[(1.0, 1.5), (1.0, 1.0), (0.5, 1.0), (0.5, 1.5), (1.0, 1.5)]]],
                [[[(1.0, 0.5), (1.0, 1.0), (1.5, 1.0), (1.5, 0.5), (1.0, 0.5)]]],
                [[[(1.0, 1.0), (1.0, 1.5), (1.5, 1.5), (1.5, 1.0), (1.0, 1.0)]]]
            ]

            for feature in src:
                print(feature['geometry']['coordinates'])
                assert feature['geometry']['coordinates'] in coords
                assert feature['geometry']['type'] == 'MultiPolygon'
                assert feature['properties'].keys() == {'measure', 'from_label', 'to_label', 'id'}
                assert np.isclose(feature['properties']['measure'], area, rtol=1e-2)
Esempio n. 3
0
def test_intersection_line():
    one_degree = 4e7 / 360

    with tempfile.TemporaryDirectory() as dirpath:
        vector_fp, data_fp = intersect(
            lines,
            'name',
            grid,
            'name',
            dirpath=dirpath,
            compress=False,
            log_dir=dirpath
        )
        data = json.load(open(data_fp))
        data_dct = {(x, y): z for x, y, z in data['data']}

        assert len(data['data']) == 4
        assert np.isclose(data_dct[('A', 'grid cell 0')], 62000, rtol=1e-2)
        assert np.isclose(data_dct[('A', 'grid cell 1')], one_degree, rtol=1e-2)
        assert np.isclose(data_dct[('A', 'grid cell 3')], 50000, rtol=1e-2)
        assert np.isclose(data_dct[('B', 'grid cell 2')], sqrt(2) * one_degree / 2, rtol=2e-2)

        assert data['metadata'].keys() == {'first', 'second', 'when'}
        assert data['metadata']['first'].keys() == {'field', 'filename', 'path', 'sha256'}
        assert data['metadata']['second'].keys() == {'field', 'filename', 'path', 'sha256'}

        with fiona.open(vector_fp) as src:
            meta = src.meta

            assert meta['driver'] == 'GeoJSON'
            assert meta['schema'] == {
                'geometry': 'MultiLineString',
                'properties': dict([
                    ('measure', 'float'),
                    ('from_label', 'str'),
                    ('id', 'int'),
                    ('to_label', 'str')
                ])
            }
            assert meta['crs'] == {'init': 'epsg:4326'}

            coords = [
                [[(1.0, 1.5), (1.5, 1.5)]],
                [[(0.5, 1.0), (0.5, 1.5), (1.0, 1.5)]],
                [[(0.5, 0.5), (0.5, 1.0)]],
                [[(1.0, 1.0), (1.5, 0.5)]]
            ]

            for feature in src:
                assert feature['geometry']['coordinates'] in coords
                assert feature['geometry']['type'] == 'MultiLineString'
                assert feature['properties'].keys() == {'measure', 'from_label', 'to_label', 'id'}
Esempio n. 4
0
def test_intersect_default_path(monkeypatch):
    monkeypatch.setattr('pandarus.calculate.intersection_dispatcher',
                        fake_intersection)

    vector_fp, data_fp = intersect(grid, 'name', square, 'name', cpus=None)

    print(vector_fp)
    print(data_fp)

    assert os.path.isfile(vector_fp)
    os.remove(vector_fp)
    assert os.path.isfile(data_fp)
    os.remove(data_fp)
Esempio n. 5
0
def test_intersection_point_projection():
    with tempfile.TemporaryDirectory() as dirpath:
        vector_fp, data_fp = intersect(
            ppoints,
            'name',
            pgrid,
            'name',
            dirpath=dirpath,
            compress=False,
            log_dir=dirpath
        )
        data = json.load(open(data_fp))
        data_dct = {(x, y): z for x, y, z in data['data']}

        assert len(data['data']) == 2
        assert data_dct[('point 1', 'grid cell 0')] == 1
        assert data_dct[('point 2', 'grid cell 3')] == 1
        assert data['metadata'].keys() == {'first', 'second', 'when'}
        assert data['metadata']['first'].keys() == {'field', 'filename', 'path', 'sha256'}
        assert data['metadata']['second'].keys() == {'field', 'filename', 'path', 'sha256'}

        with fiona.open(vector_fp) as src:
            meta = src.meta

            assert meta['driver'] == 'GeoJSON'
            assert meta['schema'] == {
                'geometry': 'MultiPoint',
                'properties': dict([
                    ('measure', 'float'),
                    ('from_label', 'str'),
                    ('id', 'int'),
                    ('to_label', 'str')
                ])
            }
            assert meta['crs'] == {'init': 'epsg:4326'}

            coords = [[(0.5, 0.5)], [(1.5, 1.5)]]

            for feature in src:
                print(feature)
                assert feature['geometry']['coordinates'] in coords
                assert feature['geometry']['type'] == 'MultiPoint'
                assert feature['properties'].keys() == {'measure', 'from_label', 'to_label', 'id'}
Esempio n. 6
0
def test_intersect(monkeypatch):
    monkeypatch.setattr('pandarus.calculate.intersection_dispatcher',
                        fake_intersection)

    with tempfile.TemporaryDirectory() as dirpath:
        vector_fp, data_fp = intersect(grid,
                                       'name',
                                       square,
                                       'name',
                                       dirpath=dirpath,
                                       compress=False,
                                       cpus=None)

        data = json.load(open(data_fp))
        assert data['data'] == [['grid cell 0', 'single', 42]]
        assert data['metadata'].keys() == {'first', 'second', 'when'}
        assert data['metadata']['first'].keys() == {
            'field', 'filename', 'path', 'sha256'
        }
        assert data['metadata']['second'].keys() == {
            'field', 'filename', 'path', 'sha256'
        }

        expected = {
            'id':
            '0',
            'type':
            'Feature',
            'geometry': {
                'coordinates': [[(0.5, 0.5), (0.5, 1.5), (1.5, 1.5),
                                 (1.5, 0.5), (0.5, 0.5)]],
                'type':
                'Polygon'
            },
            'properties':
            dict([('id', 0), ('to_label', 'single'),
                  ('from_label', 'grid cell 0'), ('measure', 42.0)])
        }
        assert next(iter(fiona.open(vector_fp))) == expected
Esempio n. 7
0
def test_intersection_line_projection():
    one_degree = 4e7 / 360

    with tempfile.TemporaryDirectory() as dirpath:
        vector_fp, data_fp = intersect(
            plines,
            'name',
            pgrid,
            'name',
            dirpath=dirpath,
            compress=False,
            log_dir=dirpath
        )
        data = json.load(open(data_fp))
        data_dct = {(x, y): z for x, y, z in data['data']}

        assert len(data['data']) == 5
        assert np.isclose(data_dct[('A', 'grid cell 0')], 62000, rtol=1e-2)
        assert np.isclose(data_dct[('A', 'grid cell 1')], one_degree, rtol=1e-2)
        assert np.isclose(data_dct[('A', 'grid cell 3')], 50000, rtol=1e-2)
        assert np.isclose(data_dct[('B', 'grid cell 2')], sqrt(2) * one_degree / 2, rtol=2e-2)
        assert data_dct[('B', 'grid cell 3')] < 1e-3

        assert data['metadata'].keys() == {'first', 'second', 'when'}
        assert data['metadata']['first'].keys() == {'field', 'filename', 'path', 'sha256'}
        assert data['metadata']['second'].keys() == {'field', 'filename', 'path', 'sha256'}

        with fiona.open(vector_fp) as src:
            meta = src.meta

            assert meta['driver'] == 'GeoJSON'
            assert meta['schema'] == {
                'geometry': 'MultiLineString',
                'properties': dict([
                    ('measure', 'float'),
                    ('from_label', 'str'),
                    ('id', 'int'),
                    ('to_label', 'str')
                ])
            }
            assert meta['crs'] == {'init': 'epsg:4326'}

            coords = [np.array(x) for x in [
                [[[1., 1.], [1., 1.]]],
                [[(1.0, 1.5), (1.5, 1.5)]],
                [[(0.5, 1.0), (0.5, 1.5), (1.0, 1.5)]],
                [[(0.5, 0.5), (0.5, 1.0)]],
                [[(1.0, 1.0), (1.5, 0.5)]]
            ]]

            arrays = [
                round_to_x_significant_digits(np.array(x['geometry']['coordinates']))
                for x in src
            ]

            for array in arrays:
                print(array)
                assert any(np.allclose(array, obj)
                    for obj in coords
                    if array.shape == obj.shape)

            for feature in src:
                assert feature['geometry']['type'] == 'MultiLineString'
                assert feature['properties'].keys() == {'measure', 'from_label', 'to_label', 'id'}
Esempio n. 8
0
def intersect_task(id1, id2, output):
    try:
        cpus = int(os.environ['PANDARUS_CPUS'])
        assert cpus
    except:
        cpus = None

    first = File.get(File.id == id1)
    second = File.get(File.id == id2)

    print("Intersect task:", first.name, second.name)

    # Job enqueued twice
    if Intersection.select().where(
            (Intersection.first == first) &
            (Intersection.second == second)).count():
        return

    vector, data = intersect(
        first.filepath,
        first.field,
        second.filepath,
        second.field,
        dirpath=output,
        cpus=cpus,
        driver=EXPORT_FORMAT,
        log_dir=logs_dir
    )

    if Intersection.select().where(
            (Intersection.first == first) &
            (Intersection.second == second)).count():
        return

    Intersection(
        first=first,
        second=second,
        data_fp=data,
        vector_fp=vector,
    ).save()

    # Save intersection data files for new spatial scale
    with fiona.open(vector) as src:
        geom_type = src.meta['schema']['geometry']

    third = File.create(
        filepath=vector,
        name=os.path.basename(vector),
        sha256=sha256(vector),
        band=None,
        layer=None,
        field='id',
        kind='vector',
        geometry_type=geom_type,
    )

    fp1, fp2 = intersections_from_intersection(
        vector,
        data,
        output
    )

    Intersection(
        first=third,
        second=first,
        data_fp=fp1,
        vector_fp=vector
    ).save()

    Intersection(
        first=third,
        second=second,
        data_fp=fp2,
        vector_fp=vector
    ).save()