tunned-geobr 0.2.10__py3-none-any.whl → 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tunned_geobr/__init__.py +4 -2
- tunned_geobr/list_geobr.py +6 -1
- tunned_geobr/read_amazon.py +1 -1
- tunned_geobr/read_ibama_embargoes.py +85 -0
- tunned_geobr/read_icmbio_embargoes.py +83 -0
- tunned_geobr/read_icmbio_infractions.py +83 -0
- tunned_geobr/read_immediate_region.py +59 -71
- tunned_geobr/read_indigenous_land.py +66 -33
- tunned_geobr/read_intermediate_region.py +67 -59
- tunned_geobr/read_meso_region.py +60 -69
- tunned_geobr/read_micro_region.py +60 -69
- tunned_geobr/read_neighborhood.py +88 -28
- tunned_geobr/read_region.py +59 -21
- {tunned_geobr-0.2.10.dist-info → tunned_geobr-1.0.1.dist-info}/METADATA +1 -1
- {tunned_geobr-0.2.10.dist-info → tunned_geobr-1.0.1.dist-info}/RECORD +18 -16
- tunned_geobr/read_neighborhoods_2022.py +0 -99
- {tunned_geobr-0.2.10.dist-info → tunned_geobr-1.0.1.dist-info}/WHEEL +0 -0
- {tunned_geobr-0.2.10.dist-info → tunned_geobr-1.0.1.dist-info}/entry_points.txt +0 -0
- {tunned_geobr-0.2.10.dist-info → tunned_geobr-1.0.1.dist-info}/licenses/LICENSE.txt +0 -0
@@ -1,61 +1,69 @@
|
|
1
|
-
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
from zipfile import ZipFile
|
6
|
+
from io import BytesIO
|
2
7
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
with high resolution or a dataset with 'simplified' borders (Default)
|
27
|
-
verbose : bool, optional
|
28
|
-
by default False
|
29
|
-
|
30
|
-
Returns
|
31
|
-
-------
|
32
|
-
gpd.GeoDataFrame
|
33
|
-
Metadata and geopackage of selected states
|
34
|
-
|
35
|
-
Raises
|
36
|
-
------
|
37
|
-
Exception
|
38
|
-
If parameters are not found or not well defined
|
39
|
-
|
40
|
-
Example
|
41
|
-
-------
|
42
|
-
>>> from geobr import read_intermediate_region
|
43
|
-
|
44
|
-
# Read specific state at a given year
|
45
|
-
>>> df = read_intermediate_region(year=2019)
|
8
|
+
def read_intermediate_region(simplified=False):
|
9
|
+
"""Download official intermediate region data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes intermediate region data from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
The data includes intermediate regions of Brazil for the year 2023.
|
13
|
+
Original source: IBGE
|
14
|
+
|
15
|
+
Parameters
|
16
|
+
----------
|
17
|
+
simplified : boolean, by default False
|
18
|
+
If True, returns a simplified version of the dataset with fewer columns
|
19
|
+
|
20
|
+
Returns
|
21
|
+
-------
|
22
|
+
gpd.GeoDataFrame
|
23
|
+
Geodataframe with intermediate region data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from geobr import read_intermediate_region
|
28
|
+
|
29
|
+
# Read intermediate region data
|
30
|
+
>>> intermediate_region = read_intermediate_region()
|
46
31
|
"""
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2023/Brasil/BR_RG_Intermediarias_2023.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Download the zip file
|
37
|
+
response = requests.get(url)
|
38
|
+
if response.status_code != 200:
|
39
|
+
raise Exception("Failed to download data from IBGE")
|
40
|
+
|
41
|
+
# Create a temporary directory
|
42
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
43
|
+
# Extract zip content
|
44
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
45
|
+
zip_ref.extractall(temp_dir)
|
46
|
+
|
47
|
+
# Find the shapefile
|
48
|
+
shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
|
49
|
+
if not shp_files:
|
50
|
+
raise Exception("No shapefile found in the downloaded data")
|
51
|
+
|
52
|
+
# Read the shapefile
|
53
|
+
gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
|
54
|
+
|
55
|
+
if simplified:
|
56
|
+
# Keep only the most relevant columns
|
57
|
+
# Note: These columns are based on typical intermediate region data structure
|
58
|
+
# You may want to adjust these based on the actual data
|
59
|
+
columns_to_keep = [
|
60
|
+
'geometry',
|
61
|
+
'CD_RGINT', # Intermediate region code
|
62
|
+
'NM_RGINT', # Intermediate region name
|
63
|
+
]
|
64
|
+
gdf = gdf[columns_to_keep]
|
65
|
+
|
66
|
+
except Exception as e:
|
67
|
+
raise Exception(f"Error downloading intermediate region data: {str(e)}")
|
68
|
+
|
69
|
+
return gdf
|
tunned_geobr/read_meso_region.py
CHANGED
@@ -1,78 +1,69 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
from zipfile import ZipFile
|
6
|
+
from io import BytesIO
|
7
|
+
|
8
|
+
def read_meso_region(simplified=False):
|
9
|
+
"""Download official mesoregion data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes mesoregion data from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
The data includes mesoregions of Brazil for the year 2022.
|
13
|
+
Original source: IBGE
|
14
|
+
|
9
15
|
Parameters
|
10
16
|
----------
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
If code_meso="all", all meso regions of the country are loaded.
|
15
|
-
year : int, optional
|
16
|
-
Year of the data, by default 2010
|
17
|
-
simplified: boolean, by default True
|
18
|
-
Data 'type', indicating whether the function returns the 'original' dataset
|
19
|
-
with high resolution or a dataset with 'simplified' borders (Default)
|
20
|
-
verbose : bool, optional
|
21
|
-
by default False
|
22
|
-
|
17
|
+
simplified : boolean, by default False
|
18
|
+
If True, returns a simplified version of the dataset with fewer columns
|
19
|
+
|
23
20
|
Returns
|
24
21
|
-------
|
25
22
|
gpd.GeoDataFrame
|
26
|
-
|
27
|
-
|
28
|
-
Raises
|
29
|
-
------
|
30
|
-
Exception
|
31
|
-
If parameters are not found or not well defined
|
32
|
-
|
23
|
+
Geodataframe with mesoregion data
|
24
|
+
|
33
25
|
Example
|
34
26
|
-------
|
35
27
|
>>> from geobr import read_meso_region
|
36
|
-
|
37
|
-
# Read
|
38
|
-
>>>
|
39
|
-
|
40
|
-
# Read all meso regions of a state at a given year
|
41
|
-
>>> df = read_meso_region(code_meso=12, year=2017)
|
42
|
-
>>> df = read_meso_region(code_meso="AM", year=2000)
|
43
|
-
|
44
|
-
# Read all meso regions of the country at a given year
|
45
|
-
>>> df = read_meso_region(code_meso="all", year=2010)
|
28
|
+
|
29
|
+
# Read mesoregion data
|
30
|
+
>>> meso_region = read_meso_region()
|
46
31
|
"""
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2022/Brasil/BR/BR_Mesorregioes_2022.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Download the zip file
|
37
|
+
response = requests.get(url)
|
38
|
+
if response.status_code != 200:
|
39
|
+
raise Exception("Failed to download data from IBGE")
|
40
|
+
|
41
|
+
# Create a temporary directory
|
42
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
43
|
+
# Extract zip content
|
44
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
45
|
+
zip_ref.extractall(temp_dir)
|
46
|
+
|
47
|
+
# Find the shapefile
|
48
|
+
shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
|
49
|
+
if not shp_files:
|
50
|
+
raise Exception("No shapefile found in the downloaded data")
|
51
|
+
|
52
|
+
# Read the shapefile
|
53
|
+
gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
|
54
|
+
|
55
|
+
if simplified:
|
56
|
+
# Keep only the most relevant columns
|
57
|
+
# Note: These columns are based on typical mesoregion data structure
|
58
|
+
# You may want to adjust these based on the actual data
|
59
|
+
columns_to_keep = [
|
60
|
+
'geometry',
|
61
|
+
'CD_MESO', # Mesoregion code
|
62
|
+
'NM_MESO', # Mesoregion name
|
63
|
+
]
|
64
|
+
gdf = gdf[columns_to_keep]
|
65
|
+
|
66
|
+
except Exception as e:
|
67
|
+
raise Exception(f"Error downloading mesoregion data: {str(e)}")
|
68
|
+
|
69
|
+
return gdf
|
@@ -1,78 +1,69 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
from zipfile import ZipFile
|
6
|
+
from io import BytesIO
|
7
|
+
|
8
|
+
def read_micro_region(simplified=False):
|
9
|
+
"""Download official microregion data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes microregion data from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
The data includes microregions of Brazil for the year 2022.
|
13
|
+
Original source: IBGE
|
14
|
+
|
9
15
|
Parameters
|
10
16
|
----------
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
If code_micro="all", all micro regions of the country are loaded.
|
15
|
-
year : int, optional
|
16
|
-
Year of the data, by default 2010
|
17
|
-
simplified: boolean, by default True
|
18
|
-
Data 'type', indicating whether the function returns the 'original' dataset
|
19
|
-
with high resolution or a dataset with 'simplified' borders (Default)
|
20
|
-
verbose : bool, optional
|
21
|
-
by default False
|
22
|
-
|
17
|
+
simplified : boolean, by default False
|
18
|
+
If True, returns a simplified version of the dataset with fewer columns
|
19
|
+
|
23
20
|
Returns
|
24
21
|
-------
|
25
22
|
gpd.GeoDataFrame
|
26
|
-
|
27
|
-
|
28
|
-
Raises
|
29
|
-
------
|
30
|
-
Exception
|
31
|
-
If parameters are not found or not well defined
|
32
|
-
|
23
|
+
Geodataframe with microregion data
|
24
|
+
|
33
25
|
Example
|
34
26
|
-------
|
35
27
|
>>> from geobr import read_micro_region
|
36
|
-
|
37
|
-
# Read
|
38
|
-
>>>
|
39
|
-
|
40
|
-
# Read all meso regions of a state at a given year
|
41
|
-
>>> df = read_micro_region(code_micro=12, year=2017)
|
42
|
-
>>> df = read_micro_region(code_micro="AM", year=2000)
|
43
|
-
|
44
|
-
# Read all meso regions of the country at a given year
|
45
|
-
>>> df = read_micro_region(code_micro="all", year=2010)
|
28
|
+
|
29
|
+
# Read microregion data
|
30
|
+
>>> micro_region = read_micro_region()
|
46
31
|
"""
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2022/Brasil/BR/BR_Microrregioes_2022.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Download the zip file
|
37
|
+
response = requests.get(url)
|
38
|
+
if response.status_code != 200:
|
39
|
+
raise Exception("Failed to download data from IBGE")
|
40
|
+
|
41
|
+
# Create a temporary directory
|
42
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
43
|
+
# Extract zip content
|
44
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
45
|
+
zip_ref.extractall(temp_dir)
|
46
|
+
|
47
|
+
# Find the shapefile
|
48
|
+
shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
|
49
|
+
if not shp_files:
|
50
|
+
raise Exception("No shapefile found in the downloaded data")
|
51
|
+
|
52
|
+
# Read the shapefile
|
53
|
+
gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
|
54
|
+
|
55
|
+
if simplified:
|
56
|
+
# Keep only the most relevant columns
|
57
|
+
# Note: These columns are based on typical microregion data structure
|
58
|
+
# You may want to adjust these based on the actual data
|
59
|
+
columns_to_keep = [
|
60
|
+
'geometry',
|
61
|
+
'CD_MICRO', # Microregion code
|
62
|
+
'NM_MICRO', # Microregion name
|
63
|
+
]
|
64
|
+
gdf = gdf[columns_to_keep]
|
65
|
+
|
66
|
+
except Exception as e:
|
67
|
+
raise Exception(f"Error downloading microregion data: {str(e)}")
|
68
|
+
|
69
|
+
return gdf
|
@@ -1,39 +1,99 @@
|
|
1
|
-
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
import subprocess
|
6
|
+
from io import BytesIO
|
2
7
|
|
3
|
-
|
4
|
-
|
5
|
-
|
8
|
+
def read_neighborhood(simplified=False):
|
9
|
+
"""Download Brazilian Neighborhoods data from IBGE (2022 Census).
|
10
|
+
|
11
|
+
This function downloads and processes the Brazilian Neighborhoods data
|
12
|
+
from IBGE (Brazilian Institute of Geography and Statistics) for the 2022 Census.
|
13
|
+
Original source: IBGE - Instituto Brasileiro de Geografia e Estatística
|
6
14
|
|
7
15
|
Parameters
|
8
16
|
----------
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
Data 'type', indicating whether the function returns the 'original' dataset
|
13
|
-
with high resolution or a dataset with 'simplified' borders (Default)
|
14
|
-
verbose : bool, optional
|
15
|
-
by default False
|
16
|
-
|
17
|
+
simplified : boolean, by default False
|
18
|
+
If True, returns a simplified version of the dataset with fewer columns
|
19
|
+
|
17
20
|
Returns
|
18
21
|
-------
|
19
22
|
gpd.GeoDataFrame
|
20
|
-
|
21
|
-
|
22
|
-
Raises
|
23
|
-
------
|
24
|
-
Exception
|
25
|
-
If parameters are not found or not well defined
|
26
|
-
|
23
|
+
Geodataframe with Brazilian neighborhoods data
|
24
|
+
|
27
25
|
Example
|
28
26
|
-------
|
29
|
-
>>> from
|
30
|
-
|
31
|
-
# Read
|
32
|
-
>>>
|
27
|
+
>>> from tunned_geobr import read_neighborhoods_2022
|
28
|
+
|
29
|
+
# Read neighborhoods data
|
30
|
+
>>> neighborhoods = read_neighborhoods_2022()
|
33
31
|
"""
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2022/bairros/shp/BR/BR_bairros_CD2022.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Create a temporary directory
|
37
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
38
|
+
# Download the zip file to the temporary directory
|
39
|
+
zip_file_path = os.path.join(temp_dir, "neighborhoods.zip")
|
40
|
+
|
41
|
+
# Download the file
|
42
|
+
response = requests.get(url)
|
43
|
+
if response.status_code != 200:
|
44
|
+
raise Exception("Failed to download neighborhoods data from IBGE")
|
45
|
+
|
46
|
+
# Save the content to a file
|
47
|
+
with open(zip_file_path, 'wb') as f:
|
48
|
+
f.write(response.content)
|
49
|
+
|
50
|
+
# Use unzip command line tool to extract the file (handles more compression methods)
|
51
|
+
try:
|
52
|
+
subprocess.run(['unzip', '-o', zip_file_path, '-d', temp_dir],
|
53
|
+
check=True,
|
54
|
+
stdout=subprocess.PIPE,
|
55
|
+
stderr=subprocess.PIPE)
|
56
|
+
except subprocess.CalledProcessError as e:
|
57
|
+
raise Exception(f"Failed to extract zip file: {e.stderr.decode()}")
|
58
|
+
|
59
|
+
# Find the shapefile
|
60
|
+
shp_files = []
|
61
|
+
for root, dirs, files in os.walk(temp_dir):
|
62
|
+
shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
|
63
|
+
|
64
|
+
if not shp_files:
|
65
|
+
raise Exception("No shapefile found in the downloaded data")
|
66
|
+
|
67
|
+
# Read the shapefile
|
68
|
+
gdf = gpd.read_file(shp_files[0])
|
69
|
+
|
70
|
+
# Convert to SIRGAS 2000 (EPSG:4674) if not already
|
71
|
+
if gdf.crs is None or gdf.crs.to_epsg() != 4674:
|
72
|
+
gdf = gdf.to_crs(4674)
|
73
|
+
|
74
|
+
if simplified:
|
75
|
+
# Keep only the most relevant columns
|
76
|
+
# Note: Column names may need adjustment based on actual data
|
77
|
+
columns_to_keep = [
|
78
|
+
'geometry',
|
79
|
+
'CD_BAIRRO', # Neighborhood Code
|
80
|
+
'NM_BAIRRO', # Neighborhood Name
|
81
|
+
'CD_MUN', # Municipality Code
|
82
|
+
'NM_MUN', # Municipality Name
|
83
|
+
'CD_UF', # State Code
|
84
|
+
'NM_UF', # State Name
|
85
|
+
'SIGLA_UF', # State Abbreviation
|
86
|
+
'AREA_KM2' # Area in square kilometers
|
87
|
+
]
|
88
|
+
|
89
|
+
# Filter columns that actually exist in the dataset
|
90
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
91
|
+
gdf = gdf[existing_columns]
|
92
|
+
|
93
|
+
except Exception as e:
|
94
|
+
raise Exception(f"Error downloading neighborhoods data: {str(e)}")
|
95
|
+
|
39
96
|
return gdf
|
97
|
+
|
98
|
+
if __name__ == '__main__':
|
99
|
+
read_neighborhoods_2022()
|
tunned_geobr/read_region.py
CHANGED
@@ -1,31 +1,69 @@
|
|
1
|
-
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
from zipfile import ZipFile
|
6
|
+
from io import BytesIO
|
2
7
|
|
3
|
-
|
4
|
-
|
5
|
-
|
8
|
+
def read_region(simplified=False):
|
9
|
+
"""Download official region data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes region data from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
The data includes regions of Brazil for the year 2022.
|
13
|
+
Original source: IBGE
|
6
14
|
|
7
|
-
Data at scale 1:250,000, using Geodetic reference system "SIRGAS2000" and CRS(4674)
|
8
|
-
|
9
15
|
Parameters
|
10
16
|
----------
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
Data 'type', indicating whether the function returns the 'original' dataset
|
15
|
-
with high resolution or a dataset with 'simplified' borders (Default)
|
16
|
-
verbose : bool, optional
|
17
|
-
by default False
|
18
|
-
|
17
|
+
simplified : boolean, by default False
|
18
|
+
If True, returns a simplified version of the dataset with fewer columns
|
19
|
+
|
19
20
|
Returns
|
20
21
|
-------
|
21
22
|
gpd.GeoDataFrame
|
22
|
-
|
23
|
-
|
23
|
+
Geodataframe with region data
|
24
|
+
|
24
25
|
Example
|
25
26
|
-------
|
26
|
-
>>> from
|
27
|
-
|
28
|
-
# Read
|
29
|
-
>>>
|
27
|
+
>>> from geobr import read_region
|
28
|
+
|
29
|
+
# Read region data
|
30
|
+
>>> region = read_region()
|
30
31
|
"""
|
31
|
-
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2022/Brasil/BR/BR_Regioes_2022.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Download the zip file
|
37
|
+
response = requests.get(url)
|
38
|
+
if response.status_code != 200:
|
39
|
+
raise Exception("Failed to download data from IBGE")
|
40
|
+
|
41
|
+
# Create a temporary directory
|
42
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
43
|
+
# Extract zip content
|
44
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
45
|
+
zip_ref.extractall(temp_dir)
|
46
|
+
|
47
|
+
# Find the shapefile
|
48
|
+
shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
|
49
|
+
if not shp_files:
|
50
|
+
raise Exception("No shapefile found in the downloaded data")
|
51
|
+
|
52
|
+
# Read the shapefile
|
53
|
+
gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
|
54
|
+
|
55
|
+
if simplified:
|
56
|
+
# Keep only the most relevant columns
|
57
|
+
# Note: These columns are based on typical region data structure
|
58
|
+
# You may want to adjust these based on the actual data
|
59
|
+
columns_to_keep = [
|
60
|
+
'geometry',
|
61
|
+
'CD_RG', # Region code
|
62
|
+
'NM_RG', # Region name
|
63
|
+
]
|
64
|
+
gdf = gdf[columns_to_keep]
|
65
|
+
|
66
|
+
except Exception as e:
|
67
|
+
raise Exception(f"Error downloading region data: {str(e)}")
|
68
|
+
|
69
|
+
return gdf
|