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

Don't create a Location statement if the photo has null coordinates #462

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 24 additions & 3 deletions src/flickypedia/apis/structured_data/create_structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def create_license_statement(license_id: str) -> NewStatement:
}


def create_location_statement(location: LocationInfo) -> NewStatement:
def create_location_statement(location: LocationInfo | None) -> NewStatement | None:
"""
Creates a structured data statement for the "coordinates of
the point of view" statement.
Expand All @@ -253,6 +253,25 @@ def create_location_statement(location: LocationInfo) -> NewStatement:

See https://flickrfoundation.slack.com/archives/C05AVC1JYL9/p1696947242703349
"""
if location is None:
return None

# Some Flickr photos have "null coordinates" -- location data which
# is obviously nonsensical.
#
# e.g. https://www.flickr.com/photos/ed_webster/16125227798/
#
# <location latitude="0.000000" longitude="0.000000" accuracy="16" context="0">
# <neighbourhood woeid="0"/>
# </location>
#
# In this case we should just discard the information as useless, rather
# than write null coordinates into WMC.
#
# See https://github.com/Flickr-Foundation/flickypedia/issues/461
if location == {"accuracy": 16, "latitude": 0.0, "longitude": 0.0}:
return None

# The accuracy parameter in the Flickr API response tells us
# the precision of the location information (15 November 2023):
#
Expand Down Expand Up @@ -504,8 +523,10 @@ def _create_sdc_claims_for_flickr_photo(

statements.extend([license_statement, copyright_statement])

if photo["location"] is not None:
statements.append(create_location_statement(location=photo["location"]))
location_statement = create_location_statement(location=photo["location"])

if location_statement is not None:
statements.append(location_statement)

if photo["date_taken"] is not None:
statements.append(create_date_taken_statement(date_taken=photo["date_taken"]))
Expand Down
13 changes: 13 additions & 0 deletions tests/apis/test_create_structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,19 @@ def test_includes_location_statement_if_photo_has_location_data(self) -> None:

assert len(location_statements) == 1

def test_no_location_statement_if_no_location_data(self) -> None:
assert create_location_statement(location=None) is None

def test_no_location_statement_if_null_location_data(self) -> None:
"""
Regression test for https://github.com/Flickr-Foundation/flickypedia/issues/461
"""
# From https://www.flickr.com/photos/ed_webster/16125227798/
# Retrieved 23 June 2024
location: LocationInfo = {"accuracy": 16, "latitude": 0.0, "longitude": 0.0}

assert create_location_statement(location=location) is None


def test_create_flickr_photo_id_statement() -> None:
statement = create_flickr_photo_id_statement(photo_id="1234567")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ def test_shows_posted_statement(app: Flask, vcr_cassette: str) -> None:
def test_shows_location_statement(
app: Flask, vcr_cassette: str, location: LocationInfo, expected_value: str
) -> None:
actual = get_html(claims=[create_location_statement(location=location)])
location_statement = create_location_statement(location=location)
assert location_statement is not None

actual = get_html(claims=[location_statement])

expected = prettify_html(
f"""
Expand Down