Geocoding node-red-node-google. Error "Invalid request. Invalid 'latlng' parameter" - google-maps-api-3

I´m not an expert in node-red and truly appreciate some help on using the node-red-node-google geocoding node.
link to Google Geolocation node-red node
I'm trying to obtain the location address via google Geolocation API from previously obtained coordinates using the node mentioned above (geocode by coordinates option). I take latitude and length via POST from an external application.
How should the latitude and longitude data be fed into the geocode node to get the correct address data? After tens of tests, I always obtain the same error: "Invalid request. Invalid 'latlng' parameter".
What I'm doing is:
Take data generated via POST http://my_public_ip:1880/geo?id={device}&time={time}&lat={lat}&lng={lng}&radius={radius}
The first node of the flow is a node-red Http In node, that implements the htp end-point, gets the info from that callback. Te info is retrieved correctly (see debug output) :
{"id":"18A834","time":"1503231980","lat":"40.xxxxxxxxxxxxxx","lng":"-3.xxxxxxxxxxxxxx","radius":"1651"}
Note: coordinates shadowed by privacy reasons.
If lng and lat values from the HTTP In node are fed into the Google Geocoding node, the message error "Invalid request. Invalid 'latlng' parameter" is received.
I fill the dialog box this way:
Latitude text box: msg.payload.lat
Longitude Text box:msg.payload.lng
I have tried several options inserting the coords function node:
convert the latitude and longitude values to float, keep only the first 7 decimals, convert again to strings ..., and always the same error is received: "Invalid request. Invalid 'latlng' parameter"
The coords function node configuration:
var coords ={};
latitude= parseFloat(msg.payload.lat).toFixed(7);
latitude1 = parseFloat(latitude);
var length = parseFloat(msg.payload.lng).toFixed(7);
var length1= parseFloat(length);
coords.payload = {lat:latitude1, lng:length1};
return coords;
The function output seems to be correct:
{"lat":40.xxxxxxx,"lng":-3.xxxxxxx}
So I expected to fill the text boxes with msg.payload.lat and msg.payload.lng should be ok. Clearly something is wrong ...
Does anybody know which is the correct way to feed longitude and latitude values to the node-red-node-google google-geocoding to get no errors?
Find below node-red used code (remember, also tested different types of variable and also tested with no function at all):
[{"id":"4dadb4d9.246a1c","type":"http in","z":"76138ea1.88752","name":"get_coords_from_post","url":"/geo","method":"get","upload":false,"swaggerDoc":"","x":300,"y":200,"wires":[["4a0b74fb.d10fcc","fb89b080.ef1cf","dac1b7ce.4cc2c8"]]},{"id":"fb89b080.ef1cf","type":"function","z":"76138ea1.88752","name":"coords","func":"var coords ={};\nlatitude= parseFloat(msg.payload.lat).toFixed(7);\nlatitude1 = parseFloat(latitude);\nvar length = parseFloat(msg.payload.lng).toFixed(7);\nvar length1= parseFloat(length);\ncoords.payload = {lat:latitude1, lng:length1};\nreturn coords; \n","outputs":1,"noerr":0,"x":510,"y":200,"wires":[["a24b08cf.22ebf8","dac1b7ce.4cc2c8"]]},{"id":"4a0b74fb.d10fcc","type":"http response","z":"76138ea1.88752","name":"","statusCode":"","headers":{},"x":484,"y":131,"wires":[]},{"id":"dac1b7ce.4cc2c8","type":"debug","z":"76138ea1.88752","name":"","active":true,"console":"false","complete":"false","x":990,"y":480,"wires":[]},{"id":"a24b08cf.22ebf8","type":"google geocoding","z":"76138ea1.88752","name":"find_address_from_coords","geocodeBy":"coordinates","address":"","lat":"msg.payload.lat","lon":"msg.payload.lng","googleAPI":"","bounds":"","language":"es","region":"","components":"","x":740,"y":200,"wires":[["dac1b7ce.4cc2c8"]]}]

