Skip to content

Instantly share code, notes, and snippets.

@marcellobenigno
Last active March 12, 2025 11:31
Show Gist options
  • Save marcellobenigno/ea4e7f92fe1771d47a3e08af6c50d2a8 to your computer and use it in GitHub Desktop.
Save marcellobenigno/ea4e7f92fe1771d47a3e08af6c50d2a8 to your computer and use it in GitHub Desktop.
Exemplo de Script de Processamento no QGIS
# Substitua o path, apontando para o seu GeoPackage:
gpkg = '/Users/marcellodebarrosfilho/code/curso_geopandas/dados/pb.gpkg'
drenagem = f'{gpkg}|layername=drenagem'
# 1. Filtrar o rio, em função do seu nome
# nome do rio (verifique na tabela de atributos)
value = 'Rio Paraíba'
# raio do buffer
radius = 6000
params = {
'INPUT': drenagem,
'FIELD':'nome',
'OPERATOR':0,
'VALUE': value,
'OUTPUT':'TEMPORARY_OUTPUT'
}
rio_layer = processing.run("native:extractbyattribute", params)['OUTPUT']
rio_layer.setName(value)
# 2. Reprojetar para uma projeção cônica (5880)
params = {
'INPUT': rio_layer,
'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:5880'),
'OUTPUT':'TEMPORARY_OUTPUT'
}
rio_5880_layer = processing.run("native:reprojectlayer", params)['OUTPUT']
rio_5880_layer.setName(f"{value} - (Reprojetado EPSG:5880)")
# 3. Gerar um buffer, com x km de raio
params = {
'INPUT':rio_5880_layer,
'DISTANCE': radius,
'SEGMENTS':5,
'END_CAP_STYLE':0,
'JOIN_STYLE':0,
'MITER_LIMIT':2,
'DISSOLVE':True,
'OUTPUT':'TEMPORARY_OUTPUT'
}
buffer_layer = processing.run("native:buffer", params)['OUTPUT']
buffer_layer.setName(f'Buffer {radius} m - {value}')
# 4. Recortar os municípios (clip), dentro da área do buffer
municipios = f'{gpkg}|layername=municipios'
params = {
'INPUT':municipios,
'OVERLAY':buffer_layer,
'OUTPUT':'TEMPORARY_OUTPUT'
}
clip_municipios_rio = processing.run("native:clip", params)['OUTPUT']
clip_municipios_rio.setName(f'Clip Municípios - {value}')
# 5. Adicionar as camadas produzidas nos passos anteriores, no QGIS
QgsProject.instance().addMapLayer(buffer_layer)
QgsProject.instance().addMapLayer(clip_municipios_rio)
QgsProject.instance().addMapLayer(rio_5880_layer)
QgsProject.instance().addMapLayer(rio_layer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment