tunned-geobr 0.1.2__py3-none-any.whl → 0.2.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.
Files changed (44) hide show
  1. tunned_geobr/__init__.py +34 -2
  2. tunned_geobr/list_geobr.py +112 -34
  3. tunned_geobr/read_apcb_amazon.py +78 -0
  4. tunned_geobr/read_apcb_caatinga.py +78 -0
  5. tunned_geobr/read_apcb_cerrado_pantanal.py +78 -0
  6. tunned_geobr/read_apcb_mata_atlantica.py +78 -0
  7. tunned_geobr/read_apcb_pampa.py +78 -0
  8. tunned_geobr/read_apcb_zcm.py +78 -0
  9. tunned_geobr/read_archaeological_sites.py +94 -0
  10. tunned_geobr/read_atlantic_forest_law_limits.py +74 -0
  11. tunned_geobr/read_baze_sites.py +155 -0
  12. tunned_geobr/read_biosphere_reserves.py +85 -0
  13. tunned_geobr/read_cave_potential.py +79 -0
  14. tunned_geobr/read_census_tract_2022.py +101 -0
  15. tunned_geobr/read_ebas.py +80 -0
  16. tunned_geobr/read_federal_highways.py +79 -0
  17. tunned_geobr/read_fossil_occurrences.py +94 -0
  18. tunned_geobr/read_geographic_regions.py +88 -0
  19. tunned_geobr/read_heliports.py +81 -0
  20. tunned_geobr/read_municipality_direct.py +127 -0
  21. tunned_geobr/read_natural_caves.py +83 -0
  22. tunned_geobr/read_neighborhoods_2022.py +99 -0
  23. tunned_geobr/read_pan_strategic_areas.py +89 -0
  24. tunned_geobr/read_ports.py +80 -0
  25. tunned_geobr/read_private_aerodromes.py +81 -0
  26. tunned_geobr/read_public_aerodromes.py +81 -0
  27. tunned_geobr/read_quilombola_areas.py +85 -0
  28. tunned_geobr/read_quilombola_areas_temp.py +103 -0
  29. tunned_geobr/read_railways.py +80 -0
  30. tunned_geobr/read_rppn.py +107 -0
  31. tunned_geobr/read_sigef_properties.py +83 -0
  32. tunned_geobr/read_snci_properties.py +83 -0
  33. tunned_geobr/read_state_direct.py +103 -0
  34. tunned_geobr/read_state_highways.py +79 -0
  35. tunned_geobr/read_transmission_lines_ons.py +87 -0
  36. tunned_geobr/read_vegetation.py +84 -0
  37. tunned_geobr/read_water_bodies_ana.py +87 -0
  38. tunned_geobr/read_waterways.py +80 -0
  39. {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.1.dist-info}/METADATA +35 -3
  40. tunned_geobr-0.2.1.dist-info/RECORD +82 -0
  41. tunned_geobr-0.1.2.dist-info/RECORD +0 -46
  42. {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.1.dist-info}/WHEEL +0 -0
  43. {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.1.dist-info}/entry_points.txt +0 -0
  44. {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.1.dist-info}/licenses/LICENSE.txt +0 -0
