Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geocoding 2.0 #304

Merged
merged 79 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
173d618
Put parents into regions dataframe and geodataframe
IKupriyanov-HORIS Sep 11, 2020
c8f8d1b
Implementing parents in regions_builder.
IKupriyanov-HORIS Sep 14, 2020
1fccca9
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Sep 14, 2020
6fef609
WIP: parents
IKupriyanov-HORIS Sep 15, 2020
9b5ae98
Fix tests, sync protocol with server
IKupriyanov-HORIS Sep 15, 2020
38f1db7
Fix more tests
IKupriyanov-HORIS Sep 16, 2020
54b4457
Fix more tests
IKupriyanov-HORIS Sep 25, 2020
299b6aa
New server IP in demo
IKupriyanov-HORIS Sep 25, 2020
701f78c
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Oct 5, 2020
a37a3f5
WIP: answers in protocol
IKupriyanov-HORIS Oct 14, 2020
f6ca8a3
WIP: moving to Answers instead of flat list of GeocodedFeature
IKupriyanov-HORIS Oct 14, 2020
52e7c1f
WIP: link between request and response
IKupriyanov-HORIS Oct 15, 2020
be18cc5
Getting rid of a query string from Answer and GeocodedFeature
IKupriyanov-HORIS Oct 17, 2020
e8219a8
Remove query from Answer and GeocodedFeature
IKupriyanov-HORIS Oct 19, 2020
326550b
Fix assertion condition
IKupriyanov-HORIS Oct 20, 2020
f6fc7c8
Remove chunked requests, better docs for new API
IKupriyanov-HORIS Oct 20, 2020
cbc1745
WIP: scope
IKupriyanov-HORIS Oct 21, 2020
02aa0ba
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Oct 21, 2020
cca501d
Update example with counties
IKupriyanov-HORIS Oct 22, 2020
91b027a
Update example with counties
IKupriyanov-HORIS Oct 22, 2020
21ec386
Basic support for scope in the new geocoding API
IKupriyanov-HORIS Oct 22, 2020
6f99bbe
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Oct 23, 2020
60968ca
Default scope value to None
IKupriyanov-HORIS Oct 26, 2020
f44d979
Unit tests for new geocoding API
IKupriyanov-HORIS Oct 28, 2020
9ed17b0
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Oct 28, 2020
539ef98
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Oct 28, 2020
4345821
Request validation, tests
IKupriyanov-HORIS Oct 28, 2020
648eef7
Experiment with map and nbviewer
IKupriyanov-HORIS Oct 29, 2020
d492167
Experiment with map and nbviewer and stamen tiles
IKupriyanov-HORIS Oct 29, 2020
bc846fa
Better parents/scope support, more tests
IKupriyanov-HORIS Oct 30, 2020
dcfd17b
where(..., near=..., within=...)
IKupriyanov-HORIS Nov 2, 2020
41e0ce1
Better error handling
IKupriyanov-HORIS Nov 3, 2020
26804b4
Tests for not available features in new API
IKupriyanov-HORIS Nov 4, 2020
fbd7c50
Better error handling for where function (missing key, scope and othe…
IKupriyanov-HORIS Nov 5, 2020
c444036
map_join with multikeys
IKupriyanov-HORIS Nov 14, 2020
0e213d9
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Nov 14, 2020
f7f5df8
Single key data_join_on and map_join_on.
IKupriyanov-HORIS Nov 15, 2020
a31ca74
Multi key data_join_on and map_join_on.
IKupriyanov-HORIS Nov 16, 2020
1f69608
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Nov 23, 2020
08f250f
map_join with dups, but livemap fails with null values in data
IKupriyanov-HORIS Nov 24, 2020
71060c2
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Nov 25, 2020
6c5f57b
Handle null values in pie/bar
IKupriyanov-HORIS Nov 26, 2020
cec1bdc
LP-62
IKupriyanov-HORIS Nov 27, 2020
c5375fb
Better error message on using scope with parents
IKupriyanov-HORIS Dec 4, 2020
5a2569d
Better error message for invalid scope type, countries request suppor…
IKupriyanov-HORIS Dec 7, 2020
4926246
More tests for us-48
IKupriyanov-HORIS Dec 8, 2020
c7c72ba
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Dec 8, 2020
dc5a21f
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Dec 14, 2020
b0ba74f
WIP: changing public API
IKupriyanov-HORIS Dec 15, 2020
536ed25
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Dec 18, 2020
fe750f7
Support Geocoder as geom_xxx(map=...) parameter
IKupriyanov-HORIS Dec 21, 2020
c317a16
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Dec 21, 2020
7531be1
Code cleanup, more tests
IKupriyanov-HORIS Dec 22, 2020
425fef3
Copy not matched dups from map on map_join (fix for last cell in geop…
IKupriyanov-HORIS Dec 23, 2020
ed5f0ea
Use single entry parent for all names
IKupriyanov-HORIS Dec 23, 2020
371525a
Fix empty result for select all kind of request
IKupriyanov-HORIS Dec 24, 2020
481b90d
where -> scope, near -> closest_to, single entry scope, remove auto-c…
IKupriyanov-HORIS Jan 11, 2021
4f3e114
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Jan 11, 2021
5002f32
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Jan 11, 2021
d316cbe
Remove MapRegion from 'scope' type error message
IKupriyanov-HORIS Jan 13, 2021
bfe2d0b
scope can now be used with county and state. scope and country will c…
IKupriyanov-HORIS Jan 14, 2021
f1a3014
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Jan 18, 2021
e747a7e
Handle any Iterable types in parents functions (counties/states/count…
IKupriyanov-HORIS Jan 20, 2021
3915dba
Enable gzip for geocoding responses
IKupriyanov-HORIS Jan 21, 2021
d8b1fac
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Jan 21, 2021
93c7aaa
Update docs
IKupriyanov-HORIS Jan 21, 2021
f1e98cd
Update docs
IKupriyanov-HORIS Jan 21, 2021
4b4a9ba
Keep original query name for ambiguous result with allow_ambiguous flag
IKupriyanov-HORIS Jan 22, 2021
ffaaab8
Fix error message
IKupriyanov-HORIS Jan 22, 2021
50156ed
Remove method to_data_frame and interface CanToDataFrame
IKupriyanov-HORIS Jan 25, 2021
8131764
WIP: update geocoding.md
IKupriyanov-HORIS Jan 26, 2021
1917b39
WIP: update geocoding.md
IKupriyanov-HORIS Jan 26, 2021
a908666
Revert changes to builder.ipynb
IKupriyanov-HORIS Jan 26, 2021
ff331eb
WIP: update geocoding.md
IKupriyanov-HORIS Jan 26, 2021
b6fa9b4
Use geocoding level instead of 'request' for a column name, remove da…
IKupriyanov-HORIS Jan 28, 2021
851fa40
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Jan 28, 2021
83002eb
Support Geocoder in ggplot(data=...)
IKupriyanov-HORIS Jan 29, 2021
99bd3c3
Switch to the new geocoding server
IKupriyanov-HORIS Jan 29, 2021
a203c3b
Merge branch 'master' into geoservices-1.1.0
IKupriyanov-HORIS Jan 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Better parents/scope support, more tests
  • Loading branch information
IKupriyanov-HORIS committed Oct 30, 2020
commit bc846fa9fa7036473df72f0434ed3c2fe70f832c
3 changes: 0 additions & 3 deletions python-package/lets_plot/geo_data/gis/geocoding_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

class GeocodingService:
def do_request(self, request: Request) -> Response:
return self._execute(request)

def _execute(self, request: Request) -> Response:
if not has_global_value(GEOCODING_PROVIDER_URL):
raise ValueError('Geocoding server url is not defined')

Expand Down
22 changes: 12 additions & 10 deletions python-package/lets_plot/geo_data/new_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def _ensure_is_parent_list(obj):
if obj is None:
return None

if isinstance(obj, str):
return [obj]
if isinstance(obj, Regions):
return obj.as_list()

Expand All @@ -40,7 +38,7 @@ def _ensure_is_parent_list(obj):

return [obj]

def _make_parents(values) -> List[Optional[MapRegion]]:
def _make_parents(values: parent_types) -> List[Optional[MapRegion]]:
values = _ensure_is_parent_list(values)

if values is None:
Expand Down Expand Up @@ -76,15 +74,15 @@ def highlights(self, v: bool) -> 'RegionsBuilder2':
self._highlights = v
return self

def countries(self, countries) -> 'RegionsBuilder2':
def countries(self, countries: parent_types) -> 'RegionsBuilder2':
self._countries = _make_parents(countries)
return self

def states(self, states) -> 'RegionsBuilder2':
def states(self, states: parent_types) -> 'RegionsBuilder2':
self._states = _make_parents(states)
return self

def counties(self, counties) -> 'RegionsBuilder2':
def counties(self, counties: parent_types) -> 'RegionsBuilder2':
self._counties = _make_parents(counties)
return self

Expand Down Expand Up @@ -165,15 +163,19 @@ def _validate_parents_size(parents: List, parents_level: str):

return request

def _build_regions(self, response: Response, queries: List[RegionQuery]) -> Regions:
if not isinstance(response, SuccessResponse):
_raise_exception(response)

return Regions(response.level, response.answers, queries, self._highlights)


def build(self) -> Regions:
request: GeocodingRequest = self._build_request()

response: Response = GeocodingService().do_request(request)

if not isinstance(response, SuccessResponse):
_raise_exception(response)

return Regions(response.level, response.answers, request.region_queries, self._highlights)
return self._build_regions(response, request.region_queries)


def __eq__(self, o):
Expand Down
2 changes: 1 addition & 1 deletion python-package/lets_plot/geo_data/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _get_features(self, feature_id: str) -> List[GeocodedFeature]:

request_types = Optional[Union[str, List[str], Series]]
scope_types = Optional[Union[str, List[str], Regions, List[Regions]]]
parent_types = Optional[Union[str, Regions]]
parent_types = Optional[Union[str, Regions, MapRegion, List]] # list of same types


def _raise_exception(response: Response):
Expand Down
28 changes: 23 additions & 5 deletions python-package/test/geo_data/request_assertion.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
# 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 TypeVar, Generic, Optional, List
from typing import TypeVar, Generic, Optional, List, Union

from lets_plot.geo_data.gis.request import Request, GeocodingRequest, RegionQuery, MapRegion, AmbiguityResolver, PayloadKind
from lets_plot.geo_data.regions import _ensure_is_list
from lets_plot.geo_data.gis.request import Request, GeocodingRequest, RegionQuery, MapRegion, AmbiguityResolver, \
PayloadKind, MapRegionKind

T = TypeVar('T')


class ValueMatcher(Generic[T]):
def check(self, value):
raise ValueError('abstract')


class any(ValueMatcher[T]):
def check(self, value):
return


class eq(ValueMatcher[T]):
def __init__(self, v):
self.expected = v

def check(self, value):
assert self.expected == value, '{} != {}'.format(self.expected, value)

class eq_map_region_with_name(eq):

class eq_map_region_with_name(eq[MapRegion]):
def __init__(self, name: str):
self.expected = MapRegion.with_name(name)

class eq_map_region_with_id(eq):
def __init__(self, ids: List[str]):

class eq_map_region_with_id(ValueMatcher[MapRegion]):
"""
Checks only id
"""
def __init__(self, ids: Union[str, List[str]]):
ids = _ensure_is_list(ids)
self.expected = MapRegion.scope(ids)

def check(self, value):
assert value.kind == MapRegionKind.id or value.kind == MapRegionKind.place
assert self.expected.values == value.values


class empty(ValueMatcher[T]):
def check(self, value):
assert value is None, '{} is not None'.format(value)


class item_exists(ValueMatcher[T]):
def __init__(self, value):
self._expected = value
Expand All @@ -55,6 +71,7 @@ class ScopeMatcher:
Scope with ids should have length exactly 1.

'''

def __init__(self):
self._names: Optional[List[str]] = None
self._ids: Optional[List[str]] = None
Expand Down Expand Up @@ -128,6 +145,7 @@ def check(self, q: RegionQuery):
self._state.check(q.state)
self._county.check(q.county)


class GeocodingRequestAssertion:
def __init__(self, request: Request):
assert isinstance(request, GeocodingRequest)
Expand Down
42 changes: 34 additions & 8 deletions python-package/test/geo_data/test_new_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from lets_plot.geo_data.regions import Regions
from .geo_data import make_answer
from .request_assertion import GeocodingRequestAssertion, QueryMatcher, ScopeMatcher, ValueMatcher, eq, empty, \
eq_map_region_with_name
eq_map_region_with_name, eq_map_region_with_id


def test_simple():
Expand All @@ -30,23 +30,49 @@ def test_no_parents_where_should_override_scope():
.has_query(0, no_parents(request=eq('foo'), scope=eq_map_region_with_name('bar')))


def test_when_regions_in_parent_should_take_region_id():
builder = RegionsBuilder2(request='foo') \
.states(make_simple_region('bar'))

assert_that(builder) \
.has_query(0, QueryMatcher()
.with_name('foo')\
.state(eq_map_region_with_id('bar_id'))
)


def test_parents_can_contain_nulls():
builder = RegionsBuilder2(request=['foo', 'bar'])\
.states([None, 'baz'])

assert_that(builder) \
.has_query(0, QueryMatcher()
.with_name('foo') \
.state(empty())
) \
.has_query(1, QueryMatcher()
.with_name('bar') \
.state(eq_map_region_with_name('baz'))
)


def test_where_with_given_parents_and_duplicated_names():
# should update scope only for matching name and parents - query with index 1

request = RegionsBuilder2(request=['foo', 'foo']) \
.counties(['bar', 'baz']) \
.where(name='foo', county='baz', scope='spam') \
.states(['bar', 'baz']) \
.where(name='foo', state='baz', scope='spam') \
._build_request()

assert_that(request) \
.has_query(0, QueryMatcher()
.with_name('foo')
.county(eq_map_region_with_name('bar'))
.state(eq_map_region_with_name('bar'))
.scope(empty())
) \
.has_query(1, QueryMatcher()
.with_name('foo')
.county(eq_map_region_with_name('baz'))
.state(eq_map_region_with_name('baz'))
.scope(eq_map_region_with_name('spam'))
)

Expand Down Expand Up @@ -114,7 +140,7 @@ def test_error_where_scope_len_is_invalid():
)


def make_simple_region(requests: Union[str, List[str]], geo_object_ids: Union[str, List[str]] = None) -> Regions:
def make_simple_region(requests: Union[str, List[str]], geo_object_ids: Union[str, List[str]] = None, level_kind: LevelKind = LevelKind.county) -> Regions:
requests = requests if isinstance(requests, (list, tuple)) else [requests]
geo_object_ids = geo_object_ids if geo_object_ids is not None else [request + '_id' for request in requests]
geo_object_ids = geo_object_ids if isinstance(geo_object_ids, (list, tuple)) else [geo_object_ids]
Expand All @@ -125,7 +151,7 @@ def make_simple_region(requests: Union[str, List[str]], geo_object_ids: Union[st
queries.append(RegionQuery(request=request))
answers.append(make_answer(request, id, []))

return Regions(LevelKind.county, answers, queries)
return Regions(level_kind, answers, queries)


def no_parents(request: ValueMatcher[Optional[str]],
Expand All @@ -136,7 +162,7 @@ def no_parents(request: ValueMatcher[Optional[str]],
country=empty(), state=empty(), county=empty())


def assert_that(request):
def assert_that(request: Union[RegionsBuilder2, GeocodingRequest]):
if isinstance(request, RegionsBuilder2):
return GeocodingRequestAssertion(request._build_request())
elif isinstance(request, GeocodingRequest):
Expand Down