Skip to content

Commit

Permalink
PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchadwin committed Jan 16, 2018
1 parent 74b143e commit ea84906
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ before_script:
- export LD_LIBRARY_PATH=${QGIS_PREFIX_PATH}/lib

script:
- pycodestyle --exclude=test ./ --format=pylint
- pycodestyle --exclude=test,resources*.py,help ./ --format=pylint
- make pylint
- QGIS_DEBUG=0 xvfb-run --server-args="-screen 0, 1024x768x24" nosetests -s --nologcapture -A 'not slow' -v --rednose --verbose

Expand Down
3 changes: 2 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/***************************************************************************
GridRef
A QGIS plugin
This plugin takes the coords of the map cnavas and translates to an Ordnance Survey Grid Reference e.g. SX4855
This plugin takes the coords of the map cnavas and translates to an Ordnance
Survey Grid Reference e.g. SX4855
-------------------
begin : 2014-08-21
copyright : (C) 2014 by Matt Travis
Expand Down
36 changes: 17 additions & 19 deletions grid_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/***************************************************************************
GridRef
A QGIS plugin
This plugin takes the coords of the map cnavas and translates to an Ordnance Survey Grid Reference e.g. SX4855
This plugin takes the coords of the map cnavas and translates to an Ordnance
Survey Grid Reference e.g. SX4855
-------------------
begin : 2014-08-21
git sha : $Format:%H$
Expand Down Expand Up @@ -81,20 +82,18 @@ def tr(self, message):
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('GridRef', message)


def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
shortcut=None,
checkable=None,
parent=None):
def add_action(self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
shortcut=None,
checkable=None,
parent=None):
"""Add a toolbar icon to the InaSAFE toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
Expand Down Expand Up @@ -184,7 +183,7 @@ def initGui(self):

precision_field = QSpinBox()
precision_field.setToolTip("Coordinates precision")
precision_field.setRange(2,4)
precision_field.setRange(2, 4)
self.toolbar.addWidget(precision_field)

self.widget = OSGBWidget(self.iface, self, precision_field)
Expand All @@ -204,12 +203,10 @@ def unload(self):
del self.actionRun
del self.widget


def run(self):
"""Run method that performs all the real work"""
self.widget.setVisible(not self.widget.isVisible())


def run_keyboard(self):
""" This is the function called by the action assigned to a
keyboard shortcut. It will determine the position of the mouse
Expand All @@ -223,4 +220,5 @@ def run_keyboard(self):
os_ref = xy_to_osgb(self.x, self.y)
# QMessageBox.information(None, "Info", "Grid Ref: " + os_ref)
QApplication.clipboard().setText(os_ref)
self.iface.messageBar().pushMessage("Grid reference copied to clipboard.", duration=1)
self.iface.messageBar().pushMessage(
"Grid reference copied to clipboard.", duration=1)
26 changes: 17 additions & 9 deletions grid_ref_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

from PyQt4 import uic
from PyQt4.QtCore import *
Expand All @@ -24,48 +25,55 @@
from qgis.gui import *
from qgis.core import *


class GridRefException(Exception):
pass


def reproject_point_to_4326(canvas, point):
crsSrc = canvas.mapSettings().destinationCrs() # 27700
crsSrc = canvas.mapSettings().destinationCrs() # 27700
crsDest = QgsCoordinateReferenceSystem(4326)
xform = QgsCoordinateTransform(crsSrc, crsDest)
return xform.transform(point)


def reproject_point_from_4326(canvas, point):
crsSrc = QgsCoordinateReferenceSystem(4326)
crsDest = canvas.mapSettings().destinationCrs() # 27700
crsDest = canvas.mapSettings().destinationCrs() # 27700
xform = QgsCoordinateTransform(crsSrc, crsDest)
return xform.transform(point)


def gen_marker(canvas, point):
marker = QgsVertexMarker(canvas)
marker.setColor(QColor(255, 0, 0))
marker.setPenWidth(2)
marker.setCenter(point)
return marker


def centre_on_point(canvas, point):
rect = QgsRectangle(point, point)
canvas.setExtent(rect)
canvas.refresh()


def point_from_longlat_text(longlat):
longlat = longlat.split(",")
if len(longlat) != 2:
raise GridRefException()

longitude = float(longlat[0].strip()) #regex validator ensures it passes
latitude = float(longlat[1].strip()) #regex validator ensures it passes
if longitude>180 or longitude<-180:
longitude = float(longlat[0].strip()) # regex validator ensures it passes
latitude = float(longlat[1].strip()) # regex validator ensures it passes
if longitude > 180 or longitude < -180:
raise GridRefException()
if latitude>90 or latitude<-90:
if latitude > 90 or latitude < -90:
raise GridRefException()
return QgsPoint(longitude, latitude)


def load_ui(name):
ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'ui',
name + '.ui')
'ui',
name + '.ui')
return uic.loadUiType(ui_file)
49 changes: 28 additions & 21 deletions grid_ref_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

from PyQt4.QtCore import *
from PyQt4.QtGui import *
Expand All @@ -43,6 +44,7 @@

uiWidget, qtBaseClass = load_ui('grid_ref_widget')


class OSGBWidget(qtBaseClass, uiWidget):
def __init__(self, iface, plugin, precision_field, parent=None):
qtBaseClass.__init__(self)
Expand All @@ -58,17 +60,20 @@ def __init__(self, iface, plugin, precision_field, parent=None):
self._connect_signals(plugin)

def _set_icons(self):
self.btnClose.setIcon(QgsApplication.getThemeIcon( "/mIconClose.png"))
self.btnClose.setIconSize(QSize( 18, 18 ))
self.btnClose.setIcon(QgsApplication.getThemeIcon("/mIconClose.png"))
self.btnClose.setIconSize(QSize(18, 18))

self.btnPointTool.setIcon(QgsApplication.getThemeIcon( "/mActionWhatsThis.svg"))
self.btnPointTool.setIconSize(QSize( 18, 18 ))
self.btnPointTool.setIcon(
QgsApplication.getThemeIcon("/mActionWhatsThis.svg"))
self.btnPointTool.setIconSize(QSize(18, 18))

def _add_validators(self):
re = QRegExp(r'''^(\s*[a-zA-Z]{2}\s*\d{1,4}\s*\d{1,4}\s*|\[out of bounds\])$''')
re = QRegExp(
r'''^(\s*[a-zA-Z]{2}\s*\d{1,4}\s*\d{1,4}\s*|\[out of bounds\])$''')
self.editCoords.setValidator(QRegExpValidator(re, self))

re = QRegExp("^\s*[-+]?[0-9]*\.?[0-9]+\s*\,\s*[-+]?[0-9]*\.?[0-9]+\s*$")
re = QRegExp(
"^\s*[-+]?[0-9]*\.?[0-9]+\s*\,\s*[-+]?[0-9]*\.?[0-9]+\s*$")
self.editLongLat.setValidator(QRegExpValidator(re, self))

def _connect_signals(self, plugin):
Expand All @@ -83,13 +88,13 @@ def _setEditCooordsOnMouseMove(self, pt):
# dynamically determine the most sensible precision for the given scale
log_scale = math.log(self.iface.mapCanvas().scale()) / math.log(10)
if log_scale >= 6:
precision = 1000
precision = 1000
elif log_scale >= 5:
precision = 100
precision = 100
elif log_scale >= 4:
precision = 10
precision = 10
else:
precision = 1
precision = 1

if self.tool:
precision = self.tool.precision
Expand Down Expand Up @@ -126,10 +131,10 @@ def trackCoords(self, pt):

def setCoords(self):
try:
x,y = osgb_to_xy(self.editCoords.text())
point27700 = QgsPoint(x,y)
centre_on_point(self.iface.mapCanvas(), point27700)
self._add_marker(point27700)
x, y = osgb_to_xy(self.editCoords.text())
point27700 = QgsPoint(x, y)
centre_on_point(self.iface.mapCanvas(), point27700)
self._add_marker(point27700)
except GridRefException:
QMessageBox.warning(
self.iface.mapCanvas(),
Expand All @@ -138,19 +143,21 @@ def setCoords(self):

def setLongLat(self):
try:
longlat = self.editLongLat.text()
point4326 = point_from_longlat_text(longlat)
point27700 = reproject_point_from_4326(self.iface.mapCanvas(), point4326)
centre_on_point(self.iface.mapCanvas(), point27700)
self._add_marker(point27700)
longlat = self.editLongLat.text()
point4326 = point_from_longlat_text(longlat)
point27700 = reproject_point_from_4326(self.iface.mapCanvas(),
point4326)
centre_on_point(self.iface.mapCanvas(), point27700)
self._add_marker(point27700)
except GridRefException:
QMessageBox.warning(
self.iface.mapCanvas(),
"Format",
"The coordinates should be in format ##.##, ##.##")

def pickPoint(self):
self.tool = PointTool(self.iface.mapCanvas(), pow(10,self.precision_field.value()))
self.tool = PointTool(self.iface.mapCanvas(),
pow(10, self.precision_field.value()))
self.change_precision()
self.tool.setButton(self.btnPointTool)
self.iface.mapCanvas().setMapTool(self.tool)
12 changes: 8 additions & 4 deletions point_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

from qgis.gui import QgsMapTool
from qgis.core import QgsRectangle
Expand All @@ -21,6 +22,7 @@
from xy_to_osgb import xy_to_osgb
from grid_ref_utils import reproject_point_to_4326


class PointTool(QgsMapTool):
def __init__(self, canvas, precision):
QgsMapTool.__init__(self, canvas)
Expand All @@ -31,13 +33,15 @@ def canvasReleaseEvent(self, event):
x = event.pos().x()
y = event.pos().y()
point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
bbox = QgsRectangle(4999.99,4999.69,660000.06,1225000.12)
bbox = QgsRectangle(4999.99, 4999.69, 660000.06, 1225000.12)

if bbox.contains(point):
os_ref = xy_to_osgb(point.x(), point.y(), self.precision)
point_4326 = reproject_point_to_4326(self.canvas, point)
QApplication.clipboard().setText(os_ref)
msg = "Grid Ref: {}\n\nLong,Lat: {:.2f}, {:.2f}\n\nCopied to clipboard".format(os_ref, point_4326.x(), point_4326.y())
msg = "Grid Ref: {}\n\nLong,Lat: {:.2f}, {:.2f}\n\n{}".format(
os_ref, point_4326.x(), point_4326.y(), "Copied to clipboard")
QMessageBox.information(None, "OS Grid Reference", msg)
else:
QMessageBox.information(None, "OS Grid Reference", "Point out of bounds")
QMessageBox.information(None, "OS Grid Reference",
"Point out of bounds")
Loading

0 comments on commit ea84906

Please sign in to comment.