Additonal arguments are passed to barnehagefakta_get_json""" j = barnehagefakta_get_json(nbr_id, *args, **kwargs) if j is None: return {} elif j == '404': raise NotFoundException('nbr_id={0} returned 404'.format(nbr_id)) #return {} dct = json.loads(j) if logger.isEnabledFor(logging.DEBUG): logger.debug('barnehagefakta_get(%s) -> %s', nbr_id, pretty_printer.pformat(dct)) return dct if __name__ == '__main__': import argparse_util parser = argparse_util.get_parser('Helper script for requesting (with local cache) and parsing json data from "Utdanningdsdirektoratet Nasjonalt barnehageregister (NBR)"') parser.add_argument('nbr_id', nargs='+', help='Unique NBR-id(s) to download (e.g. 1015988).') parser.add_argument('--cache_dir', default='data', help='Specify directory for cached .json files, defaults to data/') argparse_util.add_verbosity(parser, default=logging.DEBUG) args = parser.parse_args() logging.basicConfig(level=args.loglevel) if args.nbr_id: # list of ids given for nbr_id in args.nbr_id: print 'Getting', nbr_id barnehagefakta_get(nbr_id, cache_dir=args.cache_dir)
today = datetime.utcnow() td = (today - datetime(1970, 1, 1)) td_s = td.total_seconds() with open('history.csv', 'a') as f: f.write('{0},{1},{2}\n'.format(td_s, total_nbr, total_osm)) def get_osm_data(): xml = update_osm.overpass_nsrid() osm = osmapis.OSMnsrid.from_xml(xml) # osm_elements = list(update_osm.find_all_nsrid_osm_elements(osm)) print len(osm.nsrids), osm.nsrids return osm if __name__ == '__main__': import argparse_util parser = argparse_util.get_parser('Looks for <data_dir>/<kommune_id>/*.osm files and generates html for http://obtitus.github.io/barnehagefakta_osm_data/. The site is generated in the current directory by default and assumes template.html and index_template.html exists in the current directory.') parser.add_argument('--data_dir', default='data', help='Specify directory for .osm files, defaults to data/') parser.add_argument('--output_dir', default='.', help="Specify output directory, defaults to current directory") parser.add_argument('--template', default='template.html', help="Specify template file for each of the kommune pages, defaults to template.html") parser.add_argument('--index_template', default='index_template.html', help="Specify template file for index.html, defaults to index_template.html") parser.add_argument('--no-overpass', default=False, action='store_true', help="Do not call the openstreetmap overpass api looking for no-barnehage:nsrid") argparse_util.add_verbosity(parser, default=logging.WARNING) args = parser.parse_args() logging.basicConfig(level=args.loglevel)
for elem in overpass_osm: if not(elem) in all_modified: overpass_osm.discard(elem) if len(modified) != 0: print 'Saving conflated data as "%s", open this in JOSM, review and upload. Remember to include "data.udir.no" in source' % output_filename overpass_osm.save(output_filename) else: print 'No changes made, nothing to upload' if __name__ == '__main__': import argparse_util parser = argparse_util.get_parser("""A tool for assisting with the conflation of openstreetmap and NBR data. You will need JSON to review and upload changes to openstreetmap. Using this tool is therefore completely safe, play around, you changes will not be visible on openstreetmap! As a 'working area' you need to supply either a --relation_id or --bounding_box, as an NBR data input, you need to supply either a --osm_kommune or --osm_filename. """, epilog='Example: ./conflate_osm.py --relation_id 406130 --osm_kommune Ski') group = parser.add_mutually_exclusive_group() group.add_argument('--relation_id', help="Bounding box as a OSM relation id (e.g. 406130 for Ski kommune)") group.add_argument('--bounding_box', help="""Bounding box [west, south, east, north], e.g. '10.8,59.7,10.9,59.7', use (almost) whatever delimiter you like""") parser.add_argument('--osm_kommune', nargs='+', help="""Specify one or more kommune, either by kommunenummer (e.g. 0213 or 213) or kommunename (e.g. Ski). If the correct file can not be found,
else: logger.warning('Unresolved conflict, NBR has modified %s="%s" to "%s", but osm does not have this key', key, osm_outdated.tags[key], osm_updated.tags[key]) return False # fixme: Re-write, do not return False all over the place? if osm_element_tags_original != osm_element.tags: return 'update' else: return True if __name__ == '__main__': import argparse_util parser = argparse_util.get_parser('Keeps OSM objects with no-barnehage:nsrid=* updated if there are changes in the NBR data. Does not overwrite modified OSM data.') parser.add_argument('--data_dir', default='data', help='Specify directory for .osm files, defaults to data/') parser.add_argument('--batch', default=False, action='store_true', help='Do not promt for user input (will not update OSM, run without --batch to clear all conflicts)') parser.add_argument('--log_filename', default='update_osm.log', help='log file for all logging levels, defaults to update_osm.log.') argparse_util.add_verbosity(parser, default=logging.WARNING) args = parser.parse_args() #logging.basicConfig(level=args.loglevel) # logger_adapter_dict = dict(nbr_id=None) # main_logger = logging.LoggerAdapter(main_logger, logger_adapter_dict) main_logger = logging.getLogger('barnehagefakta')
def update_kommune(kommune_id, cache_dir = 'data'): filename_output = os.path.join(cache_dir, kommune_id, 'nbr_udir_no.json') file_util.create_dirname(filename_output) with open(filename_output, 'w') as f_out: for row in all_pages(kommune_id): # all_location(kommune_id): # f_out.write(json.dumps(row) + '\n') # FIXME: newline makes it human readable, but not json readable.. return filename_output def get_kommune(kommune_id, cache_dir='data'): """Assumes update_kommune has been called""" # fixme? filename_input = os.path.join(cache_dir, kommune_id, 'nbr_udir_no.json') with open(filename_input, 'r') as f_out: for row in f_out: yield json.loads(row) if __name__ == '__main__': import argparse_util parser = argparse_util.get_parser('''Gets nbr_ids for the given kommune by searching https://nbr.udir.no/sok/. Note: this file is called by barnehagefakta_osm.py when --update_kommune is passed.''') parser.add_argument('kommunenr', nargs='+', help='List of kommune-ids') parser.add_argument('--cache_dir', default='data', help='Specify directory for cached .html files and .json outputs, defaults to data/') argparse_util.add_verbosity(parser, default=logging.DEBUG) args = parser.parse_args() logging.basicConfig(level=args.loglevel) for kommune_id in args.kommunenr: update_kommune(kommune_id)