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
17 changes: 0 additions & 17 deletions docs/source/firstDraft.py

This file was deleted.

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
54 changes: 54 additions & 0 deletions firstDraft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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:
"""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():
parcely_uri = os.path.join(os.path.dirname(__file__), "test", "sample_data", "testData.gpkg") + "|layername=parcely"
parcely_layer = QgsVectorLayer(parcely_uri, "parcely", "ogr")
parcely_field = "DruhPozemkuKod"

# headTail = os.path.split(os.path.dirname(__file__)) # TODO: po zarazeni do MainApp upravit cestu
csv_path = os.path.join(os.path.dirname(__file__), "files", "druh_pozemku.csv")
csv_layer = QgsVectorLayer(f"file:///{csv_path}?delimiter=;", "DruhPozemku", "delimitedtext")
csv_field = "KOD"

add_join_csv_file(parcely_layer, csv_layer, parcely_field, csv_field)
QgsProject.instance().addMapLayer(parcely_layer)
QgsProject.instance().addMapLayer(csv_layer, addToLegend=False)

if __name__ in ('__main__', '__script__', '__console__'):
main()
Binary file added test/sample_data/testData.gpkg
Binary file not shown.