diff --git a/docs/source/firstDraft.py b/docs/source/firstDraft.py deleted file mode 100644 index 39120c8..0000000 --- a/docs/source/firstDraft.py +++ /dev/null @@ -1,17 +0,0 @@ -from qgis.core import QgsProject - -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) \ No newline at end of file diff --git a/files/druh_pozemku.csv b/files/druh_pozemku.csv new file mode 100644 index 0000000..44fd0e3 --- /dev/null +++ b/files/druh_pozemku.csv @@ -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 diff --git a/firstDraft.py b/firstDraft.py new file mode 100644 index 0000000..88c16d9 --- /dev/null +++ b/firstDraft.py @@ -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() diff --git a/test/sample_data/testData.gpkg b/test/sample_data/testData.gpkg new file mode 100644 index 0000000..72d919d Binary files /dev/null and b/test/sample_data/testData.gpkg differ