Coverage for geoparser/gazetteer/feature.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 18:08 +0000

1""" 

2Runtime representation of a gazetteer feature. 

3 

4Features are plain Python objects backed by a gazetteer artifact (a 

5self-contained SQLite file). They are not ORM models: attributes are stored as 

6a JSON object and geometry as WKB, both read directly from the artifact. 

7""" 

8 

9from __future__ import annotations 

10 

11import json 

12import typing as t 

13from functools import cached_property 

14 

15from shapely import wkb 

16from shapely.geometry.base import BaseGeometry 

17 

18if t.TYPE_CHECKING: 

19 from geoparser.gazetteer.artifact import GazetteerArtifact 

20 

21 

22class Feature: 

23 """ 

24 A geographic feature from an installed gazetteer. 

25 

26 Each feature has a stable identifier within its gazetteer, a source (the 

27 name of the gazetteer source it was built from), a set of data values 

28 (arbitrary per source), an optional geometry and one or more searchable 

29 names. 

30 """ 

31 

32 def __init__( 

33 self, 

34 artifact: "GazetteerArtifact", 

35 id: int, 

36 identifier: str, 

37 source: str, 

38 data: str, 

39 geometry: t.Optional[bytes], 

40 ): 

41 """ 

42 Initialize a feature from an artifact row. 

43 

44 Args: 

45 artifact: The artifact this feature belongs to 

46 id: Internal feature id within the artifact 

47 identifier: Stable identifier within the gazetteer 

48 source: Name of the gazetteer source this feature was built from 

49 data: JSON-encoded data object 

50 geometry: WKB-encoded geometry, or None 

51 """ 

52 self._artifact = artifact 

53 self.id = id 

54 self.identifier = identifier 

55 self.source = source 

56 self._data_json = data 

57 self._geometry_wkb = geometry 

58 

59 @property 

60 def gazetteer_name(self) -> str: 

61 """Name of the gazetteer this feature belongs to.""" 

62 return self._artifact.name 

63 

64 @cached_property 

65 def data(self) -> t.Dict[str, t.Any]: 

66 """ 

67 The feature's data as a dictionary. 

68 

69 Returns: 

70 Dictionary of data keys to values 

71 """ 

72 return json.loads(self._data_json) 

73 

74 @cached_property 

75 def geometry(self) -> t.Optional[BaseGeometry]: 

76 """ 

77 The feature's geometry as a Shapely object. 

78 

79 The geometry is stored in the gazetteer's coordinate reference system 

80 (available as ``Feature.crs``). 

81 

82 Returns: 

83 Shapely geometry object, or None if the feature has no geometry 

84 """ 

85 if self._geometry_wkb is None: 

86 return None 

87 return wkb.loads(bytes(self._geometry_wkb)) 

88 

89 @property 

90 def crs(self) -> str: 

91 """Coordinate reference system of the feature's geometry.""" 

92 return self._artifact.crs 

93 

94 @cached_property 

95 def names(self) -> t.List[str]: 

96 """ 

97 All searchable names of this feature. 

98 

99 Returns: 

100 List of name strings 

101 """ 

102 return self._artifact.get_feature_names(self.id) 

103 

104 def __eq__(self, other: object) -> bool: 

105 if not isinstance(other, Feature): 

106 return NotImplemented 

107 return ( 

108 self.gazetteer_name == other.gazetteer_name 

109 and self.identifier == other.identifier 

110 ) 

111 

112 def __hash__(self) -> int: 

113 return hash((self.gazetteer_name, self.identifier)) 

114 

115 def __str__(self) -> str: 

116 """ 

117 Return a string representation of the feature. 

118 

119 Returns: 

120 String with feature indicator showing gazetteer and identifier 

121 """ 

122 return f"Feature({self.gazetteer_name}:{self.identifier})" 

123 

124 def __repr__(self) -> str: 

125 """ 

126 Return a developer representation of the feature. 

127 

128 Returns: 

129 Same as __str__ method 

130 """ 

131 return self.__str__()