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.
@@ -1,83 +1,127 @@
1
- from geobr.utils import select_metadata, download_gpkg
2
-
3
-
4
- def read_municipality(code_muni="all", year=2010, simplified=False, verbose=False):
5
- """Download shape files of Brazilian municipalities as sf objects.
6
-
7
- Data at scale 1:250,000, using Geodetic reference system "SIRGAS2000" and CRS(4674)
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
- 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
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
- Metadata and geopackage of selected states
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 geobr import read_municipality
30
+ >>> from tunned_geobr import read_municipality
36
31
 
37
- # Read specific meso region at a given year
38
- >>> df = read_municipality(code_muni=1200179, year=2018)
32
+ # Read all municipalities
33
+ >>> municipalities = read_municipality()
39
34
 
40
- # Read all meso regions of a state at a given year
41
- >>> df = read_municipality(code_muni=12, year=2017)
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 meso regions of the country at a given year
45
- >>> df = read_municipality(code_muni="all", year=2010)
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
- metadata = select_metadata("municipality", year=year, simplified=simplified)
49
-
50
- if year < 1992:
51
-
52
- return download_gpkg(metadata)
53
-
54
- if code_muni == "all":
55
-
56
- if verbose:
57
- print("Loading data for the whole country. This might take a few minutes.")
58
-
59
- return download_gpkg(metadata)
60
-
61
- metadata = metadata[
62
- metadata[["code", "code_abbrev"]].apply(
63
- lambda x: str(code_muni)[:2] in str(x["code"])
64
- or str(code_muni)[:2] # if number e.g. 12
65
- in str(x["code_abbrev"]), # if UF e.g. RO
66
- 1,
67
- )
68
- ]
69
-
70
- if not len(metadata):
71
- raise Exception("Invalid Value to argument code_muni.")
72
-
73
- gdf = download_gpkg(metadata)
74
-
75
- if len(str(code_muni)) == 2:
76
- return gdf
77
-
78
- elif code_muni in gdf["code_muni"].tolist():
79
- return gdf.query(f"code_muni == {code_muni}")
80
-
81
- else:
82
- raise Exception("Invalid Value to argument code_muni.")
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()
@@ -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
- from geobr.utils import select_metadata, download_gpkg
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
- Data at scale 1:250,000, using Geodetic reference system "SIRGAS2000" and CRS(4674)
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
- year : int, optional
17
- Year of the data, by default 2010
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
- Metadata and geopackage of selected states
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 geobr import read_state
29
+ >>> from tunned_geobr import read_state
37
30
 
38
- # Read specific state at a given year
39
- >>> uf = read_state(code_state=12, year=2017)
31
+ # Read all states
32
+ >>> states = read_state()
40
33
 
41
- # Read specific state at a given year with normal geopackages
42
- >>> uf = read_state(code_state="SC", year=2000, tp='normal')
34
+ # Read specific state by code
35
+ >>> state = read_state(code_state=33)
43
36
 
44
- # Read all states at a given year
45
- >>> ufs = read_state(code_state="all", year=2010)
37
+ # Read specific state by abbreviation
38
+ >>> state = read_state(code_state="RJ")
46
39
  """
47
-
48
- metadata = select_metadata("state", year=year, simplified=simplified)
49
-
50
- if code_state is None:
51
- raise Exception("Value to argument 'code_state' cannot be None")
52
-
53
- # From 1872 to 1991 and all
54
- if (year < 1992) or (code_state == "all"):
55
-
56
- if verbose:
57
- print("Loading data for the whole country\n")
58
-
59
- return download_gpkg(metadata)
60
-
61
- # From 2000 onwards
62
- else:
63
-
64
- if (
65
- str(code_state)[0:2] not in metadata["code"].unique()
66
- and str(code_state)[0:2] not in metadata["code_abbrev"].unique()
67
- ):
68
-
69
- raise Exception("Error: Invalid Value to argument code_state.")
70
-
71
- else:
72
-
73
- if isinstance(code_state, int):
74
- metadata = metadata.query(f'code == "{str(code_state)[0:2]}"')
75
-
76
- if isinstance(code_state, str):
77
- metadata = metadata.query(f'code_abbrev == "{code_state[0:2]}"')
78
-
79
- gdf = download_gpkg(metadata)
80
-
81
- if len(str(code_state)) == 2:
82
- return gdf
83
-
84
- elif code_state in list(gdf["code_state"]):
85
- return gdf.query('code_state == "code_state"')
86
-
87
- else:
88
- raise Exception("Error: Invalid Value to argument code_state.")
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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tunned-geobr
3
- Version: 0.2.6
3
+ Version: 0.2.7
4
4
  Summary: Fork personalizado do geobr com funcionalidades extras como download de dados da ANM
5
5
  Author: Anderson Stolfi
6
6
  License: MIT
@@ -1,7 +1,7 @@
1
- tunned_geobr-0.2.6.dist-info/METADATA,sha256=pDrSN-1ydH0SBHMfAVrgIMEmMgSqlM3mOnzNbcxXFsU,5018
2
- tunned_geobr-0.2.6.dist-info/WHEEL,sha256=thaaA2w1JzcGC48WYufAs8nrYZjJm8LqNfnXFOFyCC4,90
3
- tunned_geobr-0.2.6.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
4
- tunned_geobr-0.2.6.dist-info/licenses/LICENSE.txt,sha256=mECZRcbde3HssOKe1Co4zgqBLGVN0OWpTsEy3LIbcRA,75
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=k3uOalCwO4R33sG9aUWojED1CezFIo5JD7kLTC-slic,2594
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=P5iLvT_j67al7KlDJo0Y9vys6X7rmI63Cr19dZ6eU2o,2699
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.6.dist-info/RECORD,,
138
+ tunned_geobr-0.2.7.dist-info/RECORD,,