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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
"""
@package mapwin.analysis
@brief Map display controllers for analyses (profiling, measuring)
Classes:
- analysis::AnalysisControllerBase
- analysis::ProfileController
- analysis::MeasureDistanceController
(C) 2013 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Anna Petrasova <kratochanna gmail.com>
"""
import os
import math
import wx
import core.units as units
from core.gcmd import RunCommand
from core.giface import Notification
from grass.pydispatch.signal import Signal
from grass.script.utils import parse_key_val
class AnalysisControllerBase:
"""Base class for analysis which require drawing line in map display."""
def __init__(self, giface, mapWindow):
"""
:param giface: grass interface
:param mapWindow: instance of BufferedMapWindow
"""
self._giface = giface
self._mapWindow = mapWindow
self._registeredGraphics = None
self._graphicsType = None
self._oldMouseUse = None
self._oldCursor = None
def IsActive(self):
"""Returns True if analysis mode is activated."""
return bool(self._registeredGraphics)
def _start(self, x, y):
"""Handles the actual start of drawing line
and adding each new point.
:param x,y: east north coordinates
"""
if not self._registeredGraphics.GetAllItems():
item = self._registeredGraphics.AddItem(coords=[[x, y]])
item.SetPropertyVal("penName", "analysisPen")
else:
# needed to switch mouse begin and end to draw intermediate line
# properly
coords = self._registeredGraphics.GetItem(0).GetCoords()[-1]
self._mapWindow.mouse["begin"] = self._mapWindow.Cell2Pixel(coords)
def _addPoint(self, x, y):
"""New point added.
:param x,y: east north coordinates
"""
# avoid duplicating point in the beginning
begin = self._mapWindow.mouse["begin"]
end = self._mapWindow.mouse["end"]
if begin == end:
return
# add new point and calculate distance
item = self._registeredGraphics.GetItem(0)
coords = item.GetCoords() + [[x, y]]
item.SetCoords(coords)
# draw
self._mapWindow.ClearLines()
self._registeredGraphics.Draw()
self._mapWindow.Refresh()
wx.SafeYield()
self._doAnalysis(coords)
def _doAnalysis(self, coords):
"""Perform the required analysis
(compute distance, update profile)
:param coords: EN coordinates
"""
raise NotImplementedError()
def _disconnectAll(self):
"""Disconnect all mouse signals
to stop drawing."""
raise NotImplementedError()
def _connectAll(self):
"""Connect all mouse signals to draw."""
raise NotImplementedError()
def _getPen(self):
"""Returns wx.Pen instance."""
raise NotImplementedError()
def Stop(self, restore=True):
"""Analysis mode is stopped.
:param restore: if restore previous cursor, mouse['use']
"""
self._mapWindow.ClearLines(pdc=self._mapWindow.pdcTmp)
self._mapWindow.mouse["end"] = self._mapWindow.mouse["begin"]
# disconnect mouse events
self._disconnectAll()
# unregister
self._mapWindow.UnregisterGraphicsToDraw(self._registeredGraphics)
self._registeredGraphics = None
self._mapWindow.UpdateMap(render=False)
if restore:
# restore mouse['use'] and cursor to the state before measuring
# starts
self._mapWindow.SetNamedCursor(self._oldCursor)
self._mapWindow.mouse["use"] = self._oldMouseUse
def Start(self):
"""Init analysis: register graphics to map window,
connect required mouse signals.
"""
self._oldMouseUse = self._mapWindow.mouse["use"]
self._oldCursor = self._mapWindow.GetNamedCursor()
self._registeredGraphics = self._mapWindow.RegisterGraphicsToDraw(
graphicsType=self._graphicsType, mapCoords=True
)
self._connectAll()
# change mouse['box'] and pen to draw line during dragging
# TODO: better solution for drawing this line
self._mapWindow.mouse["use"] = None
self._mapWindow.mouse["box"] = "line"
self._mapWindow.pen = wx.Pen(colour="red", width=2, style=wx.SHORT_DASH)
self._registeredGraphics.AddPen("analysisPen", self._getPen())
# change the cursor
self._mapWindow.SetNamedCursor("pencil")
class ProfileController(AnalysisControllerBase):
"""Class controls profiling in map display.
It should be used inside ProfileFrame
"""
def __init__(self, giface, mapWindow):
AnalysisControllerBase.__init__(self, giface=giface, mapWindow=mapWindow)
self.transectChanged = Signal("ProfileController.transectChanged")
self._graphicsType = "line"
def _doAnalysis(self, coords):
"""Informs profile dialog that profile changed.
:param coords: EN coordinates
"""
self.transectChanged.emit(coords=coords)
def _disconnectAll(self):
self._mapWindow.mouseLeftDown.disconnect(self._start)
self._mapWindow.mouseLeftUp.disconnect(self._addPoint)
def _connectAll(self):
self._mapWindow.mouseLeftDown.connect(self._start)
self._mapWindow.mouseLeftUp.connect(self._addPoint)
def _getPen(self):
return wx.Pen(colour=wx.Colour(0, 100, 0), width=2, style=wx.SHORT_DASH)
def Stop(self, restore=True):
AnalysisControllerBase.Stop(self, restore=restore)
self.transectChanged.emit(coords=[])
class MeasureDistanceController(AnalysisControllerBase):
"""Class controls measuring distance in map display."""
def __init__(self, giface, mapWindow):
AnalysisControllerBase.__init__(self, giface=giface, mapWindow=mapWindow)
self._projInfo = self._mapWindow.Map.projinfo
self._totaldist = 0.0 # total measured distance
self._useCtypes = False
self._graphicsType = "line"
def _doAnalysis(self, coords):
"""New point added.
:param x,y: east north coordinates
"""
self.MeasureDist(coords[-2], coords[-1])
def _disconnectAll(self):
self._mapWindow.mouseLeftDown.disconnect(self._start)
self._mapWindow.mouseLeftUp.disconnect(self._addPoint)
self._mapWindow.mouseDClick.disconnect(self.Stop)
def _connectAll(self):
self._mapWindow.mouseLeftDown.connect(self._start)
self._mapWindow.mouseLeftUp.connect(self._addPoint)
self._mapWindow.mouseDClick.connect(self.Stop)
def _getPen(self):
return wx.Pen(colour="green", width=2, style=wx.SHORT_DASH)
def Stop(self, restore=True):
if not self.IsActive():
return
AnalysisControllerBase.Stop(self, restore=restore)
self._giface.WriteCmdLog(_("Measuring finished"))
def Start(self):
"""Init measurement routine that calculates map distance
along transect drawn on map display
"""
if self.IsActive():
return
AnalysisControllerBase.Start(self)
self._totaldist = 0.0 # total measured distance
# initiating output (and write a message)
# e.g., in Layer Manager switch to output console
# TODO: this should be something like: write important message or write tip
# TODO: mixed 'switching' and message? no, measuring handles 'swithing'
# on its own
self._giface.WriteWarning(
_(
"Click and drag with left mouse button "
"to measure.%s"
"Double click with left button to clear."
)
% (os.linesep)
)
if self._projInfo["proj"] != "xy":
mapunits = self._projInfo["units"]
self._giface.WriteCmdLog(_("Measuring distance") + " (" + mapunits + "):")
else:
self._giface.WriteCmdLog(_("Measuring distance:"))
if self._projInfo["proj"] == "ll":
try:
import grass.lib.gis as gislib
gislib.G_begin_distance_calculations()
self._useCtypes = True
except ImportError as e:
self._giface.WriteWarning(
_(
"Geodesic distance calculation "
"is not available.\n"
"Reason: %s" % e
)
)
def MeasureDist(self, beginpt, endpt):
"""Calculate distance and print to output window.
:param beginpt,endpt: EN coordinates
"""
# move also Distance method?
dist, (north, east) = self._mapWindow.Distance(beginpt, endpt, screen=False)
dist = round(dist, 3)
mapunits = self._projInfo["units"]
if mapunits == "degrees" and self._useCtypes:
mapunits = "meters"
d, dunits = units.formatDist(dist, mapunits)
self._totaldist += dist
td, tdunits = units.formatDist(self._totaldist, mapunits)
if dunits == "units" and mapunits:
dunits = tdunits = mapunits
strdist = str(d)
strtotdist = str(td)
if self._projInfo["proj"] == "xy" or "degree" not in self._projInfo["unit"]:
angle = int(math.degrees(math.atan2(north, east)) + 0.5)
# uncomment below (or flip order of atan2(y,x) above) to use
# the mathematical theta convention (CCW from +x axis)
# angle = 90 - angle
if angle < 0:
angle = 360 + angle
mstring = "%s = %s %s\n%s = %s %s\n%s = %d %s\n%s" % (
_("segment"),
strdist,
dunits,
_("total distance"),
strtotdist,
tdunits,
_("bearing"),
angle,
_("degrees (clockwise from grid-north)"),
"-" * 60,
)
else:
mstring = "%s = %s %s\n%s = %s %s\n%s" % (
_("segment"),
strdist,
dunits,
_("total distance"),
strtotdist,
tdunits,
"-" * 60,
)
self._giface.WriteLog(mstring, notification=Notification.MAKE_VISIBLE)
return dist
class MeasureAreaController(AnalysisControllerBase):
"""Class controls measuring area in map display."""
def __init__(self, giface, mapWindow):
AnalysisControllerBase.__init__(self, giface=giface, mapWindow=mapWindow)
self._graphicsType = "polygon"
def _doAnalysis(self, coords):
"""New point added.
:param coords: east north coordinates as a list
"""
self.MeasureArea(coords)
def _disconnectAll(self):
self._mapWindow.mouseLeftDown.disconnect(self._start)
self._mapWindow.mouseLeftUp.disconnect(self._addPoint)
self._mapWindow.mouseDClick.disconnect(self.Stop)
def _connectAll(self):
self._mapWindow.mouseLeftDown.connect(self._start)
self._mapWindow.mouseLeftUp.connect(self._addPoint)
self._mapWindow.mouseDClick.connect(self.Stop)
def _getPen(self):
return wx.Pen(colour="green", width=2, style=wx.SOLID)
def Stop(self, restore=True):
if not self.IsActive():
return
AnalysisControllerBase.Stop(self, restore=restore)
self._giface.WriteCmdLog(_("Measuring finished"))
def Start(self):
"""Init measurement routine that calculates area of polygon
drawn on map display.
"""
if self.IsActive():
return
AnalysisControllerBase.Start(self)
self._giface.WriteWarning(
_(
"Click and drag with left mouse button "
"to measure.%s"
"Double click with left button to clear."
)
% (os.linesep)
)
self._giface.WriteCmdLog(_("Measuring area:"))
def MeasureArea(self, coords):
"""Calculate area and print to output window.
:param coords: list of E, N coordinates
"""
# TODO: make sure appending first point is needed for m.measure
coordinates = coords + [coords[0]]
coordinates = ",".join(
[str(item) for sublist in coordinates for item in sublist]
)
result = RunCommand(
"m.measure", flags="g", coordinates=coordinates, read=True
).strip()
result = parse_key_val(result)
if "units" not in result:
self._giface.WriteWarning(_("Units not recognized, measurement failed."))
unit = ""
else:
unit = result["units"].split(",")[1]
if "area" not in result:
text = _("Area: {area} {unit}\n").format(area=0, unit=unit)
else:
text = _("Area: {area} {unit}\n").format(area=result["area"], unit=unit)
self._giface.WriteLog(text, notification=Notification.MAKE_VISIBLE)
|