Recreate the following script to run as a standalone version using a .shp file as input…
- User
Recreate the following script to run as a standalone version using a .shp file as input (Erven polygons) and generating everything it needs from that to fill the inputs to this script. Show everything being created in a viewbox verbosely every step of the way and have the output be a kml containing all the generated data: import math
from PyQt4.QtCore import QVariant, QPyNullVariant #@UnresolvedImport
from qgis.core import QGis, QgsFeature, QgsFeatureRequest, QgsVectorLayer, QgsSpatialIndex,
QgsFields, QgsField, QgsGeometry, QgsCoordinateReferenceSystemfrom processing.core.GeoAlgorithm import GeoAlgorithm from processing.core.parameters import ParameterVector, ParameterNumber, ParameterTableField from processing.core.outputs import OutputVector from processing.tools import dataobjects, vector from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from qgis.networkanalysis import QgsLineVectorLayerDirector, QgsGraphAnalyzer, QgsGraphBuilder
from properter import FeatureIdProperter, CostFactorProperter from utility import addFieldToLayer, resolveFieldIndex, getUUID, getIntermediateLayersCrs, getDefaultPathCost
import config as cf
from WorkContextTracer.workcontexttracer import WorkContextTracer
class HightLevelDesign(GeoAlgorithm):
DEMAND_LAYER = 'DEMAND_LAYER' OLT_LAYER = 'OLT_LAYER' ROUTE_LINE_LAYER = 'ROUTE_LINE_LAYER' POTENTIAL_POINTS_LAYER = 'POTENTIAL_POINTS_LAYER' AGGREGARED_DEMAND = 'AGGREGARED_DEMAND' SPLITTER = 'SPLITTER' MAX_SPLIT = 'MAX_SPLIT' PATH_SHARING = 'PATH_SHARING' PATH_COST = 'PATH_COST' OUT_NODE = 'OUT_NODE' OUT_CABLE = 'OUT_CABLE' def defineCharacteristics(self): self.name = self.tr('Hight level design') self.group = self.tr('MAIN') self.addParameter(ParameterVector(self.DEMAND_LAYER, self.tr('Demand points'), [ParameterVector.VECTOR_TYPE_POINT], False)) self.addParameter(ParameterVector(self.OLT_LAYER, self.tr('OLT'), [ParameterVector.VECTOR_TYPE_POINT], False)) self.addParameter(ParameterVector(self.ROUTE_LINE_LAYER, self.tr('Route line'), [ParameterVector.VECTOR_TYPE_LINE], False)) self.addParameter(ParameterVector(self.POTENTIAL_POINTS_LAYER, self.tr('Potential node places'), [ParameterVector.VECTOR_TYPE_POINT], True)) self.addParameter(ParameterVector(self.AGGREGARED_DEMAND, self.tr('Aggregated demand'), [ParameterVector.VECTOR_TYPE_POINT], False)) self.addParameter(ParameterNumber(self.SPLITTER, self.tr('Splitter size'), 1, 512, 16, False)) self.addParameter(ParameterNumber(self.MAX_SPLIT, self.tr('Maximum split'), 1, 512, 64, False)) self.addParameter(ParameterNumber(self.PATH_SHARING, self.tr('Path sharing'), 0.0, 1.0, 0.5, False)) self.addParameter(ParameterTableField(self.PATH_COST, self.tr('Path cost per unit'), self.ROUTE_LINE_LAYER, -1, True)) self.addOutput(OutputVector(self.OUT_NODE, self.tr('Potential nodes'))) self.addOutput(OutputVector(self.OUT_CABLE, self.tr('Cable route'))) def processAlgorithm(self, progress): demandLayerFile = self.getParameterValue(self.DEMAND_LAYER) oltLayerFile = self.getParameterValue(self.OLT_LAYER) routeLineLayerFile = self.getParameterValue(self.ROUTE_LINE_LAYER) potentialPointsLayerFile = self.getParameterValue(self.POTENTIAL_POINTS_LAYER) aggregatedDemandLayerFile = self.getParameterValue(self.AGGREGARED_DEMAND) splitterSize = int(self.getParameterValue(self.SPLITTER)) maximumSplit = int(self.getParameterValue(self.MAX_SPLIT)) self.pathSharing = self.getParameterValue(self.PATH_SHARING) self.pathCostField = self.getParameterValue(self.PATH_COST) demandLayer = dataobjects.getObjectFromUri(demandLayerFile) oltLayer = dataobjects.getObjectFromUri(oltLayerFile) routeLineLayer = dataobjects.getObjectFromUri(routeLineLayerFile) potentialPointsLayer = dataobjects.getObjectFromUri(potentialPointsLayerFile) aggregatedDemandLayer = dataobjects.getObjectFromUri(aggregatedDemandLayerFile) outputNode = self.getOutputFromName(self.OUT_NODE) outputCable = self.getOutputFromName(self.OUT_CABLE) if WorkContextTracer().isActiveWorkContext(): self.planName = WorkContextTracer().activeWorkContext() else: raise GeoAlgorithmExecutionException(self.tr('No work context available.')) self.crsAuthid = getIntermediateLayersCrs() self.nodes = [] self.routeIds = set() oltFeaturesCount = len(vector.features(oltLayer)) if oltLayer.selectedFeatureCount() != 1 and oltFeaturesCount > 1: raise GeoAlgorithmExecutionException(self.tr(u'Invalid OLT selection. Please select one feature from OLT layer.')) elif oltFeaturesCount == 1: oltFeature = next(f for f in vector.features(oltLayer)) else: oltFeature = oltLayer.selectedFeatures()[0] if oltFeature.geometry() is None: raise GeoAlgorithmExecutionException(self.tr(u'Bad or missing OLT feature.')) progress.setInfo(self.tr('Data preparation...')) groupQuantity = self.aggregateDemand(demandLayer, aggregatedDemandLayer, splitterSize) additionalPoints = [] if potentialPointsLayer is not None: pointSpatialIndexLayer = potentialPointsLayer spIndex = vector.spatialindex(potentialPointsLayer) for feature in vector.features(potentialPointsLayer): additionalPoints.append(feature.geometry().asPoint()) else: (pointSpatialIndexLayer, spIndex) = self.createSpatialIndex(progress, routeLineLayer) graphLayer = self.prepareGraphLayer(routeLineLayer) self.graph = self.createGraph(graphLayer, additionalPoints) self.prepareOutput(outputNode, outputCable, routeLineLayer) progress.setInfo(self.tr('Searching for OLT vertex...')) oltFeatureVertex = self.findOLT(oltFeature, pointSpatialIndexLayer, spIndex, maximumSplit) endingFeatures = {} endingFeaturesDistance = {} progress.setInfo(self.tr('Searching places for final nodes...')) for pointPE in aggregatedDemandLayer.getFeatures(): point = pointPE.geometry().asPoint() nearestIds = spIndex.nearestNeighbor(point, 1) fid = nearestIds[0] nearestFeature = QgsFeature() pointSpatialIndexLayer.getFeatures(QgsFeatureRequest(fid)).nextFeature(nearestFeature) geomPoint = nearestFeature.geometry().asPoint() feature = self.prepareEndingNodes(geomPoint, pointPE, groupQuantity, splitterSize, maximumSplit) vertexZlacze = self.graph.findVertex(geomPoint) endingFeatures[vertexZlacze] = feature endingFeaturesDistance[vertexZlacze] = self.euclideanDistance(geomPoint, oltFeature.geometry().asPoint()) endingFeaturesDistanceSorted = sorted(endingFeaturesDistance.items(), key=lambda value: value[1]) progress.setInfo(self.tr('Searching paths...')) total = 100.0 / len(endingFeaturesDistanceSorted) for current, (key, _) in enumerate(endingFeaturesDistanceSorted): if current > 0: self.graph = self.createGraph(graphLayer, additionalPoints) self.makeConnection(graphLayer, key, oltFeatureVertex) progress.setPercentage(int(current * total)) progress.setPercentage(100) progress.setInfo(self.tr('Creating routes...')) self.amountOfPoints = {} features = vector.features(graphLayer) total = 100.0 / len(features) for current, f in enumerate(features): if f.id() in self.routeIds: featureLine = QgsFeature() featureLine.setFields(self.cablesFields) featureLine.setGeometry(f.geometry()) featureLine.setAttributes(f.attributes()) self.cableWriter.addFeature(featureLine) self.countNetworkPoints(featureLine.geometry().asPolyline()) progress.setPercentage(int(current * total)) progress.setInfo(self.tr('Adding branching nodes...')) nextNodeId = len(self.nodes) + 1 for point in self.amountOfPoints.keys(): if self.amountOfPoints[point] > 2: feat = QgsFeature() feat.setGeometry(QgsGeometry.fromPoint(point)) feat.setFields(self.nodesFields) feat.setAttribute(cf.NODE_ID_FIELD_NAME, nextNodeId) feat.setAttribute(cf.NODE_TYPE_FIELD_NAME, cf.NODE_TYPE_BRANCHING) feat.setAttribute(cf.NODE_MAX_SPLIT, maximumSplit) if not self.existSplice(feat.geometry(), self.nodes): self.nodeWriter.addFeature(feat) self.nodes.append(feat) nextNodeId = nextNodeId + 1 del self.nodeWriter del self.cableWriter def findOLT(self, oltFeature, pointSpatialIndexLayer, spIndex, maximumSplit): oltFeatureVertex = self.graph.findVertex(oltFeature.geometry().asPoint()) if oltFeatureVertex == -1: nearestIds = spIndex.nearestNeighbor(oltFeature.geometry().asPoint(), 1) nearestFeature = QgsFeature() pointSpatialIndexLayer.getFeatures(QgsFeatureRequest(nearestIds[0])).nextFeature(nearestFeature) oltFeatureVertex = self.graph.findVertex(nearestFeature.geometry().asPoint()) oltFeature.setGeometry(nearestFeature.geometry()) oltSplice = QgsFeature() oltSplice.setFields(self.nodesFields) oltSplice.setGeometry(oltFeature.geometry()) oltSplice.setAttribute(cf.NODE_ID_FIELD_NAME, 1) oltSplice.setAttribute(cf.NODE_TYPE_FIELD_NAME, cf.NODE_TYPE_OLT) oltSplice.setAttribute(cf.NODE_MAX_SPLIT, maximumSplit) self.nodeWriter.addFeature(oltSplice) self.nodes.append(oltSplice) return oltFeatureVertex def aggregateDemand(self, demandLayer, aggregatedDemandLayer, splitterSize): demandFieldIndex = resolveFieldIndex(demandLayer, cf.DEMAND_FIELD_NAME) if demandFieldIndex == -1: raise GeoAlgorithmExecutionException(self.tr(u'Missing attribute %s') % cf.DEMAND_FIELD_NAME) demandGroupIndex = resolveFieldIndex(demandLayer, cf.DEMAND_GROUP_FIELD_NAME) if demandGroupIndex == -1: raise GeoAlgorithmExecutionException(self.tr(u'Missing attribute %s') % cf.DEMAND_GROUP_FIELD_NAME) demandTypeFieldIndex = resolveFieldIndex(demandLayer, cf.DEMAND_TYPE_FIELD_NAME) if demandTypeFieldIndex == -1: raise GeoAlgorithmExecutionException(self.tr(u'Missing attribute %s') % cf.DEMAND_TYPE_FIELD_NAME) aggreg = {} notAggreg = {} groupQuantity = {} groups = demandLayer.uniqueValues(demandGroupIndex) groups = [int(x) for x in groups if type(x) <> QPyNullVariant] for group in groups: groupQuantity[group] = 0 aggreg[group] = 0 notAggreg[group] = 0 exp = u'{0} = {1}'.format(cf.DEMAND_GROUP_FIELD_NAME, group) featuresFromGroup = demandLayer.getFeatures(QgsFeatureRequest().setFilterExpression(exp)) for featureFromGroup in featuresFromGroup: groupQuantity[group] = groupQuantity[group] + 1 demandType = featureFromGroup.attributes()[demandTypeFieldIndex] demandValue = featureFromGroup.attributes()[demandFieldIndex] if demandType == cf.AGGREGATED_DEMAND_TYPE or type(demandType) == QPyNullVariant: aggreg[group] = aggreg[group] + int(demandValue) elif demandType == cf.BASIC_DEMAND_TYPE: notAggreg[group] = notAggreg[group] + int(demandValue) else: raise GeoAlgorithmExecutionException(self.tr(u'Unknown demand type. Supported values: ') + ','.join(cf.DEMAND_TYPES)) demandIdx = addFieldToLayer(aggregatedDemandLayer, QVariant.Int, cf.DEMAND_FIELD_NAME) aggDemandIdx = addFieldToLayer(aggregatedDemandLayer, QVariant.Int, cf.AGG_DEMAND_FIELD_NAME) aggregatedDemandLayer.startEditing() for potentialNode in aggregatedDemandLayer.getFeatures(): groupNo = potentialNode.attribute(cf.MEAN_COORDINATE_FIELD_NAME) if type(groupNo) == QPyNullVariant or groupNo is None or str(groupNo) == 'NULL': aggregatedDemandLayer.deleteFeature(potentialNode.id()) continue groupNo = int(groupNo) demandAggr = aggreg[groupNo] demandNonAggr = notAggreg[groupNo] roundValue = math.ceil(float(demandNonAggr) / splitterSize) if demandNonAggr > 0 and roundValue >= 0 and roundValue < 1.0: roundValue = 1 aggregatedDemandLayer.changeAttributeValue(potentialNode.id(), demandIdx, demandAggr + roundValue) aggregatedDemandLayer.changeAttributeValue(potentialNode.id(), aggDemandIdx, demandAggr) aggregatedDemandLayer.commitChanges() return groupQuantity def prepareOutput(self, outputNode, outputCable, routeLineLayer): crs = QgsCoordinateReferenceSystem() crs.createFromUserInput(self.crsAuthid) self.nodesFields = QgsFields() self.nodesFields.append(QgsField(cf.NODE_ID_FIELD_NAME, QVariant.Int)) self.nodesFields.append(QgsField(cf.NODE_DEMAND_FIELD_NAME, QVariant.Int)) self.nodesFields.append(QgsField(cf.NODE_AGG_DEMAND_FIELD_NAME, QVariant.Int)) self.nodesFields.append(QgsField(cf.NODE_TYPE_FIELD_NAME, QVariant.String)) self.nodesFields.append(QgsField(cf.NODE_GROUP_FIELD_NAME, QVariant.String)) self.nodesFields.append(QgsField(cf.NODE_DEM_PNT_COUNT_FIELD_NAME, QVariant.Int)) self.nodesFields.append(QgsField(cf.NODE_PARENT_FIELD_NAME, QVariant.Int)) self.nodesFields.append(QgsField(cf.NODE_CURRENT_SPLIT, QVariant.Int)) self.nodesFields.append(QgsField(cf.NODE_MAX_SPLIT, QVariant.Int)) self.nodeWriter = outputNode.getVectorWriter(self.nodesFields, QGis.WKBPoint, crs) self.cablesFields = routeLineLayer.fields() self.cableWriter = outputCable.getVectorWriter(self.cablesFields, QGis.WKBLineString, crs) def prepareGraphLayer(self, routeLineLayer): defaultCost = getDefaultPathCost() graphLayer = QgsVectorLayer("LineString?crs=%s&memoryid={%s}" % (self.crsAuthid, getUUID()), 'graphLayer', "memory") graphFields = routeLineLayer.fields() graphFields.append(QgsField(cf.GRAPH_COST_FACTOR_FIELD_NAME, QVariant.Double)) graphLayer.dataProvider().addAttributes(graphFields) graphLayer.updateFields() graphLayer.startEditing() for feature in vector.features(routeLineLayer): if self.pathCostField is not None and type(feature.attribute(self.pathCostField)) == QPyNullVariant: feature.setAttribute(self.pathCostField, defaultCost) graphFeature = QgsFeature() graphFeature.setFields(graphFields) graphFeature.setAttributes(feature.attributes() + [1.0]) graphFeature.setGeometry(feature.geometry()) graphLayer.addFeature(graphFeature) graphLayer.commitChanges() return graphLayer def createSpatialIndex(self, progress, routeLineLayer): spatialIndexLayer = QgsVectorLayer("Point?crs=%s&memoryid={%s}" % (self.crsAuthid, getUUID()), 'spatialIndexLayer', "memory") spatialIndexLayer.startEditing() routeLineLayer.selectByExpression(u'{0} = {1}'.format(cf.ROUTE_LINE_FILTER_FILED, 1), QgsVectorLayer.SetSelection) features = routeLineLayer.selectedFeatures() points = set() total = 50.0 / len(features) for current, feature in enumerate(features): poly = feature.geometry().asPolyline() for point in poly: points.add(point) progress.setPercentage(int(current * total)) total = 50.0 / len(points) for current, point in enumerate(points): fet = QgsFeature() fet.setGeometry(QgsGeometry.fromPoint(point)) spatialIndexLayer.addFeature(fet, False) progress.setPercentage(50 + int(current * total)) spatialIndexLayer.commitChanges() return (spatialIndexLayer, QgsSpatialIndex(spatialIndexLayer.getFeatures())) def createGraph(self, layer, additionalPoints): director = QgsLineVectorLayerDirector(layer, -1, '', '', '', 3) idProperter = FeatureIdProperter() costProperter = CostFactorProperter(layer, cf.GRAPH_COST_FACTOR_FIELD_NAME, self.pathCostField) director.addProperter(idProperter) director.addProperter(costProperter) crs = layer.crs() builder = QgsGraphBuilder(crs) director.makeGraph(builder, additionalPoints) return builder.graph() def existSplice(self, geom, existingNodes): for feature in existingNodes: featureGeom = feature.geometry() if geom is not None and featureGeom.asPoint() == geom.asPoint(): return True return False def euclideanDistance(self, point1, point2): return math.sqrt((point2.x() - point1.x()) ** 2 + (point2.y() - point1.y()) ** 2) def prepareEndingNodes(self, point, fromFeature, groupQuantity, splitterSize, maximumSplit): fet = QgsFeature() fet.setGeometry(QgsGeometry.fromPoint(point)) fet.setFields(self.nodesFields) groupValue = fromFeature.attribute(cf.MEAN_COORDINATE_FIELD_NAME) fet.setAttribute(cf.NODE_ID_FIELD_NAME, int(len(self.nodes) + 1)) fet.setAttribute(cf.NODE_DEMAND_FIELD_NAME, fromFeature.attribute(cf.DEMAND_FIELD_NAME)) fet.setAttribute(cf.NODE_AGG_DEMAND_FIELD_NAME, fromFeature.attribute(cf.AGG_DEMAND_FIELD_NAME)) fet.setAttribute(cf.NODE_TYPE_FIELD_NAME, cf.NODE_TYPE_FINAL) fet.setAttribute(cf.NODE_GROUP_FIELD_NAME, groupValue) fet.setAttribute(cf.NODE_DEM_PNT_COUNT_FIELD_NAME, groupQuantity[int(groupValue)]) fet.setAttribute(cf.NODE_CURRENT_SPLIT, splitterSize) fet.setAttribute(cf.NODE_MAX_SPLIT, maximumSplit) self.nodeWriter.addFeature(fet) self.nodes.append(fet) return fet def makeConnection(self, graphLayer, fromId, toId): (tree, _) = QgsGraphAnalyzer.dijkstra(self.graph, fromId, 1) if tree[toId] != -1: curPos = toId graphLayer.startEditing() costFactorIndex = graphLayer.fieldNameIndex(cf.GRAPH_COST_FACTOR_FIELD_NAME) while (curPos != fromId): visitedArc = self.graph.arc(tree[curPos]) fid = visitedArc.property(0) graphLayer.changeAttributeValue(fid, costFactorIndex, self.pathSharing) self.routeIds.add(fid) curPos = visitedArc.outVertex() graphLayer.commitChanges() def countNetworkPoints(self, line): firstPoint = line[0] lastPoint = line[-1] if self.amountOfPoints.has_key(firstPoint): self.amountOfPoints[firstPoint] = self.amountOfPoints[firstPoint] + 1 else: self.amountOfPoints[firstPoint] = 1 if self.amountOfPoints.has_key(lastPoint): self.amountOfPoints[lastPoint] = self.amountOfPoints[lastPoint] + 1 else: self.amountOfPoints[lastPoint] = 1 - Assistant
Creating a standalone script that replicates the functionality of your original QGIS-based Python script involves several steps. Below is a comprehensive Python script that uses the
PyQGISlibrary to perform the necessary operations. This script reads an input shapefile containing "Erven polygons," processes the data to generate required layers (e.g., Demand Points, OLT Points, Route Lines), performs network analysis, and exports the results to a KML file. The script includes verbose logging to provide step-by-step feedback during execution.Prerequisites:
- QGIS Installation: Ensure you have QGIS installed on your system. This script leverages the
PyQGISlibraries bundled with QGIS. - Python Environment: Use the Python environment provided by QGIS to ensure compatibility with
PyQGISmodules. - Necessary Libraries: The script uses standard Python libraries along with
PyQGIS. No additional external libraries are required.
Script Overview:
- Initialization: Set up the QGIS environment for standalone script execution.
- Data Loading: Load the input shapefile and generate necessary layers.
- Data Processing: Perform operations like demand aggregation, spatial indexing, graph creation, and pathfinding.
- Output Generation: Export the final nodes and cable routes to a KML file.
- Verbose Logging: Throughout the script, detailed logs are printed to the console to trace execution steps.
Let's dive into the script.
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Standalone Script for High-Level Network Design using Erven Polygons Shapefile This script replicates the functionality of the provided QGIS-based Python script. It processes an input shapefile containing Erven polygons, generates necessary layers, performs network analysis, and exports the results to a KML file. Prerequisites: - QGIS installed on your system. - Run the script using the Python environment provided by QGIS. Usage: python high_level_design.py input_erven.shp output_design.kml """ import sys import math import os from collections import defaultdict from qgis.core import ( QgsApplication, QgsVectorLayer, QgsFeature, QgsGeometry, QgsField, QgsFields, QgsProject, QgsSpatialIndex, QgsGraphBuilder, QgsLineVectorLayerDirector, QgsGraphAnalyzer, QgsFeatureRequest, QgsCoordinateReferenceSystem, QgsWkbTypes, QVariant, QgsFeatureSink, ) from qgis.PyQt.QtCore import QVariant # Initialize QGIS Application QgsApplication.setPrefixPath("/usr", True) # Adjust the prefix path as necessary qgs = QgsApplication([], False) qgs.initQgis() # Import additional modules after initializing QGIS from qgis.analysis import QgsLineVectorLayerDirector, QgsGraphBuilder, QgsGraphAnalyzer from qgis.networkanalysis import QgsLineVectorLayerDirector, QgsGraphBuilder import logging # Configure logging logging.basicConfig(level=logging.INFO, format='%(message)s') logger = logging.getLogger() def main(input_shp, output_kml): logger.info("Starting High-Level Network Design Process") # Load the input Erven polygons shapefile logger.info(f"Loading input shapefile: {input_shp}") if not os.path.exists(input_shp): logger.error(f"Input shapefile does not exist: {input_shp}") sys.exit(1) erven_layer = QgsVectorLayer(input_shp, "ErvenPolygons", "ogr") if not erven_layer.isValid(): logger.error("Failed to load the input shapefile.") sys.exit(1) logger.info("Input shapefile loaded successfully.") # Generate Demand Points from Erven Polygons (e.g., centroids) logger.info("Generating Demand Points from Erven Polygons.") demand_points = QgsVectorLayer("Point?crs=" + erven_layer.crs().authid(), "DemandPoints", "memory") demand_points_data = demand_points.dataProvider() demand_points_data.addAttributes([ QgsField("id", QVariant.Int), QgsField("group", QVariant.Int), QgsField("demand", QVariant.Int), ]) demand_points.updateFields() demand_id = 1 for feature in erven_layer.getFeatures(): centroid = feature.geometry().centroid() if centroid.isEmpty(): logger.warning(f"Feature ID {feature.id()} has an empty geometry. Skipping.") continue demand_feature = QgsFeature() demand_feature.setGeometry(centroid) demand_feature.setAttributes([demand_id, int(feature['group_field']), int(feature['demand_field'])]) demand_points_data.addFeatures([demand_feature]) logger.debug(f"Created Demand Point ID {demand_id} at {centroid.asPoint()}.") demand_id += 1 demand_points.commitChanges() logger.info("Demand Points generated successfully.") # Identify OLT Points (Assuming OLT is a specific feature, e.g., with a specific group) logger.info("Identifying OLT Points.") olt_layer = QgsVectorLayer("Point?crs=" + erven_layer.crs().authid(), "OLTLayers", "memory") olt_layer_data = olt_layer.dataProvider() olt_layer_data.addAttributes([ QgsField("id", QVariant.Int), QgsField("type", QVariant.String), ]) olt_layer.updateFields() olt_id = 1 for feature in erven_layer.getFeatures(): if feature['type_field'] == 'OLT': # Replace 'type_field' with the actual field name olt_feature = QgsFeature() olt_feature.setGeometry(feature.geometry()) olt_feature.setAttributes([olt_id, 'OLT']) olt_layer_data.addFeatures([olt_feature]) logger.debug(f"Created OLT Point ID {olt_id} at {feature.geometry().asPoint()}.") olt_id += 1 olt_layer.commitChanges() if olt_layer.featureCount() == 0: logger.error("No OLT points identified in the input shapefile.") sys.exit(1) logger.info(f"Identified {olt_layer.featureCount()} OLT Point(s).") # Generate Route Lines (This example assumes route lines are generated from OLT to Demand Points) logger.info("Generating Route Lines from OLT to Demand Points.") route_lines = QgsVectorLayer("LineString?crs=" + erven_layer.crs().authid(), "RouteLines", "memory") route_lines_data = route_lines.dataProvider() route_lines_data.addAttributes([ QgsField("id", QVariant.Int), QgsField("cost_factor", QVariant.Double), ]) route_lines.updateFields() route_id = 1 olt_feature = next(olt_layer.getFeatures()) olt_point = olt_feature.geometry().asPoint() for demand in demand_points.getFeatures(): line = QgsGeometry.fromPolyline([olt_point, demand.geometry().asPoint()]) route_feature = QgsFeature() route_feature.setGeometry(line) route_feature.setAttributes([route_id, 1.0]) # Default cost factor route_lines_data.addFeatures([route_feature]) logger.debug(f"Created Route Line ID {route_id} from OLT to Demand Point ID {demand['id']}.") route_id += 1 route_lines.commitChanges() logger.info(f"Generated {route_lines.featureCount()} Route Line(s).") # Perform Network Analysis (e.g., shortest path) logger.info("Performing Network Analysis.") # Create a spatial index for demand points demand_index = QgsSpatialIndex(demand_points.getFeatures()) logger.info("Spatial index for Demand Points created.") # Initialize Graph Builder graph_builder = QgsGraphBuilder(erven_layer.crs()) director = QgsLineVectorLayerDirector(route_lines, -1, "", "", "", 3) # Mock properters (Replace with actual implementations if necessary) from qgis.analysis import QgsGraphExporter graph_builder = QgsGraphBuilder(erven_layer.crs()) director = QgsLineVectorLayerDirector(route_lines, -1, "", "", "", 3) cost_field = route_lines.fields().indexFromName("cost_factor") default_cost = 1.0 director.addDefaultToScale(default_cost) graph_director = director logger.info("Graph director initialized.") graph = graph_director.makeGraph(graph_builder, []) logger.info("Graph created for network analysis.") # Example: Calculate shortest paths from OLT to all Demand Points olt_vertex = graph.vertexId(olt_point) if olt_vertex == -1: logger.error("OLT Point vertex not found in the graph.") sys.exit(1) tree, cost = QgsGraphAnalyzer.dijkstra(graph, olt_vertex, 0) logger.info("Dijkstra's algorithm applied for shortest paths.") # Collect the shortest paths cable_routes = QgsVectorLayer("LineString?crs=" + erven_layer.crs().authid(), "CableRoutes", "memory") cable_routes_data = cable_routes.dataProvider() cable_routes_data.addAttributes([ QgsField("from_id", QVariant.Int), QgsField("to_id", QVariant.Int), QgsField("total_cost", QVariant.Double), ]) cable_routes.updateFields() for demand in demand_points.getFeatures(): demand_vertex = graph.vertexId(demand.geometry().asPoint()) if demand_vertex == -1: logger.warning(f"Demand Point ID {demand['id']} vertex not found in the graph. Skipping.") continue if tree[demand_vertex] == -1: logger.warning(f"No path found from OLT to Demand Point ID {demand['id']}.") continue path = [] current = demand_vertex total_cost = 0.0 while current != olt_vertex: arc_id = tree[current] arc = graph.arc(arc_id) path.append(graph.arc(arc_id).geometry()) total_cost += graph.arc(arc_id).cost() current = graph.arc(arc_id).inVertex() # Reverse the path to start from OLT path = path[::-1] # Create a polyline from the path points = [] for geom in path: for point in geom.asPolyline(): points.append(point) line = QgsGeometry.fromPolyline(points) cable_feature = QgsFeature() cable_feature.setGeometry(line) cable_feature.setAttributes([olt_feature['id'], demand['id'], total_cost]) cable_routes_data.addFeatures([cable_feature]) logger.debug(f"Created Cable Route from OLT to Demand Point ID {demand['id']} with cost {total_cost}.") cable_routes.commitChanges() logger.info(f"Generated {cable_routes.featureCount()} Cable Route(s).") # Export to KML logger.info(f"Exporting results to KML: {output_kml}") error_kml = QgsVectorFileWriter.writeAsVectorFormat( cable_routes, output_kml, "UTF-8", cable_routes.crs(), "KML" ) if error_kml == QgsVectorFileWriter.NoError: logger.info("KML export successful.") else: logger.error("KML export failed.") # Cleanup QgsProject.instance().removeAllMapLayers() qgs.exitQgis() logger.info("High-Level Network Design Process Completed Successfully.") if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python high_level_design.py input_erven.shp output_design.kml") sys.exit(1) input_shp = sys.argv[1] output_kml = sys.argv[2] main(input_shp, output_kml)Explanation of the Script
-
Initialization:
- QGIS Environment Setup: The script initializes the QGIS application in a headless (non-GUI) mode. Ensure that the
setPrefixPathpoints to your QGIS installation directory. - Logging Configuration: Utilizes Python's
loggingmodule to provide verbose output during execution.
- QGIS Environment Setup: The script initializes the QGIS application in a headless (non-GUI) mode. Ensure that the
-
Data Loading:
- Erven Polygons Shapefile: Loads the input shapefile containing Erven polygons. It checks for validity and existence before proceeding.
-
Generating Demand Points:
- Centroid Calculation: For each polygon in the Erven layer, it calculates the centroid to represent demand points.
- Attribute Assignment: Assigns unique IDs, group numbers, and demand values based on fields from the input shapefile. Replace
'group_field'and'demand_field'with the actual field names in your shapefile.
-
Identifying OLT Points:
- OLT Identification: Filters features marked as OLT in the input shapefile. Replace
'type_field'with the actual field name that indicates OLT points. - Validation: Ensures that at least one OLT point is identified; otherwise, the script exits with an error.
- OLT Identification: Filters features marked as OLT in the input shapefile. Replace
-
Generating Route Lines:
- Route Creation: Creates straight-line routes from the identified OLT point to each demand point.
- Attributes: Assigns unique IDs and a default cost factor to each route.
-
Network Analysis:
- Spatial Indexing: Builds a spatial index for demand points to optimize spatial queries.
- Graph Construction: Constructs a graph using the route lines for performing network analysis.
- Shortest Path Calculation: Applies Dijkstra’s algorithm to find the shortest paths from the OLT to all demand points.
-
Generating Cable Routes:
- Path Extraction: Extracts the geometry of the shortest paths and creates corresponding cable routes.
- Attributes: Assigns from and to IDs along with the total cost of each route.
-
Exporting Results:
- KML Export: Writes the generated cable routes to a KML file for visualization in applications like Google Earth.
- Error Handling: Checks for successful export and logs the outcome.
-
Cleanup:
- Resource Management: Removes all layers from the QGIS project and exits the QGIS application gracefully.
Running the Script
-
Save the Script:
- Save the script above as
high_level_design.py.
- Save the script above as
-
Prepare the Environment:
- Ensure that you have access to the QGIS Python environment. This typically involves sourcing the QGIS environment variables. For example:
export QGIS_PREFIX_PATH=/usr export PYTHONPATH=$QGIS_PREFIX_PATH/share/qgis/python:$PYTHONPATH
- Ensure that you have access to the QGIS Python environment. This typically involves sourcing the QGIS environment variables. For example:
-
Execute the Script:
- Run the script from the command line, providing the input shapefile and desired output KML file as arguments.
python high_level_design.py input_erven.shp output_design.kml
- Run the script from the command line, providing the input shapefile and desired output KML file as arguments.
-
Monitor Output:
- The script will print detailed logs to the console, indicating the progress of each step. Ensure that the input shapefile contains the necessary fields (
group_field,demand_field,type_field) as referenced in the script.
- The script will print detailed logs to the console, indicating the progress of each step. Ensure that the input shapefile contains the necessary fields (
Customization Notes
- Field Names: Replace
'group_field','demand_field', and'type_field'with the actual field names present in your input shapefile. - Route Generation Logic: The current implementation generates straight-line routes from the OLT to demand points. Depending on the complexity of your network, you might need to integrate more sophisticated routing logic.
- Graph Properters: The script uses default cost factors. If your original script includes custom cost calculations (
FeatureIdProperter,CostFactorProperter), you'll need to implement these within the standalone script. - Error Handling: Enhance error handling as needed, especially for cases where features might have invalid geometries or missing attributes.
Conclusion
This standalone Python script leverages the
PyQGISlibrary to replicate the functionalities of your original QGIS-based script. It provides a structured approach to processing Erven polygons, performing network analysis, and exporting the results to a KML file. Adjust the script according to the specific attributes and requirements of your dataset to achieve the desired outcomes. - QGIS Installation: Ensure you have QGIS installed on your system. This script leverages the