Monday 4 February 2019

Extracting coordinates - user friendly tool (1) (QGIS Plugin)

In the real life (work) we rather not expect following workflow:
parse text and extract coordinates in the way described in the previous post.

Let's hide all that 'What is this all about' stuff behind QGIS Plugin. User can set all required parameters to extract coordinates and press 'extract' button to do so:


'Where can I paste my text with coordinates?' - Don't worry, this is first part of building this kind of tool. In next part I will cover missing functionality.

'Coordinate pair example' shows samples based on coordinate stored in plugin. Maybe a good idea is to parse first set of characters and check if established format finds anything.

Let's copy coord_extraction_tools.py into main plugin directory, then we need to add following import statement to be able to use extraction tools:

from . import coord_extraction_tools as cet

Because we will be extracting pairs of coordinate (see previous post) we need variables that will keep this information through all time. Hence let's add following code into 'plugin' constructor:


self.coord_order = None
self.latlon_separator = None
self.coordinate_format = None

User will set parameters for coordinates format by using comboboxes, so now we need to assign selected values to appropriate variables:

def set_coord_order(self):
    if self.dlg.comboBoxCoordOrder.currentIndex() == 0:
        self.coord_order = None
    if self.dlg.comboBoxCoordOrder.currentIndex() == 1:
        self.coord_order = cet.ORDER_LATLON
    elif self.dlg.comboBoxCoordOrder.currentIndex() == 2:
        self.coord_order = cet.ORDER_LONLAT

def set_latlon_separator(self):
    if self.dlg.comboBoxCoordSeparator.currentIndex() == 0:
        self.latlon_separator = None
    elif self.dlg.comboBoxCoordSeparator.currentIndex() == 1:  # Null separator
        self.latlon_separator = cet.SEP_NULL
    elif self.dlg.comboBoxCoordSeparator.currentIndex() == 2:  # Space
        self.latlon_separator = cet.SEP_SPACE
    elif self.dlg.comboBoxCoordSeparator.currentIndex() == 3:  # Hyphen
        self.latlon_separator = cet.SEP_HYPHEN
    elif self.dlg.comboBoxCoordSeparator.currentIndex() == 4:
        self.latlon_separator = cet.SEP_SLASH
    elif self.dlg.comboBoxCoordSeparator.currentIndex() == 5:
        self.latlon_separator = '\\'

def set_coordinate_format(self):
    if self.dlg.comboBoxCoordFormat.currentIndex() == 0:
        self.coordinate_format = None
    elif self.dlg.comboBoxCoordFormat.currentIndex() == 1:
        self.coordinate_format = cet.DMSH_COMP
    elif self.dlg.comboBoxCoordFormat.currentIndex() == 2:
        self.coordinate_format = cet.HDMS_COMP
    elif self.dlg.comboBoxCoordFormat.currentIndex() == 3:
        self.coordinate_format = cet.DMSH_SEP
    elif self.dlg.comboBoxCoordFormat.currentIndex() == 4:
        self.coordinate_format = cet.HDMS_SEP

If current index each of combobox is set to '0' - it equals option 'choose' and variable that keeps type/format of coordinate pair part is set to None, or appropriate constant otherwise.
I will use this fact when I want to display sample coordinates pairs if all of the variables are set.

Method that creates desired format and displays it on label if all parameters are chosen:

def get_sample_coord_pair_format(self):
    self.set_coord_order()
    self.set_latlon_separator()
    self.set_coordinate_format()
    if self.coord_order is not None and self.latlon_separator is not None and self.coordinate_format is not None:
        sample_format = get_sample_coordinates(self.coord_order,
                                               self.latlon_separator,
                                               self.coordinate_format)
        self.dlg.labelCoordExample.setText(sample_format)
    else:
        self.dlg.labelCoordExample.setText("Choose to see format example")

Notice that we only get information about desired format, not regular expression that can be used extracting coordinates.
Here are some examples of various formats:

No comments:

Post a Comment