<jupyter_start><jupyter_text>Allow the necessary imports to be installed<jupyter_code>%%!
pip install --trusted-host pypi.org ipython-sql

%%!
pip install wget
import os
import sys
from osgeo import ogr
os.environ['QT_QPA_PLATFORM'] = 'offscreen'
#sys.path.insert(0,'/usr/local/share/qgis/python')
from qgis.core import *
#QgsApplication.setPrefixPath("/usr/local", True) 
from qgis.server import *
from qgis.gui import *
from qgis import processing

from qgis.PyQt.QtGui import QColor, QImage
from qgis.PyQt.QtCore import QSize, QBuffer, QIODevice



qgs = QgsApplication([], False)
qgs.initQgis()
print(QgsApplication.showSettings())

from qgis.PyQt.QtGui import QColor, QImage
from qgis.PyQt.QtCore import QSize, QBuffer, QIODevice
from IPython.display import Image<jupyter_output>Application state:
QGIS_PREFIX_PATH env var:		C:/OSGEO4~1/apps/qgis
Prefix:		C:/OSGEO4~1/apps/qgis
Plugin Path:		C:/OSGEO4~1/apps/qgis/plugins
Package Data Path:	C:/OSGEO4~1/apps/qgis/.
Active Theme Name:	
Active Theme Path:	C:/OSGEO4~1/apps/qgis/./resources/themes\\icons/
Default Theme Path:	:/images/themes/default/
SVG Search Paths:	C:/OSGEO4~1/apps/qgis/./svg/
		C:/Users/andre/AppData/Roaming/python3\profiles\default/svg/
User DB Path:	C:/OSGEO4~1/apps/qgis/./resources/qgis.db
Auth DB Path:	C:/Users/andre/AppData/Roaming/python3\profiles\default/qgis-auth.db<jupyter_text>Code to Join two Layers (shp and csv) with a field in common<jupyter_code>vectorLyr=QgsVectorLayer("C:/OSGeo4W64/bin/Andre/Covid-19Brazil/Tables/ShapesBrazil/lml_unidade_federacao_a.shp","Brasil","ogr")
vectorLyr.isValid()

uri='file:///C:/OSGeo4W64/bin/Andre/Covid-19Brazil/Tables/cases-brazil-total.csv?delimiter=,'
infoLyr=QgsVectorLayer(uri,'casosBrasil','delimitedtext')
infoLyr.isValid()

QgsProject.instance().addMapLayer(vectorLyr)
QgsProject.instance().addMapLayer(infoLyr)
csvField='state'
shpField='sigla'

joinObject=QgsVectorLayerJoinInfo()
joinObject.setJoinFieldName(csvField)
joinObject.setTargetFieldName(shpField)
joinObject.setJoinLayerId(infoLyr.id())
joinObject.setUsingMemoryCache(False)
joinObject.setJoinLayer(infoLyr)
vectorLyr.addJoin(joinObject)

vlayer = QgsProject.instance().mapLayersByName('Brasil')[0]

for field in vlayer.fields():
    print("Column "+field.name()+" Load")<jupyter_output>Column nome Load
Column geometriaa Load
Column sigla Load
Column geocodigo Load
Column casosBrasil_country Load
Column casosBrasil_totalCases Load
Column casosBrasil_totalCasesMS Load
Column casosBrasil_notConfirmedByMS Load
Column casosBrasil_deaths Load
Column casosBrasil_deathsMS Load
Column casosBrasil_URL Load
Column casosBrasil_deaths_per_100k_inhabitants Load
Column casosBrasil_totalCases_per_100k_inhabitants Load
Column casosBrasil_deaths_by_totalCases Load
Column casosBrasil_recovered Load
Column casosBrasil_suspects Load
Column casosBrasil_tests Load
Column casosBrasil_tests_per_100k_inhabitants Load
Column casosBrasil_date Load
Column casosBrasil_newCases Load
Column casosBrasil_newDeaths Load<jupyter_text>Use Label (deaths) to show number of cases in Brazil per State  Number of Deaths<jupyter_code>from qgis.utils import iface


