Skip to content

Commit

Permalink
fix for geocode processing
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoklinger committed Oct 7, 2023
1 parent 93e1fcd commit d44df50
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions HqgisAlgorithm_geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,66 +177,78 @@ def initAlgorithm(self, config=None):
def convertGeocodeResponse(self, responseAddress):
geocodeResponse = {}
try:
geocodeResponse["Label"] = responseAddress["Location"]["Address"]["Label"]
geocodeResponse["Label"] = responseAddress["address"]["label"]
except BaseException:
geocodeResponse["Label"] = ""
try:
geocodeResponse["Country"] = responseAddress["Location"]["Address"]["Country"]
geocodeResponse["Country"] = responseAddress["address"]["country"]
except BaseException:
geocodeResponse["Country"] = ""
try:
geocodeResponse["State"] = responseAddress["Location"]["Address"]["State"]
geocodeResponse["State"] = responseAddress["address"]["state"]
except BaseException:
geocodeResponse["State"] = ""
try:
geocodeResponse["County"] = responseAddress["Location"]["Address"]["County"]
geocodeResponse["County"] = responseAddress["address"]["county"]
except BaseException:
geocodeResponse["County"] = ""
try:
geocodeResponse["City"] = responseAddress["Location"]["Address"]["City"]
geocodeResponse["City"] = responseAddress["address"]["city"]
except BaseException:
geocodeResponse["City"] = ""
try:
geocodeResponse["District"] = responseAddress["Location"]["Address"]["District"]
geocodeResponse["District"] = responseAddress["address"][
"district"
]
except BaseException:
geocodeResponse["District"] = ""
try:
geocodeResponse["Street"] = responseAddress["Location"]["Address"]["Street"]
geocodeResponse["Street"] = responseAddress["address"]["street"]
except BaseException:
geocodeResponse["Street"] = ""
try:
geocodeResponse["HouseNumber"] = responseAddress["Location"]["Address"]["HouseNumber"]
geocodeResponse["HouseNumber"] = responseAddress["address"][
"houseNumber"
]
except BaseException:
geocodeResponse["HouseNumber"] = ""
try:
geocodeResponse["PostalCode"] = responseAddress["Location"]["Address"]["PostalCode"]
geocodeResponse["PostalCode"] = responseAddress["address"][
"postalCode"
]
except BaseException:
geocodeResponse["PostalCode"] = ""
try:
geocodeResponse["Relevance"] = responseAddress["Relevance"]
geocodeResponse["Relevance"] = responseAddress["scoring"]["queryScore"]
except BaseException:
geocodeResponse["Relevance"] = None
try:
geocodeResponse["CountryQuality"] = responseAddress["MatchQuality"]["Country"]
geocodeResponse["CountryQuality"] = responseAddress["scoring"]["fieldscore"][
"country"
]
except BaseException:
geocodeResponse["CountryQuality"] = None
try:
geocodeResponse["CityQuality"] = responseAddress["MatchQuality"]["City"]
geocodeResponse["CityQuality"] = responseAddress["scoring"]["fieldscore"]["city"]
except BaseException:
geocodeResponse["CityQuality"] = None
try:
geocodeResponse["StreetQuality"] = responseAddress["MatchQuality"]["Street"][0]
geocodeResponse["StreetQuality"] = responseAddress["scoring"]["fieldscore"][
"street"
][0]
except BaseException:
geocodeResponse["StreetQuality"] = None
try:
geocodeResponse["NumberQuality"] = responseAddress["MatchQuality"]["HouseNumber"]
geocodeResponse["NumberQuality"] = responseAddress["scoring"]["fieldscore"][
"houseNumber"
]
except BaseException:
geocodeResponse["NumberQuality"] = None
try:
geocodeResponse["MatchType"] = responseAddress["MatchType"]
geocodeResponse["MatchType"] = responseAddress["resultType"]
except BaseException:
geocodeResponse["MatchType"] = ""
return(geocodeResponse)
return geocodeResponse

def processAlgorithm(self, parameters, context, feedback):
"""
Expand Down Expand Up @@ -322,14 +334,13 @@ def processAlgorithm(self, parameters, context, feedback):
break

# get the location from the API:
ApiUrl = "https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey=" + \
creds["id"] + "&searchtext=" + feature[addressField]
ApiUrl = "https://geocode.search.hereapi.com/v1/geocode?apiKey=" + \
creds["id"] + "&q=" + feature[addressField]
r = requests.get(ApiUrl)
responseAddress = json.loads(
r.text)["Response"]["View"][0]["Result"][0]
responseAddress = json.loads(r.text)["items"][0]
geocodeResponse = self.convertGeocodeResponse(responseAddress)
lat = responseAddress["Location"]["DisplayPosition"]["Latitude"]
lng = responseAddress["Location"]["DisplayPosition"]["Longitude"]
lat = responseAddress["position"]["lat"]
lng = responseAddress["position"]["lng"]
# Add a feature in the sink
# feedback.pushInfo(str(lat))
fet = QgsFeature()
Expand Down

0 comments on commit d44df50

Please sign in to comment.