コード例 #1
0
ファイル: main.py プロジェクト: MappingKat/pgmapcss
def main():
    print('pgmapcss version %s' % pgmapcss.version.VERSION)
    args = parser.parse_args()

    style_id = args.style_id

    m = re.match('(.*)\.mapcss$', style_id)
    if m:
        style_id = m.group(1)

    file_name = style_id + '.mapcss'

    conn = pgmapcss.db.connect(args)

    if args.database_update == 're-init':
        print('* Re-initializing database')
        pgmapcss.db.db_init(conn)

    db_version = pgmapcss.db.db_version()
    if db_version == None:
        print('* DB functions not installed; installing')
        pgmapcss.db.db_init(conn)
    else:
        db_check = pgmapcss.db.db_version_check()
        if db_check == 1 and args.database_update == 'auto':
            print('* Current DB version: {version} -> updating DB functions'.format(**db_version))
            pgmapcss.db.db_update(conn)

        elif db_check == 2:
            print('* Current DB version: {version}'.format(**db_version))
            print('pgmapcss version too new. Database needs to be re-initialized. Please re-run pgmapcss with parameter "-r re-init". All Mapnik styles need to be re-compiled afterwards.')
            sys.exit(1)

        elif args.database_update == 'update':
            pgmapcss.db.db_update(conn)

        else:
            print('* Current DB version: {version}'.format(**db_version))

    if args.eval_tests:
        pgmapcss.eval.functions().test_all()

    stat = { 'id': style_id }

    content = open(file_name).read()

# check if file is XML -> extract MapCSS code
    tree = None
    if re.match('<\?xml', content):
        import xml.dom.minidom as dom
        tree = dom.parse(file_name)
        mapcss = tree.getElementsByTagName("style")
        if mapcss.length != 1:
            print("Require exactly one <style type='text/mapcss'> node")
            sys.exit(1)

        mapcss = mapcss.item(0)
        content = mapcss.firstChild.nodeValue

    try:
        pgmapcss.parser.parse_file(stat, filename=file_name, content=content, base_style=args.base_style)
    except pgmapcss.parser.ParseError as e:
        print(e)
        sys.exit(1)

    debug = open(style_id + '.output', 'w')

    pp = pprint.PrettyPrinter()

    debug.write("***** Structure of parsed MapCSS style *****\n")
    debug.write(pp.pformat(stat) + '\n')

    pgmapcss.mapnik.init(stat)
    pgmapcss.icons.init(stat)

    try:
        style = pgmapcss.compiler.compile_style(stat)
    except pgmapcss.compiler.CompileError as e:
        print(e)
        sys.exit(1)

    #pp.pprint(style)
    for i in style:
        debug.write("\n***** " + i + " *****\n" + style[i])

    pgmapcss.db.install(style_id, style, conn)
    pgmapcss.mapnik.process_mapnik(style_id, args, stat, conn)
    pgmapcss.icons.process_icons(style_id, args, stat, conn)

    debug.close()

    if 'unresolvable_properties' in stat:
        print('WARNING: Not all values for the following properties could be guessed (e.g. as they are the result of an eval-expression, and therefore some features in the resulting image(s) may be missing: ' + ', '.join(stat['unresolvable_properties']))

    # copy result xml to original dom
    if tree:
        result_tree = dom.parse(style_id + '.mapnik')
        current = result_tree.getElementsByTagName("Map").item(0).firstChild

        while current:
            if re.search('[^\s]', current.toxml()):
                if not re.match('<!\-\-', current.toxml()):
                    copy = dom.parseString(current.toxml())
                    mapcss.parentNode.insertBefore(copy.firstChild, mapcss)
            current = current.nextSibling

        mapcss.parentNode.removeChild(mapcss)
        open(style_id + '.mapnik', 'w').write(tree.toxml())

    print('Debug output wrote to ' + style_id + '.output')
