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.
- tunned_geobr/__init__.py +34 -2
- tunned_geobr/list_geobr.py +112 -34
- 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.1.dist-info}/METADATA +35 -3
- tunned_geobr-0.2.1.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.1.dist-info}/WHEEL +0 -0
- {tunned_geobr-0.1.2.dist-info → tunned_geobr-0.2.1.dist-info}/entry_points.txt +0 -0
- {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_ebas(simplified=False):
|
9
|
+
"""Download Endemic Bird Areas (EBAs) data.
|
10
|
+
|
11
|
+
This function downloads and processes Endemic Bird Areas (EBAs) data. EBAs are
|
12
|
+
regions of the world that contain concentrations of bird species found nowhere else.
|
13
|
+
Original source: Global Forest Watch
|
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 Endemic Bird Areas data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_ebas
|
28
|
+
|
29
|
+
# Read Endemic Bird Areas data
|
30
|
+
>>> ebas = read_ebas()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "http://gfw2-data.s3.amazonaws.com/conservation/zip/endemic_bird_areas.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 Endemic Bird Areas data")
|
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 = [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
|
+
# Convert to SIRGAS 2000 (EPSG:4674)
|
56
|
+
gdf = gdf.to_crs(4674)
|
57
|
+
|
58
|
+
if simplified:
|
59
|
+
# Keep only the most relevant columns
|
60
|
+
columns_to_keep = [
|
61
|
+
'geometry',
|
62
|
+
'EBA_Name', # Endemic Bird Area name
|
63
|
+
'EBA_ID', # Endemic Bird Area ID
|
64
|
+
'Area_km2', # Area in square kilometers
|
65
|
+
'Priority', # Conservation priority
|
66
|
+
'Region', # Geographic region
|
67
|
+
'Country' # Country
|
68
|
+
]
|
69
|
+
|
70
|
+
# Filter columns that actually exist in the dataset
|
71
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
72
|
+
gdf = gdf[existing_columns]
|
73
|
+
|
74
|
+
except Exception as e:
|
75
|
+
raise Exception(f"Error downloading Endemic Bird Areas data: {str(e)}")
|
76
|
+
|
77
|
+
return gdf
|
78
|
+
|
79
|
+
if __name__ == '__main__':
|
80
|
+
read_ebas()
|
@@ -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_federal_highways(simplified=False):
|
9
|
+
"""Download Federal Highways data from MapBiomas.
|
10
|
+
|
11
|
+
This function downloads and processes federal highways data from MapBiomas.
|
12
|
+
The data includes information about federally-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 federal highways data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_federal_highways
|
28
|
+
|
29
|
+
# Read federal highways data
|
30
|
+
>>> highways = read_federal_highways()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://brasil.mapbiomas.org/wp-content/uploads/sites/4/2023/08/rodovia-federal.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 (BR-XXX)
|
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 federal highways data: {str(e)}")
|
78
|
+
|
79
|
+
return gdf
|
@@ -0,0 +1,94 @@
|
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
import fiona
|
6
|
+
from zipfile import ZipFile
|
7
|
+
from io import BytesIO
|
8
|
+
|
9
|
+
def read_fossil_occurrences(simplified=False):
|
10
|
+
"""Download Fossil Occurrences data from SGB.
|
11
|
+
|
12
|
+
This function downloads and processes data about fossil occurrences in Brazil
|
13
|
+
from SGB (Serviço Geológico do Brasil). The data comes from a File Geodatabase (.gdb)
|
14
|
+
and includes information about fossil sites across the country.
|
15
|
+
Original source: SGB - Serviço Geológico do Brasil
|
16
|
+
|
17
|
+
Parameters
|
18
|
+
----------
|
19
|
+
simplified : boolean, by default False
|
20
|
+
If True, returns a simplified version of the dataset with fewer columns
|
21
|
+
|
22
|
+
Returns
|
23
|
+
-------
|
24
|
+
gpd.GeoDataFrame
|
25
|
+
Geodataframe with fossil occurrences data
|
26
|
+
|
27
|
+
Example
|
28
|
+
-------
|
29
|
+
>>> from tunned_geobr import read_fossil_occurrences
|
30
|
+
|
31
|
+
# Read fossil occurrences data
|
32
|
+
>>> fossils = read_fossil_occurrences()
|
33
|
+
"""
|
34
|
+
|
35
|
+
url = "https://geoportal.sgb.gov.br/downloads/paleo.gdb.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 data from SGB")
|
42
|
+
|
43
|
+
# Create a temporary directory
|
44
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
45
|
+
# Save the .gdb.zip file
|
46
|
+
gdb_zip = os.path.join(temp_dir, "paleo.gdb.zip")
|
47
|
+
with open(gdb_zip, 'wb') as f:
|
48
|
+
f.write(response.content)
|
49
|
+
|
50
|
+
# Create the .gdb directory
|
51
|
+
gdb_path = os.path.join(temp_dir, "paleo.gdb")
|
52
|
+
os.makedirs(gdb_path, exist_ok=True)
|
53
|
+
|
54
|
+
# Extract the .gdb.zip file directly into the .gdb directory
|
55
|
+
with ZipFile(gdb_zip) as zip_ref:
|
56
|
+
zip_ref.extractall(gdb_path)
|
57
|
+
|
58
|
+
# List all layers in the GDB
|
59
|
+
layers = fiona.listlayers(gdb_path)
|
60
|
+
if not layers:
|
61
|
+
raise Exception("No layers found in the GDB")
|
62
|
+
|
63
|
+
# Read the first layer (assuming it's the fossil occurrences)
|
64
|
+
gdf = gpd.read_file(gdb_path, layer=layers[0])
|
65
|
+
gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
|
66
|
+
|
67
|
+
if simplified:
|
68
|
+
# Keep only the most relevant columns
|
69
|
+
columns_to_keep = [
|
70
|
+
'geometry',
|
71
|
+
'LOCALIDADE', # Locality name
|
72
|
+
'DISTRITO', # District
|
73
|
+
'UNIDADE_LITOESTRATIGRAFICA', # Lithostratigraphic unit
|
74
|
+
'UNIDADE_CRONOESTRATIGRAFICA', # Chronostratigraphic unit
|
75
|
+
'LITOLOGIA', # Lithology
|
76
|
+
'VESTIGIOS_ORGANICOS', # Organic traces
|
77
|
+
'AMBIENTE_DEPOSICAO', # Depositional environment
|
78
|
+
'TAXON', # Taxon
|
79
|
+
'SISTEMATICA', # Systematics
|
80
|
+
'MATERIAL', # Material
|
81
|
+
'REFERENCIA_BIBLIOGRAFICA' # Bibliographic reference
|
82
|
+
]
|
83
|
+
|
84
|
+
# Filter columns that actually exist in the dataset
|
85
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
86
|
+
gdf = gdf[existing_columns]
|
87
|
+
|
88
|
+
except Exception as e:
|
89
|
+
raise Exception(f"Error downloading fossil occurrences data: {str(e)}")
|
90
|
+
|
91
|
+
return gdf
|
92
|
+
|
93
|
+
if __name__ == '__main__':
|
94
|
+
read_fossil_occurrences()
|
@@ -0,0 +1,88 @@
|
|
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_geographic_regions(simplified=False):
|
9
|
+
"""Download Brazilian Geographic Regions data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes the Brazilian Geographic Regions data
|
12
|
+
from IBGE (Brazilian Institute of Geography and Statistics). The data includes
|
13
|
+
the official geographic regions division of Brazil from 2017.
|
14
|
+
Original source: IBGE - Instituto Brasileiro de Geografia e Estatística
|
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 Brazilian geographic regions data
|
25
|
+
|
26
|
+
Example
|
27
|
+
-------
|
28
|
+
>>> from tunned_geobr import read_geographic_regions
|
29
|
+
|
30
|
+
# Read geographic regions data
|
31
|
+
>>> regions = read_geographic_regions()
|
32
|
+
"""
|
33
|
+
|
34
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/divisao_regional/divisao_regional_do_brasil/divisao_regional_do_brasil_em_regioes_geograficas_2017/shp/RG2017_regioesgeograficas2017_20180911.zip"
|
35
|
+
|
36
|
+
try:
|
37
|
+
# Download the zip file
|
38
|
+
response = requests.get(url)
|
39
|
+
if response.status_code != 200:
|
40
|
+
raise Exception("Failed to download geographic regions data from IBGE")
|
41
|
+
|
42
|
+
# Create a temporary directory
|
43
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
44
|
+
# Extract the zip file
|
45
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
46
|
+
zip_ref.extractall(temp_dir)
|
47
|
+
|
48
|
+
# Find the shapefile
|
49
|
+
shp_files = []
|
50
|
+
for root, dirs, files in os.walk(temp_dir):
|
51
|
+
shp_files.extend([os.path.join(root, f) for f in files if f.endswith('.shp')])
|
52
|
+
|
53
|
+
if not shp_files:
|
54
|
+
raise Exception("No shapefile found in the downloaded data")
|
55
|
+
|
56
|
+
# Read the shapefile
|
57
|
+
gdf = gpd.read_file(shp_files[0])
|
58
|
+
|
59
|
+
# Convert to SIRGAS 2000 (EPSG:4674) if not already
|
60
|
+
if gdf.crs is None or gdf.crs.to_epsg() != 4674:
|
61
|
+
gdf = gdf.to_crs(4674)
|
62
|
+
|
63
|
+
if simplified:
|
64
|
+
# Keep only the most relevant columns
|
65
|
+
# Note: Column names may need adjustment based on actual data
|
66
|
+
columns_to_keep = [
|
67
|
+
'geometry',
|
68
|
+
'CD_RGGI', # Immediate Geographic Region Code
|
69
|
+
'NM_RGGI', # Immediate Geographic Region Name
|
70
|
+
'CD_RGINT', # Intermediate Geographic Region Code
|
71
|
+
'NM_RGINT', # Intermediate Geographic Region Name
|
72
|
+
'CD_UF', # State Code
|
73
|
+
'NM_UF', # State Name
|
74
|
+
'SIGLA_UF', # State Abbreviation
|
75
|
+
'AREA_KM2' # Area in square kilometers
|
76
|
+
]
|
77
|
+
|
78
|
+
# Filter columns that actually exist in the dataset
|
79
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
80
|
+
gdf = gdf[existing_columns]
|
81
|
+
|
82
|
+
except Exception as e:
|
83
|
+
raise Exception(f"Error downloading geographic regions data: {str(e)}")
|
84
|
+
|
85
|
+
return gdf
|
86
|
+
|
87
|
+
if __name__ == '__main__':
|
88
|
+
read_geographic_regions()
|
@@ -0,0 +1,81 @@
|
|
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_heliports(simplified=False):
|
9
|
+
"""Download Heliports data from MapBiomas.
|
10
|
+
|
11
|
+
This function downloads and processes heliports data from MapBiomas.
|
12
|
+
The data includes information about heliports 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 heliports data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_heliports
|
28
|
+
|
29
|
+
# Read heliports data
|
30
|
+
>>> heliports = read_heliports()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://brasil.mapbiomas.org/wp-content/uploads/sites/4/2023/08/Helipontos.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', # Heliport name
|
64
|
+
'municipio', # Municipality
|
65
|
+
'uf', # State
|
66
|
+
'codigo_oaci', # ICAO code
|
67
|
+
'altitude', # Altitude
|
68
|
+
'tipo_uso', # Usage type
|
69
|
+
'compriment', # Length
|
70
|
+
'largura', # Width
|
71
|
+
'tipo_pista' # Surface type
|
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 heliports data: {str(e)}")
|
80
|
+
|
81
|
+
return gdf
|
@@ -0,0 +1,127 @@
|
|
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=True):
|
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
|
+
|
14
|
+
Parameters
|
15
|
+
----------
|
16
|
+
code_muni : str, optional
|
17
|
+
The 7-digit code of a municipality. If the two-digit code or a two-letter uppercase abbreviation of
|
18
|
+
a state is passed, (e.g. 33 or "RJ") the function will load all municipalities of that state.
|
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
|
+
|
23
|
+
Returns
|
24
|
+
-------
|
25
|
+
gpd.GeoDataFrame
|
26
|
+
Geodataframe with municipality boundaries
|
27
|
+
|
28
|
+
Example
|
29
|
+
-------
|
30
|
+
>>> from tunned_geobr import read_municipality
|
31
|
+
|
32
|
+
# Read all municipalities
|
33
|
+
>>> municipalities = read_municipality()
|
34
|
+
|
35
|
+
# Read all municipalities in a state by code
|
36
|
+
>>> state_municipalities = read_municipality(code_muni=33)
|
37
|
+
|
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)
|
43
|
+
"""
|
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
|
+
|
124
|
+
return gdf
|
125
|
+
|
126
|
+
if __name__ == '__main__':
|
127
|
+
read_municipality()
|
@@ -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_natural_caves(simplified=False):
|
9
|
+
"""Download Natural Caves data from ICMBio.
|
10
|
+
|
11
|
+
This function downloads and processes natural caves data from ICMBio's WFS service.
|
12
|
+
The data includes registered natural caves across Brazil.
|
13
|
+
Original source: ICMBio (Instituto Chico Mendes de Conservação da Biodiversidade)
|
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 natural caves data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_natural_caves
|
28
|
+
|
29
|
+
# Read natural caves data
|
30
|
+
>>> caves = read_natural_caves()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://geoservicos.inde.gov.br/geoserver/ICMBio/ows?service=wfs&version=1.3.0&request=GetFeature&TYPENAMES=cavernas_092022_p&SRSNAME=EPSG:4674&OUTPUTFORMAT=shape-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 ICMBio WFS service")
|
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
|
+
# CRS should already be 4674 (SIRGAS 2000) as requested in WFS query
|
59
|
+
# but let's ensure it
|
60
|
+
if gdf.crs is None or gdf.crs.to_epsg() != 4674:
|
61
|
+
gdf = gdf.to_crs(4674)
|
62
|
+
|
63
|
+
if simplified:
|
64
|
+
# Keep only the most relevant columns
|
65
|
+
columns_to_keep = [
|
66
|
+
'geometry',
|
67
|
+
'nome', # Cave name
|
68
|
+
'municipio', # Municipality
|
69
|
+
'uf', # State
|
70
|
+
'litologia', # Lithology
|
71
|
+
'desenvolvimento_m', # Cave development in meters
|
72
|
+
'tipo', # Type
|
73
|
+
'status' # Status
|
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 natural caves data: {str(e)}")
|
82
|
+
|
83
|
+
return gdf
|