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
Use geocoding level instead of 'request' for a column name, remove da…
…ta_join_on/map_join_on, generate map_join for map automatically if map is a Geocoder object
  • Loading branch information
IKupriyanov-HORIS committed Jan 28, 2021
commit b6fa9b4213a8e6f8cb6fb10776caf0dec6638a08
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ class LiveMap : PlotConfigDemoBase() {
"geocoding": {
"url": "https://172.31.52.145:3025"
},
"data_join_on": "States",
"map_join_on": "request"
"map_join": ["States", "state"]
}
]
}
Expand Down Expand Up @@ -230,8 +229,7 @@ class LiveMap : PlotConfigDemoBase() {
"geocoding": {
"url": "https://172.31.52.145:3025"
},
"data_join_on": "States",
"map_join_on": "request"
"map_join": ["States", "state"]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.data.DataFrameUtil
import jetbrains.datalore.plot.base.data.DataFrameUtil.findVariableOrFail
import jetbrains.datalore.plot.base.data.DataFrameUtil.variables
import jetbrains.datalore.plot.base.data.Dummies
import jetbrains.datalore.plot.config.Option.Meta
Expand Down Expand Up @@ -45,7 +46,7 @@ object ConfigUtil {
}

fun computeMultiKeys(dataFrame: DataFrame, keyVarNames: List<*>): List<List<Any?>> {
val keyVars = keyVarNames.map { keyVarName -> variables(dataFrame)[keyVarName] ?: error("Key $keyVarName not found") }
val keyVars = keyVarNames.map { keyVarName -> findVariableOrFail(dataFrame, keyVarName as String)}
return (0 until dataFrame.rowCount()).map { rowIndex -> keyVars.map { dataFrame.get(it)[rowIndex] } }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class GeoConfig(
const val RECT_YMIN = "latmin"
const val RECT_XMAX = "lonmax"
const val RECT_YMAX = "latmax"
const val MAP_JOIN_REQUIRED_MESSAGE = "map_join or data_join_on/map_join_on is required when both data and map parameters used"
const val MAP_JOIN_REQUIRED_MESSAGE = "map_join is required when both data and map parameters used"

fun isApplicable(layerOptions: Map<*, *>, combinedMappings: Map<*, *>): Boolean {
if (combinedMappings.keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ open class Polygons : PlotConfigDemoBase() {
"continent"
]
],
"data_join_on": null,
"map_join_on": null,
"color": "white"
}
]
Expand Down
29 changes: 10 additions & 19 deletions python-package/lets_plot/geo_data/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from pandas import Series

from .geocodes import _to_level_kind, request_types, Geocodes, _raise_exception, \
_ensure_is_list
from .geocodes import _to_level_kind, request_types, Geocodes, _raise_exception, _ensure_is_list
from .gis.geocoding_service import GeocodingService
from .gis.geometry import GeoRect, GeoPoint
from .gis.request import RequestBuilder, GeocodingRequest, RequestKind, MapRegion, AmbiguityResolver, \
Expand Down Expand Up @@ -223,26 +222,18 @@ def __init__(self, lon, lat, level: Optional[Union[str, LevelKind]], scope=None)

def _geocode(self) -> Geocodes:
if self._geocodes is None:
self._geocodes = self._geocode()
response: Response = GeocodingService().do_request(self._request)
if not isinstance(response, SuccessResponse):
_raise_exception(response)
self._geocodes = Geocodes(
response.level,
response.answers,
[RegionQuery(request='[{}, {}]'.format(pt.lon, pt.lat)) for pt in self._request.coordinates],
highlights=False
)

return self._geocodes

def _geocode(self):
response: Response = GeocodingService().do_request(self._request)

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

return Geocodes(
response.level,
response.answers,
[
RegionQuery(request='[{}, {}]'.format(pt.lon, pt.lat)) for pt in self._request.coordinates
],
False
)



class NamesGeocoder(Geocoder):
def __init__(self,
Expand Down
69 changes: 50 additions & 19 deletions python-package/lets_plot/geo_data/geocodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
NO_OBJECTS_FOUND_EXCEPTION_TEXT = 'No objects were found.'
MULTIPLE_OBJECTS_FOUND_EXCEPTION_TEXT = "Multiple objects were found. Use all_result=True to see them."

DF_REQUEST = 'request'
DF_ID = 'id'
DF_FOUND_NAME = 'found name'
DF_HIGHLIGHTS = 'highlights'
DF_GROUP = 'group'
DF_PARENT_COUNTRY = 'country'
DF_PARENT_STATE = 'state'
DF_PARENT_COUNTY = 'county'
DF_COLUMN_ID = 'id'
DF_COLUMN_FOUND_NAME = 'found name'
DF_COLUMN_HIGHLIGHTS = 'highlights'
DF_COLUMN_CITY = 'city'
DF_COLUMN_COUNTRY = 'country'
DF_COLUMN_STATE = 'state'
DF_COLUMN_COUNTY = 'county'


class Resolution(enum.Enum):
Expand Down Expand Up @@ -55,6 +54,19 @@ def select_request_string(request: Optional[str], name: str) -> str:
return request


def level_to_column_name(level_kind: LevelKind):
if level_kind == LevelKind.city:
return DF_COLUMN_CITY
elif level_kind == LevelKind.county:
return DF_COLUMN_COUNTY
elif level_kind == LevelKind.state:
return DF_COLUMN_STATE
elif level_kind == LevelKind.country:
return DF_COLUMN_COUNTRY
else:
raise ValueError('Unknown level kind: {}'.format(level_kind))


def zip_answers(queries: List, answers: List):
if len(queries) > 0:
return zip(queries, answers)
Expand All @@ -63,7 +75,8 @@ def zip_answers(queries: List, answers: List):


class PlacesDataFrameBuilder:
def __init__(self):
def __init__(self, level_kind: LevelKind):
self.level_kind: LevelKind = level_kind
self._request: List[str] = []
self._found_name: List[str] = []
self._county: List[Optional[str]] = []
Expand All @@ -88,22 +101,25 @@ def contains_values(column):
return any(v is not None for v in column)

data = {}
data[DF_REQUEST] = self._request
data[DF_FOUND_NAME] = self._found_name

request_column = level_to_column_name(self.level_kind)

data[request_column] = self._request
data[DF_COLUMN_FOUND_NAME] = self._found_name

if contains_values(self._county):
data[DF_PARENT_COUNTY] = self._county
data[DF_COLUMN_COUNTY] = self._county

if contains_values(self._state):
data[DF_PARENT_STATE] = self._state
data[DF_COLUMN_STATE] = self._state

if contains_values(self._country):
data[DF_PARENT_COUNTRY] = self._country
data[DF_COLUMN_COUNTRY] = self._country

return data

@abstractmethod
def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery] = []) -> DataFrame:
def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery], level_kind: LevelKind) -> DataFrame:
raise ValueError('Not implemented')


