incra 0.1.0__tar.gz

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.
incra-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Open Geodata
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
incra-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.4
2
+ Name: incra
3
+ Version: 0.1.0
4
+ Summary: Obtem dados do INCRA usando python!
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.11
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3.11
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Classifier: Programming Language :: Python :: 3.14
12
+ Requires-Dist: geopandas (>=1.1.1)
13
+ Requires-Dist: requests (>=2.32.5)
14
+ Requires-Dist: urllib3 (>=2.5.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # INCRA
18
+
19
+ [![Repo](https://img.shields.io/badge/GitHub-repo-blue?logo=github&logoColor=f5f5f5)](https://github.com/open-geodata/br_incra)
20
+
21
+ <br>
22
+
23
+ _Script_ para baixar dados do INCRA.\
24
+ Elaborado em 18.06.2021.
25
+
26
+ <br>
27
+
28
+ ---
29
+
30
+ ## WMS
31
+
32
+ _Site_ para exportar:
33
+
34
+ - https://certificacao.incra.gov.br/csv_shp/export_shp.py
35
+
36
+ <br>
37
+
38
+ Servidores WMS:
39
+
40
+ - http://acervofundiario.incra.gov.br/i3geo/ogc.php?tema=certificada_sigef_particular_pa
41
+ - http://acervofundiario.incra.gov.br/i3geo/ogc.php?tema=certificada_sigef_publico_pa
42
+
43
+ <br>
44
+
45
+ ---
46
+
47
+ ### _TODO_
48
+
49
+ 1. ~~Ideal clicar em "obter" para que ele crie um arquivo novo.~~
50
+
incra-0.1.0/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # INCRA
2
+
3
+ [![Repo](https://img.shields.io/badge/GitHub-repo-blue?logo=github&logoColor=f5f5f5)](https://github.com/open-geodata/br_incra)
4
+
5
+ <br>
6
+
7
+ _Script_ para baixar dados do INCRA.\
8
+ Elaborado em 18.06.2021.
9
+
10
+ <br>
11
+
12
+ ---
13
+
14
+ ## WMS
15
+
16
+ _Site_ para exportar:
17
+
18
+ - https://certificacao.incra.gov.br/csv_shp/export_shp.py
19
+
20
+ <br>
21
+
22
+ Servidores WMS:
23
+
24
+ - http://acervofundiario.incra.gov.br/i3geo/ogc.php?tema=certificada_sigef_particular_pa
25
+ - http://acervofundiario.incra.gov.br/i3geo/ogc.php?tema=certificada_sigef_publico_pa
26
+
27
+ <br>
28
+
29
+ ---
30
+
31
+ ### _TODO_
32
+
33
+ 1. ~~Ideal clicar em "obter" para que ele crie um arquivo novo.~~
@@ -0,0 +1 @@
1
+ from .main import INCRA
@@ -0,0 +1,218 @@
1
+ """
2
+ Módulo do INCRA
3
+ Para obter e atualizar dado.
4
+ """
5
+
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+ from typing import Literal
9
+ from urllib.parse import quote, urljoin
10
+ from zipfile import ZipFile
11
+
12
+ import geopandas as gpd
13
+
14
+ from incra.net import create_session
15
+
16
+
17
+ class INCRA:
18
+ def __init__(
19
+ self,
20
+ layer: Literal[
21
+ 'Imóvel certificado SIGEF Total',
22
+ 'Imóvel certificado SIGEF Público',
23
+ 'Imóvel certificado SIGEF Privado',
24
+ 'Imóvel certificado SNCI Total',
25
+ 'Imóvel certificado SNCI Público',
26
+ 'Imóvel certificado SNCI Privado',
27
+ 'Projetos de Assentamento Total',
28
+ 'Projetos de Assentamento Federal',
29
+ 'Projetos de Assentamento Reconhecimento',
30
+ 'Áreas de Quilombolas',
31
+ ],
32
+ uf: Literal[
33
+ 'AC',
34
+ 'AL',
35
+ 'AP',
36
+ 'AM',
37
+ 'BA',
38
+ 'CE',
39
+ 'DF',
40
+ 'ES',
41
+ 'GO',
42
+ 'MA',
43
+ 'MT',
44
+ 'MS',
45
+ 'MG',
46
+ 'PA',
47
+ 'PB',
48
+ 'PR',
49
+ 'PE',
50
+ 'PI',
51
+ 'RJ',
52
+ 'RN',
53
+ 'RS',
54
+ 'RO',
55
+ 'RR',
56
+ 'SC',
57
+ 'SP',
58
+ 'SE',
59
+ 'TO',
60
+ None,
61
+ ] = None,
62
+ ) -> None:
63
+ dict_layers = {
64
+ 'Imóvel certificado SIGEF Total': {
65
+ 'filename': 'Sigef Brasil.zip',
66
+ 'dropdown': 'sigef',
67
+ },
68
+ 'Imóvel certificado SIGEF Público': {
69
+ 'filename': 'Sigef Público.zip',
70
+ 'dropdown': 'sigef_pub',
71
+ },
72
+ 'Imóvel certificado SIGEF Privado': {
73
+ 'filename': 'Sigef Privado.zip',
74
+ 'dropdown': 'sigef_pri',
75
+ },
76
+ 'Imóvel certificado SNCI Total': {
77
+ 'filename': 'Imóvel certificado SNCI Brasil.zip',
78
+ 'dropdown': 'snci',
79
+ },
80
+ 'Imóvel certificado SNCI Público': {
81
+ 'filename': 'Imóvel certificado SNCI Público.zip',
82
+ 'dropdown': 'snci_pub',
83
+ },
84
+ 'Imóvel certificado SNCI Privado': {
85
+ 'filename': 'Imóvel certificado SNCI Privado.zip',
86
+ 'dropdown': 'snci_pri',
87
+ },
88
+ 'Projetos de Assentamento Total': {
89
+ 'filename': 'Assentamento Brasil.zip',
90
+ 'dropdown': 'assent',
91
+ },
92
+ 'Projetos de Assentamento Federal': {
93
+ 'filename': 'Assentamento Federal.zip',
94
+ 'dropdown': 'assent_f',
95
+ },
96
+ 'Projetos de Assentamento Reconhecimento': {
97
+ 'filename': 'Assentamento Reconhecimento.zip',
98
+ 'dropdown': 'assent_r',
99
+ },
100
+ 'Áreas de Quilombolas': {
101
+ 'filename': 'Áreas de Quilombolas.zip',
102
+ 'dropdown': 'quilom',
103
+ },
104
+ }
105
+ # Parâmetro
106
+ self.layer = layer
107
+ self._filename = dict_layers[layer]['filename']
108
+ self._dropdown = dict_layers[layer]['dropdown']
109
+ self.uf = uf
110
+
111
+ # Create Session
112
+ self.session = create_session()
113
+
114
+ @property
115
+ def _uf_to_update(self) -> str:
116
+ """
117
+ Ajusta a UF para fazer o update dos dados
118
+
119
+ :return: uf para inserir no `requests.post`
120
+ :rtype: str
121
+ """
122
+ if self.uf is None:
123
+ return ''
124
+ else:
125
+ return self.uf
126
+
127
+ @property
128
+ def _filename_to_download(self) -> str:
129
+ """
130
+ Ajusta o filename para fazer o download dos dados
131
+
132
+ :return: filename para inserir no `requests.get`
133
+ :rtype: str
134
+ """
135
+
136
+ # Estado
137
+ if self.uf is not None:
138
+ filename = self._filename.strip()
139
+ filename = filename.removesuffix('.zip')
140
+ filename = f'{filename}_{self.uf.upper()}.zip'
141
+ return filename
142
+
143
+ else:
144
+ return self._filename
145
+
146
+ def update(self):
147
+ """
148
+ Atualiza
149
+ """
150
+
151
+ # Faz o request para atualizar o dado
152
+ s = self.session.post(
153
+ url='https://certificacao.incra.gov.br/csv_shp/export_shp.py',
154
+ params={
155
+ 'selectshp': self._dropdown,
156
+ 'selectuf': self._uf_to_update,
157
+ },
158
+ stream=True,
159
+ )
160
+ if s.status_code != 200:
161
+ raise Exception(f'Não foi possível atualizar. {s.status_code}')
162
+
163
+ def download(self, output_path) -> None:
164
+ """
165
+ Fazer o download
166
+
167
+ :param output_path: Pasta que irá receber os arquivos
168
+ :type output_path: str | Path
169
+ """
170
+ url_lyr = urljoin(
171
+ base='https://certificacao.incra.gov.br/csv_shp/zip/',
172
+ url=quote(self._filename_to_download),
173
+ )
174
+
175
+ # Faz requisição
176
+ s = self.session.get(url=url_lyr, stream=True)
177
+
178
+ # Define nome do arquivo
179
+ self.output_file = Path(output_path) / self._filename_to_download
180
+
181
+ # Faz o request para obter o dado
182
+ with open(file=self.output_file, mode='wb') as f:
183
+ for chunk in s.iter_content(chunk_size=8192):
184
+ if chunk: # filtra keep-alive chunks
185
+ f.write(chunk)
186
+
187
+ print(self.get_shp_datetime())
188
+
189
+ def get_shp_datetime(self) -> datetime | None:
190
+ """
191
+ Obtem data do aquivo shapefile
192
+
193
+ :return: Data do arquivo shapefile
194
+ :rtype: datetime
195
+ """
196
+
197
+ with ZipFile(file=self.output_file, mode='r') as zf:
198
+ for info in zf.infolist():
199
+ if info.filename.endswith(".shp"):
200
+ return datetime(*info.date_time)
201
+
202
+ # dddd
203
+ return None
204
+
205
+ def to_geodtataframe(self) -> gpd.GeoDataFrame:
206
+ """
207
+ Lê o arquivo .zip baixado para o formato geodataframe
208
+
209
+ :return: Geodataframe
210
+ :rtype: gpd.GeoDataFrame
211
+ """
212
+ return gpd.read_file(filename=self.output_file)
213
+
214
+
215
+ if __name__ == '__main__':
216
+ inc = INCRA(layer='Imóvel certificado SIGEF Privado', uf='RJ')
217
+ print(inc._filename)
218
+ print(inc._dropdown)
@@ -0,0 +1,34 @@
1
+ """
2
+ Módulo necessário para requisição em TLS 1.2
3
+ """
4
+
5
+ import requests
6
+ import urllib3
7
+ from urllib3.util import create_urllib3_context
8
+
9
+
10
+ class CustomSSLContextHTTPAdapter(requests.adapters.HTTPAdapter):
11
+ def __init__(self, ssl_context=None, **kwargs) -> None:
12
+ self.ssl_context = ssl_context
13
+ super().__init__(**kwargs)
14
+
15
+ def init_poolmanager(
16
+ self, connections, maxsize, block=False, **kwargs
17
+ ) -> None:
18
+ self.poolmanager = urllib3.poolmanager.PoolManager(
19
+ num_pools=connections,
20
+ maxsize=maxsize,
21
+ block=block,
22
+ ssl_context=self.ssl_context,
23
+ )
24
+
25
+
26
+ def create_session() -> requests.Session:
27
+ ctx = create_urllib3_context()
28
+ ctx.load_default_certs()
29
+ ctx.set_ciphers('AES256-GCM-SHA384')
30
+
31
+ session = requests.session()
32
+ session.adapters.pop('https://', None)
33
+ session.mount('https://', CustomSSLContextHTTPAdapter(ctx))
34
+ return session
@@ -0,0 +1,22 @@
1
+ [project]
2
+ name = "incra"
3
+ version = "0.1.0"
4
+ description = "Obtem dados do INCRA usando python!"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ dependencies = [
8
+ "geopandas>=1.1.1",
9
+ "requests>=2.32.5",
10
+ "urllib3>=2.5.0",
11
+ ]
12
+
13
+ [dependency-groups]
14
+ dev = [
15
+ "notebook>=7.4.7",
16
+ ]
17
+ docs = [
18
+ "mkdocs-jupyter>=0.25.1",
19
+ "mkdocs-material[imaging]>=9.6.21",
20
+ "mkdocs-pdf>=0.1.2",
21
+ "pymdown-extensions>=10.16.1",
22
+ ]