label_settings = QgsPalLayerSettings()
label_settings.drawBackground = True
label_settings.fieldName = "casosBrasil_deaths"


text_format = QgsTextFormat()
background_color = QgsTextBackgroundSettings()

background_color.setEnabled(False)
text_format.setBackground(background_color )
label_settings.setFormat(text_format)

vlayer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))
vlayer.setLabelsEnabled(True)
vlayer.triggerRepaint()


vlayer = QgsProject.instance().mapLayersByName('Brasil')[0]

xt = vlayer.extent()
# print(xt)
width  = 1000
height = int(width*xt.height()/xt.width())
print("Gerar mapa com {} por {}".format(width, height))

options = QgsMapSettings()
options.setLayers([vlayer])
options.setBackgroundColor(QColor(255, 255, 255))

options.setOutputSize(QSize(width, height))
options.setExtent(xt)
           
render = QgsMapRendererParallelJob(options)
render.start()
render.waitForFinished()

image = render.renderedImage()
imgbuf= QBuffer()
imgbuf.open( QIODevice.ReadWrite )
image.save( imgbuf,"PNG" )
image.save('fig/brasilDe.png',"PNG")
Image( imgbuf.data() )<jupyter_output>Gerar mapa com 1000 por 864<jupyter_text>Number os tests per State<jupyter_code>from qgis.utils import iface


label_settings = QgsPalLayerSettings()
label_settings.drawBackground = True
label_settings.fieldName = "casosBrasil_tests"


text_format = QgsTextFormat()
background_color = QgsTextBackgroundSettings()

background_color.setEnabled(False)
text_format.setBackground(background_color )
label_settings.setFormat(text_format)

vlayer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))
vlayer.setLabelsEnabled(True)
vlayer.triggerRepaint()

vlayer = QgsProject.instance().mapLayersByName('Brasil')[0]

xt = vlayer.extent()
# print(xt)
width  = 1000
height = int(width*xt.height()/xt.width())
print("Gerar mapa com {} por {}".format(width, height))

options = QgsMapSettings()
options.setLayers([vlayer])
options.setBackgroundColor(QColor(255, 255, 255))

options.setOutputSize(QSize(width, height))
options.setExtent(xt)
           
render = QgsMapRendererParallelJob(options)
render.start()
render.waitForFinished()

image = render.renderedImage()
imgbuf= QBuffer()
imgbuf.open( QIODevice.ReadWrite )
image.save( imgbuf,"PNG" )
image.save('fig/brasilTe.png',"PNG")
Image( imgbuf.data() )

from qgis.PyQt import QtGui

myVectorLayer = QgsProject.instance().mapLayersByName('Brasil')[0]

myTargetField = 'casosBrasil_tests'
myRangeList = []
myOpacity = 1
# Make our first symbol and range...
myMin = 0
myMax = 12714.0
myLabel = 'Group 1'
myColour = QtGui.QColor('#ffffff')
mySymbol1 = QgsSymbol.defaultSymbol(myVectorLayer.geometryType())
mySymbol1.setColor(myColour)
mySymbol1.setOpacity(myOpacity)
myRange1 = QgsRendererRange(myMin, myMax, mySymbol1, myLabel)
myRangeList.append(myRange1)

#now make another symbol and range...
myMin = 12714.1
myMax = 19939.0
myLabel = 'Group 2'
myColour = QtGui.QColor('#ffbfbf')
mySymbol2 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol2.setColor(myColour)
mySymbol2.setOpacity(myOpacity)
myRange2 = QgsRendererRange(myMin, myMax, mySymbol2, myLabel)
myRangeList.append(myRange2)

#and another
myMin = 19939.1
myMax = 42162.0
myLabel = 'Group 3'
myColour = QtGui.QColor('#ff8080')
mySymbol3 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol3.setColor(myColour)
mySymbol3.setOpacity(myOpacity)
myRange3 = QgsRendererRange(myMin, myMax, mySymbol3, myLabel)
myRangeList.append(myRange3)

