Exemple #1
0
def generate_br_muni_list():
    br_muni_json = read_json(export_dir / 'br_muni.json')
    counties = sorted(br_muni_json.values(), key=lambda k: k['name'])

    doc_md = f'# Brazil municipality IBGE code list\n'
    doc_md += '[GeoJSON for all municipalities](../export/geojson/q5/br_muni_all.geojson)'

    county_by_state = {}
    for item in sorted(counties, key=lambda k: k['state_code']):
        state_code = item['state_code']
        county_by_state.setdefault(state_code, [])
        county_by_state[state_code].append(item)

    for state_code, state_items in county_by_state.items():
        doc_md += f'\n\n#### {state_code}\n'
        doc_md += 'Name | IBGE code | GeoJSON | population \n'
        doc_md += '--- | --- | --- | --: \n'

        for item in sorted(state_items, key=lambda k: k['name']):
            ibge_code = item['ibge_code']
            name_long = item['name_long']
            population = item['population']
            geojson_path = item['geojson_path']

            population_str = ''
            if population:
                population_str = f'{population:,}'

            geojson_link = f'[GeoJSON](../geojson/q8/{geojson_path})'

            doc_md += f'{name_long} | {ibge_code} | {geojson_link} | {population_str}\n'

    docs_dir.mkdir(parents=True, exist_ok=True)
    write_file(docs_dir / 'br_muni_list.md', doc_md)
Exemple #2
0
def generate_fips_list():
    fips_json = read_json(export_dir / 'fips.json')
    counties = sorted(fips_json.values(), key=lambda k: k['name'])
    state_by_code = get_state_codes()[0]

    doc_md = f'# US county FIPS code list\n'
    doc_md += '[GeoJSON for all counties](../export/geojson/q5/fips_all.geojson)'

    county_by_state = {}
    for item in sorted(counties, key=lambda k: k['state_code']):
        state_code = item['state_code']
        county_by_state.setdefault(state_code, [])
        county_by_state[state_code].append(item)

    for state_code, state_items in county_by_state.items():
        state_name = state_by_code[state_code]
        doc_md += f'\n\n#### {state_name}, state code: {state_code}\n'
        doc_md += 'Name | FIPS | GeoJSON | population \n'
        doc_md += '--- | --- | --- | --: \n'

        for item in sorted(state_items, key=lambda k: k['name']):
            fips = item['fips']
            name = item['name']
            population = item['population']

            population_str = ''
            if population:
                population_str = f'{population:,}'

            state_code_str = fips[:2]
            geojson_link = f'[GeoJSON](../export/geojson/q8/fips/{state_code_str}/{fips}.geojson)'

            doc_md += f'{name} | {fips} | {geojson_link} | {population_str}\n'

    write_file(docs_dir / 'fips_list.md', doc_md)
Exemple #3
0
def generate_iso1_list():
    iso1_json = read_json(export_dir / 'iso1.json')

    iso2_json = read_json(export_dir / 'iso2.json')
    iso2_country_codes = {i.split('-')[0] for i in iso2_json}

    doc_md = (
        '# ISO 3166-1 list\n'
        'Name | ISO1 | ISO2 | GeoJSON | OSM | Wikidata | Wikipedia | population \n'
        '--- | --- | --- | --- | --- | --- | --- | --: \n')

    for item in sorted(iso1_json.values(), key=lambda k: k['name']):
        data = build_row_data(item)

        iso1 = item['iso1']
        geojson_path = item['geojson_path']
        geojson_link = f'[GeoJSON](../export/geojson/q7/{geojson_path})'

        if iso1 in iso2_country_codes:
            iso_2_link = f'[ISO2](iso2_list/{iso1}.md)'
        else:
            iso_2_link = ''

        doc_md += (
            f'{data["name"]} | {iso1} | {iso_2_link} | {geojson_link} | '
            f'{data["osm_link"]} | {data["wikidata_link"]} | '
            f'{data["wikipedia_link"]} | {data["population_str"]}'
            f'\n')

    write_file(docs_dir / 'iso1_list.md', doc_md)
Exemple #4
0
def generate_iso2_list_country(iso1):
    iso2_json = read_json(export_dir / 'iso2.json')
    iso2_filtered = [
        i for i in iso2_json.values() if i['iso2'].split('-')[0] == iso1
    ]

    doc_md = f'# ISO 3166-2 list: {iso1}\n'

    iso2_by_level = {}
    for item in sorted(iso2_filtered, key=lambda k: k['admin_level']):
        level = item['admin_level']
        iso2_by_level.setdefault(level, [])
        iso2_by_level[level].append(item)

    for level, level_items in iso2_by_level.items():
        doc_md += f'\n\n#### Level {level}\n'
        doc_md += (
            'Name | ISO2 | GeoJSON | OSM | Wikidata | Wikipedia | population \n'
            '--- | --- | --- | --- | --- | --- | --: \n')

        for item in sorted(level_items, key=lambda k: k['name']):
            data = build_row_data(item)

            iso2 = item['iso2']
            geojson_path = item['geojson_path']
            geojson_link = f'[GeoJSON](../../export/geojson/q8/{geojson_path})'

            doc_md += (f'{data["name"]} | {iso2} | {geojson_link} | '
                       f'{data["osm_link"]} | {data["wikidata_link"]} | '
                       f'{data["wikipedia_link"]} | {data["population_str"]}'
                       f'\n')

    write_file(docs_dir / 'iso2_list' / f'{iso1}.md', doc_md)
