forked from warrieka/geopunt4Qgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometryhelper.py
245 lines (210 loc) · 9.72 KB
/
geometryhelper.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: utf-8 -*-
"""
/***************************************************************************
geometryHelper
A QGIS plugin
"Tool om geopunt in QGIS te gebruiken"
-------------------
begin : 2013-12-05
copyright : (C) 2013 by Kay Warrie
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import *
from qgis.core import *
from PyQt4.QtGui import QFileDialog, QColor
from qgis.gui import QgsVertexMarker
import os
class geometryHelper:
def __init__(self , iface ):
self.iface = iface
self.canvas = iface.mapCanvas()
self.adreslayerid = ''
@staticmethod
def getGetMapCrs(iface):
new24 = QGis.QGIS_VERSION_INT >= 20400
return ( iface.mapCanvas().mapSettings().destinationCrs() if new24
else iface.mapCanvas().mapRenderer().destinationCrs() )
def prjPtToMapCrs( self, xy , fromCRS=4326 ):
point = QgsPoint( xy[0], xy[1] )
fromCrs = QgsCoordinateReferenceSystem(fromCRS)
toCrs = self.getGetMapCrs(self.iface)
xform = QgsCoordinateTransform( fromCrs, toCrs )
return xform.transform( point )
def prjPtFromMapCrs( self, xy , toCRS=31370 ):
point = QgsPoint( xy[0], xy[1] )
toCrs = QgsCoordinateReferenceSystem(toCRS)
fromCrs = self.getGetMapCrs(self.iface)
xform = QgsCoordinateTransform( fromCrs, toCrs )
return xform.transform( point )
def prjLineFromMapCrs(self, lineString, toCRS=4326 ):
fromCrs = self.getGetMapCrs(self.iface)
toCrs = QgsCoordinateReferenceSystem(toCRS)
xform = QgsCoordinateTransform(fromCrs, toCrs)
wgsLine = [ xform.transform( xy ) for xy in lineString.asPolyline()]
return QgsGeometry.fromPolyline( wgsLine )
def prjLineToMapCrs(self, lineString, fromCRS=4326 ):
fromCrs = QgsCoordinateReferenceSystem(fromCRS)
toCrs = self.getGetMapCrs(self.iface)
xform = QgsCoordinateTransform(fromCrs, toCrs)
if isinstance(lineString, QgsGeometry):
wgsLine = [ xform.transform( xy ) for xy in lineString.asPolyline()]
if hasattr(lineString, '__iter__'):
wgsLine = [ xform.transform( QgsPoint(xy[0], xy[1]) ) for xy in lineString]
return QgsGeometry.fromPolyline( wgsLine )
def zoomtoRec(self, xyMin, xyMax , crs=None):
"""zoom to rectangle from 2 points with given crs, default= mapCRS"""
if crs is None:
crs = self.getGetMapCrs(self.iface)
maxpoint = QgsPoint(xyMax[0], xyMax[1])
minpoint = QgsPoint(xyMin[0], xyMin[1])
pmaxpoint = self.prjPtToMapCrs(maxpoint, crs)
pminpoint = self.prjPtToMapCrs(minpoint, crs)
# Create a rectangle to cover the new extent
rect = QgsRectangle( pmaxpoint, pminpoint )
# Set the extent to our new rectangle
self.iface.mapCanvas().setExtent(rect)
# Refresh the map
self.iface.mapCanvas().refresh()
def zoomtoRec2(self, bounds, crs=None):
"zoom to rectangle from a list containing: [xmin,ymin,xmax,ymax] with given crs, default= mapCRS"
if not bounds or len(bounds) != 4:
return
if crs is None:
crs = self.getGetMapCrs(self.iface)
maxpoint = QgsPoint( bounds[0], bounds[1])
minpoint = QgsPoint( bounds[2], bounds[3])
pmaxpoint = self.prjPtToMapCrs(maxpoint, crs)
pminpoint = self.prjPtToMapCrs(minpoint, crs)
# Create a rectangle to cover the new extent
rect = QgsRectangle( pmaxpoint, pminpoint )
# Set the extent to our new rectangle
self.iface.mapCanvas().setExtent(rect)
# Refresh the map
self.iface.mapCanvas().refresh()
def save_adres_point(self, point, address, typeAddress='', layername="Geopunt_adres", saveToFile=False, sender=None, startFolder=None ):
attributes = [QgsField("adres", QVariant.String), QgsField("type", QVariant.String)]
mapcrs = self.getGetMapCrs(self.iface)
if not QgsMapLayerRegistry.instance().mapLayer(self.adreslayerid):
self.adreslayer = QgsVectorLayer("Point", layername, "memory")
self.adresProvider = self.adreslayer.dataProvider()
self.adresProvider.addAttributes(attributes)
self.adreslayer.updateFields()
# add a feature
fields= self.adreslayer.pendingFields()
fet = QgsFeature(fields)
#set geometry and project from mapCRS
xform = QgsCoordinateTransform( mapcrs, self.adreslayer.crs() )
prjPoint = xform.transform( point )
fet.setGeometry(QgsGeometry.fromPoint(prjPoint))
#populate fields
fet['adres'] = address
fet['type'] = typeAddress
self.adresProvider.addFeatures([ fet ])
""
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
self.adreslayer.updateExtents()
# save memoryLAYER to file and replace all references
if saveToFile and not QgsMapLayerRegistry.instance().mapLayer(self.adreslayerid):
save = self._saveToFile( sender, startFolder )
if save:
fpath, flType = save
error = QgsVectorFileWriter.writeAsVectorFormat(self.adreslayer, fpath, "utf-8", None, flType)
if error == QgsVectorFileWriter.NoError:
self.adreslayer = QgsVectorLayer( fpath, layername, "ogr")
self.adresProvider = self.adreslayer.dataProvider()
else:
del self.adreslayer, self.adresProvider
return
else:
del self.adreslayer, self.adresProvider
return
# add to map
QgsMapLayerRegistry.instance().addMapLayer(self.adreslayer)
#labels
palyr = QgsPalLayerSettings()
palyr.readFromLayer( self.adreslayer )
palyr.enabled = True
palyr.fieldName = 'adres'
palyr.placement = QgsPalLayerSettings.Free
palyr.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'8','')
palyr.dist = 1
palyr.bufferSize = 1
palyr.bufferDraw = 1
palyr.writeToLayer( self.adreslayer )
# store layer id and refresh
self.adreslayerid = self.adreslayer.id()
self.canvas.refresh()
def _saveToFile( self, sender, startFolder=None ):
'save to file'
#"Shape Files (*.shp);;Geojson File (*.geojson);;GML ( *.gml);;Comma separated value File (excel) (*.csv);;MapInfo TAB (*.TAB);;Any File (*.*)"
filter = "ESRI Shape Files (*.shp);;SpatiaLite (*.sqlite);;Any File (*.*)" #show only formats with update capabilty
Fdlg = QFileDialog()
Fdlg.setFileMode(QFileDialog.AnyFile)
fName = QFileDialog.getSaveFileName(sender, "open file", filter=filter, directory=startFolder)
if fName:
ext = os.path.splitext( fName )[1]
if "SHP" in ext.upper():
flType = "ESRI Shapefile"
elif "SQLITE" in ext.upper():
flType = "SQLite"
elif "GEOJSON" in ext.upper(): #no update possible -> hidden
flType = "GeoJSON"
elif "GML" in ext.upper():
flType = "GML"
elif 'TAB' in ext.upper():
flType = 'MapInfo File'
elif 'KML' in ext.upper():
flType = 'KML'
else:
fName = fName + ".shp"
flType = "ESRI Shapefile"
return (fName , flType )
else:
return None
def addPointGraphic(self, xy, color="#FFFF00", size=1, pen=10, markerType=QgsVertexMarker.ICON_BOX ):
"create a point Graphic at location xy and return it"
x, y = list( xy )[:2]
m = QgsVertexMarker(self.canvas)
m.setCenter(QgsPoint(x,y))
m.setColor(QColor(color))
m.setIconSize(size)
m.setIconType(markerType)
m.setPenWidth(pen)
return m
@staticmethod
def getBoundsOfPointArray( pointArray, delta=100):
minX = 1.7976931348623157e+308
maxX = -1.7976931348623157e+308
minY = 1.7976931348623157e+308
maxY = -1.7976931348623157e+308
for xy in pointArray:
x, y = list(xy)[:2]
if x > maxX: maxX = x
if x < minX: minX = x
if y > maxY: maxY = y
if y < minY: minY = y
Xdelta = (maxX - minX) /delta
Ydelta = (maxY - minY) /delta
return [ minX - Xdelta, minY - Ydelta, maxX + Xdelta, maxY + Ydelta]
@staticmethod
def getBoundsOfPoint( x, y, delta=None):
if delta is None:
if x >= 360:
delta = 300 #x bigger then 360 -> meters
else:
delta = 0.0025 #x smaller then 360 -> degrees
xmax = x + delta
xmin = x - delta
ymax = y + delta
ymin = y - delta
return [xmin, ymin, xmax,ymax]