tunned-geobr 0.1.2__py3-none-any.whl → 0.2.0__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 +34 -2
- tunned_geobr/read_apcb_amazon.py +78 -0
- tunned_geobr/read_apcb_caatinga.py +78 -0
- tunned_geobr/read_apcb_cerrado_pantanal.py +78 -0
- tunned_geobr/read_apcb_mata_atlantica.py +78 -0
- tunned_geobr/read_apcb_pampa.py +78 -0
- tunned_geobr/read_apcb_zcm.py +78 -0
- tunned_geobr/read_archaeological_sites.py +94 -0
- tunned_geobr/read_atlantic_forest_law_limits.py +74 -0
- tunned_geobr/read_baze_sites.py +155 -0
- tunned_geobr/read_biosphere_reserves.py +85 -0
- tunned_geobr/read_cave_potential.py +79 -0
- tunned_geobr/read_census_tract_2022.py +101 -0
- tunned_geobr/read_ebas.py +80 -0
- tunned_geobr/read_federal_highways.py +79 -0
- tunned_geobr/read_fossil_occurrences.py +94 -0
- tunned_geobr/read_geographic_regions.py +88 -0
- tunned_geobr/read_heliports.py +81 -0
- tunned_geobr/read_municipality_direct.py +127 -0
- tunned_geobr/read_natural_caves.py +83 -0
- tunned_geobr/read_neighborhoods_2022.py +99 -0
- tunned_geobr/read_pan_strategic_areas.py +89 -0
- tunned_geobr/read_ports.py +80 -0
- tunned_geobr/read_private_aerodromes.py +81 -0
- tunned_geobr/read_public_aerodromes.py +81 -0
- tunned_geobr/read_quilombola_areas.py +85 -0
- tunned_geobr/read_quilombola_areas_temp.py +103 -0
- tunned_geobr/read_railways.py +80 -0
- tunned_geobr/read_rppn.py +107 -0
- tunned_geobr/read_sigef_properties.py +83 -0
- tunned_geobr/read_snci_properties.py +83 -0
- tunned_geobr/read_state_direct.py +103 -0
- tunned_geobr/read_state_highways.py +79 -0
- tunned_geobr/read_transmission_lines_ons.py +87 -0
- tunned_geobr/read_vegetation.py +84 -0
- tunned_geobr/read_water_bodies_ana.py +87 -0
- tunned_geobr/read_waterways.py +80 -0
- {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.0.dist-info}/METADATA +35 -3
- tunned_geobr-0.2.0.dist-info/RECORD +82 -0
- tunned_geobr-0.1.2.dist-info/RECORD +0 -46
- {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.0.dist-info}/WHEEL +0 -0
- {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.0.dist-info}/entry_points.txt +0 -0
- {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.0.dist-info}/licenses/LICENSE.txt +0 -0
@@ -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()
|
@@ -0,0 +1,84 @@
|
|
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_vegetation(simplified=False):
|
9
|
+
"""Download Brazilian Vegetation data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes the Brazilian Vegetation data at 1:250,000 scale
|
12
|
+
from IBGE (Brazilian Institute of Geography and Statistics).
|
13
|
+
Original source: IBGE - Instituto Brasileiro de Geografia e Estatística
|
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 vegetation data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_vegetation
|
28
|
+
|
29
|
+
# Read vegetation data
|
30
|
+
>>> vegetation = read_vegetation()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/informacoes_ambientais/vegetacao/vetores/escala_250_mil/versao_2023/vege_area.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 vegetation data from IBGE")
|
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
|
+
|
58
|
+
# Convert to SIRGAS 2000 (EPSG:4674) if not already
|
59
|
+
if gdf.crs is None or gdf.crs.to_epsg() != 4674:
|
60
|
+
gdf = gdf.to_crs(4674)
|
61
|
+
|
62
|
+
if simplified:
|
63
|
+
# Keep only the most relevant columns
|
64
|
+
# Note: Column names may need adjustment based on actual data
|
65
|
+
columns_to_keep = [
|
66
|
+
'geometry',
|
67
|
+
'NOME', # Vegetation name
|
68
|
+
'TIPO', # Vegetation type
|
69
|
+
'REGIAO', # Region
|
70
|
+
'BIOMA', # Biome
|
71
|
+
'AREA_KM2' # Area in square kilometers
|
72
|
+
]
|
73
|
+
|
74
|
+
# Filter columns that actually exist in the dataset
|
75
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
76
|
+
gdf = gdf[existing_columns]
|
77
|
+
|
78
|
+
except Exception as e:
|
79
|
+
raise Exception(f"Error downloading vegetation data: {str(e)}")
|
80
|
+
|
81
|
+
return gdf
|
82
|
+
|
83
|
+
if __name__ == '__main__':
|
84
|
+
read_vegetation()
|
@@ -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_water_bodies_ana(simplified=False):
|
9
|
+
"""Download Brazilian Water Bodies data from ANA.
|
10
|
+
|
11
|
+
This function downloads and processes the Brazilian Water Bodies data
|
12
|
+
from ANA (National Water Agency). The data includes lakes, reservoirs, and other water bodies.
|
13
|
+
Original source: ANA - Agência Nacional de Águas e Saneamento Básico
|
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 water bodies data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_water_bodies_ana
|
28
|
+
|
29
|
+
# Read water bodies data
|
30
|
+
>>> water_bodies = read_water_bodies_ana()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://metadados.snirh.gov.br/files/7d054e5a-8cc9-403c-9f1a-085fd933610c/geoft_bho_massa_dagua_v2019.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 water bodies data from ANA")
|
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
|
+
|
58
|
+
# Convert to SIRGAS 2000 (EPSG:4674) if not already
|
59
|
+
if gdf.crs is None or gdf.crs.to_epsg() != 4674:
|
60
|
+
gdf = gdf.to_crs(4674)
|
61
|
+
|
62
|
+
if simplified:
|
63
|
+
# Keep only the most relevant columns
|
64
|
+
# Note: Column names may need adjustment based on actual data
|
65
|
+
columns_to_keep = [
|
66
|
+
'geometry',
|
67
|
+
'nome', # Water body name
|
68
|
+
'tipo', # Type of water body
|
69
|
+
'area_km2', # Area in square kilometers
|
70
|
+
'cocursodag', # Water course code
|
71
|
+
'cobacia', # Basin code
|
72
|
+
'nuareacont', # Contribution area
|
73
|
+
'nuvolumehm', # Volume in cubic hectometers
|
74
|
+
'dsoperacao' # Operation 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 water bodies data: {str(e)}")
|
83
|
+
|
84
|
+
return gdf
|
85
|
+
|
86
|
+
if __name__ == '__main__':
|
87
|
+
read_water_bodies_ana()
|
@@ -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_waterways(simplified=False):
|
9
|
+
"""Download Waterways data from SNIRH.
|
10
|
+
|
11
|
+
This function downloads and processes waterways data from SNIRH (National Water Resources Information System).
|
12
|
+
The data includes information about navigable waterways across Brazil.
|
13
|
+
Original source: SNIRH (Sistema Nacional de Informações sobre Recursos Hídricos)
|
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 waterways data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_waterways
|
28
|
+
|
29
|
+
# Read waterways data
|
30
|
+
>>> waterways = read_waterways()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://metadados.snirh.gov.br/geonetwork/srv/api/records/48e26e99-db01-45dc-a270-79f27680167b/attachments/GEOFT_TRECHO_HIDROVIARIO.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 SNIRH")
|
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', # Waterway name
|
64
|
+
'hidrovia', # Waterway system
|
65
|
+
'rio', # River name
|
66
|
+
'situacao', # Status
|
67
|
+
'extensao_km', # Length in km
|
68
|
+
'administra', # Administration
|
69
|
+
'regime', # Water regime
|
70
|
+
'classifica' # Classification
|
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 waterways data: {str(e)}")
|
79
|
+
|
80
|
+
return gdf
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: tunned-geobr
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.2.0
|
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
|
@@ -20,6 +20,8 @@ Requires-Dist: lxml<6.0.0,>=5.1.0
|
|
20
20
|
Requires-Dist: html5lib==1.1
|
21
21
|
Requires-Dist: geobr<0.3.0,>=0.2.2
|
22
22
|
Requires-Dist: patool>=1.15.0
|
23
|
+
Requires-Dist: fiona>=1.10.1
|
24
|
+
Requires-Dist: gdown>=5.2.0
|
23
25
|
Description-Content-Type: text/markdown
|
24
26
|
|
25
27
|
# geobr: Download Official Spatial Data Sets of Brazil
|
@@ -59,8 +61,40 @@ It adds:
|
|
59
61
|
|
60
62
|
! Be aware that if the function that you are adding is more complicated than the template. So, always double check !
|
61
63
|
|
64
|
+
## System Dependencies
|
62
65
|
|
66
|
+
Some functions in geobr require additional system tools to be installed:
|
63
67
|
|
68
|
+
### For RAR file extraction (`read_baze_sites`)
|
69
|
+
|
70
|
+
This function requires one of the following tools to be installed:
|
71
|
+
|
72
|
+
- **unrar**:
|
73
|
+
- macOS: `brew install unrar`
|
74
|
+
- Ubuntu/Debian: `sudo apt-get install unrar`
|
75
|
+
- Windows: Install WinRAR
|
76
|
+
|
77
|
+
- **unar**:
|
78
|
+
- macOS: `brew install unar`
|
79
|
+
- Ubuntu/Debian: `sudo apt-get install unar`
|
80
|
+
- Windows: Install The Unarchiver
|
81
|
+
|
82
|
+
- **7-Zip**:
|
83
|
+
- macOS: `brew install p7zip`
|
84
|
+
- Ubuntu/Debian: `sudo apt-get install p7zip-full`
|
85
|
+
- Windows: Install 7-Zip
|
86
|
+
|
87
|
+
### For ZIP file extraction (IBGE files)
|
88
|
+
|
89
|
+
Some IBGE files use compression methods not supported by Python's built-in zipfile module. The following functions use the system's `unzip` command:
|
90
|
+
|
91
|
+
- `read_census_tract_2022`
|
92
|
+
- `read_neighborhoods_2022`
|
93
|
+
|
94
|
+
Make sure you have the `unzip` command available on your system:
|
95
|
+
- macOS: Typically pre-installed
|
96
|
+
- Ubuntu/Debian: `sudo apt-get install unzip`
|
97
|
+
- Windows: Install a tool like 7-Zip or add unzip via WSL
|
64
98
|
|
65
99
|
## Translation Status
|
66
100
|
|
@@ -94,10 +128,8 @@ It adds:
|
|
94
128
|
| lookup_muni | Yes | No |
|
95
129
|
| read_neighborhood | Yes | Yes |
|
96
130
|
|
97
|
-
|
98
131
|
# Release new version
|
99
132
|
|
100
133
|
```
|
101
134
|
poetry version [patch|minor|major]
|
102
135
|
poetry publish --build
|
103
|
-
```
|