Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

přidán číselník a vytvořena funkce #4

Merged
merged 10 commits into from
Jun 23, 2024
73 changes: 56 additions & 17 deletions docs/source/firstDraft.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
from qgis.core import QgsProject
theremias marked this conversation as resolved.
Show resolved Hide resolved

csv = QgsProject.instance().mapLayersByName('SC_D_POZEMKU')[0]
csvField = 'KOD'
layer = QgsProject.instance().mapLayersByName('Parcely')[0]
layerField = 'DruhPozemkuKod'

joinObject = QgsVectorLayerJoinInfo()
joinObject.setJoinLayerId(csv.id())
joinObject.setJoinFieldName(csvField)
joinObject.setTargetFieldName(layerField)
joinObject.setPrefix('g')
joinObject.setUsingMemoryCache(True)
joinObject.setJoinLayer(csv)

layer.addJoin(joinObject)
QgsProject.instance().addMapLayer(layer)
import os
from qgis.core import QgsProject, QgsVectorLayer, QgsVectorLayerJoinInfo

def add_join_csv_file(target_layer: QgsVectorLayer, csv_layer: QgsVectorLayer, target_field: str = "DruhPozemkuKod", csv_field: str = "KOD", csv_subset: list = ["NAZEV"]) -> QgsVectorLayer:
landam marked this conversation as resolved.
Show resolved Hide resolved
"""Performs left outer join from 'target_layer' to 'csv_layer'. Initially used to perform a join from 'Parcely' to code list of land types.

@type target_layer: QgsVectorLayer
@param target_layer: A layer on which the join will be performed
@type csv_layer: QgsVectorLayer
@param csv_layer: A layer which will be joined to the 'target_layer'
@type target_field: str
@param target_field: Name of the field of the 'target_layer' that will be used for join
@type csv_field: str
@param csv_field: Name of the field of the 'csv_layer' that will be used for join
@type csv_subset: list
@param csv_subset: Subset of fields to be used from joined layer

@rtype: QgsVectorLayer
@returns: A vector layer ('target_layer') with left outer join to other vector layer ('csv_layer')
"""
joinObject = QgsVectorLayerJoinInfo()
joinObject.setJoinLayerId(csv_layer.id())
joinObject.setJoinFieldName(csv_field)
joinObject.setTargetFieldName(target_field)
joinObject.setUsingMemoryCache(True)
joinObject.setPrefix("")
joinObject.setJoinLayer(csv_layer)
joinObject.setJoinFieldNamesSubset(csv_subset)

target_layer.addJoin(joinObject)

return target_layer


def rearrange_columns(self, layer: QgsVectorLayer, new_order: list) -> None:
# rearrange columns: https://gis.stackexchange.com/a/445885 (ALE POTREBUJI CELEJ LIST SLOUPCU ASI)
pass

def main():
uri ="c:\\Users\\Vojta\\_CVUT\\2023_2024\\FGIS\\ruianTest.gpkg|layername=parcely"
landam marked this conversation as resolved.
Show resolved Hide resolved
parcely_layer = QgsVectorLayer(uri, "parcely", "ogr")
parcely_field = "DruhPozemkuKod"

# headTail = os.path.split(os.path.dirname(__file__)) # TODO: po zarazeni do MainApp upravit cestu
path = "file:///c:\\Users\\Vojta\\_CVUT\\2023_2024\\FGIS\\data\\SC_D_POZEMKU.csv?delimiter=;"
csv_layer = QgsVectorLayer(path, "DruhPozemku", "delimitedtext")
csv_field = "KOD"



mylayer = add_join_csv_file(parcely_layer, csv_layer, parcely_field, csv_field)
landam marked this conversation as resolved.
Show resolved Hide resolved
QgsProject.instance().addMapLayer(mylayer)
new_order = []

if __name__ in ('__main__', '__script__', '__console__'):
main()
12 changes: 12 additions & 0 deletions files/druh_pozemku.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
KOD;NAZEV;ZEMEDELSKA_KULTURA;PLATNOST_OD;PLATNOST_DO;ZKRATKA;TYPPPD_KOD;STAVEBNI_PARCELA;POVINNA_OCHRANA_POZ;POVINNY_ZPUSOB_VYUZ
2;orná půda;a;01.01.1993;;orná půda;;n;27;n
3;chmelnice;a;01.01.1993;;chmelnice;302;n;27;n
4;vinice;a;01.01.1993;;vinice;303;n;27;n
5;zahrada;a;01.01.1993;;zahrada;304;n;27;n
6;ovocný sad;a;01.01.1993;;ovoc. sad;305;n;27;n
7;trvalý travní porost;a;01.01.1993;;travní p.;306;n;27;n
8;trvalý travní porost;a;01.01.1993;01.09.2000;travní p.;307;n;27;n
10;lesní pozemek;n;01.01.1993;;lesní poz;308;n;26;n
11;vodní plocha;n;01.01.1993;;vodní pl.;;n;;a
13;zastavěná plocha a nádvoří;n;01.01.1993;;zast. pl.;;a;;n
14;ostatní plocha;n;01.01.1993;;ostat.pl.;;n;;a
Binary file added files/testData.gpkg
Binary file not shown.