Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #286 from mozilla-services/282-hannosch
Browse files Browse the repository at this point in the history
Further tweaks to point-in-polygon lookups.
  • Loading branch information
jaredlockhart committed May 3, 2016
2 parents 8edc7f9 + fda23bb commit dc12aca
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions leaderboard/contributors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,31 @@ def _compute_ranks(cls, contributions):
(rank.contributor_id, rank.country_id): rank
for rank in ContributorRank.objects.all()
}
countries = Country.objects.all()
countries = (Country.objects.order_by('-area')
.only('id', 'geometry').all())
prepared_countries = [(country.id, country.geometry.prepared)
for country in countries]

for contribution in contributions:
# Each contribution counts towards the rank in the country in which
# it was made, as well as the global rank for that contributor.

contribution_country = None
contribution_country_id = None

# Attempt to find a country which contains the observation point
for country in countries:
if country.geometry.contains(contribution.point):
contribution_country = country
for country_id, prepared in prepared_countries:
if prepared.contains(contribution.point):
contribution_country_id = country_id
break

# If no point was found, find the nearest country
if contribution_country is None:
contribution_country = sorted([
if contribution_country_id is None:
contribution_country_id = sorted([
(country.geometry.distance(contribution.point), country)
for country in countries
])[0][1]
])[0][1].id

for country_id in (contribution_country.id, None):
for country_id in (contribution_country_id, None):
rank_key = (contribution.contributor_id, country_id)
contributor_rank = contributor_ranks.get(rank_key, None)

Expand Down

0 comments on commit dc12aca

Please sign in to comment.