2019-03-18 04:45:33 +00:00
#!/usr/bin/env python
2019-05-06 01:51:34 +00:00
# Scan the vector-maps directory, update the vector-maps-idx.json, add menu-defs
2019-03-18 04:45:33 +00:00
from geojson import Feature, Point, FeatureCollection, Polygon
import geojson
import json
import os
2019-03-19 04:36:23 +00:00
import sys
import fnmatch
2019-03-25 18:09:13 +00:00
import re
2019-03-18 04:45:33 +00:00
2019-03-19 04:36:23 +00:00
IIAB_PATH='/etc/iiab'
if not IIAB_PATH in sys.path:
sys.path.append(IIAB_PATH)
from iiab_env import get_iiab_env
2019-03-18 04:45:33 +00:00
2019-03-25 18:09:13 +00:00
SCRIPT_DIR = '/opt/admin/cmdsrv/scripts'
if not SCRIPT_DIR in sys.path:
sys.path.append(SCRIPT_DIR)
2019-04-08 22:39:17 +00:00
if os.path.exists(os.path.join(SCRIPT_DIR,'iiab_update_menus.py')):
import iiab_update_menus as menus
console_installed = True
else:
console_installed = False
2019-03-25 18:09:13 +00:00
2019-03-19 04:36:23 +00:00
doc_root = get_iiab_env('WWWROOT')
menuDefs = doc_root + "/js-menu/menu-files/menu-defs/"
2019-05-05 06:04:32 +00:00
vector_map_idx_dir = doc_root + "/common/assets"
#map_doc_root = '{{ vector_map_path }}'
2019-05-06 01:51:34 +00:00
map_doc_root = '/library/www/vector-maps'
2019-03-25 18:09:13 +00:00
# map_catalog will be global, assumed always available
2019-03-19 04:36:23 +00:00
map_catalog = {}
2019-04-02 20:18:18 +00:00
map_menu_def_list = []
2019-03-25 18:09:13 +00:00
2019-03-18 04:45:33 +00:00
def main():
2019-04-08 22:39:17 +00:00
global map_menu_def_list
2019-03-19 04:36:23 +00:00
get_map_catalog()
2019-03-25 18:09:13 +00:00
#print(json.dumps(map_catalog,indent=2))
2019-04-08 22:39:17 +00:00
2019-04-02 20:18:18 +00:00
map_menu_def_list = get_menu_def_names()
2019-04-08 22:39:17 +00:00
print(json.dumps(map_menu_def_list,indent=2))
2019-03-26 00:34:04 +00:00
installed_maps = get_installed_regions()
2019-04-02 20:18:18 +00:00
print(installed_maps)
2019-04-08 22:39:17 +00:00
2019-05-05 06:04:32 +00:00
write_vector_map_idx(installed_maps)
2019-04-10 00:47:18 +00:00
# For installed regions, check that a menu def exists, and it's on home page
2019-04-09 05:24:56 +00:00
for fname in installed_maps:
region = extract_region_from_filename(fname)
2019-04-08 22:39:17 +00:00
print('checking for %s region'%region)
2019-04-10 00:47:18 +00:00
if region == 'maplist': # it is the splash page, display only if no others
2019-04-02 20:18:18 +00:00
menu_ref = 'en-map_test'
2019-04-08 22:39:17 +00:00
item = { "perma_ref" : "en-map_test" }
2019-04-10 00:47:18 +00:00
if len(installed_maps) == 1:
menus.update_menu_json(menu_ref)
return
else:
item = map_catalog['regions'][region]
menu_ref = item['perma_ref']
if not (region in map_menu_def_list):
print('creating menu def for %s'%item['perma_ref'])
create_menu_def(region,item['perma_ref'] + '.json')
2019-04-02 20:18:18 +00:00
if fetch_menu_json_value('autoupdate_menu'):
2019-04-10 00:47:18 +00:00
print('autoudate of menu items is enabled:%s. Adding %s'%(\
fetch_menu_json_value('autoupdate_menu'),region,))
# verify this menu def is on home page
2019-04-02 20:18:18 +00:00
menus.update_menu_json(menu_ref)
2019-03-19 04:36:23 +00:00
def get_map_catalog():
global map_catalog
2019-04-08 23:14:48 +00:00
input_json = map_doc_root + '/maplist/assets/regions.json'
2019-03-18 04:45:33 +00:00
with open(input_json,'r') as regions:
reg_str = regions.read()
2019-03-19 04:36:23 +00:00
map_catalog = json.loads(reg_str)
#print(json.dumps(map_catalog,indent=2))
def get_menu_def_names(intended_use='map'):
2019-04-02 20:18:18 +00:00
menu_def_list =[]
2019-03-19 04:36:23 +00:00
os.chdir(menuDefs)
for filename in os.listdir('.'):
if fnmatch.fnmatch(filename, '*.json'):
try:
with open(filename,'r') as json_file:
readstr = json_file.read()
data = json.loads(readstr)
except:
print("failed to parse %s"%filename)
print(readstr)
if data.get('intended_use','') != intended_use:
continue
2019-03-25 18:09:13 +00:00
map_name = data.get('name','')
if map_name != '':
2019-04-02 20:18:18 +00:00
menu_def_list.append(data['name'])
return menu_def_list
2019-03-18 04:45:33 +00:00
2019-03-26 00:34:04 +00:00
def get_installed_regions():
2019-03-25 18:09:13 +00:00
installed = []
os.chdir(map_doc_root)
for filename in os.listdir('.'):
if fnmatch.fnmatch(filename, '??-osm-omt*'):
region = re.sub(r'^..-osm-omt_(.*)',r'\1',filename)
installed.append(region)
2019-04-02 20:18:18 +00:00
# add the splash page if no other maps are present
if len(installed) == 0:
2019-04-08 22:39:17 +00:00
installed.append('maplist')
2019-03-25 18:09:13 +00:00
return installed
2019-05-05 06:04:32 +00:00
def write_vector_map_idx(installed_maps):
2019-03-26 00:34:04 +00:00
map_dict ={}
2019-04-09 17:28:06 +00:00
idx_dict = {}
2019-04-09 05:24:56 +00:00
for fname in installed_maps:
region = extract_region_from_filename(fname)
2019-04-08 22:39:17 +00:00
if map == 'maplist': continue # not a real region
2019-04-10 00:47:18 +00:00
map_dict = map_catalog['regions'].get(region,'')
if map_dict == '': continue
2019-04-09 17:28:06 +00:00
# Create the idx file in format required bo js-menu system
2019-04-10 03:16:26 +00:00
item = map_dict['perma_ref']
idx_dict[item] = {}
idx_dict[item]['file_name'] = os.path.basename(map_dict['url'][:-4])
idx_dict[item]['menu_item'] = map_dict['perma_ref']
idx_dict[item]['size'] = map_dict['size']
idx_dict[item]['date'] = map_dict['date']
idx_dict[item]['region'] = region
idx_dict[item]['language'] = map_dict['perma_ref'][:2]
2019-04-09 17:28:06 +00:00
2019-05-05 06:04:32 +00:00
with open(vector_map_idx_dir + '/osm_version_idx.json','w') as idx:
2019-04-09 17:28:06 +00:00
idx.write(json.dumps(idx_dict,indent=2))
2019-03-25 18:09:13 +00:00
2019-03-26 00:34:04 +00:00
def create_menu_def(region,default_name,intended_use='map'):
item = map_catalog['regions'][region]
2019-03-25 18:09:13 +00:00
if len(item.get('language','')) > 2:
lang = item['language'][:2]
else: # default to english
lang = 'en'
2019-03-26 00:34:04 +00:00
filename = lang + '-' + item['perma_ref'] + '.json'
2019-03-25 18:09:13 +00:00
# create a stub for this zim
menuDef = {}
2019-03-26 00:34:04 +00:00
default_logo = 'osm.jpg'
2019-03-25 18:09:13 +00:00
menuDef["intended_use"] = "map"
menuDef["lang"] = lang
menuDef["logo_url"] = default_logo
2019-03-26 00:34:04 +00:00
menuitem = lang + '-' + item['perma_ref']
menuDef["menu_item_name"] = default_name
2019-04-02 20:18:18 +00:00
menuDef["title"] = "OpenStreetMap: 18 Levels of Zoom for <b> " + item.get('title','ERROR') + '</b>'
2019-03-26 00:34:04 +00:00
menuDef["map_name"] = item['perma_ref']
2019-04-10 17:48:27 +00:00
menuDef["file_name"] = lang + '-osm-omt_' + region + '_' + \
os.path.basename(item['url'])[:-4]
2019-04-02 20:18:18 +00:00
menuDef["description"] = '<p>Resolution of the Whole World to 5 KM. OpenStreetMap data for <b>' + item.get('title','') + '</b> with details down to 5 Meters</p>'
2019-03-25 18:09:13 +00:00
menuDef["extra_html"] = ""
menuDef["automatically_generated"] = "true"
if not os.path.isfile(menuDefs + default_name): # logic to here can still overwrite existing menu def
print("creating %s"%menuDefs + default_name)
2019-04-02 20:18:18 +00:00
with open(menuDefs + default_name,'w') as menufile:
2019-03-25 18:09:13 +00:00
menufile.write(json.dumps(menuDef,indent=2))
return default_name[:-5]
2019-03-19 04:36:23 +00:00
def human_readable(num):
# return 3 significant digits and unit specifier
num = float(num)
units = [ '','K','M','G']
for i in range(4):
if num<10.0:
return "%.2f%s"%(num,units[i])
if num<100.0:
return "%.1f%s"%(num,units[i])
if num < 1000.0:
return "%.0f%s"%(num,units[i])
num /= 1000.0
2019-03-18 04:45:33 +00:00
2019-04-02 20:18:18 +00:00
def fetch_menu_json_value(key):
with open( doc_root + '/home/menu.json','r') as menudef:
data = json.loads(menudef.read())
return data.get(key,'')
2019-04-09 05:24:56 +00:00
def extract_region_from_filename(fname):
substitutions = { "north": "north_america",\
"central": "central_america",\
"southeast": "southeast_asia",
"south": "south_america" }
# wish I had used - as separator between key and date
nibble = fname.split('_')[0]
nibble = substitutions.get(nibble,nibble)
return(nibble)
2019-04-02 20:18:18 +00:00
2019-03-18 04:45:33 +00:00
if __name__ == '__main__':
2019-04-08 22:39:17 +00:00
if console_installed:
main()