Skip to content

Commit

Permalink
Merge pull request #25 from t-sagara/v2_dev
Browse files Browse the repository at this point in the history
Supporet Python 3.12.
  • Loading branch information
t-sagara authored Apr 17, 2024
2 parents 8fc4793 + 0a1ce20 commit 56e1558
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 26 deletions.
14 changes: 7 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"configurations": [
{
"name": "Search noname oaza",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "jageocoder",
"args": [
Expand All @@ -17,7 +17,7 @@
},
{
"name": "Install dictionary",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "jageocoder",
"args": [
Expand All @@ -28,7 +28,7 @@
},
{
"name": "Search all results",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "jageocoder",
"args": [
Expand All @@ -43,7 +43,7 @@
},
{
"name": "Python: Reverse test",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "jageocoder",
"args": [
Expand All @@ -57,7 +57,7 @@
},
{
"name": "Python: Flask",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "flask",
"env": {
Expand All @@ -74,7 +74,7 @@
},
{
"name": "Search moved address",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "jageocoder",
"args": [
Expand All @@ -91,7 +91,7 @@
},
{
"name": "Python: Current File",
"type": "python",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
Expand Down
4 changes: 2 additions & 2 deletions jageocoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
>>> jageocoder.searchNode('<Japanese-address>')
"""

__version__ = '2.1.4' # The package version
__version__ = '2.1.5.post1' # The package version
__dictionary_version__ = '20230927' # Compatible dictionary version
__author__ = 'Takeshi Sagara <[email protected]>'

Expand All @@ -41,7 +41,7 @@
'version',
'dictionary_version',
'installed_dictionary_version',
'instaleld_dictionary_readme',
'installed_dictionary_readme',
]

from jageocoder.module import init, free, is_initialized, \
Expand Down
30 changes: 28 additions & 2 deletions jageocoder/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,43 @@ def searchNode(query: str) -> List[Result]:
return _tree.searchNode(query)


def reverse(x: float, y: float, level: Optional[int] = None) -> dict:
def reverse(
x: float,
y: float,
level: Optional[int] = None,
as_dict: Optional[bool] = True
) -> list:
"""
Reverse geocoding.
Parameters
----------
x: float
Longitude of the point.
y: float
Latitude of the point.
level: int, optional
Target node level.
as_dict: bool, default=True
If True, returns candidates as dict objects.
Returns
-------
list
Notes
-----
- The result list contains up to 3 nodes.
- Each element is a dict type with the following structure:
{"candidate":AddressNode, "dist":float}
"""
if not is_initialized():
raise JageocoderError("Not initialized. Call 'init()' first.")
from jageocoder.rtree import Index

global _tree
idx = Index(tree=_tree)
return idx.nearest(x=x, y=y, level=level)
return idx.nearest(x=x, y=y, level=level, as_dict=as_dict)


def create_trie_index() -> None:
Expand Down
2 changes: 2 additions & 0 deletions jageocoder/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def get_name(self, alt: Optional[str] = ''):
def dataset(self):
"""
Get dataset record.
"""
return self.table.datasets.get(id=self.priority)

Expand Down Expand Up @@ -395,6 +396,7 @@ def children(self) -> List[AddressNode]:
-------
List[AddressNode]
The list of the child nodes.
"""
return self.get_children()

Expand Down
67 changes: 58 additions & 9 deletions jageocoder/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ class Result(object):
It is used only for recursive search.
"""

def __init__(self,
node: Optional[AddressNode] = None,
matched: str = '',
nchars: int = 0):
def __init__(
self,
node: Optional[AddressNode] = None,
matched: str = '',
nchars: int = 0
):
self.node = node
self.matched = matched
self.nchars = nchars

def set(self, node: AddressNode,
matched: str, nchars: int = 0) -> 'Result':
def set(
self,
node: AddressNode,
matched: str,
nchars: int = 0
) -> 'Result':
"""
Set node and matched string.
"""
Expand Down Expand Up @@ -65,9 +71,20 @@ def get_matched_string(self) -> str:
return self.matched

def get_matched_nchars(self) -> int:
"""
Get the the number of matched characters.
Return
------
int
Number of characters in the matched substring.
"""
return self.nchars

def __getitem__(self, pos) -> Union[AddressNode, str]:
def __getitem__(
self,
pos
) -> Union[AddressNode, str]:
if pos == 0:
return self.node
elif pos == 1:
Expand All @@ -77,7 +94,11 @@ def __getitem__(self, pos) -> Union[AddressNode, str]:

raise IndexError()

def __setitem__(self, pos, val: Union[AddressNode, str]) -> None:
def __setitem__(
self,
pos,
val: Union[AddressNode, str]
) -> None:
from jageocoder.node import AddressNode
if pos == 0 and isinstance(val, AddressNode):
self.node = val
Expand All @@ -89,13 +110,41 @@ def __setitem__(self, pos, val: Union[AddressNode, str]) -> None:
raise RuntimeError()

def as_dict(self) -> dict:
"""
Convert Result object to dict type for display.
Return
------
dict
A dict object containing the following elements;
"node"
AddressNode object converted to dict type.
"matched"
The substring matching the query.
"""
return {
"node": self.node.as_dict(),
"matched": self.matched,
# "nchars": self.nchars
}

def as_geojson(self) -> dict:
"""
Convert Result to GeoJSON dict type for display.
Return
------
dict
A GeoJSON dict object containing the following elements;
"type"
Always "Feature".
"geometry"
A point type geometry containing latitude and longitude.
"properties"
Include in "matched" the substring that matched the query, in addition to the attributes of the node.
"""
geojson = self.node.as_geojson()
geojson['properties']['matched'] = self.matched
return geojson
Expand Down
10 changes: 7 additions & 3 deletions jageocoder/rtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ def nearest(
self,
x: float,
y: float,
level: Optional[int] = None
level: Optional[int] = None,
as_dict: Optional[bool] = True,
):
"""
Search nearest nodes of the target point.
Expand All @@ -422,10 +423,13 @@ def nearest(
level: int, optional
The level of the address ndoes to be retrieved as a result.
If omitted, search down to the AZA level.
as_dict: bool, default=True
If False is specified, the addressNode object is stored
in the "candidate" field.
Returns
-------
[{"candidate":AddressNode, "dist":float}]
[{"candidate":AddressNode or dict, "dist":float}]
Returns the results of retrieval up to 3 nodes.
"""
level = level or AddressLevel.AZA
Expand Down Expand Up @@ -497,7 +501,7 @@ def nearest(
continue

results.append({
"candidate": node.as_dict(),
"candidate": node.as_dict() if as_dict else node,
"dist": self.distance(x, y, node.x, node.y)
})
registered.add(node.id)
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jageocoder"
version = "2.1.4"
version = "2.1.5.post1"
description = "A Japanese-address geocoder for Python."
authors = ["Takeshi Sagara <[email protected]>"]
repository = "https://github.com/t-sagara/jageocoder/"
Expand Down Expand Up @@ -28,7 +28,7 @@ docopt = "^0.6.2"
geographiclib = "^2.0"
idna = ">=3.7"
jaconv = "^0.3.4"
marisa-trie = "^0.7.8"
marisa-trie = ">=0.7.8"
pycapnp = "*"
portabletab = ">=0.3.3"
rtree = "^1.0.0"
Expand Down

0 comments on commit 56e1558

Please sign in to comment.