#aaaaaaaaaaaahh
myMin = 42162.1
myMax = 65971.0
myLabel = 'Group 4'
myColour = QtGui.QColor('#ff4040')
mySymbol4 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol4.setColor(myColour)
mySymbol4.setOpacity(myOpacity)
myRange4 = QgsRendererRange(myMin, myMax, mySymbol4, myLabel)
myRangeList.append(myRange4)

#............
myMin = 65971.1
myMax = 140151.0
myLabel = 'Group 5'
myColour = QtGui.QColor('#ff0000')
mySymbol5 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol5.setColor(myColour)
mySymbol5.setOpacity(myOpacity)
myRange5 = QgsRendererRange(myMin, myMax, mySymbol5, myLabel)
myRangeList.append(myRange5)



myRenderer = QgsGraduatedSymbolRenderer('', myRangeList)
myClassificationMethod = QgsApplication.classificationMethodRegistry().method("EqualInterval")
myRenderer.setClassificationMethod(myClassificationMethod)
myRenderer.setClassAttribute(myTargetField)

myVectorLayer.setRenderer(myRenderer)
from qgis.utils import iface


label_settings = QgsPalLayerSettings()
label_settings.drawBackground = True
label_settings.fieldName = "casosBrasil_tests"


text_format = QgsTextFormat()
background_color = QgsTextBackgroundSettings()

background_color.setEnabled(False)
text_format.setBackground(background_color )
label_settings.setFormat(text_format)

vlayer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))
vlayer.setLabelsEnabled(True)
vlayer.triggerRepaint()

vlayer = QgsProject.instance().mapLayersByName('Brasil')[0]

xt = vlayer.extent()
# print(xt)
width  = 1000
height = int(width*xt.height()/xt.width())
print("Gerar mapa com {} por {}".format(width, height))

options = QgsMapSettings()
options.setLayers([vlayer])
options.setBackgroundColor(QColor(255, 255, 255))

options.setOutputSize(QSize(width, height))
options.setExtent(xt)
           
render = QgsMapRendererParallelJob(options)
render.start()
render.waitForFinished()

image = render.renderedImage()
imgbuf= QBuffer()
imgbuf.open( QIODevice.ReadWrite )
image.save( imgbuf,"PNG" )
image.save('fig/brasilTe-color.png',"PNG")
Image( imgbuf.data() )<jupyter_output>Gerar mapa com 1000 por 864<jupyter_text>Number of deaths by color<jupyter_code>vectorLyr=QgsVectorLayer("C:/OSGeo4W64/bin/Andre/Covid-19Brazil/Tables/ShapesBrazil/lml_unidade_federacao_a.shp","Brasil","ogr")
vectorLyr.isValid()

uri='file:///C:/OSGeo4W64/bin/Andre/Covid-19Brazil/Tables/cases-brazil-total.csv?delimiter=,'
infoLyr=QgsVectorLayer(uri,'casosBrasil','delimitedtext')
infoLyr.isValid()

QgsProject.instance().addMapLayer(vectorLyr)
QgsProject.instance().addMapLayer(infoLyr)
csvField='state'
shpField='sigla'

joinObject=QgsVectorLayerJoinInfo()
joinObject.setJoinFieldName(csvField)
joinObject.setTargetFieldName(shpField)
joinObject.setJoinLayerId(infoLyr.id())
joinObject.setUsingMemoryCache(False)
joinObject.setJoinLayer(infoLyr)
vectorLyr.addJoin(joinObject)

vlayer = QgsProject.instance().mapLayersByName('Brasil')[0]

for field in vlayer.fields():
    print("Column "+field.name()+" Load")

from qgis.utils import iface


label_settings = QgsPalLayerSettings()
label_settings.drawBackground = True
label_settings.fieldName = "casosBrasil_deaths"


text_format = QgsTextFormat()
background_color = QgsTextBackgroundSettings()

background_color.setEnabled(False)
text_format.setBackground(background_color )
label_settings.setFormat(text_format)

