Skip to content

Commit

Permalink
Update HqgisAlgorithm_geocode.py
Browse files Browse the repository at this point in the history
to support new API version 8
  • Loading branch information
riccardoklinger authored Oct 6, 2023
1 parent 7d40d8d commit 8f75df2
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions HqgisAlgorithm_geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,79 +173,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"
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
Expand Down Expand Up @@ -315,24 +314,24 @@ def processAlgorithm(self, parameters, context, feedback):

# get the location from the API:
ApiUrl = (
"https://geocoder.ls.hereapi.com/search/6.2/geocode.json?apiKey="
"https://geocode.search.hereapi.com/v1/geocode?apiKey="
+ creds["id"]
+ "&searchtext="
+ "&q="
+ feature[addressField]
)
r = requests.get(ApiUrl)
print(ApiUrl)
feedback.pushInfo(ApiUrl )
try:
responseAddress = json.loads(r.text)["Response"]["View"][0]["Result"][0]
responseAddress = json.loads(r.text)["items"][0]
except:
feedback.pushWarning(
"unable to geocode feature {}: {}".format(
feature.id(), feature[addressField]
)
)
geocodeResponse = self.convertGeocodeResponse(responseAddress)
lat = responseAddress["Location"]["DisplayPosition"]["Latitude"]
lng = responseAddress["Location"]["DisplayPosition"]["Longitude"]
lat = responseAddress["position"]["lat"]
lng = responseAddress["position"]["lng"]

fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(lng, lat)))
Expand Down

0 comments on commit 8f75df2

Please sign in to comment.