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,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
|
@@ -0,0 +1,99 @@
|
|
1
|
+
import geopandas as gpd
|
2
|
+
import tempfile
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
import subprocess
|
6
|
+
from io import BytesIO
|
7
|
+
|
8
|
+
def read_neighborhoods_2022(simplified=False):
|
9
|
+
"""Download Brazilian Neighborhoods data from IBGE (2022 Census).
|
10
|
+
|
11
|
+
This function downloads and processes the Brazilian Neighborhoods data
|
12
|
+
from IBGE (Brazilian Institute of Geography and Statistics) for the 2022 Census.
|
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 neighborhoods data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from tunned_geobr import read_neighborhoods_2022
|
28
|
+
|
29
|
+
# Read neighborhoods data
|
30
|
+
>>> neighborhoods = read_neighborhoods_2022()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2022/bairros/shp/BR/BR_bairros_CD2022.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Create a temporary directory
|
37
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
38
|
+
# Download the zip file to the temporary directory
|
39
|
+
zip_file_path = os.path.join(temp_dir, "neighborhoods.zip")
|
40
|
+
|
41
|
+
# Download the file
|
42
|
+
response = requests.get(url)
|
43
|
+
if response.status_code != 200:
|
44
|
+
raise Exception("Failed to download neighborhoods data from IBGE")
|
45
|
+
|
46
|
+
# Save the content to a file
|
47
|
+
with open(zip_file_path, 'wb') as f:
|
48
|
+
f.write(response.content)
|
49
|
+
|
50
|
+
# Use unzip command line tool to extract the file (handles more compression methods)
|
51
|
+
try:
|
52
|
+
subprocess.run(['unzip', '-o', zip_file_path, '-d', temp_dir],
|
53
|
+
check=True,
|
54
|
+
stdout=subprocess.PIPE,
|
55
|
+
stderr=subprocess.PIPE)
|
56
|
+
except subprocess.CalledProcessError as e:
|
57
|
+
raise Exception(f"Failed to extract zip file: {e.stderr.decode()}")
|
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
|
+
if simplified:
|
75
|
+
# Keep only the most relevant columns
|
76
|
+
# Note: Column names may need adjustment based on actual data
|
77
|
+
columns_to_keep = [
|
78
|
+
'geometry',
|
79
|
+
'CD_BAIRRO', # Neighborhood Code
|
80
|
+
'NM_BAIRRO', # Neighborhood Name
|
81
|
+
'CD_MUN', # Municipality Code
|
82
|
+
'NM_MUN', # Municipality Name
|
83
|
+
'CD_UF', # State Code
|
84
|
+
'NM_UF', # State Name
|
85
|
+
'SIGLA_UF', # State Abbreviation
|
86
|
+
'AREA_KM2' # Area in square kilometers
|
87
|
+
]
|
88
|
+
|
89
|
+
# Filter columns that actually exist in the dataset
|
90
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
91
|
+
gdf = gdf[existing_columns]
|
92
|
+
|
93
|
+
except Exception as e:
|
94
|
+
raise Exception(f"Error downloading neighborhoods data: {str(e)}")
|
95
|
+
|
96
|
+
return gdf
|
97
|
+
|
98
|
+
if __name__ == '__main__':
|
99
|
+
read_neighborhoods_2022()
|
@@ -0,0 +1,89 @@
|
|
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_pan_strategic_areas(simplified=False):
|
9
|
+
"""Download Strategic Areas data from ICMBio's PAN.
|
10
|
+
|
11
|
+
This function downloads and processes the Strategic Areas data from ICMBio's
|
12
|
+
National Action Plans (PAN). These are areas of strategic importance for
|
13
|
+
biodiversity conservation in Brazil.
|
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 PAN strategic areas data
|
25
|
+
|
26
|
+
Example
|
27
|
+
-------
|
28
|
+
>>> from tunned_geobr import read_pan_strategic_areas
|
29
|
+
|
30
|
+
# Read PAN strategic areas data
|
31
|
+
>>> strategic_areas = read_pan_strategic_areas()
|
32
|
+
"""
|
33
|
+
|
34
|
+
url = "https://geoservicos.inde.gov.br/geoserver/ICMBio/ows?request=GetFeature&service=WFS&version=1.0.0&typeName=ICMBio:pan_icmbio_areas_estrat_052024_a&outputFormat=SHAPE-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 strategic areas data from ICMBio")
|
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
|
+
'nome', # Area name
|
69
|
+
'pan', # PAN name
|
70
|
+
'tipo', # Type of strategic area
|
71
|
+
'area_km2', # Area in square kilometers
|
72
|
+
'bioma', # Biome
|
73
|
+
'uf', # State
|
74
|
+
'municipio', # Municipality
|
75
|
+
'importancia', # Importance
|
76
|
+
'descricao' # Description
|
77
|
+
]
|
78
|
+
|
79
|
+
# Filter columns that actually exist in the dataset
|
80
|
+
existing_columns = ['geometry'] + [col for col in columns_to_keep[1:] if col in gdf.columns]
|
81
|
+
gdf = gdf[existing_columns]
|
82
|
+
|
83
|
+
except Exception as e:
|
84
|
+
raise Exception(f"Error downloading PAN strategic areas data: {str(e)}")
|
85
|
+
|
86
|
+
return gdf
|
87
|
+
|
88
|
+
if __name__ == '__main__':
|
89
|
+
read_pan_strategic_areas()
|
@@ -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_ports(simplified=False):
|
9
|
+
"""Download Brazilian Ports data from SNIRH.
|
10
|
+
|
11
|
+
This function downloads and processes ports data from SNIRH (Sistema Nacional de
|
12
|
+
Informações sobre Recursos Hídricos). The data includes information about ports
|
13
|
+
across Brazil.
|
14
|
+
Original source: SNIRH (Sistema Nacional de Informações sobre Recursos Hídricos)
|
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 ports data
|
25
|
+
|
26
|
+
Example
|
27
|
+
-------
|
28
|
+
>>> from tunned_geobr import read_ports
|
29
|
+
|
30
|
+
# Read ports data
|
31
|
+
>>> ports = read_ports()
|
32
|
+
"""
|
33
|
+
|
34
|
+
url = "https://metadados.snirh.gov.br/geonetwork/srv/api/records/0afc9687-db93-4eb1-ab31-3bbd871ff303/attachments/GEOFT_PORTO.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 data from SNIRH")
|
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
|
+
gdf = gdf.to_crs(4674) # Convert to SIRGAS 2000
|
59
|
+
|
60
|
+
if simplified:
|
61
|
+
# Keep only the most relevant columns
|
62
|
+
columns_to_keep = [
|
63
|
+
'geometry',
|
64
|
+
'nome', # Port name
|
65
|
+
'municipio', # Municipality
|
66
|
+
'uf', # State
|
67
|
+
'tipo', # Port type
|
68
|
+
'administra', # Administration
|
69
|
+
'situacao', # Status
|
70
|
+
'localizaca' # Location details
|
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 ports data: {str(e)}")
|
79
|
+
|
80
|
+
return gdf
|