@@ -0,0 +1,80 @@
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_railways(simplified=False):
9
+ """Download Railways data from MapBiomas.
10
+
11
+ This function downloads and processes railways data from MapBiomas.
12
+ The data includes information about railway networks across Brazil.
13
+ Original source: MapBiomas
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 railways data
24
+
25
+ Example
26
+ -------
27
+ >>> from tunned_geobr import read_railways
28
+
29
+ # Read railways data
30
+ >>> railways = read_railways()
31
+ """
32
+
33
+ url = "https://brasil.mapbiomas.org/wp-content/uploads/sites/4/2023/08/ferrovia.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 MapBiomas")
40
+
41
+ # Create a temporary directory
42
+ with tempfile.TemporaryDirectory() as temp_dir:
43
+ # Extract the zip file
44
+ with ZipFile(BytesIO(response.content)) as zip_ref:
45
+ zip_ref.extractall(temp_dir)
46
+
47
+ # Find the shapefile
48
+ shp_files = []
49
+ for root, dirs, files in os.walk(temp_dir):
50
+ shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
51
+
52
+ if not shp_files:
53
+ raise Exception("No shapefile found in the downloaded data")
54
+
55
+ # Read the shapefile
56
+ gdf = gpd.read_file(shp_files[0])
57
+ gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
58
+
59
+ if simplified:
60
+ # Keep only the most relevant columns
61
+ columns_to_keep = [
62
+ 'geometry',
63
+ 'nome', # Railway name
64
+ 'uf', # State
65
+ 'operadora', # Operating company
66
+ 'situacao', # Status
67
+ 'extensao_km', # Length in km
68
+ 'bitola', # Track gauge
69
+ 'carga', # Cargo type
70
+ 'eletrifica' # Electrification
71
+ ]
72
+
73
+ # Filter columns that actually exist in the dataset
74
+ existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
75
+ gdf = gdf[existing_columns]
76
+
77
+ except Exception as e:
78
+ raise Exception(f"Error downloading railways data: {str(e)}")
79
+
80
+ return gdf
@@ -0,0 +1,107 @@
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_rppn(simplified=False):
9
+ """Download Private Natural Heritage Reserves (RPPN) data from ICMBio.
10
+
11
+ This function downloads and processes data about Private Natural Heritage Reserves
12
+ (Reservas Particulares do Patrimônio Natural - RPPN) in Brazil. RPPNs are private
13
+ conservation units created by landowners to protect natural areas on their properties.
14
+ Original source: ICMBio - Instituto Chico Mendes de Conservação da Biodiversidade
15
+
16
+ Parameters
17
+ ----------
18
+ simplified : boolean, by default False
19
+ If True, returns a simplified version of the dataset with fewer columns
20
+
21
+ Returns
22
+ -------
23
+ gpd.GeoDataFrame
24
+ Geodataframe with RPPN data
25
+ Columns:
26
+ - geometry: Geometry of the RPPN
27
+ - nome: RPPN name
28
+ - municipio: Municipality
29
+ - uf: State
30
+ - proprietar: Owner name
31
+ - area_ha: Area in hectares
32
+ - portaria: Ordinance number
33
+ - dt_criacao: Creation date
34
+ - bioma: Biome
35
+ - esfera: Administrative sphere (Federal/State)
36
+
37
+ Example
38
+ -------
39
+ >>> from tunned_geobr import read_rppn
40
+
41
+ # Read RPPN data
42
+ >>> rppn = read_rppn()
43
+ """
44
+
45
+ url = "https://sistemas.icmbio.gov.br/simrppn/publico/rppn/shp/"
46
+
47
+ # Headers para simular um navegador
48
+ headers = {
49
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
50
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
51
+ 'Accept-Language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
52
+ 'Accept-Encoding': 'gzip, deflate, br',
53
+ 'Connection': 'keep-alive',
54
+ 'Upgrade-Insecure-Requests': '1',
55
+ 'Sec-Fetch-Dest': 'document',
56
+ 'Sec-Fetch-Mode': 'navigate',
57
+ 'Sec-Fetch-Site': 'none',
58
+ 'Sec-Fetch-User': '?1',
59
+ 'Cache-Control': 'max-age=0'
60
+ }
61
+
62
+ try:
63
+ # Download the zip file
64
+ response = requests.get(url, headers=headers, timeout=30, allow_redirects=True)
65
+ print(f"Status: {response.status_code}")
66
+ print(f"Headers: {dict(response.headers)}")
67
+
68
+ if response.status_code != 200:
69
+ raise Exception(f"Failed to download data from ICMBio. Status code: {response.status_code}")
70
+
71
+ # Create a temporary directory
72
+ with tempfile.TemporaryDirectory() as temp_dir:
73
+ # Extract the zip file
74
+ with ZipFile(BytesIO(response.content)) as zip_ref:
75
+ zip_ref.extractall(temp_dir)
76
+
77
+ # Find the shapefile
78
+ shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
79
+ if not shp_files:
80
+ raise Exception("No shapefile found in the downloaded data")
81
+
82
+ # Read the shapefile
83
+ gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
84
+ gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
85
+
86
+ if simplified:
87
+ # Keep only the most relevant columns
88
+ columns_to_keep = [
89
+ 'geometry',
90
+ 'nome', # RPPN name
91
+ 'municipio', # Municipality
92
+ 'uf', # State
93
+ 'area_ha', # Area in hectares
94
+ 'bioma' # Biome
95
+ ]
96
+
97
+ # Filter columns that actually exist in the dataset
98
+ existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
99
+ gdf = gdf[existing_columns]
100
+
101
+ except Exception as e:
102
+ raise Exception(f"Error downloading RPPN data: {str(e)}")
103
+
104
+ return gdf
105
+
106
+ if __name__ == '__main__':
107
+ read_rppn()
@@ -0,0 +1,83 @@
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_sigef_properties(simplified=False):
9
+ """Download Certified Properties data from INCRA's SNCI.
10
+
11
+ This function downloads and processes certified properties data from INCRA's
12
+ National Property Certification System (Sistema Nacional de Certificação de Imóveis - SNCI).
13
+ The data includes information about certified rural properties across Brazil.
14
+ Original source: INCRA (Instituto Nacional de Colonização e Reforma Agrária)
15
+
16
+ Parameters
17
+ ----------
18
+ simplified : boolean, by default False
19
+ If True, returns a simplified version of the dataset with fewer columns
20
+
21
+ Returns
22
+ -------
23
+ gpd.GeoDataFrame
24
+ Geodataframe with certified properties data
25
+
26
+ Example
27
+ -------
28
+ >>> from tunned_geobr import read_snci_properties
29
+
30
+ # Read certified properties data
31
+ >>> properties = read_snci_properties()
32
+ """
33
+
34
+ url = "https://certificacao.incra.gov.br/csv_shp/zip/Sigef%20Brasil.zip"
35
+ if 1==1:
36
+ return "Essa camada está muito pesada, baixe manualmente no link: " + url
37
+ try:
38
+ # Download the zip file
39
+ # Disable SSL verification due to INCRA's certificate issues
40
+ import urllib3
41
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
42
+
43
+ response = requests.get(url, verify=False)
44
+ if response.status_code != 200:
45
+ raise Exception("Failed to download data from INCRA")
46
+
47
+ # Create a temporary directory
48
+ with tempfile.TemporaryDirectory() as temp_dir:
49
+ # Extract the zip file
50
+ with ZipFile(BytesIO(response.content)) as zip_ref:
51
+ zip_ref.extractall(temp_dir)
52
+
53
+ # Find the shapefile
54
+ shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
55
+ if not shp_files:
56
+ raise Exception("No shapefile found in the downloaded data")
57
+
58
+ # Read the shapefile
59
+ gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
60
+ gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
61
+
62
+ if simplified:
63
+ # Keep only the most relevant columns
64
+ columns_to_keep = [
65
+ 'geometry',
66
+ 'parcela', # Property ID
67
+ 'municipio', # Municipality
68
+ 'uf', # State
69
+ 'area_ha', # Area in hectares
70
+ 'status', # Certification status
71
+ 'data_cert', # Certification date
72
+ 'cod_imovel', # Property code
73
+ 'nome_imov' # Property name
74
+ ]
75
+
76
+ # Filter columns that actually exist in the dataset
77
+ existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
78
+ gdf = gdf[existing_columns]
79
+
80
+ except Exception as e:
81
+ raise Exception(f"Error downloading certified properties data: {str(e)}")
82
+
83
+ return gdf
@@ -0,0 +1,83 @@
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_snci_properties(simplified=False):
9
+ """Download Certified Properties data from INCRA's SNCI.
10
+
11
+ This function downloads and processes certified properties data from INCRA's
12
+ National Property Certification System (Sistema Nacional de Certificação de Imóveis - SNCI).
13
+ The data includes information about certified rural properties across Brazil.
14
+ Original source: INCRA (Instituto Nacional de Colonização e Reforma Agrária)
15
+
16
+ Parameters
17
+ ----------
18
+ simplified : boolean, by default False
19
+ If True, returns a simplified version of the dataset with fewer columns
20
+
21
+ Returns
22
+ -------
23
+ gpd.GeoDataFrame
24
+ Geodataframe with certified properties data
25
+
26
+ Example
27
+ -------
28
+ >>> from tunned_geobr import read_snci_properties
29
+
30
+ # Read certified properties data
31
+ >>> properties = read_snci_properties()
32
+ """
33
+
34
+ url = "https://certificacao.incra.gov.br/csv_shp/zip/Imóvel%20certificado%20SNCI%20Brasil.zip"
35
+ if 1==1:
36
+ return "Essa camada está muito pesada, baixe manualmente no link: " + url
37
+ try:
38
+ # Download the zip file
39
+ # Disable SSL verification due to INCRA's certificate issues
40
+ import urllib3
41
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
42
+
43
+ response = requests.get(url, verify=False)
44
+ if response.status_code != 200:
45
+ raise Exception("Failed to download data from INCRA")
46
+
47
+ # Create a temporary directory
48
+ with tempfile.TemporaryDirectory() as temp_dir:
49
+ # Extract the zip file
50
+ with ZipFile(BytesIO(response.content)) as zip_ref:
51
+ zip_ref.extractall(temp_dir)
52
+
53
+ # Find the shapefile
54
+ shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
55
+ if not shp_files:
56
+ raise Exception("No shapefile found in the downloaded data")
57
+
58
+ # Read the shapefile
59
+ gdf = gpd.read_file(os.path.join(temp_dir, shp_files[0]))
60
+ gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
61
+
62
+ if simplified:
63
+ # Keep only the most relevant columns
64
+ columns_to_keep = [
65
+ 'geometry',
66
+ 'parcela', # Property ID
67
+ 'municipio', # Municipality
68
+ 'uf', # State
69
+ 'area_ha', # Area in hectares
70
+ 'status', # Certification status
71
+ 'data_cert', # Certification date
72
+ 'cod_imovel', # Property code
73
+ 'nome_imov' # Property name
74
+ ]
75
+
76
+ # Filter columns that actually exist in the dataset
77
+ existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
78
+ gdf = gdf[existing_columns]
79
+
80
+ except Exception as e:
81
+ raise Exception(f"Error downloading certified properties data: {str(e)}")
82
+
83
+ return gdf
@@ -0,0 +1,103 @@
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_state(code_state="all", simplified=True):
9
+ """Download shapefiles of Brazilian states as geopandas objects.
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
+
14
+ Parameters
15
+ ----------
16
+ code_state : str, optional
17
+ The two-digit code of a state or a two-letter uppercase abbreviation
18
+ (e.g. 33 or "RJ"). If code_state="all", all states will be loaded (Default).
19
+ simplified : boolean, by default True
20
+ If True, returns a simplified version of the dataset with fewer columns
21
+
22
+ Returns
23
+ -------
24
+ gpd.GeoDataFrame
25
+ Geodataframe with state boundaries
26
+
27
+ Example
28
+ -------
29
+ >>> from tunned_geobr import read_state
30
+
31
+ # Read all states
32
+ >>> states = read_state()
33
+
34
+ # Read specific state by code
35
+ >>> state = read_state(code_state=33)
36
+
37
+ # Read specific state by abbreviation
38
+ >>> state = read_state(code_state="RJ")
39
+ """
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()
@@ -0,0 +1,79 @@
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_state_highways(simplified=False):
9
+ """Download State Highways data from MapBiomas.
10
+
11
+ This function downloads and processes state highways data from MapBiomas.
12
+ The data includes information about state-managed highways across Brazil.
13
+ Original source: MapBiomas
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 state highways data
24
+
25
+ Example
26
+ -------
27
+ >>> from tunned_geobr import read_state_highways
28
+
29
+ # Read state highways data
30
+ >>> highways = read_state_highways()
31
+ """
32
+
33
+ url = "https://brasil.mapbiomas.org/wp-content/uploads/sites/4/2023/08/rodovia-estadual.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 MapBiomas")
40
+
41
+ # Create a temporary directory
42
+ with tempfile.TemporaryDirectory() as temp_dir:
43
+ # Extract the zip file
44
+ with ZipFile(BytesIO(response.content)) as zip_ref:
45
+ zip_ref.extractall(temp_dir)
46
+
47
+ # Find the shapefile
48
+ shp_files = []
49
+ for root, dirs, files in os.walk(temp_dir):
50
+ shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
51
+
52
+ if not shp_files:
53
+ raise Exception("No shapefile found in the downloaded data")
54
+
55
+ # Read the shapefile
56
+ gdf = gpd.read_file(shp_files[0])
57
+ gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
58
+
59
+ if simplified:
60
+ # Keep only the most relevant columns
61
+ columns_to_keep = [
62
+ 'geometry',
63
+ 'sigla', # Highway code
64
+ 'uf', # State
65
+ 'jurisdicao', # Jurisdiction
66
+ 'superficie', # Surface type
67
+ 'situacao', # Status
68
+ 'extensao_km', # Length in km
69
+ 'tipo_trecho' # Section type
70
+ ]
71
+
72
+ # Filter columns that actually exist in the dataset
73
+ existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
74
+ gdf = gdf[existing_columns]
75
+
76
+ except Exception as e:
77
+ raise Exception(f"Error downloading state highways data: {str(e)}")
78
+
79
+ return gdf
@@ -0,0 +1,87 @@
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_transmission_lines_ons(simplified=False):
9
+ """Download Brazilian Transmission Lines data from ONS.
10
+
11
+ This function downloads and processes the Brazilian Transmission Lines data
12
+ from ONS (National Electric System Operator).
13
+ Original source: ONS - Operador Nacional do Sistema Elétrico
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 Brazilian transmission lines data
24
+
25
+ Example
26
+ -------
27
+ >>> from tunned_geobr import read_transmission_lines_ons
28
+
29
+ # Read transmission lines data
30
+ >>> transmission_lines = read_transmission_lines_ons()
31
+ """
32
+
33
+ # The URL provided is a blob URL which might be temporary
34
+ # This is the permanent URL to the ONS data portal
35
+ url = "https://sig.ons.org.br/download/LT_SIN.zip"
36
+
37
+ try:
38
+ # Download the zip file
39
+ response = requests.get(url)
40
+ if response.status_code != 200:
41
+ raise Exception("Failed to download transmission lines data from ONS")
42
+
43
+ # Create a temporary directory
44
+ with tempfile.TemporaryDirectory() as temp_dir:
45
+ # Extract the zip file
46
+ with ZipFile(BytesIO(response.content)) as zip_ref:
47
+ zip_ref.extractall(temp_dir)
48
+
49
+ # Find the shapefile
50
+ shp_files = []
51
+ for root, dirs, files in os.walk(temp_dir):
52
+ shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
53
+
54
+ if not shp_files:
55
+ raise Exception("No shapefile found in the downloaded data")
56
+
57
+ # Read the shapefile
58
+ gdf = gpd.read_file(shp_files[0])
59
+
60
+ # Convert to SIRGAS 2000 (EPSG:4674) if not already
61
+ if gdf.crs is None or gdf.crs.to_epsg() != 4674:
62
+ gdf = gdf.to_crs(4674)
63
+
64
+ if simplified:
65
+ # Keep only the most relevant columns
66
+ # Note: Column names may need adjustment based on actual data
67
+ columns_to_keep = [
68
+ 'geometry',
69
+ 'NOME', # Line name
70
+ 'TENSAO', # Voltage
71
+ 'EXTENSAO', # Length
72
+ 'CIRCUITO', # Circuit
73
+ 'PROPRIETAR', # Owner
74
+ 'STATUS' # Status
75
+ ]
76
+
77
+ # Filter columns that actually exist in the dataset
78
+ existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
79
+ gdf = gdf[existing_columns]
80
+
81
+ except Exception as e:
82
+ raise Exception(f"Error downloading transmission lines data: {str(e)}")
83
+
84
+ return gdf
85
+
86
+ if __name__ == '__main__':
87
+ read_transmission_lines_ons()