def _attributes(self, layer, site, name): attributes = {} attributes[layer.fieldNameIndex(Config.fieldName('site'))] = site attributes[layer.fieldNameIndex(Config.fieldName('name'))] = name attributes[layer.fieldNameIndex(Config.fieldName('created_on'))] = utils.timestamp() attributes[layer.fieldNameIndex(Config.fieldName('created_by'))] = 'Grid Tool' return attributes
def loadGridNames(self): self.grid.clearFilter() names = set() default = None for feature in self.grid.pointsLayer.getFeatures(): name = (feature.attribute(Config.fieldName('site')), feature.attribute(Config.fieldName('name'))) names.add(name) if not default: default = name if default: self.setGridNames(sorted(names)) self.setGrid(default[0], default[1]) return True return False
def updateLayerCoordinates(self, layer, updateGeometry, createMapFields): if (not self._initialised or layer is None or not layer.isValid() or layer.geometryType() != QGis.Point): return False self._addLocalMapFields(layer, createMapFields) if layer.startEditing(): local_x_idx = layer.fieldNameIndex(Config.fieldName('local_x')) local_y_idx = layer.fieldNameIndex(Config.fieldName('local_y')) map_x_idx = layer.fieldNameIndex(Config.fieldName('map_x')) map_y_idx = layer.fieldNameIndex(Config.fieldName('map_y')) if updateGeometry: for feature in layer.getFeatures(): localPoint = QgsPoint(feature.attribute(Config.fieldName('local_x')), feature.attribute(Config.fieldName('local_y'))) mapPoint = self._localTransformer.map(localPoint) layer.changeGeometry(feature.id(), QgsGeometry.fromPoint(mapPoint)) for feature in layer.getFeatures(): mapPoint = feature.geometry().asPoint() localPoint = self._mapTransformer.map(mapPoint) layer.changeAttributeValue(feature.id(), local_x_idx, localPoint.x()) layer.changeAttributeValue(feature.id(), local_y_idx, localPoint.y()) layer.changeAttributeValue(feature.id(), map_x_idx, mapPoint.x()) layer.changeAttributeValue(feature.id(), map_y_idx, mapPoint.y()) return layer.commitChanges() return False
def createGrid(self, siteCode, gridName, mapPoint1, localPoint1, mapPoint2, localPoint2, localOrigin, localTerminus, xInterval, yInterval): _localTransformer = LinearTransformer(localPoint1, mapPoint1, localPoint2, mapPoint2) local_x = Config.fieldName('local_x') local_y = Config.fieldName('local_y') map_x = Config.fieldName('map_x') map_y = Config.fieldName('map_y') points = self.grid.pointsLayer if (points is None or not points.isValid()): self.showCriticalMessage('Invalid grid points file, cannot create grid!') return False self._addGridPointsToLayer(points, _localTransformer, localOrigin.x(), xInterval, (localTerminus.x() - localOrigin.x()) / xInterval, localOrigin.y(), yInterval, (localTerminus.y() - localOrigin.y()) / yInterval, self._attributes(points, siteCode, gridName), local_x, local_y, map_x, map_y) if self.grid.settings.linesLayerName: lines = self.grid.linesLayer if lines is None or not lines.isValid(): self.showCriticalMessage('Invalid grid lines file!') else: self._addGridLinesToLayer(lines, _localTransformer, localOrigin.x(), xInterval, (localTerminus.x() - localOrigin.x()) / xInterval, localOrigin.y(), yInterval, (localTerminus.y() - localOrigin.y()) / yInterval, self._attributes(lines, siteCode, gridName), local_x, local_y, map_x, map_y) if self.grid.settings.polygonsLayerName: polygons = self.grid.polygonsLayer if lines is None or not lines.isValid(): self.showCriticalMessage('Invalid grid polygons file!') else: self._addGridPolygonsToLayer(polygons, _localTransformer, localOrigin.x(), xInterval, (localTerminus.x() - localOrigin.x()) / xInterval, localOrigin.y(), yInterval, (localTerminus.y() - localOrigin.y()) / yInterval, self._attributes(polygons, siteCode, gridName), local_x, local_y, map_x, map_y) return True
def initialiseGrid(self, siteCode, gridName): prevFilter = self.grid.filter expr = utils.eqClause(Config.fieldName('site'), siteCode) + ' and ' + utils.eqClause(Config.fieldName('name'), gridName) self.grid.applyFilter(expr) if self.grid.pointsLayer.featureCount() < 2: self.grid.applyFilter(prevFilter) return False features = [] for feature in self.grid.pointsLayer.getFeatures(): features.append(feature) if len(features) >= 2: break map1, local1 = self.transformPoints(features[0]) map2, local2 = self.transformPoints(features[1]) self._mapTransformer = LinearTransformer(map1, local1, map2, local2) self._localTransformer = LinearTransformer(local1, map1, local2, map2) return True
def _addField(self, layer, field): if layer.fieldNameIndex(Config.fieldName(field)) < 0: layer.dataProvider().addAttributes([Config.field(field)])
def transformPoints(self, feature): mapPoint = feature.geometry().asPoint() localX = feature.attribute(Config.fieldName('local_x')) localY = feature.attribute(Config.fieldName('local_y')) localPoint = QgsPoint(localX, localY) return mapPoint, localPoint