Skip to content

Commit

Permalink
Include get_city_name_by_geocode
Browse files Browse the repository at this point in the history
  • Loading branch information
luabida committed Dec 20, 2023
1 parent 2fc9f66 commit a7ef912
Show file tree
Hide file tree
Showing 3 changed files with 44,607 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pysus/tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
import pytest

from pysus.utilities.brasil import get_city_name_by_geocode


class TestGetMunNameByGeocode(unittest.TestCase):

@pytest.mark.timeout(5)
def test_get_mun_name_by_geocode(self):
rio = get_city_name_by_geocode(3304557)
self.assertEqual(rio, "Rio de Janeiro")

vale = get_city_name_by_geocode(1101757)
self.assertEqual(vale, "Vale do Anari")

santa_helena = get_city_name_by_geocode(5219308)
self.assertEqual(santa_helena, "Santa Helena de Goiás")


if __name__ == "__main__":
unittest.main()
23 changes: 23 additions & 0 deletions pysus/utilities/brasil.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import json
from pathlib import Path
from typing import Union


UFs = {
"BR": "Brasil",
"AC": "Acre",
Expand Down Expand Up @@ -43,3 +48,21 @@
11: "Novembro",
12: "Dezembro",
}


def get_city_name_by_geocode(geocode: Union[str, int]):
"""
Returns the Municipality name from its geocode (IBGE)
:param geocode: 7 digits city code, according to IBGE format
:return: City name
"""

with open(f"{Path(__file__).parent}/municipios.json") as muns:
_mun_decoded = muns.read().encode().decode("utf-8-sig")
municipalities = json.loads(_mun_decoded)

mun_by_geocode = {
mun["geocodigo"]: mun["municipio"] for mun in municipalities
}

return mun_by_geocode[int(geocode)]
Loading

0 comments on commit a7ef912

Please sign in to comment.