Ejemplo n.º 1
0
def test_allow_ambiguous_and_closest_to():
    request = geocode(names=['foo', 'bar'])\
        .where('foo', closest_to=shapely.geometry.Point(1, 2))\
        .allow_ambiguous()\
        ._build_request()

    assert_that(request) \
        .allows_ambiguous()\
        .has_query(QueryMatcher()
                   .with_name('foo')
                   .ambiguity_resolver(eq(AmbiguityResolver(closest_coord=GeoPoint(1, 2))))
                   ) \
        .has_query(QueryMatcher()
                   .with_name('bar')
                   .ambiguity_resolver(eq(AmbiguityResolver(ignoring_strategy=IgnoringStrategyKind.take_namesakes)))
                   )
Ejemplo n.º 2
0
def test_geocoding_request():
    foo = _query(request='foo')
    bar = _query(request='bar',
                 scope=MapRegion.with_name('baz'),
                 ambiguity_resolver=AmbiguityResolver(
                     ignoring_strategy=IgnoringStrategyKind.skip_all))

    request: GeocodingRequest = RequestBuilder() \
        .set_request_kind(RequestKind.geocoding) \
        .set_requested_payload([PayloadKind.boundaries, PayloadKind.centroids]) \
        .set_resolution(123) \
        .set_level(LevelKind.city) \
        .set_namesake_limit(10) \
        .set_queries([foo, bar]) \
        .build()

    json = RequestFormatter().format(request).to_dict()

    parsed_request = RequestParser().parse(json)

    assert isinstance(parsed_request, GeocodingRequest)
    assert request.requested_payload == parsed_request.requested_payload
    assert request.resolution == parsed_request.resolution

    assert request.level == parsed_request.level
    assert request.namesake_example_limit == parsed_request.namesake_example_limit
    assert 2 == len(parsed_request.region_queries)
    assert_region_query(foo, parsed_request.region_queries[0])
    assert_region_query(bar, parsed_request.region_queries[1])
Ejemplo n.º 3
0
def test_allow_ambiguous():
    request = geocode(names='foo')\
        .allow_ambiguous()\
        ._build_request()

    assert_that(request) \
        .has_query(QueryMatcher()
                   .with_name('foo')
                   .ambiguity_resolver(eq(AmbiguityResolver(ignoring_strategy=IgnoringStrategyKind.take_namesakes)))
                   )
Ejemplo n.º 4
0
def no_parents(
    request: ValueMatcher[Optional[str]],
    scope: ValueMatcher[Optional[MapRegion]] = empty(),
    ambiguity_resolver: ValueMatcher[AmbiguityResolver] = eq(
        AmbiguityResolver.empty())
) -> QueryMatcher:
    return QueryMatcher(name=request,
                        scope=scope,
                        ambiguity_resolver=ambiguity_resolver,
                        country=empty(),
                        state=empty(),
                        county=empty())
Ejemplo n.º 5
0
def test_where_closets_to_point():
    request = geocode(names=['foo']) \
        .states(['bar']) \
        .where(name='foo', state='bar', closest_to=shapely.geometry.Point(1, 2)) \
        ._build_request()

    assert_that(request) \
        .has_query(QueryMatcher()
                   .with_name('foo')
                   .state(eq_map_region_with_name('bar'))
                   .ambiguity_resolver(eq(AmbiguityResolver(closest_coord=GeoPoint(1, 2))))
                   )
Ejemplo n.º 6
0
def test_where_scope_is_box():
    request = geocode(names=['foo']) \
        .states(['bar']) \
        .where(name='foo', state='bar', scope=shapely.geometry.box(1, 2, 3, 4)) \
        ._build_request()

    assert_that(request) \
        .has_query(QueryMatcher()
                   .with_name('foo')
                   .state(eq_map_region_with_name('bar'))
                   .ambiguity_resolver(eq(AmbiguityResolver(box=GeoRect(1, 2, 3, 4))))
                   )
Ejemplo n.º 7
0
def query(request: Optional[str] = None,
          scope: Optional[Union[str, MapRegion]] = None,
          ignoring_strategy: Optional[IgnoringStrategyKind] = None,
          near: Optional[Union[str, GeoPoint]] = None,
          box: Optional[GeoRect] = None) -> RegionQuery:
    if isinstance(scope, MapRegion):
        pass
    elif isinstance(scope, str):
        scope = MapRegion.with_name(scope)
    else:
        scope = None

    return RegionQuery(request, scope,
                       AmbiguityResolver(ignoring_strategy, near, box))
Ejemplo n.º 8
0
def test_ambiguity_resolver():
    foo = _query(request='foo', ambiguity_resolver=AmbiguityResolver(closest_coord=GeoPoint(1, 2)))

    request: GeocodingRequest = RequestBuilder() \
        .set_request_kind(RequestKind.geocoding) \
        .set_namesake_limit(10) \
        .set_queries([foo]) \
        .build()

    json = RequestFormatter().format(request).to_dict()

    parsed_request = RequestParser().parse(json)

    assert isinstance(parsed_request, GeocodingRequest)
    assert 1 == len(parsed_request.region_queries)
    assert_region_query(foo, parsed_request.region_queries[0])