コード例 #2
0
ファイル: main.py プロジェクト: Komzpa/pgmapcss
def main():
    print('pgmapcss version %s' % pgmapcss.version.VERSION)
    args = parser.parse_args()

    style_id = args.style_id

    m = re.match('(.*)\.mapcss$', style_id)
    if m:
        style_id = m.group(1)

    file_name = style_id + '.mapcss'

    conn = pgmapcss.db.connect(args)

    if args.database_update == 're-init':
        print('* Re-initializing database')
        pgmapcss.db.db_init(conn)

    db_version = pgmapcss.db.db_version()
    if db_version == None:
        print('* DB functions not installed; installing')
        pgmapcss.db.db_init(conn)
    else:
        db_check = pgmapcss.db.db_version_check()
        if db_check == 1 and args.database_update == 'auto':
            print('* Current DB version: {version} -> updating DB functions'.format(**db_version))
            pgmapcss.db.db_update(conn)

        elif db_check == 2:
            print('* Current DB version: {version}'.format(**db_version))
            print('pgmapcss version too new. Database needs to be re-initialized. Please re-run pgmapcss with parameter "-r re-init". All Mapnik styles need to be re-compiled afterwards.')
            sys.exit(1)

        elif args.database_update == 'update':
            pgmapcss.db.db_update(conn)

        else:
            print('* Current DB version: {version}'.format(**db_version))

    if args.eval_tests:
        pgmapcss.eval.functions().test_all()

    stat = {}

    try:
        pgmapcss.parser.parse_file(stat, filename=file_name, base_style=args.base_style)
    except pgmapcss.parser.ParseError as e:
        print(e)
        sys.exit(1)

    debug = open(style_id + '.output', 'w')

    pp = pprint.PrettyPrinter()

    debug.write("***** Structure of parsed MapCSS style *****\n")
    debug.write(pp.pformat(stat) + '\n')

    style = pgmapcss.compiler.compile_style(style_id, stat)

    #pp.pprint(style)
    for i in style:
        debug.write("\n***** " + i + " *****\n" + style[i])

    pgmapcss.db.install(style_id, style, conn)
    pgmapcss.mapnik.process_mapnik(style_id, args, stat, conn)

    debug.close()
    print('Debug output wrote to ' + style_id + '.output')
コード例 #3
0
def main():
    print('pgmapcss version %s' % pgmapcss.version.VERSION)
    args = parser.parse_args()

    style_id = args.style_id

    m = re.match('(.*)\.mapcss$', style_id)
    if m:
        style_id = m.group(1)

    file_name = style_id + '.mapcss'

    parameters = { }
    if args.parameters is not None:
        parameters = {
            p[0:p.find('=')]: p[p.find('=')+1:]
            for p in args.parameters
        }

    if args.lang:
        lang = args.lang
    elif 'lang' in parameters:
        pass
    else:
        lang = os.environ.get('LANG')
        if lang:
            m = re.match('(.*)_', lang)
            if m:
                lang = m.group(1)
        else:
            # default: english
            lang = 'en'

    stat = pgmapcss.compiler.stat._stat({
        'id': style_id,
        'config': {},
        'base_style': args.base_style,
        'icons_dir': style_id + '.icons',
        'global_data': None,
        'mode': args.mode,
        'args': args,
        'lang': lang,
        'parameters': parameters,
    })

    if args.config:
        for v in args.config:
            v = v.split("=")
            if len(v) > 1:
                stat['config'][v[0]] = v[1]
            else:
                stat['config'][v[0]] = True

    conn = pgmapcss.db.connect(args, stat)

    stat['database'] = conn.database

    if not 'unit.srs' in stat['config']:
        stat['config']['unit.srs'] = 900913
    if not 'srs' in stat['config']:
        if stat['mode'] == 'database-function':
            stat['config']['srs'] = 900913
        else:
            stat['config']['srs'] = 4326

    if stat['config'].get('offline', False) in (False, 'false', 'no') and args.database_update in ('init', 're-init'):
        print('* Re-initializing database')
        pgmapcss.db.db_init(conn, stat)

    if stat['config'].get('offline', False) not in (False, 'false', 'no'):
        print('* Using offline mode. Attention! Some functionality might be missing.')

    else:
        db_version = pgmapcss.db.db_version()
        if db_version == None:
            print('* DB functions not installed; installing')
            pgmapcss.db.db_init(conn, stat)
        else:
            db_check = pgmapcss.db.db_version_check()
            if db_check == 1 and args.database_update == 'auto':
                print('* Current DB version: {version} -> updating DB functions'.format(**db_version))
                pgmapcss.db.db_update(conn)

            elif db_check == 2:
                print('* Current DB version: {version}'.format(**db_version))
                print('pgmapcss version too new. Database needs to be re-initialized. Please re-run pgmapcss with parameter "-r init". All Mapnik styles need to be re-compiled afterwards.')
                sys.exit(1)

            elif args.database_update == 'update':
                pgmapcss.db.db_update(conn)

            else:
                print('* Current DB version: {version}'.format(**db_version))

    if args.eval_tests is not False:
        if len(args.eval_tests):
            pgmapcss.eval.functions(stat).test_all(args.eval_tests)
        else:
            pgmapcss.eval.functions(stat).test_all()

        print('* All tests completed successfully.')

    try:
        os.mkdir(stat['icons_dir'])
    except OSError:
        pass

    eval_functions = pgmapcss.eval.functions(stat).list()

    content = open(file_name).read()

