From dcab9228de307e1a361716d476f6d24c535dc29e Mon Sep 17 00:00:00 2001 From: Mysterious-Owl <66870959+Mysterious-Owl@users.noreply.github.com> Date: Sun, 9 Oct 2022 23:58:15 +0530 Subject: [PATCH] Added Geocoode API program --- scripts/GeoCode API/README.md | 16 ++++++++ scripts/GeoCode API/code.py | 61 ++++++++++++++++++++++++++++ scripts/GeoCode API/config.ini | 3 ++ scripts/GeoCode API/index.html | 48 ++++++++++++++++++++++ scripts/GeoCode API/requirements.txt | 2 + scripts/GeoCode API/where.js | 24 +++++++++++ scripts/GeoCode API/where.txt | 22 ++++++++++ 7 files changed, 176 insertions(+) create mode 100644 scripts/GeoCode API/README.md create mode 100644 scripts/GeoCode API/code.py create mode 100644 scripts/GeoCode API/config.ini create mode 100644 scripts/GeoCode API/index.html create mode 100644 scripts/GeoCode API/requirements.txt create mode 100644 scripts/GeoCode API/where.js create mode 100644 scripts/GeoCode API/where.txt diff --git a/scripts/GeoCode API/README.md b/scripts/GeoCode API/README.md new file mode 100644 index 0000000..33435d7 --- /dev/null +++ b/scripts/GeoCode API/README.md @@ -0,0 +1,16 @@ +# Google Maps API + +This code will take the name of places from where.txt and then use *forward Geocode API* to get the coordinates of that location, then the coordinates are stored in javascript file and the webpage is opened automatically, to show the pin locations on map.
+ +### Prerequisites + +To install configparser ```pip install configparser``` or check [here](https://pypi.org/project/configparser/) +If you have [Google Geocode API](https://developers.google.com/maps/documentation/geocoding/overview), so use that, otherwise you can use +[Open Cage API](https://opencagedata.com/api) (its free, 2500 requests/day). + +### How to run the script + +Enter the API key and service URL in config file (without qoutes) and the places to be marked in where.txt file, then run the script. + + + diff --git a/scripts/GeoCode API/code.py b/scripts/GeoCode API/code.py new file mode 100644 index 0000000..5ebd635 --- /dev/null +++ b/scripts/GeoCode API/code.py @@ -0,0 +1,61 @@ +import urllib.request +import urllib.parse +import urllib.error +import json +import os +import webbrowser +import ssl +import configparser + +config = configparser.ConfigParser() +config.read('config.ini') + +api_key = config['keys']['api_key'] +service_url = config['keys']['service_url'] + +# Ignore SSL certificate errors +ctx = ssl.create_default_context() +ctx.check_hostname = False +ctx.verify_mode = ssl.CERT_NONE + +with open("where.txt") as fh, open("where.js", "w", encoding="utf-8") as where: + adrs = [] + parms = {} + for line in fh: + + address = line.strip() + parms["address"] = address + parms['key'] = api_key + url = service_url + urllib.parse.urlencode(parms) + + if url.lower().startswith('http'): + req = urllib.request.Request(url) + else: + raise ValueError from None + + with urllib.request.urlopen(req, context=ctx) as resp: + + data = resp.read().decode() + + try: + js = json.loads(data) + except Exception as e: + print(f"{e}: {data}") + continue + + try: + adrs.append([js['results'][0]['geometry']['lat'], + js['results'][0]['geometry']['lng'], + js['results'][0]['formatted']]) + print('Retrieved ', url) + except Exception as e: + print(f"Not Found: {e}: {line.strip()}") + + print("\nOpening Webpage") + + where.write("myData = [\n") + for item in adrs: + where.write(f"[{str(item[0])}, {str(item[1])}, '{str(item[2])}' ], \n") + where.write("];\n") + +webbrowser.open('file://' + os.path.realpath("index.html")) diff --git a/scripts/GeoCode API/config.ini b/scripts/GeoCode API/config.ini new file mode 100644 index 0000000..953b7bb --- /dev/null +++ b/scripts/GeoCode API/config.ini @@ -0,0 +1,3 @@ +[keys] +service_url=https://api.opencagedata.com/geocode/v1/json?q= +api_key= \ No newline at end of file diff --git a/scripts/GeoCode API/index.html b/scripts/GeoCode API/index.html new file mode 100644 index 0000000..f4970b9 --- /dev/null +++ b/scripts/GeoCode API/index.html @@ -0,0 +1,48 @@ + + + + + GOOGLE MAPS API + + + + + + +
+