vlayer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))
vlayer.setLabelsEnabled(True)
vlayer.triggerRepaint()


from qgis.PyQt import QtGui

myVectorLayer = QgsProject.instance().mapLayersByName('Brasil')[0]

myTargetField = 'casosBrasil_deaths'
myRangeList = []
myOpacity = 1
# Make our first symbol and range...
myMin = 0
myMax = 134.0
myLabel = 'Group 1'
myColour = QtGui.QColor('#ffffff')
mySymbol1 = QgsSymbol.defaultSymbol(myVectorLayer.geometryType())
mySymbol1.setColor(myColour)
mySymbol1.setOpacity(myOpacity)
myRange1 = QgsRendererRange(myMin, myMax, mySymbol1, myLabel)
myRangeList.append(myRange1)

#now make another symbol and range...
myMin = 134.1
myMax = 165.0
myLabel = 'Group 2'
myColour = QtGui.QColor('#ffbfbf')
mySymbol2 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol2.setColor(myColour)
mySymbol2.setOpacity(myOpacity)
myRange2 = QgsRendererRange(myMin, myMax, mySymbol2, myLabel)
myRangeList.append(myRange2)

#and another
myMin = 165.1
myMax = 303.0
myLabel = 'Group 3'
myColour = QtGui.QColor('#ff8080')
mySymbol3 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol3.setColor(myColour)
mySymbol3.setOpacity(myOpacity)
myRange3 = QgsRendererRange(myMin, myMax, mySymbol3, myLabel)
myRangeList.append(myRange3)

#aaaaaaaaaaaahh
myMin = 303.0
myMax = 1795.0
myLabel = 'Group 4'
myColour = QtGui.QColor('#ff4040')
mySymbol4 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol4.setColor(myColour)
mySymbol4.setOpacity(myOpacity)
myRange4 = QgsRendererRange(myMin, myMax, mySymbol4, myLabel)
myRangeList.append(myRange4)

#............
myMin = 1795.1
myMax = 1000000.0
myLabel = 'Group 5'
myColour = QtGui.QColor('#ff0000')
mySymbol5 = QgsSymbol.defaultSymbol(
     myVectorLayer.geometryType())
mySymbol5.setColor(myColour)
mySymbol5.setOpacity(myOpacity)
myRange5 = QgsRendererRange(myMin, myMax, mySymbol5, myLabel)
myRangeList.append(myRange5)



myRenderer = QgsGraduatedSymbolRenderer('', myRangeList)
myClassificationMethod = QgsApplication.classificationMethodRegistry().method("EqualInterval")
myRenderer.setClassificationMethod(myClassificationMethod)
myRenderer.setClassAttribute(myTargetField)

myVectorLayer.setRenderer(myRenderer)
vlayer = QgsProject.instance().mapLayersByName('Brasil')[0]

xt = vlayer.extent()
# print(xt)
width  = 1000
height = int(width*xt.height()/xt.width())
print("Gerar mapa com {} por {}".format(width, height))

options = QgsMapSettings()
options.setLayers([vlayer])
options.setBackgroundColor(QColor(255, 255, 255))

options.setOutputSize(QSize(width, height))
options.setExtent(xt)
           
render = QgsMapRendererParallelJob(options)
render.start()
render.waitForFinished()

image = render.renderedImage()
imgbuf= QBuffer()
imgbuf.open( QIODevice.ReadWrite )
image.save( imgbuf,"PNG" )
image.save('fig/brasildeaths-color.png',"PNG")
Image( imgbuf.data() )<jupyter_output>Gerar mapa com 1000 por 864
{'fraction_non_alphanumeric': 0.1174057735469391, 'fraction_numerical': 0.024920671774630445, 'mean_word_length': 7.380025940337225, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '592783', 'license': 'CC0-1.0', 'n_tokens_mistral': 4893, 'n_tokens_neox': 4634, 'n_words': 772, 'path': 'Casos de estudo/Brasil/Covid-19Brazil/mapsBrasil-Deaths.ipynb', 'repo_name': 'carolina55/covid-pt', 'size': '12921'}