-
-
Notifications
You must be signed in to change notification settings - Fork 28
[WIP] matrix calculator, matrix import/updt, faster project creation #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Art-Ev
wants to merge
16
commits into
AequilibraE:develop
Choose a base branch
from
Art-Ev:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
59583a3
Revision of #250 (Adding QGIS processing provider)
Art-Ev bc718fa
Renaming processings to handle menus order & add datetime to assignme…
Art-Ev 3be45ba
Differentiation between creating a new aem file and adding a matrix t…
Art-Ev 5c52d47
Update add_matrix_from_layer.py
Art-Ev 65612c5
Matrix calculator & translation optimization
Art-Ev 61f752f
Merge branch 'develop' of https://github.com/Art-Ev/qaequilibrae into…
r-akemii 22eda45
Revert "Merge branch 'develop' of https://github.com/Art-Ev/qaequilib…
r-akemii 32b9a15
Faster project creation by layer import (~10x)
Art-Ev c91457b
Merge branch 'AequilibraE:develop' into develop
Art-Ev ffa4ce6
Merge branch 'develop' of https://github.com/Art-Ev/qaequilibrae into…
Art-Ev 8a9d4d8
Better connector addition
Art-Ev 1c7d583
[new processing provider function] add links from layer to an existin…
Art-Ev 793bd91
GTFS import and PT assign (v0)
Art-Ev abd64d3
debugging qgis file filter and co
Art-Ev 0ed6a4e
Numerous bug corrections
Art-Ev 1121b6e
Bug correction
Art-Ev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
qaequilibrae/modules/processing_provider/add_matrix_from_layer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import importlib.util as iutil | ||
| import sys | ||
| import numpy as np | ||
| import pandas as pd | ||
| from os.path import join | ||
| from scipy.sparse import coo_matrix | ||
|
|
||
| from qgis.core import QgsProcessingMultiStepFeedback, QgsProcessingParameterString, QgsProcessingParameterDefinition | ||
| from qgis.core import QgsProcessingParameterField, QgsProcessingParameterMapLayer, QgsProcessingParameterFile | ||
| from qgis.core import QgsProcessingAlgorithm | ||
|
|
||
| from qaequilibrae.modules.common_tools import standard_path | ||
| from qaequilibrae.i18n.translate import trlt | ||
|
|
||
| class AddMatrixFromLayer(QgsProcessingAlgorithm): | ||
|
|
||
| def initAlgorithm(self, config=None): | ||
| self.addParameter(QgsProcessingParameterMapLayer("matrix_layer", self.tr("Matrix Layer"))) | ||
| self.addParameter( | ||
| QgsProcessingParameterField( | ||
| "origin", | ||
| self.tr("Origin"), | ||
| type=QgsProcessingParameterField.Numeric, | ||
| parentLayerParameterName="matrix_layer", | ||
| allowMultiple=False, | ||
| defaultValue="origin", | ||
| ) | ||
| ) | ||
| self.addParameter( | ||
| QgsProcessingParameterField( | ||
| "destination", | ||
| self.tr("Destination"), | ||
| type=QgsProcessingParameterField.Numeric, | ||
| parentLayerParameterName="matrix_layer", | ||
| allowMultiple=False, | ||
| defaultValue="destination", | ||
| ) | ||
| ) | ||
| self.addParameter( | ||
| QgsProcessingParameterField( | ||
| "value", | ||
| self.tr("Value"), | ||
| type=QgsProcessingParameterField.Numeric, | ||
| parentLayerParameterName="matrix_layer", | ||
| allowMultiple=False, | ||
| defaultValue="value", | ||
| ) | ||
| ) | ||
| self.addParameter( | ||
| QgsProcessingParameterFile( | ||
| "matrix_file", | ||
| self.tr("Matrix file"), | ||
|
r-akemii marked this conversation as resolved.
Outdated
|
||
| behavior=QgsProcessingParameterFile.File, | ||
| fileFilter="*.aem", | ||
| defaultValue=None, | ||
| ) | ||
|
|
||
| self.addParameter( | ||
| QgsProcessingParameterString( | ||
| "matrix_core", | ||
| self.tr("Matrix core"), | ||
| multiLine=False, | ||
| defaultValue="Value"), | ||
| ] | ||
|
|
||
| def processAlgorithm(self, parameters, context, model_feedback): | ||
| feedback = QgsProcessingMultiStepFeedback(3, model_feedback) | ||
|
|
||
| # Checks if we have access to aequilibrae library | ||
| if iutil.find_spec("aequilibrae") is None: | ||
| sys.exit(self.tr("AequilibraE module not found")) | ||
|
r-akemii marked this conversation as resolved.
|
||
|
|
||
| from aequilibrae.matrix import AequilibraeMatrix | ||
|
|
||
| origin = parameters["origin"] | ||
| destination = parameters["destination"] | ||
| value = parameters["value"] | ||
|
|
||
| core_name = parameters["matrix_core"] | ||
|
|
||
| matrix_file = parameters["matrix_file"] | ||
|
|
||
| # Import layer as a pandas df | ||
| feedback.pushInfo(self.tr("Importing layer")) | ||
| layer = self.parameterAsVectorLayer(parameters, "matrix_layer", context) | ||
| cols = [origin, destination, value] | ||
| datagen = ([f[col] for col in cols] for f in layer.getFeatures()) | ||
| matrix = pd.DataFrame.from_records(data=datagen, columns=cols) | ||
| feedback.pushInfo("") | ||
| feedback.setCurrentStep(1) | ||
|
|
||
| # Getting all zones | ||
| all_zones = np.array(sorted(list(set(list(matrix[origin].unique()) + list(matrix[destination].unique()))))) | ||
|
r-akemii marked this conversation as resolved.
Outdated
|
||
| num_zones = all_zones.shape[0] | ||
| idx = np.arange(num_zones) | ||
|
|
||
| # Creates the indexing dataframes | ||
| origs = pd.DataFrame({"from_index": all_zones, "from": idx}) | ||
| dests = pd.DataFrame({"to_index": all_zones, "to": idx}) | ||
|
|
||
| # adds the new index columns to the pandas dataframe | ||
| matrix = matrix.merge(origs, left_on=origin, right_on="from_index", how="left") | ||
| matrix = matrix.merge(dests, left_on=destination, right_on="to_index", how="left") | ||
|
|
||
| agg_matrix = matrix.groupby(["from", "to"]).sum() | ||
|
|
||
| # returns the indices | ||
| agg_matrix.reset_index(inplace=True) | ||
|
|
||
| # Creating the aequilibrae matrix file | ||
| mat = AequilibraeMatrix() | ||
| mat.load(matrix_file) | ||
|
|
||
| m = ( | ||
| coo_matrix((agg_matrix[value], (agg_matrix["from"], agg_matrix["to"])), shape=(num_zones, num_zones)) | ||
| .toarray() | ||
| .astype(np.float64) | ||
| ) | ||
| mat.matrix[core_name][:, :] = m[:, :] | ||
|
|
||
| feedback.pushInfo(self.tr("{}x{} matrix imported ").format(num_zones, num_zones)) | ||
| feedback.pushInfo(" ") | ||
| feedback.setCurrentStep(2) | ||
|
|
||
| mat.save() | ||
| mat.close() | ||
| del agg_matrix, matrix, m | ||
|
|
||
| feedback.pushInfo(" ") | ||
| feedback.setCurrentStep(3) | ||
|
|
||
| return {"Output": f"{mat.name}, {mat.description} ({file_name})"} | ||
|
|
||
| def name(self): | ||
| return self.tr("Add a matrix from a layer to an existing aem file") | ||
|
r-akemii marked this conversation as resolved.
Outdated
|
||
|
|
||
| def displayName(self): | ||
| return self.tr("Add a matrix from a layer to an existing aem file") | ||
|
|
||
| def group(self): | ||
| return self.tr("02-Data") | ||
|
|
||
| def groupId(self): | ||
| return self.tr("02-Data") | ||
|
|
||
| def shortHelpString(self): | ||
| return "\n".join([self.string_order(1), self.string_order(2), self.string_order(3), self.string_order(4), self.string_order(5)]) | ||
|
|
||
| def createInstance(self): | ||
| return AddMatrixFromLayer() | ||
|
|
||
| def string_order(self, order): | ||
| if order == 1: | ||
| return self.tr("Save a layer to an existing *.aem file. Notice that:") | ||
| elif order == 2: | ||
| return self.tr("- the original matrix stored in the layer needs to be in list format") | ||
| elif order == 3: | ||
| return self.tr("- origin and destination fields need to be integers") | ||
| elif order == 4: | ||
| return self.tr("- value field can be either integer or real") | ||
| elif order == 5: | ||
| return self.tr("- if matrix_core is already existing, then it will be updated and previous data will be lost") | ||
|
r-akemii marked this conversation as resolved.
Outdated
|
||
|
|
||
| def tr(self, message): | ||
| return trlt("AddMatrixFromLayer", message) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.