tunned-geobr 0.2.6__py3-none-any.whl → 0.2.7__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/read_municipality.py +111 -67
- tunned_geobr/read_state.py +83 -68
- {tunned_geobr-0.2.6.dist-info → tunned_geobr-0.2.7.dist-info}/METADATA +1 -1
- {tunned_geobr-0.2.6.dist-info → tunned_geobr-0.2.7.dist-info}/RECORD +7 -7
- {tunned_geobr-0.2.6.dist-info → tunned_geobr-0.2.7.dist-info}/WHEEL +0 -0
- {tunned_geobr-0.2.6.dist-info → tunned_geobr-0.2.7.dist-info}/entry_points.txt +0 -0
- {tunned_geobr-0.2.6.dist-info → tunned_geobr-0.2.7.dist-info}/licenses/LICENSE.txt +0 -0
@@ -1,83 +1,127 @@
|
|
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_municipality(code_muni="all", simplified=False):
|
9
|
+
"""Download shapefiles of Brazilian municipalities as geopandas objects.
|
10
|
+
|
11
|
+
This function downloads and processes municipality data directly from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
Data uses Geodetic reference system "SIRGAS2000" and CRS(4674).
|
13
|
+
|
9
14
|
Parameters
|
10
15
|
----------
|
11
|
-
code_muni:
|
16
|
+
code_muni : str, optional
|
12
17
|
The 7-digit code of a municipality. If the two-digit code or a two-letter uppercase abbreviation of
|
13
18
|
a state is passed, (e.g. 33 or "RJ") the function will load all municipalities of that state.
|
14
|
-
If code_muni="all", all municipalities of the country will be loaded.
|
15
|
-
|
16
|
-
|
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
|
19
|
+
If code_muni="all", all municipalities of the country will be loaded (Default).
|
20
|
+
simplified : boolean, by default True
|
21
|
+
If True, returns a simplified version of the dataset with fewer columns
|
22
22
|
|
23
23
|
Returns
|
24
24
|
-------
|
25
25
|
gpd.GeoDataFrame
|
26
|
-
|
27
|
-
|
28
|
-
Raises
|
29
|
-
------
|
30
|
-
Exception
|
31
|
-
If parameters are not found or not well defined
|
26
|
+
Geodataframe with municipality boundaries
|
32
27
|
|
33
28
|
Example
|
34
29
|
-------
|
35
|
-
>>> from
|
30
|
+
>>> from tunned_geobr import read_municipality
|
36
31
|
|
37
|
-
# Read
|
38
|
-
>>>
|
32
|
+
# Read all municipalities
|
33
|
+
>>> municipalities = read_municipality()
|
39
34
|
|
40
|
-
# Read all
|
41
|
-
>>>
|
42
|
-
>>> df = read_municipality(code_muni="AM", year=2000)
|
35
|
+
# Read all municipalities in a state by code
|
36
|
+
>>> state_municipalities = read_municipality(code_muni=33)
|
43
37
|
|
44
|
-
# Read all
|
45
|
-
>>>
|
38
|
+
# Read all municipalities in a state by abbreviation
|
39
|
+
>>> state_municipalities = read_municipality(code_muni="RJ")
|
40
|
+
|
41
|
+
# Read specific municipality by code
|
42
|
+
>>> municipality = read_municipality(code_muni=3304557)
|
46
43
|
"""
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
44
|
+
|
45
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2023/Brasil/BR_Municipios_2023.zip"
|
46
|
+
|
47
|
+
try:
|
48
|
+
# Download the zip file
|
49
|
+
response = requests.get(url)
|
50
|
+
if response.status_code != 200:
|
51
|
+
raise Exception("Failed to download municipality data from IBGE")
|
52
|
+
|
53
|
+
# Create a temporary directory
|
54
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
55
|
+
# Extract the zip file
|
56
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
57
|
+
zip_ref.extractall(temp_dir)
|
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
|
+
# Filter by code_muni if not "all"
|
75
|
+
if code_muni != "all":
|
76
|
+
if isinstance(code_muni, int) or code_muni.isdigit():
|
77
|
+
if len(str(code_muni)) == 7:
|
78
|
+
# Filter by municipality code
|
79
|
+
gdf = gdf[gdf['CD_MUN'] == str(code_muni)]
|
80
|
+
elif len(str(code_muni)) == 2:
|
81
|
+
# Filter by state code
|
82
|
+
gdf = gdf[gdf['CD_MUN'].str.startswith(str(code_muni).zfill(2))]
|
83
|
+
elif isinstance(code_muni, str) and len(code_muni) == 2:
|
84
|
+
# Filter by state abbreviation - need to get state code first
|
85
|
+
state_url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2023/Brasil/BR_UF_2023.zip"
|
86
|
+
state_response = requests.get(state_url)
|
87
|
+
|
88
|
+
if state_response.status_code == 200:
|
89
|
+
with tempfile.TemporaryDirectory() as state_temp_dir:
|
90
|
+
with ZipFile(BytesIO(state_response.content)) as zip_ref:
|
91
|
+
zip_ref.extractall(state_temp_dir)
|
92
|
+
|
93
|
+
state_shp_files = []
|
94
|
+
for root, dirs, files in os.walk(state_temp_dir):
|
95
|
+
state_shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
|
96
|
+
|
97
|
+
if state_shp_files:
|
98
|
+
state_gdf = gpd.read_file(state_shp_files[0])
|
99
|
+
state_code = state_gdf[state_gdf['SIGLA_UF'] == code_muni.upper()]['CD_UF'].values
|
100
|
+
|
101
|
+
if len(state_code) > 0:
|
102
|
+
gdf = gdf[gdf['CD_MUN'].str.startswith(state_code[0])]
|
103
|
+
|
104
|
+
if len(gdf) == 0:
|
105
|
+
raise Exception(f"No data found for code_muni={code_muni}")
|
106
|
+
|
107
|
+
if simplified:
|
108
|
+
# Keep only the most relevant columns
|
109
|
+
columns_to_keep = [
|
110
|
+
'geometry',
|
111
|
+
'CD_MUN', # Municipality code
|
112
|
+
'NM_MUN', # Municipality name
|
113
|
+
'SIGLA_UF', # State abbreviation
|
114
|
+
'AREA_KM2' # Area in square kilometers
|
115
|
+
]
|
116
|
+
|
117
|
+
# Filter columns that actually exist in the dataset
|
118
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
119
|
+
gdf = gdf[existing_columns]
|
120
|
+
|
121
|
+
except Exception as e:
|
122
|
+
raise Exception(f"Error downloading municipality data: {str(e)}")
|
123
|
+
|
83
124
|
return gdf
|
125
|
+
|
126
|
+
if __name__ == '__main__':
|
127
|
+
read_municipality()
|
tunned_geobr/read_state.py
CHANGED
@@ -1,88 +1,103 @@
|
|
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
|
-
def read_state(code_state="all", year=2010, simplified=False, verbose=False):
|
8
|
+
def read_state(code_state="all", simplified=False):
|
7
9
|
"""Download shapefiles of Brazilian states as geopandas objects.
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
This function downloads and processes state data directly from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
Data uses Geodetic reference system "SIRGAS2000" and CRS(4674).
|
13
|
+
|
11
14
|
Parameters
|
12
15
|
----------
|
13
16
|
code_state : str, optional
|
14
17
|
The two-digit code of a state or a two-letter uppercase abbreviation
|
15
18
|
(e.g. 33 or "RJ"). If code_state="all", all states will be loaded (Default).
|
16
|
-
|
17
|
-
|
18
|
-
simplified: boolean, by default True
|
19
|
-
Data 'type', indicating whether the function returns the 'original' dataset
|
20
|
-
with high resolution or a dataset with 'simplified' borders (Default)
|
21
|
-
verbose : bool, optional
|
22
|
-
by default False
|
19
|
+
simplified : boolean, by default True
|
20
|
+
If True, returns a simplified version of the dataset with fewer columns
|
23
21
|
|
24
22
|
Returns
|
25
23
|
-------
|
26
24
|
gpd.GeoDataFrame
|
27
|
-
|
28
|
-
|
29
|
-
Raises
|
30
|
-
------
|
31
|
-
Exception
|
32
|
-
If parameters are not found or not well defined
|
25
|
+
Geodataframe with state boundaries
|
33
26
|
|
34
27
|
Example
|
35
28
|
-------
|
36
|
-
>>> from
|
29
|
+
>>> from tunned_geobr import read_state
|
37
30
|
|
38
|
-
# Read
|
39
|
-
>>>
|
31
|
+
# Read all states
|
32
|
+
>>> states = read_state()
|
40
33
|
|
41
|
-
|
42
|
-
>>>
|
34
|
+
# Read specific state by code
|
35
|
+
>>> state = read_state(code_state=33)
|
43
36
|
|
44
|
-
|
45
|
-
>>>
|
37
|
+
# Read specific state by abbreviation
|
38
|
+
>>> state = read_state(code_state="RJ")
|
46
39
|
"""
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
40
|
+
|
41
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2023/Brasil/BR_UF_2023.zip"
|
42
|
+
|
43
|
+
try:
|
44
|
+
# Download the zip file
|
45
|
+
response = requests.get(url)
|
46
|
+
if response.status_code != 200:
|
47
|
+
raise Exception("Failed to download state data from IBGE")
|
48
|
+
|
49
|
+
# Create a temporary directory
|
50
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
51
|
+
# Extract the zip file
|
52
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
53
|
+
zip_ref.extractall(temp_dir)
|
54
|
+
|
55
|
+
# Find the shapefile
|
56
|
+
shp_files = []
|
57
|
+
for root, dirs, files in os.walk(temp_dir):
|
58
|
+
shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
|
59
|
+
|
60
|
+
if not shp_files:
|
61
|
+
raise Exception("No shapefile found in the downloaded data")
|
62
|
+
|
63
|
+
# Read the shapefile
|
64
|
+
gdf = gpd.read_file(shp_files[0])
|
65
|
+
|
66
|
+
# Convert to SIRGAS 2000 (EPSG:4674) if not already
|
67
|
+
if gdf.crs is None or gdf.crs.to_epsg() != 4674:
|
68
|
+
gdf = gdf.to_crs(4674)
|
69
|
+
|
70
|
+
# Filter by code_state if not "all"
|
71
|
+
if code_state != "all":
|
72
|
+
if isinstance(code_state, int) or code_state.isdigit():
|
73
|
+
# Filter by numeric code
|
74
|
+
code = str(code_state).zfill(2)
|
75
|
+
gdf = gdf[gdf['CD_UF'] == code]
|
76
|
+
elif isinstance(code_state, str) and len(code_state) == 2:
|
77
|
+
# Filter by state abbreviation
|
78
|
+
gdf = gdf[gdf['SIGLA_UF'] == code_state.upper()]
|
79
|
+
|
80
|
+
if len(gdf) == 0:
|
81
|
+
raise Exception(f"No data found for code_state={code_state}")
|
82
|
+
|
83
|
+
if simplified:
|
84
|
+
# Keep only the most relevant columns
|
85
|
+
columns_to_keep = [
|
86
|
+
'geometry',
|
87
|
+
'CD_UF', # State code
|
88
|
+
'SIGLA_UF', # State abbreviation
|
89
|
+
'NM_UF', # State name
|
90
|
+
'AREA_KM2' # Area in square kilometers
|
91
|
+
]
|
92
|
+
|
93
|
+
# Filter columns that actually exist in the dataset
|
94
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
95
|
+
gdf = gdf[existing_columns]
|
96
|
+
|
97
|
+
except Exception as e:
|
98
|
+
raise Exception(f"Error downloading state data: {str(e)}")
|
99
|
+
|
100
|
+
return gdf
|
101
|
+
|
102
|
+
if __name__ == '__main__':
|
103
|
+
read_state()
|
@@ -1,7 +1,7 @@
|
|
1
|
-
tunned_geobr-0.2.
|
2
|
-
tunned_geobr-0.2.
|
3
|
-
tunned_geobr-0.2.
|
4
|
-
tunned_geobr-0.2.
|
1
|
+
tunned_geobr-0.2.7.dist-info/METADATA,sha256=LxruVTULsN43uGIOC6L4L1K2Sv9qdt1yWb_-gnkvues,5018
|
2
|
+
tunned_geobr-0.2.7.dist-info/WHEEL,sha256=thaaA2w1JzcGC48WYufAs8nrYZjJm8LqNfnXFOFyCC4,90
|
3
|
+
tunned_geobr-0.2.7.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
|
4
|
+
tunned_geobr-0.2.7.dist-info/licenses/LICENSE.txt,sha256=mECZRcbde3HssOKe1Co4zgqBLGVN0OWpTsEy3LIbcRA,75
|
5
5
|
tunned_geobr/__init__.py,sha256=U3syU2lTvCoBLJSLcAPnn9cOc33DFqnGuGuABwguNgg,7309
|
6
6
|
tunned_geobr/data/grid_state_correspondence_table.csv,sha256=FpkBuX_-lRXQ1yBrQODxQgG9oha9Fd8A8zGKfdsDAmk,2660
|
7
7
|
tunned_geobr/list_geobr.py,sha256=bJJ5Vk25jejfmXDRnjt_QYxrIeO7gOOU8pLDvIBwC5U,16860
|
@@ -71,7 +71,7 @@ tunned_geobr/read_metro_area.py,sha256=e18jyXrRMwQTv_ZO2hGoyC8qZsV6NlYfWXsu6DusR
|
|
71
71
|
tunned_geobr/read_micro_region.py,sha256=61KbztQWYw-QPFLJOoxNWX32bHBKLb2pnunzSFo3S_0,2510
|
72
72
|
tunned_geobr/read_mining_processes.py,sha256=UmywViEDD9hx7qcDj9CMRHdPM69NQhsRB4870Y77QSs,2569
|
73
73
|
tunned_geobr/read_municipal_seat.py,sha256=9Vi-q1jzY8n086O-nNY1sVkVzV_NZbdzE5juosCcVZI,1142
|
74
|
-
tunned_geobr/read_municipality.py,sha256=
|
74
|
+
tunned_geobr/read_municipality.py,sha256=dZM1BVi3U9ZvasLADV-ciKVr9R4o92dRowpEVdVkvYw,5651
|
75
75
|
tunned_geobr/read_municipality_direct.py,sha256=VrZR_5__DsV5IbbX-sr56WT-P4M_tVdnmJp-QgdkmFg,5658
|
76
76
|
tunned_geobr/read_natural_caves.py,sha256=-XjoRxhT_yYy0fZu87S6RRUZ-cyaWPqWqOrd9Y8ERKo,3073
|
77
77
|
tunned_geobr/read_natural_gas_delivery_points.py,sha256=mKeywQ610Qw9ttY1_v-KclMIml3Tff3knhAAlBgAh0c,5309
|
@@ -122,7 +122,7 @@ tunned_geobr/read_semiarid.py,sha256=pxxYTWq8_UPUyblA7_FXXXRz-XOCrrebCvYQ-kgDSrU
|
|
122
122
|
tunned_geobr/read_settlements.py,sha256=C47Wj4DhSDa-pSFfYK4uGDwtu4sUwqPMr-CuuxS95xg,3060
|
123
123
|
tunned_geobr/read_sigef_properties.py,sha256=LZ69L6ev-7JT0chINKcgHZKl1ZpH6iLk6Je_HAxDnsQ,3204
|
124
124
|
tunned_geobr/read_snci_properties.py,sha256=lKhRSBeayD3M_ffljSf5_Sn57VhYh0g3lwFnOgpYji0,3226
|
125
|
-
tunned_geobr/read_state.py,sha256=
|
125
|
+
tunned_geobr/read_state.py,sha256=JgV3cR0LFbmwIzuzPbR_Zfy1bR_2eBeEPxunozctuag,3819
|
126
126
|
tunned_geobr/read_state_direct.py,sha256=8Tdz-gVH_t90BJngcfcpr0VLs5HfCUxRgRQj8hy4Bt0,3826
|
127
127
|
tunned_geobr/read_state_highways.py,sha256=pvRkwuensDOFh3wrcui36iTLcOtkrXoZmT50oUL8WFI,2769
|
128
128
|
tunned_geobr/read_statistical_grid.py,sha256=14fgzDrJtjDoOVzV8Qg8kkqruqiwCSwwRHVjct_w3bM,4479
|
@@ -135,4 +135,4 @@ tunned_geobr/read_water_bodies_ana.py,sha256=Z-dpTPVgRHVndTeSFxx8uXn7ufMg2jm0Dlz
|
|
135
135
|
tunned_geobr/read_waterways.py,sha256=mEdoVogYWr5EYZ8bE3xMCVWyLrHYU7xTL2lUE0XbDAM,2951
|
136
136
|
tunned_geobr/read_weighting_area.py,sha256=m2X5Ua3jRqLlkqCQbIzR2jmo58pzqkyR3UYcGtgy20E,2325
|
137
137
|
tunned_geobr/utils.py,sha256=WT9PSGWvcERjj3yhfTvyWSE5ZiEjO4tYK5xIj5jJCg8,8170
|
138
|
-
tunned_geobr-0.2.
|
138
|
+
tunned_geobr-0.2.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|