Skip to content

Commit

Permalink
Merge pull request #24 from dpriskorn/support_subclasses_of_hiking_trail
Browse files Browse the repository at this point in the history
Support subclasses and ignore items with osm way property
  • Loading branch information
dpriskorn authored Jul 14, 2023
2 parents 7283c85 + 9afd31b commit 7ab5a55
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class DebugExit(BaseException):

class NoItemError(BaseException):
pass


class WBIError(BaseException):
pass
5 changes: 3 additions & 2 deletions src/models/enrich_hiking_trails.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def __get_hiking_trails_missing_osm_id__(self) -> None:
self.__extract_item_ids__()

def __get_sparql_result__(self):
"""Get all trails in the specified country and
"""Get all hiking trails and subtrails in the specified country and
with labels in the specified language"""
self.setup_wbi()
# Support all subclasses of Q2143825 hiking trail
self.sparql_result = execute_sparql_query(
f"""
SELECT DISTINCT ?item ?itemLabel WHERE {{
?item wdt:P31 wd:Q2143825;
?item wdt:P31/wdt:P279* wd:Q2143825;
wdt:P17 wd:{config.country_qid}.
minus{{?item wdt:P402 []}}
# Fetch labels also
Expand Down
82 changes: 57 additions & 25 deletions src/models/trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import config
from src.console import console
from src.enums import ItemEnum, OsmIdSource, Property, Status
from src.exceptions import NoItemError, SummaryError
from src.exceptions import NoItemError, SummaryError, WBIError
from src.models.osm_wikidata_link_result import OsmWikidataLinkResult
from src.models.osm_wikidata_link_return import OsmWikidataLinkReturn
from src.models.project_base_model import ProjectBaseModel
Expand Down Expand Up @@ -59,6 +59,18 @@ class TrailItem(ProjectBaseModel):
class Config:
arbitrary_types_allowed = True

@property
def has_osm_way_property(self) -> bool:
try:
if not self.item:
raise NoItemError()
if self.item.claims.get(property="P10689"):
return True
else:
raise WBIError("This should never happen")
except KeyError:
return False

@property
def open_in_josm_urls(self):
if self.osm_ids:
Expand Down Expand Up @@ -175,7 +187,14 @@ def __lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__(
self,
) -> None:
if not self.label:
raise ValueError("self.label was empty")
if not self.item:
raise NoItemError()
print(
f"Skipping {self.item.get_entity_url()} because self.label "
f"was empty in the chosen language"
)
console.input("Press enter to continue")
return
if not isinstance(self.label, str):
raise TypeError("self.label was not a str")
logger.info(f"looking up: {self.label}")
Expand Down Expand Up @@ -243,7 +262,13 @@ def fetch_and_lookup_from_waymarked_trails_and_present_choice_to_user(self):
if not self.wbi:
raise ValueError("self.wbi missing")
self.__get_item_details__()
self.__lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__()
if not self.has_osm_way_property:
self.__lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__()
else:
console.print(
f"Skipping item {self.item.get_entity_url()} "
f"which already has a OSM way property"
)

def enrich_wikidata(self):
"""We enrich Wikidata based on the choice of the user"""
Expand All @@ -252,6 +277,7 @@ def enrich_wikidata(self):
else:
self.chosen_osm_id = self.osm_wikidata_link_results[0].id
if self.item:
enrich = False
if self.chosen_osm_id:
self.__add_osm_id_to_item__()
self.__remove_not_found_in_osm_claim__()
Expand All @@ -260,29 +286,35 @@ def enrich_wikidata(self):
"the [[Wikidata:Tools/hiking trail matcher"
"|hiking trail matcher]]"
)
enrich = True
else:
console.print("No match")
self.__add_or_replace_not_found_in_openstreetmap_claim__()
self.__remove_osm_relation_no_value_claim__()
self.summary = (
"Added not found in OpenStreetMap via "
"the [[Wikidata:Tools/hiking trail"
" matcher|hiking trail matcher]]"
)
if config.upload_to_wikidata:
if config.validate_before_upload:
print("Please validate that this json looks okay")
console.print(self.item.get_json())
console.input("Press enter to upload or ctrl+c to quit")
if self.summary:
self.item.write(summary=self.summary)
console.print(f"Upload done, see {self.item.get_entity_url()}")
if self.questionary_return.no_match is True:
console.print("No match")
self.__add_or_replace_not_found_in_openstreetmap_claim__()
self.__remove_osm_relation_no_value_claim__()
self.summary = (
"Added not found in OpenStreetMap via "
"the [[Wikidata:Tools/hiking trail"
" matcher|hiking trail matcher]]"
)
enrich = True
else:
raise SummaryError()
else:
console.print(
"Not uploading because config.upload_to_wikidata is False"
)
logging.info("No enriching to be done")
if enrich is True:
if config.upload_to_wikidata:
if config.validate_before_upload:
print("Please validate that this json looks okay")
console.print(self.item.get_json())
console.input("Press enter to upload or ctrl+c to quit")
if self.summary:
self.item.write(summary=self.summary)
console.print(f"Upload done, see {self.item.get_entity_url()}")
else:
raise SummaryError()
else:
console.print(
"Not uploading because config.upload_to_wikidata is False"
)

@staticmethod
def __last_update_today_statement__():
Expand Down Expand Up @@ -496,7 +528,7 @@ def __remove_osm_relation_no_value_claim__(self):
# Remove no-value claim
try:
if self.item.claims.get(Property.OSM_RELATION_ID.value):
print(
logger.info(
"Removing OSM relation id = no-value claim because "
"it was not a way to model the check that the community liked best"
)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,13 @@ def test_time_to_check_again_lets_check(self):
trail_item = TrailItem(wbi=WikibaseIntegrator())
trail_item.last_update = datetime(day=24, month=9, year=2020, tzinfo=tzutc())
assert trail_item.time_to_check_again(testing=True) is True

def test_has_osm_way_property_false(self):
trail_item = TrailItem(wbi=WikibaseIntegrator(), qid=self.last_update_test_item)
trail_item.__get_item_details__()
assert trail_item.has_osm_way_property is False

def test_has_osm_way_property_true(self):
trail_item = TrailItem(wbi=WikibaseIntegrator(), qid="Q1692894")
trail_item.__get_item_details__()
assert trail_item.has_osm_way_property is True

0 comments on commit 7ab5a55

Please sign in to comment.