You shouldn't be putting msg.payload.lat or msg.payload.lon in the config dialog. If you are passing those values in via a message object you need to leave them blank in the config dialog.
Next, as mentioned in the README.md and the info bar in Node-RED, you need to set the msg.location.lat and msg.location.lon on the incoming message to pass those value to the node.
So basically your function node should look a little bit more like this:
var coords ={};
latitude= parseFloat(msg.payload.lat).toFixed(7);
latitude1 = parseFloat(latitude);
var length = parseFloat(msg.payload.lng).toFixed(7);
var length1= parseFloat(length);
coords.location = {lat:latitude1, lon:length1};
return coords;

Related

Authorization in R API, specifically for League of Legends

I am learning how to use API in R and it is going well for the most part, but I am having trouble getting any data from the league of legends API.
For reference, I used this article as a start (https://www.dataquest.io/blog/r-api-tutorial/) and cop
res <- GET("http://api.open-notify.org/astros.json")
res
This worked just fine and has a 200 status, but I am not interested in that data.
What I want is data about league of legends, so I am trying to use:
base.url <- "https://na1.api.riotgames.com"
path <- "/lol/champion-mastery/v4/champion-masteries/by-summoner/"
API_Key <- read.table("riotkey.txt")
API_KEY <- API_Key$V1
Summoner_ID <- read.table("summonerID.txt")
SUMMONER_ID <- Summoner_ID$V1
path <- paste0(path,SUMMONER_ID)
LoL_API_Test <- GET(base.url, path = path,
add_headers(Authorization = API_KEY))
LoL_API_Test
This is Riot's explanation for the 403 error - Forbidden. "This error indicates that the server understood the request but refuses to authorize it. There is no distinction made between an invalid path or invalid authorization credentials (e.g., an API key)"
I am certain that my API key and summoner ID are correct.
So I assume the issue has to be with how I am requesting the data.
What am I doing wrong?
This particular API expects the API key to be passed in a header called "X-Riot-Token", not "Authorization". Change your call to
LoL_API_Test <- GET(base.url, path = path,
add_headers("X-Riot-Token" = API_KEY))

Spotify API - "raw" data class arbitrarily returned for some requests

I am compiling data about a set of artists on Spotify - data for each song on each album of each artist. I use a for loop to automate this API request on about 80 different artists in the data frame albums, then assign a bit of info on each album in albums to its list object from the API.
The problem: my API call doesn't always return a list object. Sometimes it returns an object where class() = raw.
#REQUEST DATA
#------------
library(plyr)
library(httr)
library(lubridate)
collablist <- as.list(NULL)
for(i in 1:nrow(albums)){
tracks_in_one_album <- as.list(NULL)
URI = paste0('https://api.spotify.com/v1/albums/', albums$album_uri[i], '/tracks')
response = GET(url = URI, add_headers(Authorization = HeaderValue))
tracks_in_one_album = content(response)
tracks_in_one_album[["album"]] = albums$album_name[i]
tracks_in_one_album[["album_artist"]] = albums$artists[i]
collablist[[i]] <- tracks_in_one_album
print(albums$artist_name[i])
}
The loop runs for somewhere between 50 and 300 albums before I inevitably get the following message:
Error in tracks_in_one_album[["album"]] <- albums$album_name[i] :
incompatible types (from character to raw) in subassignment type fix
When I assign attempting to assign the character object albums$album_name[i] to the API requested object tracks_in_one_album when it's a list, I have no issue. But occasionally the object is of a raw class. Changing it to a list by encapsulating the content() call with as.list prevents the error from occurring, but it doesn't really fix the issue because for the requests where the data come in as raw instead of as a list by default, they're sort of mangled (just a vector with .
The craziest part? This doesn't happen consistently. It could happen for the 4th album of Cat Stevens one time; if I rerun, that Cat Stevens album will be fine and get pulled into R as a list but perhaps the second album for Migos will come in raw instead.
My Question - why are the data not always coming in as a list when I make a request? How is it possible that this could be happening in such a non-reproducible way?

Geocode Error Zero Result in R

I'm currently using the google maps in R (I suppose it would be correct if I said I'm calling the google map API in R, is that right?).
Some of my address entries are not complete so the API returns an error. This is annoying because any valid addresses are not decoded after the error. Is there a way to skip the invalid addresses? Something like this:
Error in if (gc$status != "OK") { : argument is of length zero
In addition, it would be nice to have a message about the failing addresses. Something like:
Warning message: geocode failed with status ZERO_RESULTS, location = "XXX ST , SAN FRANCISC"
I'm using: loc1 <- geocode(as.character(TestAddress2$Address))
I discovered that the geocode call only returns warning for addresses it can't find. Thus the real problem was the fact that when there was no value for address it gave an error.
Thus I filtered out observations that did not have a value:
TestAddress2<- TestAddress2[!(TestAddress2$Address== " "),]

ReferenceError: QUERY_LIMIT is not defined in Google Map Using Own Lats / Longs

Thanks for any help you can provide! I've got a directions tool that lets users map a route to a start, finish, and 8 waypoints. I've also got a separate map webpage that loads about 2000 markers from a JSON object that uses latitudes and longitudes (no geocoding). I'm trying to combine these two into one mapping tool.
The problem is that when I run the combined map, I get an error in my console:
ReferenceError: QUERY_LIMIT is not defined
if(status == google.maps.DirectionsStatus.OVER-QUERY_LIMIT){
I checked out OVER_QUERY_LIMIT error with latitude-longitude addresses but it didn't seem to apply to my problem. Any ideas about what might be causing this error? Again, I appreciate your help!
Here's the line of code it's complaining about, with whitespace added for clarity:
if( status == google.maps.DirectionsStatus.OVER - QUERY_LIMIT ) {
Now you can probably see why that gets a QUERY_LIMIT is not defined message.
Can you find that code in your page? Should it have _ where it has -, so it would read OVER_QUERY_LIMIT?

Directions API overview_polyline doesn't have correct points

Has anyone had an issue with the points returned by the Google Directions API? The overview_polyline doesn't seem to match the directions I've asked for.
The Directions API gives the example:
http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false
In the JSON response I see includes:
"overview_polyline" : {
"points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E_oAsvCka#mwBk_CqbGuu#qfB{uAmrDivDw|E{nAqbDmpCyaJgdCejHk~#owAsw#adAm_#abA}Ven#qCou#cKeiBca#_cBmlAyjAsn#_kCk}#smDkVg{Bk}#gcHox#_sEaPwdC~KazF{EcpCrEmeGl]auBeEi~#yiAovCwwAgsC{i#oSsbAu~Ay_AmaBk_#iKak#mh#_BmBk#wHvG#dBvA"
},
Decoding this using the Interactive Polyline Encoder Utility shows a path starting in Toronto as expected but going a different direction with an end point nowhere near Montreal.
Any ideas?
For background, I'm trying to use the polyline to generate a static map. The static map API allows passing encoded polylines.
I have had success using Mark McClure's tools:
http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
on archive.org
Your polyline works for me when I use this tool to decode it:
http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/decode.html
on archive.org
and this to display the results:
http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/encodeForm.html
on archive.org
Encoded polylines often have issues with "\" escaping the next character.
See this example [1] (can't seem to get it to display as a link)
with each '\' changed to a single '\'
From this issue where it does display as a link.
[1] http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3|color:blue|enc:cyrdIzsee#dOiBpG~j#bA%60t#mGxu#y#xl#pDdTtOnrAtRg#~J|AdHcI~Dpn#dIbwAhY%60Yde#rxBld#d%60B~LpkAzXvtB%60]rt#n_#bqAlo#vw#|NxrApYv{#%60R%60o#hHvqA|i#blE|iArhDdaAvwFz[vkCfsAl{Ei#f_#gYb}#aNtq#rAfv#nRx_Ajg#xbBnQtXnh#%60d#jz#xe#p\pt#pVrY|e#pO%60vAjwAxhAj%60Bj_AzcEvG~c#[ht#yMfz#tIhlDnv#nwIvDfmCiPlrCj#%60sCf]%60vEtVnvCfYzcAfYt_#zUlNtbArWbb#nm#fR~pAsBhqClGjlBj]zxCjo#lbD~a#%60gAzc#ft#lf#faBt]jhAluAjfCntAjlBjk#jWbZvEn^nRxTdWldArsAxWjz#zHhoBuEryCkWrbAwIn_A%60SbhBx\tbBbZpz#hy#pdBzNfm#|Cdq#fKtjAbM~Xf_#dXh}#tArv#h[|pAbb#%60_#nKf_#pYxb#bx#bh#hbAlkAr_BrNjJ|d#dE~|#jJ%60iBfWjhBgs#jr#rEbvAbhAdUfQdU%60Ete#iCrk#aWlXiN%60%60#wFl\dIzVdUz\ls#hw#|fAh|#|pAfiAbnAp|#v{A~[rhAzUhQpf#pG~r#lSdo#te#~T|\bv#jwApf#tsAvj#l~Bfh#fyA|v#nhBdmA~dBdy#vdBz%60#hlB~[~oBbo#znAxsCzbC%60%60#bs#pm#zo#nq#ly#dm#peApi#rr#lo#pXzl#jm#vh#~_A|}AboBle#lh#|_#rOzh#yQfb#oR%60k#%60#lf#nV~p#~u#nl#xpAvnAdgBpq#~\~n#bD%60%60A_A||#fKlzCre#x|BcHbo#hPff#ze#hYbsAps#pgA~p#pgAvYlt#b~#|kDzThe#%60b#%60^zeAntBv%60#%60dAxSvrBhm#hyFfTriA~DjkA~\%60fClo#jeDnS~b#zZfcA|k#j_Cv%60AznCrv#fnAjl#vt#fcAl|#pl#%60dApb#~o#~Mb\zUbUxf#zJtXHf_#g[j\sa#hg#cP%60nAsM%60_BmBpk#_Kz_#sOft#{G~a#%60C~f#cGjj#mTlc#sKl^hGdn#bv#na#pRni#vNzf#re#~qAngAr}Al{#l~#hIt_AfLpyBzvArcAvjAdkBndAtt#faA~x#jv#dkAhy#%60h#hr#jZjWpv#t\bd#xIr\rGrZc%60#d[ad#rIa%60#dT{KhQdLnKfTnOvUdUBje#qQdYrPlRnU%60HbANrGaNbu#jR|jDvAzhChEnUjJtbA}Af[l#|\lKvQMtLtFhh#fHlh#_DZ_BmO&sensor=false
Here is your line on a static map (after changing \\ to \):
http://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3|color:blue|enc:e%60miGhmocNaN~DiBiNe#gEkEek#kNez#cJqq#sk#pGos#v]_}#aF_y#qm#qDe~#w]g~#gZ_Jo_#m_#yNsFgUpMov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KeiBca#_cBmlAyjAsn#_kCk}#smDkVg{Bk}#gcHox#_sEaPwdC~KazF{EcpCrEmeGl]auBeEi~#yiAovCwwAgsC{i#oSsbAu~Ay_AmaBk_#iKak#mh#_BmBk#wHvG#dBvA&sensor=false
The Google APi have a parameter now: path=enc:
"https://maps.googleapis.com/maps/api/staticmap?
size=300x300&key=YOUR_KEY&path=enc:" + direction.routes[0].overview_polyline
Focus on offical guides

Resources