tunned-geobr 0.1.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 +38 -0
- tunned_geobr/constants.py +13 -0
- tunned_geobr/data/grid_state_correspondence_table.csv +140 -0
- tunned_geobr/list_geobr.py +39 -0
- tunned_geobr/lookup_muni.py +111 -0
- tunned_geobr/read_amazon.py +42 -0
- tunned_geobr/read_amazon_ibas.py +92 -0
- tunned_geobr/read_atlantic_forest_ibas.py +93 -0
- tunned_geobr/read_biomes.py +43 -0
- tunned_geobr/read_census_tract.py +97 -0
- tunned_geobr/read_climate_aggressiveness.py +74 -0
- tunned_geobr/read_comparable_areas.py +75 -0
- tunned_geobr/read_conservation_units.py +43 -0
- tunned_geobr/read_country.py +43 -0
- tunned_geobr/read_disaster_risk_area.py +47 -0
- tunned_geobr/read_geology.py +77 -0
- tunned_geobr/read_geomorphology.py +77 -0
- tunned_geobr/read_health_facilities.py +49 -0
- tunned_geobr/read_health_region.py +52 -0
- tunned_geobr/read_immediate_region.py +81 -0
- tunned_geobr/read_indigenous_land.py +44 -0
- tunned_geobr/read_intermediate_region.py +61 -0
- tunned_geobr/read_meso_region.py +78 -0
- tunned_geobr/read_metro_area.py +44 -0
- tunned_geobr/read_micro_region.py +78 -0
- tunned_geobr/read_mining_processes.py +76 -0
- tunned_geobr/read_municipal_seat.py +41 -0
- tunned_geobr/read_municipality.py +83 -0
- tunned_geobr/read_neighborhood.py +39 -0
- tunned_geobr/read_pedology.py +77 -0
- tunned_geobr/read_pop_arrangements.py +45 -0
- tunned_geobr/read_region.py +41 -0
- tunned_geobr/read_schools.py +44 -0
- tunned_geobr/read_semiarid.py +42 -0
- tunned_geobr/read_settlements.py +85 -0
- tunned_geobr/read_state.py +88 -0
- tunned_geobr/read_statistical_grid.py +127 -0
- tunned_geobr/read_urban_area.py +44 -0
- tunned_geobr/read_urban_concentrations.py +46 -0
- tunned_geobr/read_weighting_area.py +74 -0
- tunned_geobr/utils.py +326 -0
- tunned_geobr-0.1.0.dist-info/METADATA +103 -0
- tunned_geobr-0.1.0.dist-info/RECORD +46 -0
- tunned_geobr-0.1.0.dist-info/WHEEL +4 -0
- tunned_geobr-0.1.0.dist-info/entry_points.txt +4 -0
- tunned_geobr-0.1.0.dist-info/licenses/LICENSE.txt +2 -0
@@ -0,0 +1,77 @@
|
|
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_pedology(simplified=False):
|
9
|
+
"""Download official pedology (soil) data from IBGE.
|
10
|
+
|
11
|
+
This function downloads and processes pedological data from IBGE (Brazilian Institute of Geography and Statistics).
|
12
|
+
The data includes soil units and classifications at 1:250,000 scale.
|
13
|
+
Original source: IBGE
|
14
|
+
|
15
|
+
Parameters
|
16
|
+
----------
|
17
|
+
simplified : boolean, by default True
|
18
|
+
If True, returns a simplified version of the dataset with fewer columns
|
19
|
+
|
20
|
+
Returns
|
21
|
+
-------
|
22
|
+
gpd.GeoDataFrame
|
23
|
+
Geodataframe with pedological data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from cursed_geobr import read_pedology
|
28
|
+
|
29
|
+
# Read pedology data
|
30
|
+
>>> pedology = read_pedology()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://geoftp.ibge.gov.br/informacoes_ambientais/pedologia/vetores/escala_250_mil/versao_2023/pedo_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 data from IBGE")
|
40
|
+
|
41
|
+
# Create a temporary directory
|
42
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
43
|
+
# Extract zip content
|
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
|
+
if simplified:
|
56
|
+
# Keep only the most relevant columns
|
57
|
+
# Note: These columns are based on typical soil data structure
|
58
|
+
# You may want to adjust these based on the actual data
|
59
|
+
columns_to_keep = [
|
60
|
+
'geometry',
|
61
|
+
'CLASSE1', # Main soil class
|
62
|
+
'CLASSE2', # Secondary soil class
|
63
|
+
'TEXTURA', # Soil texture
|
64
|
+
'RELEVO', # Relief
|
65
|
+
'FASE', # Phase
|
66
|
+
'ORDEM', # Order
|
67
|
+
'SUBORDEM', # Suborder
|
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 pedology data: {str(e)}")
|
76
|
+
|
77
|
+
return gdf
|
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
3
|
+
|
4
|
+
|
5
|
+
def read_pop_arrangements(year=2015, simplified=True, verbose=False):
|
6
|
+
r""" Download population arrangements in Brazil
|
7
|
+
|
8
|
+
This function reads the official data on population arrangements (Arranjos
|
9
|
+
Populacionais) of Brazil. Original data were generated by the Institute of
|
10
|
+
Geography and Statistics (IBGE) For more information about the methodology,
|
11
|
+
see details at \url{https://www.ibge.gov.br/apps/arranjos_populacionais/2015/pdf/publicacao.pdf}
|
12
|
+
|
13
|
+
Parameters
|
14
|
+
----------
|
15
|
+
year : int, optional
|
16
|
+
Year of the data, by default 2015
|
17
|
+
simplified: boolean, by default True
|
18
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
19
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
20
|
+
verbose : bool, optional
|
21
|
+
by default False
|
22
|
+
|
23
|
+
Returns
|
24
|
+
-------
|
25
|
+
gpd.GeoDataFrame
|
26
|
+
Metadata and geopackage of selected states
|
27
|
+
|
28
|
+
Raises
|
29
|
+
------
|
30
|
+
Exception
|
31
|
+
If parameters are not found or not well defined
|
32
|
+
|
33
|
+
Example
|
34
|
+
-------
|
35
|
+
>>> from cursed_geobr import read_pop_arrangements
|
36
|
+
|
37
|
+
# Read specific state at a given year
|
38
|
+
>>> df = read_pop_arrangements(year=2015)
|
39
|
+
"""
|
40
|
+
|
41
|
+
metadata = select_metadata('pop_arrengements', year=year, simplified=simplified)
|
42
|
+
|
43
|
+
gdf = download_gpkg(metadata)
|
44
|
+
|
45
|
+
return gdf
|
@@ -0,0 +1,41 @@
|
|
1
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
2
|
+
|
3
|
+
|
4
|
+
def read_region(year=2010, simplified=True, verbose=False):
|
5
|
+
""" Download shape file of Brazil Regions as sf objects.
|
6
|
+
|
7
|
+
Data at scale 1:250,000, using Geodetic reference system "SIRGAS2000" and CRS(4674)
|
8
|
+
|
9
|
+
Parameters
|
10
|
+
----------
|
11
|
+
year : int, optional
|
12
|
+
Year of the data, by default 2010
|
13
|
+
simplified: boolean, by default True
|
14
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
15
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
16
|
+
verbose : bool, optional
|
17
|
+
by default False
|
18
|
+
|
19
|
+
Returns
|
20
|
+
-------
|
21
|
+
gpd.GeoDataFrame
|
22
|
+
Metadata and geopackage of selected states
|
23
|
+
|
24
|
+
Raises
|
25
|
+
------
|
26
|
+
Exception
|
27
|
+
If parameters are not found or not well defined
|
28
|
+
|
29
|
+
Example
|
30
|
+
-------
|
31
|
+
>>> from cursed_geobr import read_region
|
32
|
+
|
33
|
+
# Read specific state at a given year
|
34
|
+
>>> df = read_region(year=2010)
|
35
|
+
"""
|
36
|
+
|
37
|
+
metadata = select_metadata("regions", year=year, simplified=simplified)
|
38
|
+
|
39
|
+
gdf = download_gpkg(metadata)
|
40
|
+
|
41
|
+
return gdf
|
@@ -0,0 +1,44 @@
|
|
1
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
2
|
+
|
3
|
+
|
4
|
+
def read_schools(year=2020, verbose=False):
|
5
|
+
r"""Download geolocated data of schools
|
6
|
+
|
7
|
+
Data comes from the School Census collected by INEP, the National Institute
|
8
|
+
for Educational Studies and Research Anisio Teixeira. The date of the last
|
9
|
+
data update is registered in the database in the column 'date_update'. These
|
10
|
+
data uses Geodetic reference system "SIRGAS2000" and CRS(4674). The coordinates
|
11
|
+
of each school if collected by INEP. Periodically the coordinates are revised
|
12
|
+
with the objective of improving the quality of the data. More information
|
13
|
+
available at \url{https://www.gov.br/inep/pt-br/acesso-a-informacao/dados-abertos/inep-data/catalogo-de-escolas/}
|
14
|
+
|
15
|
+
Parameters
|
16
|
+
----------
|
17
|
+
year : int, optional
|
18
|
+
Year of the data, by default 2020
|
19
|
+
verbose : bool, optional
|
20
|
+
by default False
|
21
|
+
|
22
|
+
Returns
|
23
|
+
-------
|
24
|
+
gpd.GeoDataFrame
|
25
|
+
Metadata and geopackage of selected states
|
26
|
+
|
27
|
+
Raises
|
28
|
+
------
|
29
|
+
Exception
|
30
|
+
If parameters are not found or not well defined
|
31
|
+
|
32
|
+
Example
|
33
|
+
-------
|
34
|
+
>>> from cursed_geobr import read_schools
|
35
|
+
|
36
|
+
# Read specific state at a given year
|
37
|
+
>>> df = read_schools(year=2020)
|
38
|
+
"""
|
39
|
+
|
40
|
+
metadata = select_metadata("schools", year=year, simplified=False)
|
41
|
+
|
42
|
+
gdf = download_gpkg(metadata)
|
43
|
+
|
44
|
+
return gdf
|
@@ -0,0 +1,42 @@
|
|
1
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
2
|
+
|
3
|
+
|
4
|
+
def read_semiarid(year=2017, simplified=True, verbose=False):
|
5
|
+
""" Download official data of Brazilian Semiarid as an sf object.
|
6
|
+
|
7
|
+
This data set covers the whole of Brazilian Semiarid as defined in the resolution in 23/11/2017). The original
|
8
|
+
data comes from the Brazilian Institute of Geography and Statistics (IBGE) and can be found at https://www.ibge.gov.br/geociencias/cartas-e-mapas/mapas-regionais/15974-semiarido-brasileiro.html?=&t=downloads
|
9
|
+
|
10
|
+
Parameters
|
11
|
+
----------
|
12
|
+
year : int, optional
|
13
|
+
Year of the data, by default 2017
|
14
|
+
simplified: boolean, by default True
|
15
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
16
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
17
|
+
verbose : bool, optional
|
18
|
+
by default False
|
19
|
+
|
20
|
+
Returns
|
21
|
+
-------
|
22
|
+
gpd.GeoDataFrame
|
23
|
+
Metadata and geopackage of selected states
|
24
|
+
|
25
|
+
Raises
|
26
|
+
------
|
27
|
+
Exception
|
28
|
+
If parameters are not found or not well defined
|
29
|
+
|
30
|
+
Example
|
31
|
+
-------
|
32
|
+
>>> from cursed_geobr import read_semiarid
|
33
|
+
|
34
|
+
# Read specific state at a given year
|
35
|
+
>>> df = read_semiarid(year=2017)
|
36
|
+
"""
|
37
|
+
|
38
|
+
metadata = select_metadata("semiarid", year=year, simplified=simplified)
|
39
|
+
|
40
|
+
gdf = download_gpkg(metadata)
|
41
|
+
|
42
|
+
return gdf
|
@@ -0,0 +1,85 @@
|
|
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_settlements(simplified=False):
|
9
|
+
"""Download official settlements data from INCRA.
|
10
|
+
|
11
|
+
This function downloads and processes data about settlements (assentamentos)
|
12
|
+
from INCRA (Instituto Nacional de Colonização e Reforma Agrária).
|
13
|
+
Original source: INCRA - Certificação de Imóveis Rurais
|
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 settlements data
|
24
|
+
|
25
|
+
Example
|
26
|
+
-------
|
27
|
+
>>> from cursed_geobr import read_settlements
|
28
|
+
|
29
|
+
# Read settlements data
|
30
|
+
>>> settlements = read_settlements()
|
31
|
+
"""
|
32
|
+
|
33
|
+
url = "https://certificacao.incra.gov.br/csv_shp/zip/Assentamento%20Brasil.zip"
|
34
|
+
|
35
|
+
try:
|
36
|
+
# Download the zip file
|
37
|
+
# Disable SSL verification due to INCRA's certificate issues
|
38
|
+
response = requests.get(url, verify=False)
|
39
|
+
if response.status_code != 200:
|
40
|
+
raise Exception("Failed to download data from INCRA")
|
41
|
+
|
42
|
+
# Suppress the InsecureRequestWarning
|
43
|
+
import urllib3
|
44
|
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
45
|
+
|
46
|
+
# Create a temporary directory
|
47
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
48
|
+
# Extract the zip file
|
49
|
+
with ZipFile(BytesIO(response.content)) as zip_ref:
|
50
|
+
zip_ref.extractall(temp_dir)
|
51
|
+
|
52
|
+
# Find the shapefile
|
53
|
+
shp_files = [f for f in os.listdir(temp_dir) if f.endswith('.shp')]
|
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(os.path.join(temp_dir, shp_files[0]))
|
59
|
+
|
60
|
+
if simplified:
|
61
|
+
# Keep only the most relevant columns
|
62
|
+
columns_to_keep = [
|
63
|
+
'geometry',
|
64
|
+
'NOME_PROJE', # Nome do Projeto de Assentamento
|
65
|
+
'MUNICIPIO', # Município
|
66
|
+
'UF', # Estado
|
67
|
+
'AREA_HA', # Área em hectares
|
68
|
+
'NUM_FAMILI', # Número de famílias
|
69
|
+
'CAPACIDADE', # Capacidade de famílias
|
70
|
+
'DT_CRIACAO', # Data de criação
|
71
|
+
'SITUACAO' # Situação do assentamento
|
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 settlements data: {str(e)}")
|
80
|
+
|
81
|
+
return gdf
|
82
|
+
|
83
|
+
if __name__ == '__main__':
|
84
|
+
settlements = read_settlements()
|
85
|
+
print(settlements)
|
@@ -0,0 +1,88 @@
|
|
1
|
+
import geopandas as gpd
|
2
|
+
|
3
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
4
|
+
|
5
|
+
|
6
|
+
def read_state(code_state="all", year=2010, simplified=True, verbose=False):
|
7
|
+
"""Download shapefiles of Brazilian states as geopandas objects.
|
8
|
+
|
9
|
+
Data at scale 1:250,000, using Geodetic reference system "SIRGAS2000" and CRS(4674)
|
10
|
+
|
11
|
+
Parameters
|
12
|
+
----------
|
13
|
+
code_state : str, optional
|
14
|
+
The two-digit code of a state or a two-letter uppercase abbreviation
|
15
|
+
(e.g. 33 or "RJ"). If code_state="all", all states will be loaded (Default).
|
16
|
+
year : int, optional
|
17
|
+
Year of the data, by default 2010
|
18
|
+
simplified: boolean, by default True
|
19
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
20
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
21
|
+
verbose : bool, optional
|
22
|
+
by default False
|
23
|
+
|
24
|
+
Returns
|
25
|
+
-------
|
26
|
+
gpd.GeoDataFrame
|
27
|
+
Metadata and geopackage of selected states
|
28
|
+
|
29
|
+
Raises
|
30
|
+
------
|
31
|
+
Exception
|
32
|
+
If parameters are not found or not well defined
|
33
|
+
|
34
|
+
Example
|
35
|
+
-------
|
36
|
+
>>> from cursed_geobr import read_state
|
37
|
+
|
38
|
+
# Read specific state at a given year
|
39
|
+
>>> uf = read_state(code_state=12, year=2017)
|
40
|
+
|
41
|
+
# Read specific state at a given year with normal geopackages
|
42
|
+
>>> uf = read_state(code_state="SC", year=2000, tp='normal')
|
43
|
+
|
44
|
+
# Read all states at a given year
|
45
|
+
>>> ufs = read_state(code_state="all", year=2010)
|
46
|
+
"""
|
47
|
+
|
48
|
+
metadata = select_metadata("state", year=year, simplified=simplified)
|
49
|
+
|
50
|
+
if code_state is None:
|
51
|
+
raise Exception("Value to argument 'code_state' cannot be None")
|
52
|
+
|
53
|
+
# From 1872 to 1991 and all
|
54
|
+
if (year < 1992) or (code_state == "all"):
|
55
|
+
|
56
|
+
if verbose:
|
57
|
+
print("Loading data for the whole country\n")
|
58
|
+
|
59
|
+
return download_gpkg(metadata)
|
60
|
+
|
61
|
+
# From 2000 onwards
|
62
|
+
else:
|
63
|
+
|
64
|
+
if (
|
65
|
+
str(code_state)[0:2] not in metadata["code"].unique()
|
66
|
+
and str(code_state)[0:2] not in metadata["code_abbrev"].unique()
|
67
|
+
):
|
68
|
+
|
69
|
+
raise Exception("Error: Invalid Value to argument code_state.")
|
70
|
+
|
71
|
+
else:
|
72
|
+
|
73
|
+
if isinstance(code_state, int):
|
74
|
+
metadata = metadata.query(f'code == "{str(code_state)[0:2]}"')
|
75
|
+
|
76
|
+
if isinstance(code_state, str):
|
77
|
+
metadata = metadata.query(f'code_abbrev == "{code_state[0:2]}"')
|
78
|
+
|
79
|
+
gdf = download_gpkg(metadata)
|
80
|
+
|
81
|
+
if len(str(code_state)) == 2:
|
82
|
+
return gdf
|
83
|
+
|
84
|
+
elif code_state in list(gdf["code_state"]):
|
85
|
+
return gdf.query('code_state == "code_state"')
|
86
|
+
|
87
|
+
else:
|
88
|
+
raise Exception("Error: Invalid Value to argument code_state.")
|
@@ -0,0 +1,127 @@
|
|
1
|
+
import sys
|
2
|
+
from geobr import __path__ as geobr_directory
|
3
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
4
|
+
from numpy import unique
|
5
|
+
from pandas import read_csv
|
6
|
+
|
7
|
+
|
8
|
+
def read_statistical_grid(code_grid="all", year=2010, simplified=False, verbose=False):
|
9
|
+
r"""Download spatial data of IBGE's statistical grid
|
10
|
+
|
11
|
+
@description
|
12
|
+
Data at scale 1:250,000, using Geodetic reference system "SIRGAS2000" and CRS(4674)
|
13
|
+
|
14
|
+
Parameters
|
15
|
+
----------
|
16
|
+
code_grid:
|
17
|
+
If two-letter abbreviation or two-digit code of a state is
|
18
|
+
passed, the function will load all grid quadrants that
|
19
|
+
intersect with that state. If `code_grid="all"`, the grid of
|
20
|
+
the whole country will be loaded. Users may also pass a
|
21
|
+
grid quadrant id to load an specific quadrant. Quadrant ids
|
22
|
+
can be consulted at `grid_state_correspondence_table.csv`.
|
23
|
+
year : int, optional
|
24
|
+
Year of the data, by default 2010
|
25
|
+
simplified: boolean, by default False
|
26
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
27
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
28
|
+
verbose : bool, optional
|
29
|
+
by default False
|
30
|
+
|
31
|
+
Returns
|
32
|
+
-------
|
33
|
+
gpd.GeoDataFrame
|
34
|
+
Metadata and geopackage of selected states
|
35
|
+
|
36
|
+
Raises
|
37
|
+
------
|
38
|
+
Exception
|
39
|
+
If parameters are not found or not well defined
|
40
|
+
|
41
|
+
Example
|
42
|
+
-------
|
43
|
+
>>> from cursed_geobr import read_statistical_grid
|
44
|
+
|
45
|
+
# Read specific state at a given year
|
46
|
+
>>> df = read_statistical_grid(year=2010)
|
47
|
+
"""
|
48
|
+
|
49
|
+
temp_meta = select_metadata(
|
50
|
+
geo="statistical_grid", year=year, simplified=simplified
|
51
|
+
)
|
52
|
+
|
53
|
+
if temp_meta is None:
|
54
|
+
return None
|
55
|
+
|
56
|
+
if(len(geobr_directory) == 0):
|
57
|
+
sys.exit("Geobr installation directory not found.")
|
58
|
+
|
59
|
+
grid_state_correspondence_table = None
|
60
|
+
grid_file_path = geobr_directory[0] + "/data/grid_state_correspondence_table.csv"
|
61
|
+
|
62
|
+
with open(grid_file_path, "rb") as file:
|
63
|
+
dtypes = {"name_state": str, "abbrev_state": str, "code_grid": str}
|
64
|
+
grid_state_correspondence_table = read_csv(
|
65
|
+
file, encoding="latin-1", dtype=dtypes
|
66
|
+
)
|
67
|
+
|
68
|
+
# Test if code_grid input is null
|
69
|
+
if code_grid == None:
|
70
|
+
sys.exit("Value to argument 'code_grid' cannot be NULL")
|
71
|
+
|
72
|
+
# if code_grid=="all", read the entire country
|
73
|
+
if code_grid == "all":
|
74
|
+
if verbose:
|
75
|
+
print("Loading data for the whole country. This might take a few minutes.")
|
76
|
+
|
77
|
+
file_url = temp_meta["download_path"]
|
78
|
+
temp_gpd = download_gpkg(file_url)
|
79
|
+
|
80
|
+
return temp_gpd
|
81
|
+
|
82
|
+
# Select abbrev_state column
|
83
|
+
grid_abbrev_state = grid_state_correspondence_table["abbrev_state"]
|
84
|
+
|
85
|
+
# Error if the input does not match any state abbreviation
|
86
|
+
if isinstance(code_grid, str) and not (code_grid in grid_abbrev_state.to_list()):
|
87
|
+
sys.exit(
|
88
|
+
"Error: Invalid Value to argument 'code_grid'. It must be one of the following: "
|
89
|
+
+ str(unique(grid_abbrev_state.to_numpy().tolist()))
|
90
|
+
)
|
91
|
+
|
92
|
+
# Valid state abbreviation
|
93
|
+
elif isinstance(code_grid, str) and code_grid in grid_abbrev_state.to_list():
|
94
|
+
# Find grid quadrants that intersect with the passed state abbreviation
|
95
|
+
grid_state_correspondence_table_tmp = grid_state_correspondence_table[
|
96
|
+
grid_state_correspondence_table["abbrev_state"] == code_grid
|
97
|
+
]
|
98
|
+
|
99
|
+
# Strips 'ID_' from code_grid string and gets only the int code value
|
100
|
+
grid_ids = [
|
101
|
+
substr[substr.index("_") + 1 :]
|
102
|
+
for substr in grid_state_correspondence_table_tmp["code_grid"].to_list()
|
103
|
+
]
|
104
|
+
|
105
|
+
file_url = temp_meta[temp_meta["code"].isin(grid_ids)]
|
106
|
+
temp_gpd = download_gpkg(file_url)
|
107
|
+
|
108
|
+
return temp_gpd
|
109
|
+
|
110
|
+
# If code_grid is int
|
111
|
+
if isinstance(code_grid, int):
|
112
|
+
# Converts to str to match the following queries
|
113
|
+
code_grid = str(code_grid)
|
114
|
+
|
115
|
+
# Single digit case: adds a leading 0 (ex: 4 -> 04)
|
116
|
+
if len(code_grid) == 1:
|
117
|
+
code_grid = "0" + code_grid
|
118
|
+
|
119
|
+
if not (code_grid in temp_meta["code"].to_list()):
|
120
|
+
sys.exit("Error: Invalid Value to argument code_grid.")
|
121
|
+
|
122
|
+
else:
|
123
|
+
# Filters by code then download a list of gpkg filtered paths
|
124
|
+
file_url = temp_meta[temp_meta["code"].isin([code_grid])]
|
125
|
+
temp_gpd = download_gpkg(file_url)
|
126
|
+
|
127
|
+
return temp_gpd
|
@@ -0,0 +1,44 @@
|
|
1
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
2
|
+
|
3
|
+
|
4
|
+
def read_urban_area(year=2015, simplified=True, verbose=False):
|
5
|
+
""" Download official data of urbanized areas in Brazil as an sf object.
|
6
|
+
|
7
|
+
This function reads the official data on the urban footprint of Brazilian cities
|
8
|
+
in the years 2005 and 2015. Orignal data were generated by Institute of Geography
|
9
|
+
and Statistics (IBGE) For more information about the methodology, see deails at
|
10
|
+
https://biblioteca.ibge.gov.br/visualizacao/livros/liv100639.pdf
|
11
|
+
|
12
|
+
Parameters
|
13
|
+
----------
|
14
|
+
year : int, optional
|
15
|
+
Year of the data, by default 2015
|
16
|
+
simplified: boolean, by default True
|
17
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
18
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
19
|
+
verbose : bool, optional
|
20
|
+
by default False
|
21
|
+
|
22
|
+
Returns
|
23
|
+
-------
|
24
|
+
gpd.GeoDataFrame
|
25
|
+
Metadata and geopackage of selected states
|
26
|
+
|
27
|
+
Raises
|
28
|
+
------
|
29
|
+
Exception
|
30
|
+
If parameters are not found or not well defined
|
31
|
+
|
32
|
+
Example
|
33
|
+
-------
|
34
|
+
>>> from cursed_geobr import read_urban_area
|
35
|
+
|
36
|
+
# Read specific state at a given year
|
37
|
+
>>> df = read_urban_area(year=2015)
|
38
|
+
"""
|
39
|
+
|
40
|
+
metadata = select_metadata("urban_area", year=year, simplified=simplified)
|
41
|
+
|
42
|
+
gdf = download_gpkg(metadata)
|
43
|
+
|
44
|
+
return gdf
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
from cursed_geobr.utils import select_metadata, download_gpkg
|
3
|
+
|
4
|
+
|
5
|
+
def read_urban_concentrations(year=2015, simplified=True, verbose=False):
|
6
|
+
r""" Download urban concentration areas in Brazil
|
7
|
+
|
8
|
+
@description
|
9
|
+
This function reads the official data on the urban concentration areas (Areas
|
10
|
+
de Concentracao de Populacao) of Brazil. Original data were generated by the
|
11
|
+
Institute of Geography and Statistics (IBGE) For more information about the
|
12
|
+
methodology, see details at \url{https://www.ibge.gov.br/apps/arranjos_populacionais/2015/pdf/publicacao.pdf}
|
13
|
+
|
14
|
+
Parameters
|
15
|
+
----------
|
16
|
+
year : int, optional
|
17
|
+
Year of the data, by default 2015
|
18
|
+
simplified: boolean, by default True
|
19
|
+
Data 'type', indicating whether the function returns the 'original' dataset
|
20
|
+
with high resolution or a dataset with 'simplified' borders (Default)
|
21
|
+
verbose : bool, optional
|
22
|
+
by default False
|
23
|
+
|
24
|
+
Returns
|
25
|
+
-------
|
26
|
+
gpd.GeoDataFrame
|
27
|
+
Metadata and geopackage of selected states
|
28
|
+
|
29
|
+
Raises
|
30
|
+
------
|
31
|
+
Exception
|
32
|
+
If parameters are not found or not well defined
|
33
|
+
|
34
|
+
Example
|
35
|
+
-------
|
36
|
+
>>> from cursed_geobr import read_urban_concentrations
|
37
|
+
|
38
|
+
# Read specific state at a given year
|
39
|
+
>>> df = read_urban_concentrations(year=2015)
|
40
|
+
"""
|
41
|
+
|
42
|
+
metadata = select_metadata('urban_concentrations', year=year, simplified=simplified)
|
43
|
+
|
44
|
+
gdf = download_gpkg(metadata)
|
45
|
+
|
46
|
+
return gdf
|