qgis_processing_algorithm.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/python3
  2. # https://docs.qgis.org/3.18/en/docs/pyqgis_developer_cookbook/intro.html
  3. # export LD_LIBRARY_PATH=/usr/lib/qgis
  4. # export PYTHONPATH=/usr/share/qgis/python
  5. # export LD_LIBRARY_PATH=/usr/share/qgis/python
  6. # export PYTHONPATH=$PYTHONPATH:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/ForestRoadsNetworksUPLOAD:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/AequilibraE/aequilibrae:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/csv_tools:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/networks:/usr/share/qgis/python:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins:/usr/share/qgis/python/plugins:/usr/lib/python38.zip:/usr/lib/python3.8:/usr/lib/python3.8/lib-dynload:/home/ardo/.local/lib/python3.8/site-packages:/usr/local/lib/python3.8/dist-packages:/usr/lib/python3/dist-packages:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python:/usr/lib/python3/dist-packages/IPython/extensions:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/qgis2web:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/postgisQueryBuilder:/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/postgisQueryBuilder/extlibs
  7. # print(os.environ['PATH'])
  8. # print(os.environ)
  9. # Lisa plugina metadata.txt faili: # ardo !!!!!!!!!!!
  10. # hasProcessingProvider=yes
  11. import qgis
  12. from qgis.core import *
  13. import sys
  14. import os
  15. # Supply path to qgis install location
  16. QgsApplication.setPrefixPath("/usr", True)
  17. # Create a reference to the QgsApplication.
  18. # Setting the second argument to True enables the GUI. We need
  19. # this since this is a custom application.
  20. # qgs = QgsApplication([], True)
  21. qgs = QgsApplication([], False)
  22. # load providers
  23. qgs.initQgis()
  24. # Append the path where processing plugin can be found
  25. sys.path.append('/usr/share/qgis/python/plugins')
  26. sys.path.append('/home/ardo/.local/share/QGIS/QGIS3/profiles/default/python/plugins')
  27. import processing
  28. ## QGIS core algoritmid
  29. from processing.core.Processing import Processing
  30. Processing.initialize()
  31. # qgis.core.QgsApplication.processingRegistry().addProvider(qgis.analysis.QgsNativeAlgorithms())
  32. # Otsi sõna järele mooduleid.
  33. otsi = "graph"
  34. print("\nLeitud moodulid:")
  35. print([x.id() for x in QgsApplication.processingRegistry().algorithms() if otsi in x.id()])
  36. print("\n\n")
  37. from processing.tools import *
  38. # from qgis.analysis import QgsNativeAlgorithms
  39. # QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
  40. import PyQt5
  41. from networks.creer_graphe import CreerGraphe
  42. print(CreerGraphe().name())
  43. # print(CreerGraphe().shortHelpString())
  44. # context = QgsProcessingContext()
  45. # feedback = QgsProcessingFeedback()
  46. ## !!!!!!!!! Niimoodi saab layer'eid lisada QGIS pythoni käsurealt !!!!!!!!!!
  47. ## !!!!!!!!!! Väga mugav kasutada kui palju layer'eid vaja lisada !!!!!!!!!!!
  48. ## Load postgis layer
  49. uri = QgsDataSourceUri()
  50. # set host name, port, database name, username and password
  51. uri.setConnection("localhost", "6432", "data", "osm", "osm")
  52. # set database schema, table name, geometry column and optionally
  53. # subset (WHERE clause)
  54. uri.setDataSource("minu_teed", "eesti", "geom", "id = 1", "id")
  55. vlayer = QgsVectorLayer(uri.uri(False), "my_layer", "postgres")
  56. if not vlayer.isValid():
  57. print("Layer failed to load!")
  58. else:
  59. QgsProject.instance().addMapLayer(vlayer)
  60. ## Load gpkg layer
  61. path_to_gpkg = os.path.join("/data","gpkg","artiklid","artikkel_210127_valga_matsalu_lahemaa", "links.gpkg")
  62. # append the layername part
  63. gpkg_countries_layer = path_to_gpkg + "|layername=teedevork_riigiteed"
  64. # e.g. gpkg_places_layer = "/usr/share/qgis/resources/data/world_map.gpkg|layername=countries"
  65. vlayer2 = QgsVectorLayer(gpkg_countries_layer, "Countries layer", "ogr")
  66. if not vlayer2.isValid():
  67. print("Layer failed to load!")
  68. else:
  69. QgsProject.instance().addMapLayer(vlayer2)
  70. # params = { 'IDENT' : 1, 'NOEUDS' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' password=\'osm\' sslmode=disable table=\"minu_teed\".\"aaa1\" (geom)', 'PREFIXE' : '', 'RESEAU' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/links.gpkg', 'SENS' : '' }
  71. params = { 'IDENT' : 1,
  72. 'NOEUDS' : vlayer,
  73. 'PREFIXE' : '', 'RESEAU' : vlayer2, 'SENS' : '' }
  74. # alg = "Networks:build_graph"
  75. # processing.run(alg,params)
  76. # print(CreerGraphe().createInstance())
  77. print(params['RESEAU'])
  78. print("---------------------")
  79. print(params['NOEUDS'])
  80. print("---------------------")
  81. CreerGraphe().initAlgorithm(config={})
  82. # CreerGraphe().processAlgorithm(parameters = params, context=context, feedback=feedback)
  83. # CreerGraphe().initAlgorithm(config=None)
  84. # for alg in qgs.processingRegistry().algorithms():
  85. # print(alg.id(), "->", alg.displayName())
  86. # processing.run("native:centroids", { 'ALL_PARTS' : False, \
  87. # 'INPUT' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable authcfg=0i30k14 key=\'id\' srid=3301 type=MultiPolygon checkPrimaryKeyUnicity=\'1\' table="minu_teed"."eesti" (geom)', \
  88. # 'OUTPUT' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' password=\'osm\' sslmode=disable table="minu_teed"."x325" (geom)' })
  89. # processing.run("Networks:build_graph", { 'IDENT' : 1, 'NOEUDS' : 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' password=\'osm\' sslmode=disable table=\"minu_teed\".\"aaa1\" (geom)', 'PREFIXE' : '', 'RESEAU' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/links.gpkg', 'SENS' : '' })
  90. # processing.run("qneat3:shortestpathpointtopoint", {'INPUT':'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' sslmode=disable authcfg=0i30k14 key=\'link_id\' srid=3301 type=LineString checkPrimaryKeyUnicity=\'1\' table=\"minu_teed\".\"links\" (geom)','START_POINT':'640381.958323,6519317.618656 [EPSG:3301]','END_POINT':'669499.921921,6528373.950681 [EPSG:3301]','STRATEGY':0,'ENTRY_COST_CALCULATION_METHOD':0,'DIRECTION_FIELD':'','VALUE_FORWARD':'','VALUE_BACKWARD':'','VALUE_BOTH':'','DEFAULT_DIRECTION':2,'SPEED_FIELD':'','DEFAULT_SPEED':5,'TOLERANCE':0,'OUTPUT':'TEMPORARY_OUTPUT'})
  91. # processing.algorithmHelp("qgis:creategrid")
  92. processing.algorithmHelp("Networks:build_graph")
  93. ## Load postgis layer
  94. # uri = QgsDataSourceUri()
  95. # # set host name, port, database name, username and password
  96. # uri.setConnection("localhost", "6432", "data", "osm", "osm")
  97. # # set database schema, table name, geometry column and optionally
  98. # # subset (WHERE clause)
  99. # uri.setDataSource("minu_teed", "eesti", "geom", "id = 1", "id")
  100. # vlayer = QgsVectorLayer(uri.uri(False), "my_layer", "postgres")
  101. # print (vlayer)
  102. # processing.run("native:buffer", {'INPUT': vlayer, \
  103. # 'OUTPUT': 'postgres://dbname=\'data\' host=localhost port=6432 user=\'osm\' password=\'osm\' sslmode=disable table="minu_teed"."z225" (geom)'})
  104. print('How many algorithms are there? '+ str(len(QgsApplication.processingRegistry().algorithms())))
  105. # print(sys.path)
  106. # Finally, exitQgis() is called to remove the
  107. # provider and layer registries from memory
  108. qgs.exitQgis()