Wednesday 6 February 2019

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

We have managed to define what format of coordinates we are going to extract so far, now it's time to extract coordinates - 'real work' begins.
In this post following work is to do: user can paste raw text (NOTAM, part of eAIP or any other text with coordinates) into plugin, press button to extract coordinates, and get as result nice, easy to read table with coordinates.
Let's start form adding following widgets:
  • QTextEdit  - named textEditRawText
  • QTableWidget - tableWidgetCoordinates
  • QPushButton - pushButtonExtractCoord
We need to add method that:
  • get what coordinate order, separator and format is chosen
  • get raw text
  • create coordinate regex using CoordRegexBuilder class 
  • extract pairs of latitude and longitude
  • fill the tableWidgetCoordinates with the coordinates
All that work will be done in method extract_coordinates:

def extract_coordinates(self):
    self.set_coord_order()
    self.set_latlon_separator()
    self.set_coordinate_format()
    self.dlg.tableWidgetCoordinates.setRowCount(0)
    raw_text = self.dlg.textEditRawText.toPlainText()
    coord_regex = cet.CoordRegexBuilder(self.coord_order, self.coordinate_format, self.latlon_separator)
    extracted_coord = cet.CoordinateExtractor(raw_text, coord_regex)
    coord_list = extracted_coord.get_coord_pair_list()
    for coord in coord_list:
        if self.coord_order == cet.ORDER_LATLON:
            row_pos = self.dlg.tableWidgetCoordinates.rowCount()
            self.dlg.tableWidgetCoordinates.insertRow(row_pos)
            self.dlg.tableWidgetCoordinates.setItem(row_pos, 0, QTableWidgetItem(coord[0]))
            self.dlg.tableWidgetCoordinates.setItem(row_pos, 1, QTableWidgetItem(coord[2]))
        elif self.coord_order == cet.ORDER_LONLAT:
            row_pos = self.dlg.tableWidgetCoordinates.rowCount()
            self.dlg.tableWidgetCoordinates.insertRow(row_pos)
            self.dlg.tableWidgetCoordinates.setItem(row_pos, 0, QTableWidgetItem(coord[2]))
            self.dlg.tableWidgetCoordinates.setItem(row_pos, 1, QTableWidgetItem(coord[0]))

And we have to associate button click with method:


self.dlg.pushButtonExtractCoord.clicked.connect(self.extract_coordinates)

And it's done - we can check if our tool works correctly:

And a couple of more examples with various coordinate formats:


No comments:

Post a Comment