2

I know how to make Hub Lines using the GUI, I want to know how to do the same using the Python Console, a tutorial would be great.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
amamdouh
  • 129
  • 9
  • Could you add some more information please? We don't know if you are looking for a link or a tutorial or something entirely different. – Paul Jun 30 '14 at 16:52
  • I know how create the Hub Lines, I want to know how to do it using the python console. A tutorial would be great. Thank you. – amamdouh Jun 30 '14 at 17:06
  • @amamdouh, you've just restated your question almost verbatim. Could you please add more information as Paul suggested? What have you tried so far? Could you post any code you've written? – Fezter Jun 30 '14 at 23:49
  • I asked a similar question and found this which may be of some use: http://gis.stackexchange.com/questions/11216/is-there-a-way-to-access-qgis-plugins-in-python – Joseph Jul 01 '14 at 09:49
  • it's what I am looking for, but I can't find the code for MMQGIS plugin anywhere. thanks. – amamdouh Jul 01 '14 at 11:54

2 Answers2

6

When I don't know a Python module, I use the dir()command or the see module to know what's inside.

(in the Python console)

from see import see
import mmqgis
dir(mmqgis)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'classFactory', 'mmqgis_dialogs', 'mmqgis_library', 'mmqgis_menu']
# or
see(mmqgis)
.classFactory()    .mmqgis_dialogs    .mmqgis_library    .mmqgis_menu

classFactory(), .mmqgis_dialogs() or .mmqis_menu() are clearly for the interface of the plugin, so the functions that you are looking for are in .mmqgis_library.

You can try:

see(mmqgis.mmqgis_library)
(lot of items)

but it is more interesting here to find the original mmqgis_library Python file:

import mmqgis.mmqgis_library
mmqgis.mmqgis_library.__file__
'/.../.qgis2/python/plugins/mmqgis/mmqgis_library.pyc'

The file concerned is mmqgis_library.py with (lines 3202 ...):

# --------------------------------------------------------
#    mmqgis_hub_distance - Create shapefile of distances
#              from points to nearest hub
# --------------------------------------------------------

class mmqgis_hub:
   def __init__(self, point, newname):
      self.point = point
      self.name = newname
 ....

So:

from mmqgis.mmqgis_library import mmqgis_hub

Works, but after you need to understand the class to use it...

gene
  • 54,868
  • 3
  • 110
  • 187
0

If it helps anyone else who is starting out with the python console, here's the outline of how I used the MMQGIS's geocoding function, which locally geocodes using the Census' streetfiles (with the NA's changed to zeros), after I had already installed MMQGIS :

#setup
import mmqgis.mmqgis_library
from mmqgis.mmqgis_library import mmqgis_geocode_street_layer
fpath_input_addr="/Users/mollybryan/Documents/geocoding/to_geocode/bladder/attempt20230504_"
fpath_input_cty="/Users/mollybryan/Documents/geocoding/street_files/address_feature_nonull/clean"
fpath_output="/Users/mollybryan/Documents/geocoding/automation_attempts/"

#the geocode function def rungeocode(): input_layer = QgsVectorLayer(input_layer_name)

message = mmqgis_geocode_street_layer(input_csv_name, \
    number_column, street_name_column, zip_column, \
    input_layer, street_name_attr, left_from_attr, left_to_attr, left_zip_attr, \
    right_from_attr, right_to_attr, right_zip_attr, \
    from_x_attr, from_y_attr, to_x_attr, to_y_attr, setback, \
    output_file_name, not_found_file)

print("Geocode Street Layer: " + str(message))

#variables that don't change number_column = "geo_num" street_name_column = "geo_street" zip_column = "zip"

street_name_attr = "FULLNAME" left_from_attr = "LFROMHN" left_to_attr = "LTOHN" left_zip_attr = "ZIPL" right_from_attr = "RFROMHN" right_to_attr = "RTOHN" right_zip_attr = "ZIPR"

from_x_attr = None from_y_attr = None to_x_attr = None to_y_attr = None setback = 0

#list of the counties lst_cty=["12095","12097","12117"]

#runit for x in lst_cty: print("beginning "+x) input_csv_name=fpath_input_addr+x+".csv" input_layer_name=fpath_input_cty+x+".shp" output_file_name=fpath_output+x+".shp" not_found_file=fpath_output+x+"_rejects.csv"

rungeocode()


M. M.
  • 1
  • 1