def generate_country_list():
    levels = read_json(ne_id_dir / 'ne012.json')
    doc_md = '# Country list'

    for ne0, ne0_data in sorted(levels.items(),
                                key=lambda item: item[1]['name']):
        name = ne0_data['name']
        code = ne0[4:].lower()

        doc_md += (
            f'\n{name}{md_space}'
            f'code: **{ne0}**{md_space}'
            f'[view](../export/geojson/medium/ne0/{code}.geojson){md_space}')

        if (ne3_dir / f'{code}.json').is_file():
            doc_md += f'[states/provinces](country_list_ne3/{code}.md)'

        doc_md += '\n\n'

        if 'sub1' not in ne0_data:
            continue

        sub1 = ne0_data['sub1']
        for ne1, ne1_data in sorted(sub1.items(),
                                    key=lambda item: item[1]['name']):
            name = ne1_data['name']
            level = ne1[:3]
            code = ne1[4:].lower()

            doc_md += (
                f'  - {name}{md_space}'
                f'code: **{ne1}**{md_space}'
                f'[view](../export/geojson/medium/{level}/{code}.geojson){md_space} '
                f'\n\n')
            if 'sub2' not in ne1_data:
                continue

            sub2 = ne1_data['sub2']
            for ne2, ne2_data in sorted(sub2.items(),
                                        key=lambda item: item[1]['name']):
                name = ne2_data['name']
                level = ne2[:3]
                code = ne2[4:].lower()

                doc_md += (
                    f'    - {name}{md_space}'
                    f'code: **{ne2}**{md_space}'
                    f'[view](../export/geojson/medium/{level}/{code}.geojson){md_space}'
                    f'\n\n')

    write_file(docs_dir / 'country_list.md', doc_md)
    print(f'country_list.md updated')
def generate_ne3_md(country_iso):
    filename = f'{country_iso.lower()}.json'
    level3 = read_json(ne3_dir / filename)
    level012 = read_json(ne_id_dir / 'ne012.json')

    country_name = level012[f'ne0:{country_iso.upper()}']['name']

    doc_md = f'# {country_name} states/provinces/counties'

    for ne3, ne3_data in sorted(level3.items(),
                                key=lambda item: item[1]['name']):
        name = ne3_data['name']
        ne3_country, ne3_state = ne3.split(':')[1].split('-')
        assert country_iso.lower() == ne3_country.lower()

        doc_md += (
            f'\n{name}{md_space}'
            f'code: **{ne3}**{md_space}'
            f'[view](../../export/geojson/medium/ne3/{country_iso.lower()}/{ne3_state.lower()}.geojson){md_space}'
            f'\n\n')

    write_file(docs_dir / 'country_list_ne3' / f'{country_iso}.md', doc_md)
def generate_fips_list():
    fips_json = read_json(export_dir / 'fips.json')
    counties = sorted(fips_json.values(), key=lambda k: k['name'])
    states_by_int = get_state_data()[0]

    doc_md = f'# US county FIPS code list\n'
    doc_md += '[GeoJSON for all counties](../export/geojson/q5/fips_all.geojson)'

    county_by_state = {}
    for item in sorted(counties, key=lambda k: k['state_code_int']):
        state_code_int = item['state_code_int']
        county_by_state.setdefault(state_code_int, [])
        county_by_state[state_code_int].append(item)

    for state_code_int, state_items in county_by_state.items():
        state_name = states_by_int[state_code_int]['name']
        state_code_postal = states_by_int[state_code_int]['postal_code']
        doc_md += f'\n\n#### {state_name} - {state_code_postal}, state code: {state_code_int}\n'
        doc_md += 'Name | FIPS | GeoJSON | population \n'
        doc_md += '--- | --- | --- | --: \n'

        for item in sorted(state_items, key=lambda k: k['name']):
            fips = item['fips']
            name_long = item['name_long']
            population = item['population']
            geojson_path = item['geojson_path']

            population_str = ''
            if population:
                population_str = f'{population:,}'

            geojson_link = f'[GeoJSON](../geojson/q8/{geojson_path})'

            doc_md += f'{name_long} | {fips} | {geojson_link} | {population_str}\n'

    docs_dir.mkdir(parents=True, exist_ok=True)
    write_file(docs_dir / 'fips_list.md', doc_md)