예제 #1
0
파일: qpy3.py 프로젝트: abeldan/qpy3
 def close(self):
     # Exit applications
     QgsApplication.exitQgis()
     QgsApplication.exit()
예제 #2
0
FOR WINDOWS:
    Open the OSGeo Shell and run the python-qgis-ltr.bat file, or just execute python-qgis-ltr.bat from the file explorer

FOR LINUX:
    You need to prepare the environment with PYTHONPATH and LD_LIBRARY_PATH variables. For example:
       export PYTHONPATH=/docs/dev/qgis/QGIS/3_14_0/output/python/
       export LD_LIBRARY_PATH=/docs/dev/qgis/QGIS/3_14_0/lib/
"""
from qgis.core import (QgsApplication, QgsVectorLayer)

# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix
# Hint: print(QgsApplication.showSettings())
QgsApplication.setPrefixPath('/docs/dev/qgis/QGIS/3_14_0/output', True)
qgs = QgsApplication([], False)  # No GUI
qgs.initQgis()

airport_layer = QgsVectorLayer(
    "/docs/tr/qgis_col/eventos/talleres/Semana_ICG_2019/data/sample_data_4326.gpkg|layername=Airports",
    "airport", "ogr")

print("\n\n\n############### RESULTADOS ####################")
print("\nLayer is valid:", airport_layer.isValid())
print("Layer CRS:", airport_layer.crs().description())
print("Layer's feature count:", airport_layer.featureCount())
print("\n       NOMBRE        \t\t\t              GEOMETRÍA")
for f in airport_layer.getFeatures():
    print(f['name'], '\t\t\t', f.geometry().asWkt())
print("\n\n\n#############################################")

qgs.exit()
from qgis.core import QgsApplication
from PyQt5.QtWidgets import QDialog

GUIEnabled = True
app = QgsApplication([], GUIEnabled)

dlg = QDialog()
dlg.exec_()

app.exit(app.exec_())
예제 #4
0
def convert_qgis_gpkg_to_kml(qgs_file: str, output_kml_path: str, stage_dir: str = None) -> str:

    app = None
    driver_name = "libkml"

    try:
        from qgis.core import QgsApplication, QgsProject

        # Load application to load QApplication dependencies.
        app_directory = stage_dir  # hosts various session type files like qgis.db etc...
        app = QgsApplication([], False, app_directory)
        app.initQgis()

        # Application needs to be initialized before importing.
        from qgis.core import QgsVectorFileWriter

        # Load project from template output.
        project = QgsProject.instance()
        if not project.read(qgs_file):
            raise Exception(f"Could not read from {qgs_file}")

        layers = project.mapLayers()
        symbology_scale = 20000
        if stage_dir:
            output_dir = os.path.join(stage_dir, "kml")
        else:
            output_dir = os.path.join(".", "kml")

        kml_files = []
        for layer_name, layer in layers.items():
            output_filename = f"{layer.source().partition('layername=')[2].split('|')[0]}.kml"
            output_filepath = os.path.join(output_dir, output_filename)
            kml_files.append(output_filename)
            if not os.path.exists(output_dir):
                os.mkdir(output_dir)
            QgsVectorFileWriter.writeAsVectorFormat(
                layer=layer,
                fileName=output_filepath,
                fileEncoding="utf-8",
                driverName=driver_name,
                symbologyExport=QgsVectorFileWriter.SymbolLayerSymbology,
                symbologyScale=symbology_scale,
            )

        out_driver = ogr.GetDriverByName(driver_name)
        if os.path.exists(output_kml_path):
            out_driver.DeleteDataSource(output_kml_path)

        out_driver.CreateDataSource(output_kml_path)

        # Ensure the land_polygons and boundary layers are at the bottom when the files get merged.
        try:
            kml_files.append(kml_files.pop(kml_files.index("land_polygons.kml")))
            kml_files.append(kml_files.pop(kml_files.index("boundary.kml")))
        except ValueError as ve:
            logger.warning(ve)

        # Merge all of the KML files into a single file.
        for kml_file in kml_files:
            kml_path = os.path.join(output_dir, kml_file)
            gdal.VectorTranslate(output_kml_path, kml_path, accessMode="append")

        return output_kml_path

    finally:
        if app:
            app.exit()