Expand Down Expand Up @@ -283,10 +299,10 @@ def centroids(self):
)

def to_data_frame(self) -> DataFrame:
places = PlacesDataFrameBuilder()
places = PlacesDataFrameBuilder(self._level_kind)

data = {}
data[DF_ID] = [feature.id for feature in self._geocoded_features]
data[DF_COLUMN_ID] = [feature.id for feature in self._geocoded_features]

# for us-48 queries doesnt' count
for query, answer in zip_answers(self._queries, self._answers):
Expand All @@ -296,7 +312,7 @@ def to_data_frame(self) -> DataFrame:
data = {**data, **places.build_dict()}

if self._highlights:
data[DF_HIGHLIGHTS] = [feature.highlights for feature in self._geocoded_features]
data[DF_COLUMN_HIGHLIGHTS] = [feature.highlights for feature in self._geocoded_features]

return DataFrame(data)

Expand All @@ -313,7 +329,7 @@ def _execute(self, request_builder: RequestBuilder, df_converter):

self._join_payload(features)

return df_converter.to_data_frame(self._answers, self._queries)
return df_converter.to_data_frame(self._answers, self._queries, self._level_kind)

def _request_builder(self, payload_kind: PayloadKind) -> RequestBuilder:
assert_type(payload_kind, PayloadKind)
Expand Down Expand Up @@ -343,6 +359,21 @@ def _get_features(self, feature_id: str) -> List[GeocodedFeature]:
return [feature for feature in self._geocoded_features if feature.id == feature_id]


@classmethod
def find_name_columns(cls, geocodes_df) -> List[str]:
names = []
if DF_COLUMN_CITY in geocodes_df:
names.append(DF_COLUMN_CITY)
if DF_COLUMN_COUNTY in geocodes_df:
names.append(DF_COLUMN_COUNTY)
if DF_COLUMN_STATE in geocodes_df:
names.append(DF_COLUMN_STATE)
if DF_COLUMN_COUNTRY in geocodes_df:
names.append(DF_COLUMN_COUNTRY)

return names


request_types = Optional[Union[str, List[str], Series]]


Expand Down
16 changes: 8 additions & 8 deletions python-package/lets_plot/geo_data/to_geo_data_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from pandas import DataFrame
from shapely.geometry import box

from lets_plot.geo_data import PlacesDataFrameBuilder, zip_answers, select_request_string, abstractmethod
from lets_plot.geo_data.gis.request import RegionQuery
from lets_plot.geo_data import PlacesDataFrameBuilder, zip_answers, abstractmethod
from lets_plot.geo_data.gis.request import RegionQuery, LevelKind
from lets_plot.geo_data.gis.response import Answer, GeocodedFeature, GeoRect, Boundary, Multipolygon, Polygon, GeoPoint

ShapelyPoint = shapely.geometry.Point
Expand Down Expand Up @@ -40,9 +40,9 @@ def __init__(self):
self._lonmax: List[float] = []
self._latmax: List[float] = []

def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery]) -> DataFrame:
def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery], level_kind: LevelKind) -> DataFrame:
assert len(answers) == len(queries)
places = PlacesDataFrameBuilder()
places = PlacesDataFrameBuilder(level_kind)

for query, answer in zip_answers(queries, answers):
for feature in answer.features:
Expand Down Expand Up @@ -80,8 +80,8 @@ def __init__(self):
self._lons: List[float] = []
self._lats: List[float] = []

def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery]) -> DataFrame:
places = PlacesDataFrameBuilder()
def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery], level_kind: LevelKind) -> DataFrame:
places = PlacesDataFrameBuilder(level_kind)

for query, answer in zip_answers(queries, answers):
for feature in answer.features:
Expand All @@ -97,8 +97,8 @@ class BoundariesGeoDataFrame:
def __init__(self):
super().__init__()

def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery]) -> DataFrame:
places = PlacesDataFrameBuilder()
def to_data_frame(self, answers: List[Answer], queries: List[RegionQuery], level_kind: LevelKind) -> DataFrame:
places = PlacesDataFrameBuilder(level_kind)

geometry = []
for query, answer in zip_answers(queries, answers):
Expand Down
Loading