Skip to content

Commit

Permalink
feat : 322 - list of locations (api) (#348)
Browse files Browse the repository at this point in the history
* feat (partial): checks spreadsheet cities against placenames api. currently not properly set up to use list of cities

* feat: prints out list of communities that we are looking for and a list of communities found with the api

* chore: cleanup

* removes extra line

* chore: removes uneccessary line

* chore: refines function , adds logic for missing names, moves feature types to constant

* chore: fixes how communities are returned
  • Loading branch information
emi-hi authored Jul 4, 2024
1 parent 309a851 commit 83352e3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
22 changes: 22 additions & 0 deletions django/api/bcngws_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class FEATURES():
features_list = [
"Canadian Forces Base",
"Canadian Forces Station",
"City",
"Community",
"District Municipality (1)",
"First Nation Village",
"Former Locality",
"Indian Government District",
"Indian Government District : Land Unit",
"Indian Reserve-Réserve indienne",
"Locality",
"Recreation Facility",
"Recreational Community",
"Region",
"Regional District",
"Resort Municipality",
"Urban Community",
"Village (1)",
"Town"
]
34 changes: 34 additions & 0 deletions django/api/services/bcngws.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import requests
from django.conf import settings
from api.bcngws_constants import FEATURES


def get_placename_matches(names_list, page_size, start_index, result):
names_string = ", ".join(map(str, names_list))

query = {
'outputFormat': 'json',
'name': names_string,
'itemsPerPage': 200,
'startIndex': start_index,
'exactSpelling': 0
}

try:
response = requests.get(settings.PLACENAMES_ENDPOINT, params=query)
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
response = response.json()

filtered_names = [
feature['properties']['name']
for feature in response['features']
if feature['properties']['featureType'] in FEATURES.features_list
]

result.extend(filtered_names)

if response['properties']['totalResults'] >= start_index + page_size:
get_placename_matches(names_list, page_size, start_index + page_size, result)

except requests.RequestException as e:
print(f"Error fetching data: {e}")
5 changes: 4 additions & 1 deletion django/api/services/spreadsheet_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd
import traceback
from django.db import transaction
from api.services.spreadsheet_uploader_prep import typo_checker
from api.services.spreadsheet_uploader_prep import typo_checker, location_checker

def get_field_default(model, field):
field = model._meta.get_field(field)
Expand Down Expand Up @@ -160,6 +160,9 @@ def import_from_xls(
if check_for_warnings:
## do the error checking
typo_warnings = typo_checker(df, df['applicant_name'].dropna(), .8)
locations, names_from_spreadsheet = location_checker(df)
locations_set = set(locations)
names_without_match = [item for item in names_from_spreadsheet if item not in locations_set]
if typo_warnings:
return {
"success": True,
Expand Down
9 changes: 9 additions & 0 deletions django/api/services/spreadsheet_uploader_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import pandas as pd
import difflib as dl
from api.services.bcngws import get_placename_matches

def prepare_arc_project_tracking(df):
df["Publicly Announced"] = df["Publicly Announced"].replace(
Expand Down Expand Up @@ -238,3 +239,11 @@ def typo_checker(df, s, c=0.7):
return match_dict
else:
print('No issues')

def location_checker(df):
# get list of unique locations from df
names =df['city'].unique()
communities = []
# send request to api with list of names, returns all the communities that somewhat matched
get_placename_matches(names, 200, 1, communities)
return communities, names
2 changes: 2 additions & 0 deletions django/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,5 @@
},
},
}

PLACENAMES_ENDPOINT = PLACENAMES_ENDPOINT = os.getenv("PLACENAMES_ENDPOINT", "https://apps.gov.bc.ca/pub/bcgnws/names/search")

0 comments on commit 83352e3

Please sign in to comment.