How to format string?

Post your questions and help other users.

Moderator: Martin

Post Reply
lord_EarlGray
Posts: 19
Joined: 16 Jun 2018 21:17

How to format string?

Post by lord_EarlGray » 18 Jun 2018 14:21

Hi!

I'm currently working on flow, that will show my current location with address on status bar. I have initialized global variable that will stores bu current location in format
lat,long for example: 33.16537,28.73459
Now I want to send http request to nominatim.openstreetmap.org in format: https://nominatim.openstreetmap.org/rev ... sdetails=1
so as you can see I have to paste first part of string assigned to my variable in one place and second part after coma to another place.

My question is: how can I split data from global variable and assign them to two local variables?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: How to format string?

Post by Desmanto » 18 Jun 2018 16:11

the lat long is a location type? Or it is just a string with a separate comma that seems like a lat,long coordinate? Because location is a kind of object, different from string. You have show it using locationformat, so it will become string. But if it is a string already, you don't need location format, simply split it directly.

Code: Select all

loc = newLocation(37.4219998,-122.0840572);
str = split("{loc,locationformat,decimal}", ",");
lat = str[0];
lon = str[1];

url = "https://nominatim.openstreetmap.org/reverse?format=json&lat={lat}&lon={lon}&zoom=18&addressdetails=1"
Replace the loc with the glovar that you use. Use the {url} in the http request action to get the json response. Later use fromJSON() to parse the response and get the data you need. The coordinate you provide is a dummy one that doesn't decoded to some places, so I use googleplex address instead.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

lord_EarlGray
Posts: 19
Joined: 16 Jun 2018 21:17

Re: How to format string?

Post by lord_EarlGray » 18 Jun 2018 19:56

Thanks! That split was exactly what I was asking for. It works and I'm getting json in response. For the sake of clarity, could you please explain me this syntax:

Code: Select all

split("{loc,locationformat,decimal}", ",");
returned JSON looks like this:

Code: Select all

{
  "place_id": "91015268",
  "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
  "osm_type": "way",
  "osm_id": "90394420",
  "lat": "52.54877605",
  "lon": "-1.81627033283164",
  "display_name": "137, Pilkington Avenue, Sutton Coldfield, Birmingham, West Midlands Combined Authority, West Midlands, England, B72 1LH, United Kingdom",
  "address": {
    "house_number": "137",
    "road": "Pilkington Avenue",
    "town": "Sutton Coldfield",
    "city": "Birmingham",
    "county": "West Midlands Combined Authority",
    "state_district": "West Midlands",
    "state": "England",
    "postcode": "B72 1LH",
    "country": "United Kingdom",
    "country_code": "gb"
  },
  "boundingbox": [
    "52.5487321",
    "52.5488299",
    "-1.8163514",
    "-1.8161885"
  ]
}
I would appreciate if you could explain me how to get "city" value.

By the way, where can I find full documentation of this scripting language? There are examples, but they don's cover many things.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: How to format string?

Post by Desmanto » 19 Jun 2018 03:23

Need to start from the inner one first. {loc} is the variable which contain the location coordinate (you use your own global variable in this case). It is an object type, which can't be access like usual string, unless you show in one of the formatting.

"{loc,locationformat,decimal}" is one of the formatting you can use for location. It is almost like excel cell formatting, where you can determine how many decimal point (example 1.23), or how the date are shown in the final form (example 20180619 or 19 June 2018). At the script action, press the icon question mark (?), and search for locationformat, at 2nd occurrence you will find the documentation on how to format the location. "decimal" is one of the format, which will show the location as decimal numbers, in lat,long format, string type.

What you want to do here, actually can be done using "{loc,locationformat}", it will query the geocode using google map api. However it is very limited, as I've tested, it only accept 3 requests everyday (already used up during testing the flow :D). So I don't use it, I also use the openMap API.

We then wrap this string into the split() function, as the first parameter. Since it has one comma, it will be splitted to 2 parts, and the result is a list type variable {str}, which contains 2 elements. We then accessed each part using the list index, 0 is the lat and 1 is the lon.


After you substitute this in the url, send the http request and get the response in JSON; we need to parse this JSON to get the data we need. Since Automagic is very JSON friendly, we can simply convert the JSON to a map/list object by using fromJSON() and store it into a new variable, example {js}.

This will convert the response into usable map/list object. It has nested map/list element, so we need to dig deeper to get the city. If you use a condition debug dialog after this, you can try to change value at {js}, you can dig down each value to the nested map/list. city is located under the address, so we need to access it by

Code: Select all

js = fromJSON(response);
city = js["address"]["city"];
You will get city = Birmingham.

==================================
The full documentation is already in each action help page. But the complete documentation of all elements can be found in the main website (not forum). You can find the link to each of the documentation, examples and quick tutorial in my index, at the General section. Or you can find more example usages (including this JSON format) at the HTTP request, JSON, .... section and another one at Scripting section. There is also a step by step using Debug Dialog to help and troubleshooting scripting problem at the troubleshooting section. By using the debug diaog, you can dig down into js and access the other value you might need.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

dragon
Posts: 29
Joined: 10 Sep 2015 08:13

Re: How to format string?

Post by dragon » 12 Jul 2018 19:27

Hello, I am a user that doesn't understand code or computer programming. I am always willing to research and learn. Some of these instructions are beyond my understanding. I understand the part that requires me to change "glovar" to my liking.

Would it be possible to post a flow out of this where the glovar is 'city' and the flow just populates the current City I am in into the city glovar? Then I could complete my automatic VPN connection flow that I want to build.

I'm not trying to be lazy but I just don't know what to do with these scripts.

Would be very much appreciated!

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: How to format string?

Post by Desmanto » 14 Jul 2018 18:51

dragon wrote:Hello, I am a user that doesn't understand code or computer programming. I am always willing to research and learn. Some of these instructions are beyond my understanding. I understand the part that requires me to change "glovar" to my liking.

Would it be possible to post a flow out of this where the glovar is 'city' and the flow just populates the current City I am in into the city glovar? Then I could complete my automatic VPN connection flow that I want to build.

I'm not trying to be lazy but I just don't know what to do with these scripts.

Would be very much appreciated!
I have created the flow that use the concept. It simply init your current location, use that coordinate to query the geocode to the osm. Then parse the city, save it to global_city. Modify the flow to your need
http://automagic4android.com/flow.php?i ... 64fbfa7805
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

dragon
Posts: 29
Joined: 10 Sep 2015 08:13

Re: How to format string?

Post by dragon » 25 Jul 2018 13:44

Thank you so much! Now I can study the flow and learn how it's done!

jmckeejr
Posts: 34
Joined: 03 May 2015 12:29

Re: How to format string?

Post by jmckeejr » 23 Feb 2019 20:07

Thank you. I'm just replying to save my place for future use.

Post Reply