# check if file is XML -> extract MapCSS code
    tree = None
    if re.match('<\?xml', content):
        import xml.dom.minidom as dom
        tree = dom.parse(file_name)
        mapcss = tree.getElementsByTagName("style")
        if mapcss.length != 1:
            print("Require exactly one <style type='text/mapcss'> node")
            sys.exit(1)

        mapcss = mapcss.item(0)
        content = mapcss.firstChild.nodeValue

    try:
        pgmapcss.parser.parse_file(stat, filename=file_name, content=content, base_style=args.base_style, defaults=args.defaults)
    except pgmapcss.parser.ParseError as e:
        print(e)
        sys.exit(1)

    debug = open(style_id + '.output', 'w')

    pp = pprint.PrettyPrinter()

    debug.write("***** Structure of parsed MapCSS style *****\n")
    debug.write(pp.pformat(stat) + '\n')

    pgmapcss.renderer.init(stat)
    pgmapcss.icons.init(stat)
    pgmapcss.symbols.init(stat)

    try:
        style = pgmapcss.compiler.compile_style(stat)
    except pgmapcss.compiler.CompileError as e:
        print(e)
        sys.exit(1)

    #pp.pprint(style)
    for i in style:
        debug.write("\n***** " + i + " *****\n" + style[i])

    if stat['mode'] == 'database-function':
        pgmapcss.db.install(style_id, style, conn)
        pgmapcss.renderer.process_renderer(style_id, args, stat, conn)
    elif stat['mode'] == 'standalone':
        open(style_id + '.py', 'w').write(style['function_match'])
        os.chmod(style_id + '.py', 0o755)
        print('Created executable {}.py'.format(style_id))

    pgmapcss.icons.process_icons(style_id, args, stat, conn)
    pgmapcss.symbols.process_symbols(style_id, args, stat, conn)

    debug.close()

    if 'unresolvable_properties' in stat:
        print('WARNING: Not all values for the following properties could be guessed (e.g. as they are the result of an eval-expression, and therefore some features in the resulting image(s) may be missing: ' + ', '.join(stat['unresolvable_properties']))

    # copy result xml to original dom
    if tree:
        result_tree = dom.parse(style_id + '.mapnik')
        current = result_tree.getElementsByTagName("Map").item(0).firstChild

        while current:
            if re.search('[^\s]', current.toxml()):
                if not re.match('<!\-\-', current.toxml()):
                    copy = dom.parseString(current.toxml())
                    mapcss.parentNode.insertBefore(copy.firstChild, mapcss)
            current = current.nextSibling

        mapcss.parentNode.removeChild(mapcss)
        open(style_id + '.mapnik', 'w').write(tree.toxml())

    print('Debug output wrote to ' + style_id + '.output')