Created
November 28, 2020 02:18
-
-
Save bkamapantula/9461ee050925343aa0c9c2904460f1a1 to your computer and use it in GitHub Desktop.
Import and export several files - QGIS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Add several CSV files as Delimited Text Layer(s) with x coordinate as Longitude and y as Lattitude. | |
This script is adapted from https://chris35wills.github.io/multi_csv_into_qgis/. | |
Updated `addAttributeAlias` to `setFieldAlias` as it's renamed. | |
""" | |
import os.path, glob | |
layers = [] | |
path_to_csvs = "" # file path to all CSVs | |
for f in glob.glob(path_to_csvs): | |
# the source files I used have attributes `Longitude` and `Lattitude`. update the names accordingly. | |
uri = "file:///" + f + "?type=csv&xField=%s&yField=%s&spatialIndex=no&subsetIndex=no&watchFile=no&crs=epsg:4326" % ('Longitude', 'Lattitude') | |
vlayer = QgsVectorLayer(uri, os.path.basename(f), "delimitedtext") | |
vlayer.setFieldAlias(0,'x') | |
vlayer.setFieldAlias(1,'y') | |
layers.append(vlayer) | |
QgsProject.instance().addMapLayers(layers) | |
# this will iteratively add layers to QGIS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Exports current layers in QGIS to GeoJSON files. | |
This script is adapted from https://gis.stackexchange.com/a/131177 | |
""" | |
geojsons_dir = "" # file path to save all GeoJSON files | |
for vLayer in iface.mapCanvas().layers(): | |
QgsVectorFileWriter.writeAsVectorFormat( vLayer, | |
geojsons_dir+ vLayer.name() + ".shp", "utf-8", | |
vLayer.crs(), "GeoJSON") | |
# this will iteratively create GeoJSON files in target directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment