Skip to content

Instantly share code, notes, and snippets.

@marcellobenigno
Last active October 27, 2023 12:55
Show Gist options
  • Save marcellobenigno/a849f367057a1837e397bc8e22fef1b3 to your computer and use it in GitHub Desktop.
Save marcellobenigno/a849f367057a1837e397bc8e22fef1b3 to your computer and use it in GitHub Desktop.
Exemplo de criação de pontos no QGIS
def create_point(name):
uri = "point?crs=epsg:4326"
points = QgsVectorLayer(uri, name, 'memory')
QgsProject.instance().addMapLayer(points)
# chamando o provider para poder criar
# tanto os campos, quanto validar todo o processo:
provider = points.dataProvider()
# definindo os campos da minha camada
attributes = [
QgsField('id', QVariant.Int),
QgsField('nome', QVariant.String),
QgsField('x', QVariant.Double),
QgsField('y', QVariant.Double),
]
# adicionando os campos a camada
provider.addAttributes(attributes)
# atualiza a tabela de atributos
points.updateFields()
def add_point_feature(lyr_name, x, y, id, name):
lyr = QgsProject.instance().mapLayersByName(lyr_name)[0]
provider = lyr.dataProvider()
feat = QgsFeature()
feat.setGeometry(QgsPoint(x,y))
feat.setAttributes([
id,
name,
x,
y
])
provider.addFeatures([feat])
create_point('pontos')
pontos_list = [
{'nome': 'A', 'x': -36.79, 'y': -7.24},
{'nome': 'B', 'x': -36.87, 'y': -7.94},
{'nome': 'C', 'x': -35.20, 'y': -7.15},
{'nome': 'D', 'x': -38.49, 'y': -6.71},
]
i = 0
for ponto in pontos_list:
i += 1
add_point_feature('pontos', ponto['x'], ponto['y'], i, ponto['nome'])
#add_point_feature('pontos', -36.883, -6.951, 1, 'Ponto 1')
#add_point_feature('pontos', -36.870, -6.950, 2, 'Ponto 2')
#add_point_feature('pontos', -36.860, -6.852, 3, 'Ponto 3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment