|
Post by vovchik on Mar 3, 2021 10:50:10 GMT 1
Dear Joe,
Thanks. Nice and simple, and it works....
With kind regards, vovchik
|
|
|
Post by bigbass on Mar 3, 2021 17:29:34 GMT 1
Hello vovchik thanks for testing and feedback I tried to keep it simple for the demo but we can expand on this easily also we can add an ip to the end like this our proboards forum ip is 104.16.176.19 freegeoip.app/json/104.16.176.19so we can add it to the end of the url or like this to keep it easier freegeoip.app/json/basic-converter.proboards.comI didn't want to post my personal gps data but the server of the website gives you just for "educational use" to test json parsing using geo data and is public data {"ip":"104.16.176.19","country_code":"US","country_name":"United States","region_code":"","region_name":"","city":"","zip_code":"","time_zone":"America/Chicago","latitude":37.751,"longitude":-97.822,"metro_code":0}
to give some terminal output of what you should expect mapping locations can be made easily from here Joe
|
|
|
Post by bigbass on Mar 9, 2021 4:19:05 GMT 1
In another thread Alex and vovchik showed a weather app basic-converter.proboards.com/post/14076basic-converter.proboards.com/post/14080I thought to take that and show how we could easily parse JSON data with it (I know JSON is much hated here but is used as an internet standard for data) and it can be less painful to look at if you see how it works and using bacon to do it this time a very slightly modified version of the parser I posted on page 5 what is happening in the code SYSTEM "curl -s wttr.in/GDL?format=j1 > GDL.json" 1.) /GDL I used the Guadalajara airport code for this weather report change this for where you live 2.) format=j1 outputs to JSON format 3.) redirect as a file > GDL.json 4.) normally you use dimension -1 to get the whole thing in an array but I wanted to show that I was getting just the array 1 though 11 so you could see how to easily modify this to get any values you need 5.) -s just keeps the output silent while downloading Joe
'---convert json to easily readable data '---shorten the code a lot using wget for a json data website SYSTEM "curl -s wttr.in/GDL?format=j1 > GDL.json" DECLARE txt$ TYPE STRING txt$ = LOAD$("GDL.json")
SPLIT txt$ BY "," TO array$ SIZE dimension FOR i = 1 TO 11
array$[i] = REPLACE$(array$[i], "{" , "") array$[i] = REPLACE$(array$[i], "}" , "") array$[i] = REPLACE$(array$[i], "[" , "") array$[i] = REPLACE$(array$[i], "]" , "") '--- work around for http:// and https:// '--- we should not have a <|> in json so it '--- is used as a place holder then is returned again array$[i] = REPLACE$(array$[i], "://" , "<|>") array$[i] = REPLACE$(array$[i], ": " , " = ") array$[i] = REPLACE$(array$[i], "<|>" , "://") '---remove double quotes both sides array$[i] = REPLACE$(array$[i], "\"" , "") PRINT array$[i] NEXT
expected output FeelsLikeF = 79 cloudcover = 0 humidity = 13 localObsDateTime = 2021-03-08 09:37 PM observation_time = 03:37 AM precipInches = 0.0 precipMM = 0.0 pressure = 1022 pressureInches = 31 temp_C = 28 temp_F = 82
|
|
|
Post by bigbass on Mar 11, 2021 17:46:49 GMT 1
How can you parse JSON and use that data with javascript and html and at the same time use bacon to get it done I wanted to get the latitude and longitude automatically to center your location and then have the official live map showing the action Rik in another post used VesselFinder.com and their javascript app which for me seems the easiest to use and modify for this demo basic-converter.proboards.com/post/14084thanks Rik! now how to add JSON parsing and javascript embedded in html easily below just having a little fun with it (and I don't even have a boat) what's the advantage here? you can pass the html to any device such as your phone or tablet after Updated to improve the mobile phone viewercall it geo-vessel.bac OPEN "vessel.html" FOR WRITING AS myfile
DECLARE longitude$ TYPE STRING DECLARE latitude$ TYPE STRING
'--- convert json to easily readable data '--- get longitute and latitude into variables '--- they will be used to generate automatically your location '--- using aismap.js embedded in html for you MY_GEO$ = EXEC$("curl -s https://freegeoip.app/json/")
SPLIT MY_GEO$ BY "," TO array$ SIZE dimension FOR i = 1 TO 9
array$[i] = REPLACE$(array$[i], "{" , "") array$[i] = REPLACE$(array$[i], "}" , "") array$[i] = REPLACE$(array$[i], "[" , "") array$[i] = REPLACE$(array$[i], "]" , "") '---remove double quotes both sides array$[i] = REPLACE$(array$[i], "\"" , "") IF REGEX(array$[i], "latitude:") THEN latitude$ = REPLACE$(array$[i], "latitude:" , "") PRINT latitude$ END IF IF REGEX(array$[i], "longitude:") THEN longitude$ = REPLACE$(array$[i], "longitude:" , "") PRINT longitude$ END IF
NEXT
DECLARE DQ$ TYPE STRING DQ$ = "\""
'---header and code that place latitude and longitude in html with javascript WRITELN "<html> " TO myfile WRITELN " <head></head> " TO myfile WRITELN " <body> " TO myfile WRITELN " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=18.0, user-scalable=yes\" />" TO myfile WRITELN " <script type=\"text/javascript\"> " TO myfile WRITELN " var width=\"100%\"; " TO myfile WRITELN " var height=\"800\";" TO myfile WRITELN " var latitude=",DQ$, latitude$ ,DQ$,";" TO myfile WRITELN " var longitude=",DQ$, longitude$ ,DQ$,";" TO myfile WRITELN " var zoom=\"13\"; " TO myfile WRITELN " </script>" TO myfile WRITELN " <script" TO myfile WRITELN " type=\"text/javascript\" " TO myfile WRITELN " src=\"https://www.vesselfinder.com/aismap.js\"> " TO myfile WRITELN " </script> " TO myfile WRITELN " </body> " TO myfile WRITELN "</html>" TO myfile PRINT " now open vessel.html with your browser" CLOSE FILE myfile
|
|
|
Post by bigbass on Mar 12, 2021 7:46:10 GMT 1
This Time I will use leaflet leafletjs.com/its actually easier than you think to generate a personalized land map with your city and latitude and longitude automatically and of course all light weight fast and full zooming allowed this one is more complex but shows how you can do this when complex data needs to be parsed and changed automatically json-advanced.tar.gz (56.82 KB) Joe
|
|
|
Post by vovchik on Mar 12, 2021 11:22:40 GMT 1
Dear Joe,
Now I have to study the code (which is terse and compact). It works very nicely. Thanks.
With kind regards, vovchik
|
|
|
Post by bigbass on Mar 12, 2021 16:32:40 GMT 1
Hello vovchik thanks for testing
I finished late last night but now in the morning I went back and documented the code it is the same code.bac as in the tar.gz above
but I hope it clears up what is going on so that this same idea could be used to parse any html that has <script> embedded using bacon
html makes all this lite and easy for phones tablets or your linux OS when I want a quick map I passed this in my phone storage and it opens in chrome the added feature is you can click on places and get in the GUI the latitude and longitude values quickly
Joe
'--- (0)pre step '--- this is the in memory temporary area your are replacing variables in '--- it is created when you run the program '--- then gets RENAMED to "yourcity".html at the end of the program '--- this way I can rename it with your real city's name automatically OPEN "city_name2.html" FOR WRITING AS myfile
DECLARE longitude$ TYPE STRING DECLARE latitude$ TYPE STRING '---step 1 '--- convert json to easily readable data '--- get longitute and latitude into variables '--- they will be used to generate automatically your location '--- using leaflet.js embedded in html for you MY_GEO$ = EXEC$("curl -s https://freegeoip.app/json/")
'---step 2 '--- The geo data from freegeoip.app is in JSON format '--- it will all be placed in an array so that we can parse it by index '--- and replace any text or symbol this is how it is done in javascript also '--- but in javascript there are many powerful functions that make it easy for you '--- MY_GEO$ gets cleaned up below and what I need for data '--- gets placed in bacon variables latitude$,longitude$,city_name$ SPLIT MY_GEO$ BY "," TO array$ SIZE dimension FOR i = 1 TO 9
array$[i] = REPLACE$(array$[i], "{" , "") array$[i] = REPLACE$(array$[i], "}" , "") array$[i] = REPLACE$(array$[i], "[" , "") array$[i] = REPLACE$(array$[i], "]" , "") '---remove double quotes both sides array$[i] = REPLACE$(array$[i], "\"" , "") IF REGEX(array$[i], "latitude:") THEN latitude$ = REPLACE$(array$[i], "latitude:" , "") PRINT latitude$ END IF IF REGEX(array$[i], "longitude:") THEN longitude$ = REPLACE$(array$[i], "longitude:" , "") PRINT longitude$ END IF IF REGEX(array$[i], "city:") THEN city_name$ = REPLACE$(array$[i], "city:" , "") PRINT city_name$ END IF
NEXT '--- step 3 '--- READ html and javascript and just change latitude longitude and the city name '--- the template file called city_name.html gets replaced with the bacon variables '--- I pulled out of step 2 from the JSON data at freegeoip.app/json/ '--- I used a custom template to greatly simplify writing all that html and javascipt '--- with the usual WRITELN that would have forced me to escape everything '--- and reformat all the data so as you can see it is much better to create '--- variables LATITUDE ,LONGITUDE and CITY_NAME and only replace those '--- this shows how complex html with javascript can be easily parsed '--- now we just READ in the template data city_name.html and selectively replace the variables
DECLARE txt$ TYPE STRING txt$ = LOAD$("city_name.html")
SPLIT txt$ BY NL$ TO array2$ SIZE dimension2 FOR i = 0 TO dimension2 -1 array2$[i] = REPLACE$(array2$[i], "LATITUDE" , latitude$) array2$[i] = REPLACE$(array2$[i], "LONGITUDE" , longitude$) array2$[i] = REPLACE$(array2$[i], "CITY_NAME" , city_name$) WRITELN array2$[i] TO myfile NEXT
PRINT "Now open ",city_name$,".html with your browser"
CLOSE FILE myfile RENAME "city_name2.html" TO city_name$ & ".html"
|
|
|
Post by alexfish on Mar 12, 2021 18:40:23 GMT 1
Hi Joe After downloading the json-advanced Nice. Picky Attachments:
|
|
|
Post by rikky on Mar 13, 2021 8:03:11 GMT 1
|
|
|
Post by vovchik on Mar 13, 2021 10:56:41 GMT 1
Dear all, I stuck in my own exact co-ordinates into geoip.txt and then modded geoleaflet.bac to use it. I got a nice map of my location, down to a metre or so accuracy. Nice. We could pass location name, latitude and longitude on the command line and get maps of what interests us nearly instantly. With kind regard, vovchik Attachments:
|
|
|
Post by vovchik on Mar 13, 2021 11:43:33 GMT 1
Dear all,
Now that Joe has done all the hard work, we can make a simple command-line version:
DECLARE html$, my_file$ TYPE STRING html$ = LOAD$("city_name.html")
' ------------------ SUB CHK_ARGS$ ' ------------------ LOCAL args$ TYPE STRING args$ = ARGUMENT$ IF AMOUNT(args$) <> 4 THEN PRINT "Bad arguments. Supply location_name, latitude and longitude." PRINT "Example: ./geo-leaflet-vov London 51.509865 -0.118092" END ELSE html$ = REPLACE$(html$, "CITY_NAME", TOKEN$(args$, 2)) html$ = REPLACE$(html$, "LATITUDE", TOKEN$(args$, 3)) html$ = REPLACE$(html$, "LONGITUDE", TOKEN$(args$, 4)) my_file$ = LCASE$(TOKEN$(args$, 2)) & ".html" SAVE html$ TO my_file$ END IF END SUB CHK_ARGS$ PRINT "Created file ", my_file$, ". Load in browser to view map."
With kind regards, vovchik
|
|
|
Post by bigbass on Mar 13, 2021 16:24:43 GMT 1
Hello Guys Thanks Rik and Alex for giving it a try Thanks vovchik for your CLI version which is very nice and shows that bacon can be used to get the job done keeping things compact and easy to use I first had to figure out what was going on in javascript before I could replace node and javascript with all the parsing being done with baconthese are complex maps with many possible overlays we could use I added OpenSeaMap to the city_name.html (without breaking your cli) // manually added OpenStreetMap_Mapnik and OpenSeaMap as an overlay leaflet-extras.github.io/leaflet-providers/preview/so any one of those maps or overlays could be added now and still keep it somewhat easy to have it play nicely with bacon Joe json-advanced-2.tar.gz (101.12 KB)
|
|
|
Post by rikky on Mar 13, 2021 17:46:26 GMT 1
|
|
|
Post by bigbass on Mar 14, 2021 0:03:58 GMT 1
gps mobilThis is so you never get lost again because it follows your gps data in real time (a three second delay) this was fun making it a standalone one html file you place in your phones Internal storage you first have to generate the html file using gps-leaflet.bacthe README gives more info (It is possible that it may also work if you are receiving real gps data) with your raspberry pi ) Joe GPS_MOBIL.tar.gz (2.89 KB)
|
|
|
Post by bigbass on Oct 14, 2022 17:36:45 GMT 1
a few more demos and some porting tips to bacon sourceforge.net/p/bacon-qt5/wiki/porting-c%2B%2B/
PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-deprecated PRAGMA INCLUDE <iostream> <bits/stdc++.h> OPTION PARSE FALSE
CLASS Car public: const char* brand; const char* model; int year; void printyear() { std::cout << "Year is: " << year; } END CLASS
'---pointer to an object declaration (we use a class called Car) DECLARE Car TYPE class DECLARE car1 TYPE Car* DECLARE car2 TYPE Car*
'--- the correct syntax for class close with () or {} car1 = new Car() car2 = new Car()
' Constructor definition outside the class converted to a simple function FUNCTION carinfo(const char* x, const char* y, int z) TYPE char* brand = x model = y year = z PRINT x," ", y," ",z END FUNCTION
'
'---initialize the strings const char* in c++ '---brand model and year are declared in the class '---car1 is a copy of the class or a new object '---since we used the "new" commamnd it is now a pointer car1->brand = "BMW" car1->model = "X5" car1->year = 1999
' carinfo("Honda", "civic" , 2023)
PRINT "car1 brand : " ,car1->brand FORMAT "%s%s\n" PRINT "car1 model : ", car1->model FORMAT "%s%s\n" PRINT "car1 year : ", car1->year FORMAT "%s%d\n"
'call a function inside the class from outside of the class car1->printyear()
OUTPUT Honda civic 2023 car1 brand : BMW car1 model : X5 car1 year : 1999 Year is: 1999
|
|