Esempio n. 1
0
def run(args):
    if fiona is None:  # pragma: no cover
        raise SystemExit(
            'fiona and shapely must be installed for this command')

    with fiona.collection(
            str(args.repos.path("geo", "level2-shape/level2.shp")),
            "r") as source:
        regions = [f for f in source]

    with update_ordered(args.repos.path("geo", "societies_tdwg.json"),
                        indent=4) as soc_tdwg:
        for ds in args.repos.datasets:
            for soc in ds.societies:
                spec = soc_tdwg.get(
                    soc.id,
                    dict(lat=soc.Lat, lon=soc.Long, name=None, code=None))
                if math.isclose(spec['lat'], soc.Lat) \
                        and math.isclose(spec['lon'], soc.Long) \
                        and spec['code']:
                    continue

                region, dist = geo.match(spec['lon'], spec['lat'], regions)
                spec['name'] = region['properties']['REGION_NAM']
                spec['code'] = str(region['properties']['TDWG_CODE'])

                if dist == 0:  # pragma: no cover
                    args.log.info('{0} contained in region {1}'.format(
                        soc, spec['name']))
                else:
                    args.log.warning(
                        'assigning {0} to nearest region {1}, distance {2}'.
                        format(soc, region['properties']['REGION_NAM'], dist))

                soc_tdwg[soc.id] = spec
Esempio n. 2
0
def test_match_contains_not(mocker):
    mocker.patch(
        'pydplace.geo.shape',
        mocker.Mock(return_value=mocker.Mock(contains=lambda p: False)))
    mocker.patch('pydplace.geo.Point',
                 mocker.Mock(return_value=mocker.Mock(distance=lambda p: 100)))
    assert geo.match(0, 0, [mocker.MagicMock()])[1] == 100
Esempio n. 3
0
def tdwg(args):
    """
    Assign socities to TDWG regions
    """
    try:
        import fiona
        from shapely.geometry import Point
    except ImportError:
        args.log.error('fiona and shapely must be installed for this command')
        return

    def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
        return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

    with fiona.collection(
            args.repos.path("geo", "level2-shape/level2.shp").as_posix(),
            "r") as source:
        regions = [f for f in source]

    with update(args.repos.path("geo", "societies_tdwg.json"),
                default={},
                indent=4) as soc_tdwg:
        for ds in args.repos.datasets:
            for soc in ds.societies:
                spec = soc_tdwg.get(
                    soc.id,
                    dict(lat=soc.Lat, lon=soc.Long, name=None, code=None))
                if isclose(spec['lat'], soc.Lat) \
                        and isclose(spec['lon'], soc.Long) \
                        and spec['code']:
                    continue

                region, dist = geo.match(Point(spec['lon'], spec['lat']),
                                         regions)
                spec['name'] = region['properties']['REGION_NAM']
                spec['code'] = region['properties']['TDWG_CODE']

                if dist == 0:
                    args.log.info('{0} contained in region {1}'.format(
                        soc, spec['name']))
                else:
                    args.log.warn(
                        'assigning {0} to nearest region {1}, distance {2}'.
                        format(soc, region['properties']['REGION_NAM'], dist))

                soc_tdwg[soc.id] = spec
Esempio n. 4
0
def test_match_contains(mocker):
    mocker.patch('pydplace.geo.shape', mocker.Mock())
    mocker.patch('pydplace.geo.Point', mocker.Mock())
    assert geo.match(0, 0, [mocker.MagicMock()])[1] == 0
Esempio n. 5
0
def test_match():
    with pytest.raises(AssertionError):
        geo.match(0, 0, [])