-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparalleloffset.py
More file actions
147 lines (129 loc) · 5.56 KB
/
paralleloffset.py
File metadata and controls
147 lines (129 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ParallelOffset
A QGIS plugin
Creates a parallell line at a given distance
-------------------
begin : 2013-07-23
copyright : (C) 2013 by City and County of Swansea
email : steven.gardiner@swansea.gov.uk
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#Import other Libraries
from qgis.core import *
from shapely.geometry import LineString
from qgis.utils import iface
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from paralleloffsetdialog import ParallelOffsetDialog
class ParallelOffset:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + "/python/plugins/paralleloffset"
# initialize locale
localePath = ""
locale = QSettings().value("locale/userLocale")[0:2]
if QFileInfo(self.plugin_dir).exists():
localePath = self.plugin_dir + "/i18n/paralleloffset_" + locale + ".qm"
if QFileInfo(localePath).exists():
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = ParallelOffsetDialog()
def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(
QIcon(":/plugins/paralleloffset/icon.png"),
u"Line Offset", self.iface.mainWindow())
# connect the action to the run method
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu(u"&Line Offset", self.action)
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu(u"&Line Offset", self.action)
self.iface.removeToolBarIcon(self.action)
# run method that performs all the real work
def run(self):
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result == 1:
layers = iface.legendInterface().layers()
if len(layers) != 0:
#Check that there is a selected layer
layer = iface.mapCanvas().currentLayer()
if layer.type() == 0:
#check selected layer is a vector layer
if layer.geometryType() == 1:
#check to see if selected layer is a linestring geometry
if len(layer.selectedFeatures()) != 0:
#Dir = float(self.dlg.ui.radLeft.isChecked()
layers = iface.legendInterface().layers()
Lexists = "false"
#check to see if the virtual layer already exists
for layer in layers:
if layer.name() == "Parallel_Offset":
Lexists = "true"
vl = layer
#if it doesn't exist create it
if Lexists == "false":
vl = QgsVectorLayer("Linestring?field=offset:integer&field=direction:string(10)&field=method:string(10)","Parallel_Offset","memory")
#vl = QgsVectorLayer("Linestring", "Parallel_Offset", "memory")
pr = vl.dataProvider()
vl.startEditing()
#pr.addAttributes( [ QgsField("offset", Double), QgsField("direction", String), QgsField("method", String) ] )
else:
pr = vl.dataProvider()
vl.startEditing()
#get the direction
Dir = 'right'
if self.dlg.ui.radLeft.isChecked():
Dir = 'left'
#get the method
if self.dlg.ui.radRound.isChecked():
js = 1
if self.dlg.ui.radMitre.isChecked():
js = 2
if self.dlg.ui.radBevel.isChecked():
js = 3
#get the offset
loffset = float(self.dlg.ui.txtDistance.text())
#create the new feature from the selection
for feature in layer.selectedFeatures():
geom = feature.geometry()
h = geom.asPolyline()
line = LineString(h)
nline = line.parallel_offset(loffset, Dir,resolution=16,join_style=js,mitre_limit=10.0)
#turn nline back into a polyline and add it to the map as a new layer.
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromWkt(str(nline)))
fet.setAttributes( [loffset,Dir ,js] )
pr.addFeatures( [ fet ] )
vl.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(vl)
mc=self.iface.mapCanvas()
mc.refresh()
# do something useful (delete the line containing pass and
# substitute with your code)
#pass