Coverage for geoparser/gazetteer/gazetteer.py: 100%
25 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
1"""
2Public gazetteer query interface.
4A Gazetteer wraps an installed artifact (a self-contained, read-only SQLite
5file) and exposes name search and identifier lookup over its features.
6"""
8from __future__ import annotations
10import re
11from typing import List, Optional
13from geoparser.gazetteer.artifact import GazetteerArtifact, artifact_path
14from geoparser.gazetteer.feature import Feature
17class Gazetteer:
18 """
19 A gazetteer interface for querying geographic features.
21 This class provides access to an installed gazetteer artifact, allowing
22 retrieval of candidate features for name matching using different search
23 strategies: exact, phrase, partial, and fuzzy matching.
24 """
26 def __init__(self, gazetteer_name: str):
27 """
28 Initialize the gazetteer interface.
30 Args:
31 gazetteer_name: Name of the gazetteer to query for candidates
33 Raises:
34 ValueError: If the gazetteer is not installed. Querying an
35 uninstalled gazetteer would silently return no results, so we
36 fail here instead of letting that happen unnoticed.
37 """
38 path = artifact_path(gazetteer_name)
39 if not path.exists():
40 raise ValueError(
41 f"Gazetteer '{gazetteer_name}' is not installed. Install it by running "
42 f"'python -m geoparser install {gazetteer_name}', or run "
43 "'python -m geoparser list' to see which gazetteers are installed."
44 )
45 self.gazetteer_name = gazetteer_name
46 self._artifact = GazetteerArtifact(path)
48 @property
49 def crs(self) -> str:
50 """Coordinate reference system of the gazetteer's geometries."""
51 return self._artifact.crs
53 def search(
54 self, name: str, method: str = "exact", limit: int = 10000, tiers: int = 1
55 ) -> List[Feature]:
56 """
57 Search for features using the specified search method.
59 Args:
60 name: Name string to search for
61 method: Search method to use ("exact", "phrase", "partial", "fuzzy")
62 limit: Maximum number of results to return (default: 10000)
63 tiers: Number of rank tiers to include in results (default: 1,
64 ignored for exact method)
66 Returns:
67 List of Feature objects matching the search criteria
69 Raises:
70 ValueError: If an unknown search method is specified
71 """
72 # Remove quotes and trim whitespace
73 normalized_name = re.sub(r'"', "", name).strip()
74 if not normalized_name:
75 return []
77 method_map = {
78 "exact": lambda: self._artifact.search_exact(normalized_name, limit),
79 "phrase": lambda: self._artifact.search_phrase(
80 normalized_name, limit, tiers
81 ),
82 "partial": lambda: self._artifact.search_partial(
83 normalized_name, limit, tiers
84 ),
85 "fuzzy": lambda: self._artifact.search_fuzzy(normalized_name, limit, tiers),
86 }
88 if method not in method_map:
89 raise ValueError(f"Unknown search method: {method}")
91 return method_map[method]()
93 def find(self, identifier: str) -> Optional[Feature]:
94 """
95 Find a feature by its identifier.
97 Args:
98 identifier: The identifier value of the feature to find
100 Returns:
101 Feature object if found, None otherwise
102 """
103 return self._artifact.find(identifier)