__init__.py 3.03 KB
Newer Older
Daniel Childs's avatar
Daniel Childs committed
1 2 3 4 5 6 7
# -*- coding: utf-8 -*-
"""
/***************************************************************************
 Stereonet
                                 A QGIS plugin
 Displays a geologic stereonet of selected data
                             -------------------
8 9 10 11
        begin               : 2016-11-29
        copyright           : (C) 2016 by Daniel Childs
        email               : daniel@childsgeo.com
        git sha             : $Format:%H$
Daniel Childs's avatar
Daniel Childs committed
12 13 14
 ***************************************************************************/

/***************************************************************************
15 16 17 18 19 20
 *                                                                          *
 *   This program is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation; either version 2 of the License, or      *
 *   (at your option) any later version.                                    *
 *                                                                          *
Daniel Childs's avatar
Daniel Childs committed
21 22 23 24
 ***************************************************************************/
 This script initializes the plugin, making it known to QGIS.
"""

Daniel Childs's avatar
Daniel Childs committed
25 26 27 28 29 30 31 32 33
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import mplstereonet
from qgis.core import *
from qgis.gui import *
import qgis.utils
34
import os
Daniel Childs's avatar
Daniel Childs committed
35 36

def classFactory(iface):
Daniel Childs's avatar
Daniel Childs committed
37
    return Stereonet(iface)
Daniel Childs's avatar
Daniel Childs committed
38 39 40 41 42 43

class Stereonet:
    def __init__(self, iface):
        self.iface = iface

    def initGui(self):
44 45
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.contourAction = QAction(QIcon(str(dir_path)+"/icon.png"), u'Stereonet', self.iface.mainWindow())
Daniel Childs's avatar
Daniel Childs committed
46 47
        self.contourAction.triggered.connect(self.contourPlot)
        self.iface.addToolBarIcon(self.contourAction)
48

Daniel Childs's avatar
Daniel Childs committed
49 50 51 52 53 54 55
    def unload(self):
        self.iface.removeToolBarIcon(self.contourAction)
        del self.contourAction
    def contourPlot(self):
        fig, ax = mplstereonet.subplots()
        strikes = list()
        dips = list()
56 57 58
        layers = self.iface.legendInterface().layers()
        for layer in layers:
            if layer.type() == QgsMapLayer.VectorLayer:
59
                iter = layer.selectedFeatures()
60 61 62
                strikeExists = layer.fieldNameIndex('strike')
                ddrExists = layer.fieldNameIndex('ddr')
                dipExists = layer.fieldNameIndex('dip')
63
                for feature in iter:
64 65 66 67 68 69 70 71 72 73 74
                    if strikeExists != -1 and dipExists != -1:
                        strikes.append(feature['strike'])
                        dips.append(feature['dip'])
                    elif ddrExists != -1 and dipExists != -1:
                        strikes.append(feature['ddr']-90)
                        dips.append(feature['dip'])
            else:
                continue
        cax = ax.density_contourf(strikes, dips, measurement='poles',cmap=cm.coolwarm)
        ax.pole(strikes, dips, 'k+', markersize=7)
        ax.grid(True)
75
#        fig.colorbar(cax)
76
        plt.show()