About this Map

+

+This is a cool script by +Mysterious-Owl with the use of API.
+To see the details of a marker, hover over the marker. +

+ + diff --git a/scripts/GeoCode API/requirements.txt b/scripts/GeoCode API/requirements.txt new file mode 100644 index 0000000..572e830 --- /dev/null +++ b/scripts/GeoCode API/requirements.txt @@ -0,0 +1,2 @@ +configparser +ssl \ No newline at end of file diff --git a/scripts/GeoCode API/where.js b/scripts/GeoCode API/where.js new file mode 100644 index 0000000..808f0ec --- /dev/null +++ b/scripts/GeoCode API/where.js @@ -0,0 +1,24 @@ +myData = [ +[41.89193, 12.51133, 'Rome, Italy' ], +[12.937243, 77.6925796, 'The Address, Outer Ring Road, Kaadubeesanahalli, Bengaluru - 530103, Karnataka, India' ], +[51.575646, -0.0986474, 'Address, Endymion Road, London, N4 1EQ, United Kingdom' ], +[-33.86785, 151.20732, 'Sydney, Australia' ], +[35.6718484, 139.7419907, 'Address Building, Sotobori-dori, Akasaka 2-chome, Minato, 100-8968, Japan' ], +[55.75222, 37.61556, 'Moscow, Russia' ], +[34.05223, -118.24368, 'Los Angeles, California, United States of America' ], +[-23.582841, -46.6847335, 'Hotel Intercity Adress Faria Lima, Rua Amauri 513, Vila Olímpia, São Paulo - SP, 01453-020, Brazil' ], +[-33.92584, 18.42322, 'Cape Town, City of Cape Town, South Africa' ], +[22.28552, 114.15769, 'Hong Kong' ], +[30.0439192, 30.9812426, 'District 12, Sheikh Zayed, Giza, Egypt' ], +[43.6557254, -79.456573, 'The Address at High Park, 1638 Bloor Street West, Toronto, ON M6P 0A6, Canada' ], +[-20.0, 47.0, 'Madagascar' ], +[1.114186, 103.9852544, 'Manisee Syariah Homestay (actual address), Tiban Mc Dermoth, Batam City 29427, Riau Islands, Indonesia' ], +[64.00028, -150.00028, 'Alaska, United States of America' ], +[18.0, -2.0, 'Mali' ], +[60.0, 100.0, 'Russia' ], +[62.0, 10.0, 'Norway' ], +[-23.2029627, -65.3474844, 'La Nueva Puerta Verde good (good address), Avenida General Belgrano, Humahuaca, Municipio de Humahuaca, Argentina' ], +[20.75028, -156.50028, 'Hawaii, United States of America' ], +[46.0, 105.0, 'Mongolia' ], +[-37.9032307, 144.7585649, 'The Address, Point Cook VIC 3030, Australia' ], +]; diff --git a/scripts/GeoCode API/where.txt b/scripts/GeoCode API/where.txt new file mode 100644 index 0000000..f92227b --- /dev/null +++ b/scripts/GeoCode API/where.txt @@ -0,0 +1,22 @@ +rome +delhi +london +sydney +japan +moscow +los angeles +brazil +cape town +hong kong +egypt +canada +madagascar +indonesia +alaska +mali +russia +norway +argentina +hawaii +mongolia +australia \ No newline at end of file