Ejemplo n.º 9
0
def test_ambiguity_resolver_with_box():
    foo = _query(request='foo', ambiguity_resolver=AmbiguityResolver(box=GeoRect(0, 1, 2, 3)))

    request: GeocodingRequest = RequestBuilder() \
        .set_request_kind(RequestKind.geocoding) \
        .set_namesake_limit(10) \
        .set_queries([foo]) \
        .set_allow_ambiguous(True) \
        .build()

    json = RequestFormatter().format(request).to_dict()

    parsed_request = RequestParser().parse(json)

    assert isinstance(parsed_request, GeocodingRequest)
    assert 1 == len(parsed_request.region_queries)
    assert parsed_request.allow_ambiguous
    assert_region_query(foo, parsed_request.region_queries[0])
Ejemplo n.º 10
0
def _query(request: Optional[str],
           scope: Optional[MapRegion] = None,
           ambiguity_resolver: AmbiguityResolver = AmbiguityResolver.empty()):
    return RegionQuery(request, scope, ambiguity_resolver)
Ejemplo n.º 11
0
#  Copyright (c) 2020. JetBrains s.r.o.
#  Use of this source code is governed by the MIT license that can be found in the LICENSE file.

from typing import Optional, List

import pytest

from lets_plot.geo_data.gis.request import MapRegion, RegionQuery, MISSING_LEVEL_AND_SCOPE_OR_REQUEST_EXCEPTION_TEXT, \
    MISSING_LEVEL_OR_REQUEST_EXCEPTION_TEXT, GeocodingRequest, \
    AmbiguityResolver
from lets_plot.geo_data.gis.response import LevelKind

REACTION_KIND_ALERT: AmbiguityResolver = AmbiguityResolver.empty()

NAME = 'NY'
LEVEL: LevelKind = LevelKind.city
REGION_STR = 'ignored'
PARENT = MapRegion.with_name(REGION_STR)


@pytest.mark.parametrize('region_queries,level,message', [
    ([], None, MISSING_LEVEL_AND_SCOPE_OR_REQUEST_EXCEPTION_TEXT),
    ([RegionQuery(None, None, REACTION_KIND_ALERT)], None, MISSING_LEVEL_AND_SCOPE_OR_REQUEST_EXCEPTION_TEXT),
    ([RegionQuery(None, PARENT, REACTION_KIND_ALERT)], None, MISSING_LEVEL_OR_REQUEST_EXCEPTION_TEXT),
])
def test_args_that_fail(region_queries: List[RegionQuery],
                        level: Optional[LevelKind],
                        message: str):
    with pytest.raises(ValueError) as exception:
        GeocodingRequest._check_required_parameters(region_queries, level)
Ejemplo n.º 12
0
RECT_DATA_FRAME = DataFrame({DATAFRAME_COLUMN_NAME: DATAFRAME_NAME_LIST, 'lonmin': [1, 2], 'latmin': [3, 4], 'lonmax': [5, 6], 'latmax': [7, 8]})

LEVEL_KIND: LevelKind = LevelKind.county
LEVEL: str = LEVEL_KIND.value
FILTER = 'Texas'
FILTER_LIST = ['LA', 'NY']
REGION_NAME = 'USA'
REGION_ID = '8898'
REGION_LIST = [REGION_ID]
RESOLUTION = 12
REQUEST = 'united states'
REGION_HIGHLIGHTS = ['united states', 'united states of america']

PARENT_WITH_NAME = MapRegion.with_name(REGION_NAME)

REGION_QUERY_LA = RegionQuery('LA', None, AmbiguityResolver())
REGION_QUERY_NY = RegionQuery('NY', None, AmbiguityResolver())

NAMESAKES_EXAMPLE_LIMIT = 10


def feature_id(answer: Answer) -> str:
    assert len(answer.features) == 1
    return answer.features[0].id


def make_expected_map_region(region_kind: RegionKind, values):
    return {
        'type': region_kind.value,
        'data': values
    }
Ejemplo n.º 13
0
})

LEVEL_KIND: LevelKind = LevelKind.county
LEVEL: str = LEVEL_KIND.value
FILTER = 'Texas'
FILTER_LIST = ['LA', 'NY']
REGION_NAME = 'USA'
REGION_ID = '8898'
REGION_LIST = [REGION_ID]
RESOLUTION = 12
REQUEST = 'united states'
REGION_HIGHLIGHTS = ['united states', 'united states of america']

PARENT_WITH_NAME = MapRegion.with_name(REGION_NAME)

REGION_QUERY_LA = RegionQuery('LA', PARENT_WITH_NAME, AmbiguityResolver())
REGION_QUERY_NY = RegionQuery('NY', PARENT_WITH_NAME, AmbiguityResolver())

NAMESAKES_EXAMPLE_LIMIT = 10


def make_expected_map_region(region_kind: RegionKind, values):
    return {'type': region_kind.value, 'data': values}


@mock.patch.object(GeocodingService, 'do_request')
def test_regions(mock_geocoding):
    try:
        regions(level=LEVEL, request=FILTER_LIST,
                within=REGION_NAME).to_data_frame()
    except Exception: