Skip to content

Instantly share code, notes, and snippets.

@egoddard
Created September 29, 2016 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egoddard/c188c62115f2fe8df24f32b3119e51b3 to your computer and use it in GitHub Desktop.
Save egoddard/c188c62115f2fe8df24f32b3119e51b3 to your computer and use it in GitHub Desktop.
PYQGIS/Processing from a standalone script. Issue with initializing Processing.
import sys
import os
from datetime import datetime
from qgis.core import *
import exifread
QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication([], True)
qgs.initQgis()
# Initialize processing according to the SO: http://gis.stackexchange.com/questions/129915/cannot-run-standalone-qgis-script
sys.path.append("/home/eric/.qgis2/python/plugins")
from processing.core.Processing import Processing
Processing.Initialize() # Crash!
from processing.tools import *
BOUNDARY_FIELD_IDX = 25
boundaries_lyr = QgsVectorLayer("boundaries.shp", "boundaries", "ogr")
photos_lyr = QgsVectorLayer("Photos.gpkg", "photo_points", "ogr")
# Contains feature ids as keys with an inner dictionary of the attribute index
# and new value pairs, structured like { 220: {24: 'May 2016', 25: 'Allen #2'} }
update_features = {}
# Extract the picture date from metadata. Working as intended.
for feature in photos_lyr.getFeatures():
photo = feature['photo_name']
# Make sure the photo isn't null and that the picture exists
if photo and os.path.exists("{}.JPG".format(os.path.join(PHOTO_DIR, photo))):
with open("{}.JPG".format(os.path.join(PHOTO_DIR, photo)), 'rb') as f:
tags = exifread.process_file(f, details=False,
stop_tag='EXIF DateTimeOriginal')
photo_date = datetime.strptime(str(tags['EXIF DateTimeOriginal']),
EXIF_DATE_FORMAT).strftime(OUTPUT_DATE_FORMAT)
attrs = {PHOTO_DATE_FIELD_IDX: photo_date}
update_features[feature.id()] = attrs
boundary_filter = QgsExpression(""""zone" = 2""")
boundaries = set([f['boundary'] for f in boundaries_lyr.getFeatures(
QgsFeatureRequest(boundary_filter))])
# for each unique boundary name in the set of boundaries, select the features
# that have that boundary name to be used in the select by location algorithm
for boundary in boundaries:
filter_expression = """"boundary" = '{}'"""
ids = [f.id() for f in boundaries_lyr.getFeatures(QgsFeatureRequest(
QgsExpression(filter_expression.format(boundary))))]
boundaries_lyr.setSelectedFeatures(ids)
general.runalg('qgis:selectbylocation', photos_lyr, boundaries_lyr,
u'intersect', 0)
# Update the update_features dictionary with the name of the boundary the
# point is in
for photo in photos_lyr.selectedFeatures():
update_features[photo.id()][BOUNDARY_FIELD_IDX] = boundary
well_photos_lyr.dataProvider().changeAttributeValues(update_features)
#print update_features
del photos_lyr, boundaries_lyr
qgs.exitQgis()
@gacarrillor
Copy link

I haven't tested the whole script, but I notice you should call initialize() and not Initialize() in line #15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment