I'd like to transfer manual processing steps to a python script. Though I'm not used to python yet.
The steps I do in QGIS:
- Loading csv via right click in browser window into layer window.
- the loaded table contains the coordinates in one column called "coords_lat_long". therefore, I split those coordinates in the Field Calculator using "lat" = left("coords_lat_long", strpos("coords_lat_long", ',')-1) and "long" = right("coords_lat_long", strpos("coords_lat_long", ',')-1)
- after saving that, I load the csv again using "Add Tab Delimited Layer", tab separated with x-value = long and y-value = lat and WGS84
- Then I export the layer to an existing GeoPackage and change the reference system to EPSG:2056 LV95.
- Finally I'll have my point layer.
However, how can I transfer those steps into a python script?
What I have so far but can't extract lat and long:
uri = "thePathToMyCSV.csv"
csv_layer = iface.addVectorLayer(uri, "Standort_table", "ogr")
for field in csv_layer.fields():
print(field.name())
f = QgsFeature()
csv_layer.startEditing()
lat_value = 'lat'
csv_layer.addAttribute(QgsField(lat_value, QVariant.Double))
long_value = 'long'
csv_layer.addAttribute(QgsField(long_value, QVariant.Double))
csv_layer.updateFields()
exp1 = QgsExpression('strpos("coords_lat_long", \',\')')
exp3 = QgsExpression('left(coords_lat_long, exp1)-1)')
exp4 = QgsExpression('right(coords_lat_long, exp1)-1)')
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(csv_layer))
with edit(csv_layer):
for f in csv_layer.getFeatures():
context.setFeature(f)
f['lat'] = exp3.evaluate(context)
f['long'] = exp4.evaluate(context)
csv_layer.updateFeature(f)