|
| 1 | +import os |
| 2 | +from datetime import datetime |
| 3 | +from pytz import timezone |
| 4 | +import pytz |
| 5 | +from astral.sun import sun |
| 6 | +import astral |
| 7 | + |
| 8 | +from qgis.core import ( |
| 9 | + QgsPointXY, QgsFeature, QgsGeometry, QgsField, |
| 10 | + QgsProject, QgsWkbTypes, QgsCoordinateTransform) |
| 11 | + |
| 12 | +from qgis.core import ( |
| 13 | + QgsProcessing, |
| 14 | + QgsProcessingAlgorithm, |
| 15 | + QgsProcessingException, |
| 16 | + QgsProcessingParameterBoolean, |
| 17 | + QgsProcessingParameterDateTime, |
| 18 | + QgsProcessingParameterFeatureSource, |
| 19 | + QgsProcessingParameterFeatureSink) |
| 20 | + |
| 21 | +from qgis.PyQt.QtGui import QIcon |
| 22 | +from qgis.PyQt.QtCore import QVariant, QUrl |
| 23 | + |
| 24 | +from .settings import epsg4326, tzf_instance |
| 25 | + |
| 26 | +class AddAstronomicalAlgorithm(QgsProcessingAlgorithm): |
| 27 | + """ |
| 28 | + Algorithm to time zone attribute. |
| 29 | + """ |
| 30 | + |
| 31 | + PrmInputLayer = 'InputLayer' |
| 32 | + PrmUseUTC = 'UseUTC' |
| 33 | + PrmOutputLayer = 'OutputLayer' |
| 34 | + PrmDate = 'Date' |
| 35 | + PrmUseISO = 'UseISO' |
| 36 | + |
| 37 | + def initAlgorithm(self, config): |
| 38 | + self.addParameter( |
| 39 | + QgsProcessingParameterFeatureSource( |
| 40 | + self.PrmInputLayer, |
| 41 | + 'Input point layer', |
| 42 | + [QgsProcessing.TypeVectorPoint]) |
| 43 | + ) |
| 44 | + self.addParameter( |
| 45 | + QgsProcessingParameterBoolean( |
| 46 | + self.PrmUseUTC, |
| 47 | + 'Use UTC for date and time', |
| 48 | + True, |
| 49 | + optional=True) |
| 50 | + ) |
| 51 | + self.addParameter( |
| 52 | + QgsProcessingParameterBoolean( |
| 53 | + self.PrmUseISO, |
| 54 | + 'Use ISO8601 timestamps', |
| 55 | + False, |
| 56 | + optional=True) |
| 57 | + ) |
| 58 | + self.addParameter( |
| 59 | + QgsProcessingParameterDateTime( |
| 60 | + self.PrmDate, |
| 61 | + 'Select date for solar calculations', |
| 62 | + type=QgsProcessingParameterDateTime.Date, |
| 63 | + optional=False, |
| 64 | + ) |
| 65 | + ) |
| 66 | + self.addParameter( |
| 67 | + QgsProcessingParameterFeatureSink( |
| 68 | + self.PrmOutputLayer, |
| 69 | + 'Output layer') |
| 70 | + ) |
| 71 | + |
| 72 | + def processAlgorithm(self, parameters, context, feedback): |
| 73 | + source = self.parameterAsSource(parameters, self.PrmInputLayer, context) |
| 74 | + use_utc = self.parameterAsBool(parameters, self.PrmUseUTC, context) |
| 75 | + use_iso = self.parameterAsBool(parameters, self.PrmUseISO, context) |
| 76 | + dt = self.parameterAsDateTime(parameters, self.PrmDate, context) |
| 77 | + qdate = dt.date() |
| 78 | + if not qdate.isValid(): |
| 79 | + raise QgsProcessingException('Use a proper date and rerun algorithm') |
| 80 | + date = datetime(qdate.year(), qdate.month(), qdate.day()) |
| 81 | + |
| 82 | + fields = source.fields() |
| 83 | + src_crs = source.sourceCrs() |
| 84 | + if fields.append(QgsField("dawn", QVariant.String)) is False: |
| 85 | + raise QgsProcessingException("Field names must be unique. There is already a field named 'dawn'") |
| 86 | + if fields.append(QgsField("sunrise", QVariant.String)) is False: |
| 87 | + raise QgsProcessingException("Field names must be unique. There is already a field named 'sunrise'") |
| 88 | + if fields.append(QgsField("noon", QVariant.String)) is False: |
| 89 | + raise QgsProcessingException("Field names must be unique. There is already a field named 'noon'") |
| 90 | + if fields.append(QgsField("sunset", QVariant.String)) is False: |
| 91 | + raise QgsProcessingException("Field names must be unique. There is already a field named 'sunset'") |
| 92 | + if fields.append(QgsField("dusk", QVariant.String)) is False: |
| 93 | + raise QgsProcessingException("Field names must be unique. There is already a field named 'dusk'") |
| 94 | + |
| 95 | + (sink, dest_id) = self.parameterAsSink( |
| 96 | + parameters, self.PrmOutputLayer, context, fields, |
| 97 | + source.wkbType(), src_crs) |
| 98 | + |
| 99 | + if src_crs != epsg4326: |
| 100 | + transform = QgsCoordinateTransform(src_crs, epsg4326, QgsProject.instance()) |
| 101 | + if use_iso: |
| 102 | + if use_utc: |
| 103 | + fmt = '%Y-%m-%dT%H:%M:%SZ' |
| 104 | + else: |
| 105 | + fmt = '%Y-%m-%dT%H:%M:%S%z' |
| 106 | + else: |
| 107 | + fmt = '%Y-%m-%d %H:%M:%S %Z%z' |
| 108 | + tzf = tzf_instance.getTZF() |
| 109 | + total = 100.0 / source.featureCount() if source.featureCount() else 0 |
| 110 | + |
| 111 | + iterator = source.getFeatures() |
| 112 | + for cnt, feature in enumerate(iterator): |
| 113 | + if feedback.isCanceled(): |
| 114 | + break |
| 115 | + f = QgsFeature() |
| 116 | + f.setGeometry(feature.geometry()) |
| 117 | + pt = feature.geometry().asPoint() |
| 118 | + if src_crs != epsg4326: |
| 119 | + pt = transform.transform(pt) |
| 120 | + |
| 121 | + try: |
| 122 | + locl = astral.LocationInfo('','','',pt.y(), pt.x()) |
| 123 | + if use_utc: |
| 124 | + s = sun(locl.observer, date=date) |
| 125 | + else: |
| 126 | + tz_name = tzf.timezone_at(lng=pt.x(), lat=pt.y()) |
| 127 | + tz = timezone(tz_name) |
| 128 | + loc_dt = tz.localize(date) |
| 129 | + s = sun(locl.observer, date=date, tzinfo=loc_dt.tzinfo) |
| 130 | + dawn = s["dawn"].strftime(fmt) |
| 131 | + sunrise = s["sunrise"].strftime(fmt) |
| 132 | + noon = s["noon"].strftime(fmt) |
| 133 | + sunset = s["sunset"].strftime(fmt) |
| 134 | + dusk = s["dusk"].strftime(fmt) |
| 135 | + except Exception: |
| 136 | + dawn = "" |
| 137 | + sunrise = "" |
| 138 | + noon = "" |
| 139 | + sunset = "" |
| 140 | + dusk = "" |
| 141 | + f.setAttributes(feature.attributes() + [dawn, sunrise, noon, sunset, dusk]) |
| 142 | + sink.addFeature(f) |
| 143 | + |
| 144 | + if cnt % 100 == 0: |
| 145 | + feedback.setProgress(int(cnt * total)) |
| 146 | + |
| 147 | + return {self.PrmOutputLayer: dest_id} |
| 148 | + |
| 149 | + def name(self): |
| 150 | + return 'addsunattributes' |
| 151 | + |
| 152 | + def displayName(self): |
| 153 | + return 'Add sun attributes' |
| 154 | + |
| 155 | + def icon(self): |
| 156 | + return QIcon(os.path.dirname(__file__) + '/images/sun.svg') |
| 157 | + |
| 158 | + def helpUrl(self): |
| 159 | + file = os.path.dirname(__file__) + '/index.html' |
| 160 | + if not os.path.exists(file): |
| 161 | + return '' |
| 162 | + return QUrl.fromLocalFile(file).toString(QUrl.FullyEncoded) |
| 163 | + |
| 164 | + def createInstance(self): |
| 165 | + return AddAstronomicalAlgorithm() |
0 commit comments