12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920 |
- """
- MODULE: mapdisp.py
- CLASSES:
- - Command
- - MapWindow
- - BufferedWindow
- - MapFrame
- - MapApp
- PURPOSE: GIS map display canvas, with toolbar for various display
- management functions, and second toolbar for vector
- digitizing. Can be used either from GIS Manager or as p.mon backend
- Usage:
- python mapdisp.py monitor-identifier /path/to/command/file
- AUTHORS: Michael Barton
- Jachym Cepicky
- Martin Landa <landa.martin gmail.com>
- COPYRIGHT: (C) 2006-2008 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.
- """
- import os
- import sys
- import time
- import glob
- import math
- import tempfile
- import wx
- import wx.aui
- from threading import Thread
- import globalvar
- try:
- import subprocess
- except:
- CompatPath = os.path.join(globalvar.ETCWXDIR)
- sys.path.append(CompatPath)
- from compat import subprocess
- gmpath = os.path.join(globalvar.ETCWXDIR, "icons")
- sys.path.append(gmpath)
- import render
- import toolbars
- import grassenv
- import track
- import menuform
- import gselect
- import disp_print
- import gcmd
- import dbm
- import histogram
- import profile
- import globalvar
- import utils
- import gdialogs
- from vdigit import VDigitCategoryDialog as VDigitCategoryDialog
- from vdigit import VDigitZBulkDialog as VDigitZBulkDialog
- from vdigit import VDigitDuplicatesDialog as VDigitDuplicatesDialog
- from vdigit import GV_LINES as VDigit_Lines_Type
- from debug import Debug as Debug
- from icon import Icons as Icons
- from preferences import globalSettings as UserSettings
- import images
- imagepath = images.__path__[0]
- sys.path.append(imagepath)
- ###
- ### global variables
- ###
- # for standalone app
- cmdfilename = None
- class Command(Thread):
- """
- Creates thread which will observe the command file and see, if
- there is new command to be executed
- """
- def __init__ (self, parent, Map):
- Thread.__init__(self)
- global cmdfilename
- self.parent = parent
- self.map = Map
- self.cmdfile = open(cmdfilename, "r")
- def run(self):
- """
- Run this in thread
- """
- dispcmd = []
- while 1:
- self.parent.redraw = False
- line = self.cmdfile.readline().strip()
- if line == "quit":
- break
- if line:
- try:
- Debug.msg (3, "Command.run(): cmd=%s" % (line))
- self.map.AddLayer(item=None, type="raster",
- name='',
- command=line,
- l_opacity=1)
- self.parent.redraw =True
- except Exception, e:
- print "Command Thread: ",e
- time.sleep(0.1)
- sys.exit()
- class MapWindow(object):
- """Abstract map window class
- Parent for BufferedWindow class (2D display mode) and
- GLWindow (3D display mode)
- """
- def __init__(self, parent, id=wx.ID_ANY,
- pos=wx.DefaultPosition,
- size=wx.DefaultSize,
- style=wx.NO_FULL_REPAINT_ON_RESIZE,
- Map=None, tree=None, gismgr=None):
- self.parent = parent # MapFrame
-
- def EraseMap(self):
- """
- Erase the canvas (virtual method)
- """
- pass
- def UpdateMap(self):
- """
- Updates the canvas anytime there is a change to the
- underlaying images or to the geometry of the canvas.
- """
- pass
- def OnLeftDown(self, event):
- pass
- def OnLeftUp(self, event):
- pass
- def OnMouseMotion(self, event):
- pass
- def OnZoomToMap(self, event):
- pass
- def OnZoomToRaster(self, event):
- pass
- def GetSelectedLayer(self, type='layer'):
- """Get selected layer from layer tree
- @param type 'item' / 'layer' / 'nviz'
- @return layer / map layer properties / nviz properties
- @return None on failure
- """
- # get currently selected map layer
- if not self.tree or not self.tree.GetSelection():
- return None
-
- item = self.tree.GetSelection()
- if not item.IsChecked():
- return None
- if type == 'item':
- return item
-
- try:
- if type == 'nviz':
- layer = self.tree.GetPyData(item)[0]['nviz']
- else:
- layer = self.tree.GetPyData(item)[0]['maplayer']
- except:
- layer = None
-
- return layer
- class BufferedWindow(MapWindow, wx.Window):
- """
- A Buffered window class.
- When the drawing needs to change, you app needs to call the
- UpdateMap() method. Since the drawing is stored in a bitmap, you
- can also save the drawing to file by calling the
- SaveToFile(self,file_name,file_type) method.
- """
- def __init__(self, parent, id,
- pos = wx.DefaultPosition,
- size = wx.DefaultSize,
- style=wx.NO_FULL_REPAINT_ON_RESIZE,
- Map=None, tree=None, gismgr=None):
- MapWindow.__init__(self, parent, id, pos, size, style,
- Map, tree, gismgr)
- wx.Window.__init__(self, parent, id, pos, size, style)
- self.Map = Map
- self.tree = tree
- self.gismanager = gismgr
- #
- # Flags
- #
- self.resize = False # indicates whether or not a resize event has taken place
- self.dragimg = None # initialize variable for map panning
- #
- # Variable for drawing on DC
- #
- self.pen = None # pen for drawing zoom boxes, etc.
- self.polypen = None # pen for drawing polylines (measurements, profiles, etc)
- # List of wx.Point tuples defining a polyline (geographical coordinates)
- self.polycoords = []
- # ID of rubber band line
- self.lineid = None
- # ID of poly line resulting from cumulative rubber band lines (e.g. measurement)
- self.plineid = None
- #
- # Event bindings
- #
- self.Bind(wx.EVT_PAINT, self.OnPaint)
- self.Bind(wx.EVT_SIZE, self.OnSize)
- self.Bind(wx.EVT_IDLE, self.OnIdle)
- self.Bind(wx.EVT_MOTION, self.MouseActions)
- self.Bind(wx.EVT_MOUSE_EVENTS, self.MouseActions)
- self.processMouse = True
-
- #
- # Render output objects
- #
- self.mapfile = None # image file to be rendered
- self.img = "" # wx.Image object (self.mapfile)
- # used in digitization tool (do not redraw vector map)
- self.imgVectorMap = None
- # decoration overlays
- self.overlays = {}
- # images and their PseudoDC ID's for painting and dragging
- self.imagedict = {}
- self.select = {} # selecting/unselecting decorations for dragging
- self.textdict = {} # text, font, and color indexed by id
- self.currtxtid = None # PseudoDC id for currently selected text
- #
- # Zoom objects
- #
- self.zoomhistory = [] # list of past zoom extents
- self.currzoom = 0 # current set of extents in zoom history being used
- #
- # mouse attributes like currently pressed buttons, position on
- # the screen, begin and end of dragging, and type of drawing
- #
- self.mouse = {
- 'l' : False,
- 'r' : False,
- 'm' : False,
- 'begin': [0, 0], # screen coordinates
- 'end' : [0, 0],
- 'use' : "pointer",
- 'box' : "point"
- }
- self.zoomtype = 1 # 1 zoom in, 0 no zoom, -1 zoom out
- self.hitradius = 10 # distance for selecting map decorations
- self.dialogOffset = 5 # offset for dialog (e.g. DisplayAttributesDialog)
- # OnSize called to make sure the buffer is initialized.
- # This might result in OnSize getting called twice on some
- # platforms at initialization, but little harm done.
- ### self.OnSize(None)
- # create PseudoDC used for background map, map decorations like scales and legends
- self.pdc = wx.PseudoDC()
- # used for digitization tool
- self.pdcVector = None
- # pseudoDC for temporal objects (select box, measurement tool, etc.)
- self.pdcTmp = wx.PseudoDC()
- # redraw all pdc's, pdcTmp layer is redrawn always (speed issue)
- self.redrawAll = True
- # will store an off screen empty bitmap for saving to file
- self._buffer = ''
- self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None)
- # vars for handling mouse clicks
- self.dragid = -1
- self.lastpos = (0, 0)
- def Draw(self, pdc, img=None, drawid=None, pdctype='image', coords=[0, 0, 0, 0]):
- """
- Draws map and overlay decorations
- """
- if drawid == None:
- if pdctype == 'image' and img:
- drawid = self.imagedict[img]
- elif pdctype == 'clear':
- drawid == None
- else:
- drawid = wx.NewId()
-
- if img and pdctype == 'image':
- # self.imagedict[img]['coords'] = coords
- self.select[self.imagedict[img]['id']] = False # ?
- pdc.BeginDrawing()
- if drawid != 99:
- bg = wx.TRANSPARENT_BRUSH
- else:
- bg = wx.Brush(self.GetBackgroundColour())
- pdc.SetBackground(bg)
- #pdc.Clear()
- self.Refresh()
- Debug.msg (5, "BufferedWindow.Draw(): id=%s, pdctype=%s, coord=%s" % \
- (drawid, pdctype, coords))
- # set PseudoDC id
- if drawid is not None:
- pdc.SetId(drawid)
-
- if pdctype == 'clear': # erase the display
- bg = wx.WHITE_BRUSH
- # bg = wx.Brush(self.GetBackgroundColour())
- pdc.SetBackground(bg)
- pdc.RemoveAll()
- pdc.Clear()
- self.Refresh()
- pdc.EndDrawing()
- return
- if pdctype == 'image': # draw selected image
- bitmap = wx.BitmapFromImage(img)
- w,h = bitmap.GetSize()
- pdc.DrawBitmap(bitmap, coords[0], coords[1], True) # draw the composite map
- pdc.SetIdBounds(drawid, (coords[0],coords[1], w, h))
- elif pdctype == 'box': # draw a box on top of the map
- if self.pen:
- pdc.SetBrush(wx.Brush(wx.CYAN, wx.TRANSPARENT))
- pdc.SetPen(self.pen)
- x2 = max(coords[0],coords[2])
- x1 = min(coords[0],coords[2])
- y2 = max(coords[1],coords[3])
- y1 = min(coords[1],coords[3])
- rwidth = x2-x1
- rheight = y2-y1
- rect = wx.Rect(x1,y1,rwidth,rheight)
- pdc.DrawRectangleRect(rect)
- pdc.SetIdBounds(drawid,rect)
- # self.ovlcoords[drawid] = coords
- elif pdctype == 'line': # draw a line on top of the map
- if self.pen:
- pdc.SetBrush(wx.Brush(wx.CYAN, wx.TRANSPARENT))
- pdc.SetPen(self.pen)
- pdc.DrawLine(coords[0], coords[1], coords[2], coords[3])
- pdc.SetIdBounds(drawid,(coords[0], coords[1], coords[2], coords[3]))
- # self.ovlcoords[drawid] = coords
- elif pdctype == 'polyline': # draw a polyline on top of the map
- if self.polypen:
- pdc.SetBrush(wx.Brush(wx.CYAN, wx.TRANSPARENT))
- pdc.SetPen(self.polypen)
- pdc.DrawLines(coords)
- # get bounding rectangle for polyline
- xlist = []
- ylist = []
- if len(coords) > 0:
- for point in coords:
- x,y = point
- xlist.append(x)
- ylist.append(y)
- x1=min(xlist)
- x2=max(xlist)
- y1=min(ylist)
- y2=max(ylist)
- pdc.SetIdBounds(drawid,(x1,y1,x2,y2))
- # self.ovlcoords[drawid] = [x1,y1,x2,y2]
- elif pdctype == 'point': # draw point
- if self.pen:
- pdc.SetPen(self.pen)
- pdc.DrawPoint(coords[0], coords[1])
- coordsBound = (coords[0] - 5,
- coords[1] - 5,
- coords[0] + 5,
- coords[1] + 5)
- pdc.SetIdBounds(drawid, coordsBound)
- # self.ovlcoords[drawid] = coords
- elif pdctype == 'text': # draw text on top of map
- text = img[0]
- rotation = float(img[3])
- w, h = self.GetFullTextExtent(img[0])[0:2]
- pdc.SetFont(img[1])
- pdc.SetTextForeground(img[2])
- coords, w, h = self.TextBounds(img, coords)
- if rotation == 0:
- pdc.DrawText(img[0], coords[0], coords[1])
- else:
- pdc.DrawRotatedText(img[0], coords[0], coords[1], rotation)
- pdc.SetIdBounds(drawid, (coords[0], coords[1], w, h))
- # self.ovlcoords[drawid] = coords
- pdc.EndDrawing()
- self.Refresh()
- return drawid
- def TextBounds(self, textinfo, coords):
- """
- Return text boundary data
- @param textinfo text metadata (text, font, color, rotation)
- @param coords reference point
- """
- rotation = float(textinfo[3])
- Debug.msg (4, "BufferedWindow.TextBounds(): text=%s, rotation=%f" % \
- (textinfo[0], rotation))
- self.Update()
- self.Refresh()
- self.SetFont(textinfo[1])
- w, h = self.GetTextExtent(textinfo[0])
- if rotation == 0:
- coords[2], coords[3] = coords[0] + w, coords[1] + h
- return coords, w, h
- boxh = math.fabs(math.sin(math.radians(rotation)) * w) + h
- boxw = math.fabs(math.cos(math.radians(rotation)) * w) + h
- coords[2] = coords[0] + boxw
- coords[3] = coords[1] + boxh
-
- return coords, boxw, boxh
- def OnPaint(self, event):
- """
- Draw PseudoDC's to buffered paint DC
- self.pdc for background and decorations
- self.pdcVector for vector map which is edited
- self.pdcTmp for temporaly drawn objects (self.polycoords)
- If self.redrawAll is False on self.pdcTmp content is re-drawn
- """
- Debug.msg(4, "BufferedWindow.OnPaint(): redrawAll=%s" % self.redrawAll)
- dc = wx.BufferedPaintDC(self, self.buffer)
- # we need to clear the dc BEFORE calling PrepareDC
- #bg = wx.Brush(self.GetBackgroundColour())
- dc.SetBackground(wx.Brush("White"))
- dc.Clear()
- # use PrepareDC to set position correctly
- self.PrepareDC(dc)
- # create a clipping rect from our position and size
- # and update region
- rgn = self.GetUpdateRegion().GetBox()
- dc.SetClippingRect(rgn)
- if self.redrawAll: # redraw pdc and pdcVector
- # draw to the dc using the calculated clipping rect
- self.pdc.DrawToDCClipped(dc, rgn)
- # draw vector map layer
- if self.pdcVector:
- self.pdcVector.DrawToDCClipped(dc, rgn)
- self.bufferLast = None
- else: # do not redraw pdc and pdcVector
- if self.bufferLast is None:
- # draw to the dc
- self.pdc.DrawToDC(dc)
- if self.pdcVector:
- self.pdcVector.DrawToDC(dc)
- # store buffered image
- # self.bufferLast = wx.BitmapFromImage(self.buffer.ConvertToImage())
- self.bufferLast = dc.GetAsBitmap(wx.Rect(0, 0, self.Map.width, self.Map.height))
- pdcLast = wx.PseudoDC()
- pdcLast.DrawBitmap(bmp=self.bufferLast, x=0, y=0)
- pdcLast.DrawToDC(dc)
- # draw temporary object on the foreground
- # self.pdcTmp.DrawToDCClipped(dc, rgn)
- self.pdcTmp.DrawToDC(dc)
- def OnSize(self, event):
- """
- Scale map image so that it is
- the same size as the Window
- """
- Debug.msg(3, "BufferedWindow.OnSize():")
- # set size of the input image
- self.Map.ChangeMapSize(self.GetClientSize())
- # align extent based on center point and display resolution
- # this causes that image is not resized when display windows is resized
- # self.Map.AlignExtentFromDisplay()
- # Make new off screen bitmap: this bitmap will always have the
- # current drawing in it, so it can be used to save the image to
- # a file, or whatever.
- self.buffer = wx.EmptyBitmap(max(1, self.Map.width), max(1, self.Map.height))
- # get the image to be rendered
- self.img = self.GetImage()
- # update map display
- if self.img and self.Map.width + self.Map.height > 0: # scale image during resize
- self.img = self.img.Scale(self.Map.width, self.Map.height)
- if len(self.Map.GetListOfLayers()) > 0:
- self.UpdateMap()
- # re-render image on idle
- self.resize = True
- # reposition checkbox in statusbar
- self.parent.StatusbarReposition()
- # update statusbar
- self.parent.StatusbarUpdate()
- def OnIdle(self, event):
- """
- Only re-render a composite map image from GRASS during
- idle time instead of multiple times during resizing.
- """
- if self.resize:
- self.UpdateMap(render=True)
- event.Skip()
- def SaveToFile(self, FileName, FileType):
- """
- This draws the psuedo DC to a buffer that
- can be saved to a file.
- """
- dc = wx.BufferedPaintDC(self, self.buffer)
- self.pdc.DrawToDC(dc)
- if self.pdcVector:
- self.pdcVector.DrawToDC(dc)
- self.buffer.SaveFile(FileName, FileType)
- def GetOverlay(self):
- """
- Converts rendered overlay files to wx.Image
- Updates self.imagedict
- @return list of images
- """
- imgs = []
- for overlay in self.Map.GetListOfLayers(l_type="overlay", l_active=True):
- if os.path.isfile(overlay.mapfile) and os.path.getsize(overlay.mapfile):
- img = wx.Image(overlay.mapfile, wx.BITMAP_TYPE_ANY)
- self.imagedict[img] = { 'id' : overlay.id,
- 'layer' : overlay }
- imgs.append(img)
- return imgs
- def GetImage(self):
- """
- Converts redered map files to wx.Image
- Updates self.imagedict (id=99)
- @return wx.Image instance (map composition)
- """
- imgId = 99
- if self.Map.mapfile and os.path.isfile(self.Map.mapfile) and \
- os.path.getsize(self.Map.mapfile):
- img = wx.Image(self.Map.mapfile, wx.BITMAP_TYPE_ANY)
- else:
- img = None
- self.imagedict[img] = { 'id': imgId }
- return img
- def UpdateMap(self, render=True, renderVector=True):
- """
- Updates the canvas anytime there is a change to the
- underlaying images or to the geometry of the canvas.
- @param render re-render map composition
- @param renderVector re-render vector map layer enabled for editing (used for digitizer)
- """
- start = time.clock()
- self.resize = False
- # if len(self.Map.GetListOfLayers()) == 0:
- # return False
-
- if self.img is None:
- render = True
- #
- # initialize process bar (only on 'render')
- #
- if render is True or renderVector is True:
- self.parent.onRenderGauge.Show()
- if self.parent.onRenderGauge.GetRange() > 0:
- self.parent.onRenderGauge.SetValue(1)
-
- #
- # render background image if needed
- #
-
- # update layer dictionary if there has been a change in layers
- if self.tree.reorder == True:
- self.tree.ReorderLayers()
-
- # reset flag for auto-rendering
- self.tree.rerender = False
-
- if render:
- # update display size
- self.Map.ChangeMapSize(self.GetClientSize())
- windres = False
- if self.parent.compResolution.GetValue():
- # use computation region resolution for rendering
- windres = True
- self.mapfile = self.Map.Render(force=True, mapWindow=self.parent,
- windres=windres)
- else:
- self.mapfile = self.Map.Render(force=False, mapWindow=self.parent)
-
- self.img = self.GetImage() # id=99
-
- #
- # clear pseudoDcs
- #
- self.pdc.Clear()
- self.pdc.RemoveAll()
- self.pdcTmp.Clear()
- self.pdcTmp.RemoveAll()
- #
- # draw background map image to PseudoDC
- #
- if not self.img:
- self.Draw(self.pdc, pdctype='clear')
- else:
- try:
- id = self.imagedict[self.img]['id']
- except:
- return False
- self.Draw(self.pdc, self.img, drawid=id)
- #
- # render vector map layer
- #
- digitToolbar = self.parent.toolbars['vdigit']
- if renderVector and digitToolbar and \
- digitToolbar.GetLayer():
- # set region
- self.parent.digit.driver.UpdateRegion()
- # re-calculate threshold for digitization tool
- self.parent.digit.driver.GetThreshold()
- # draw map
- self.pdcVector.Clear()
- self.pdcVector.RemoveAll()
- try:
- item = self.tree.FindItemByData('maplayer', digitToolbar.GetLayer())
- except TypeError:
- item = None
- if item and self.tree.IsItemChecked(item):
- self.parent.digit.driver.DrawMap()
- #
- # render overlays
- #
- for img in self.GetOverlay():
- # draw any active and defined overlays
- if self.imagedict[img]['layer'].IsActive():
- id = self.imagedict[img]['id']
- self.Draw(self.pdc, img=img, drawid=id,
- pdctype=self.overlays[id]['pdcType'], coords=self.overlays[id]['coords'])
- for id in self.textdict.keys():
- self.Draw(self.pdc, img=self.textdict[id], drawid=id,
- pdctype='text', coords=[10, 10, 10, 10])
- # optionally draw computational extent box
- self.DrawCompRegionExtent()
- #
- # redraw pdcTmp if needed
- #
- if len(self.polycoords) > 0:
- self.DrawLines(self.pdcTmp)
- if self.parent.gismanager.georectifying:
- # -> georectifier (redraw GCPs)
- if self.parent.toolbars['georect']:
- coordtype = 'gcpcoord'
- else:
- coordtype = 'mapcoord'
- self.parent.gismanager.georectifying.DrawGCP(coordtype)
-
- stop = time.clock()
- #
- # hide process bar
- #
- self.parent.onRenderGauge.Hide()
- #
- # update statusbar
- #
- ### self.Map.SetRegion()
- self.parent.StatusbarUpdate()
- Debug.msg (2, "BufferedWindow.UpdateMap(): render=%s, renderVector=%s -> time=%g" % \
- (render, renderVector, (stop-start)))
- return True
- def DrawCompRegionExtent(self):
- """Draw computational region extent in the display
-
- Display region is drawn as a blue box inside the computational region,
- computational region inside a display region as a red box).
- """
- if hasattr(self, "regionCoords"):
- compReg = self.Map.GetRegion()
- dispReg = self.Map.GetCurrentRegion()
- reg = None
- if self.IsInRegion(dispReg, compReg):
- self.polypen = wx.Pen(colour='blue', width=3, style=wx.SOLID)
- reg = dispReg
- else:
- self.polypen = wx.Pen(colour='red', width=3, style=wx.SOLID)
- reg = compReg
-
- self.regionCoords = []
- self.regionCoords.append((reg['w'], reg['n']))
- self.regionCoords.append((reg['e'], reg['n']))
- self.regionCoords.append((reg['e'], reg['s']))
- self.regionCoords.append((reg['w'], reg['s']))
- self.regionCoords.append((reg['w'], reg['n']))
- # draw region extent
- self.DrawLines(pdc=self.pdcTmp, polycoords=self.regionCoords)
- def IsInRegion(self, region, refRegion):
- """Test if 'region' is inside of 'refRegion'
- @param region input region
- @param refRegion reference region (e.g. computational region)
- @return True if region is inside of refRegion
- @return False
- """
- if region['s'] >= refRegion['s'] and \
- region['n'] <= refRegion['n'] and \
- region['w'] >= refRegion['w'] and \
- region['e'] <= refRegion['e']:
- return True
- return False
- def EraseMap(self):
- """
- Erase the canvas
- """
- self.Draw(self.pdc, pdctype='clear')
-
- if self.pdcVector:
- self.Draw(self.pdcVector, pdctype='clear')
-
- self.Draw(self.pdcVector, pdctype='clear')
-
- def DragMap(self, moveto):
- """
- Drag the entire map image for panning.
- """
- dc = wx.BufferedDC(wx.ClientDC(self))
- dc.SetBackground(wx.Brush("White"))
- dc.Clear()
- self.dragimg = wx.DragImage(self.buffer)
- self.dragimg.BeginDrag((0, 0), self)
- self.dragimg.GetImageRect(moveto)
- self.dragimg.Move(moveto)
- self.dragimg.DoDrawImage(dc, moveto)
- self.dragimg.EndDrag()
- return True
- def DragItem(self, id, event):
- """
- Drag an overlay decoration item
- """
- Debug.msg (5, "BufferedWindow.DragItem(): id=%d" % \
- id)
- x, y = self.lastpos
- dx = event.GetX() - x
- dy = event.GetY() - y
- self.pdc.SetBackground(wx.Brush(self.GetBackgroundColour()))
- r = self.pdc.GetIdBounds(id)
- if self.dragid > 100: # text dragging
- rtop = (r[0],r[1]-r[3],r[2],r[3])
- r = r.Union(rtop)
- rleft = (r[0]-r[2],r[1],r[2],r[3])
- r = r.Union(rleft)
- self.pdc.TranslateId(id, dx, dy)
- r2 = self.pdc.GetIdBounds(id)
- r = r.Union(r2)
- r.Inflate(4,4)
- self.Update()
- self.RefreshRect(r, False)
- self.lastpos = (event.GetX(), event.GetY())
- def MouseDraw(self, pdc=None, begin=None, end=None):
- """
- Mouse box or line from 'begin' to 'end'
- If not given from self.mouse['begin'] to self.mouse['end'].
- """
- self.redrawAll = False
-
- if not pdc:
- return
- if begin is None:
- begin = self.mouse['begin']
- if end is None:
- end = self.mouse['end']
- Debug.msg (5, "BufferedWindow.MouseDraw(): use=%s, box=%s, begin=%f,%f, end=%f,%f" % \
- (self.mouse['use'], self.mouse['box'],
- begin[0], begin[1], end[0], end[1]))
- if self.mouse['box'] == "box":
- boxid = wx.ID_NEW
- mousecoords = [begin[0], begin[1],
- end[0], end[1]]
- r = pdc.GetIdBounds(boxid)
- r.Inflate(4,4)
- pdc.ClearId(boxid)
- self.RefreshRect(r, False)
- pdc.SetId(boxid)
- self.Draw(pdc, drawid=boxid, pdctype='box', coords=mousecoords)
- elif self.mouse['box'] == "line":
- self.lineid = wx.ID_NEW
- mousecoords = [begin[0], begin[1], \
- end[0], end[1]]
- x1=min(begin[0],end[0])
- x2=max(begin[0],end[0])
- y1=min(begin[1],end[1])
- y2=max(begin[1],end[1])
- r = wx.Rect(x1,y1,x2-x1,y2-y1)
- r.Inflate(4,4)
- try:
- pdc.ClearId(self.lineid)
- except:
- pass
- self.RefreshRect(r, False)
- pdc.SetId(self.lineid)
- self.Draw(pdc, drawid=self.lineid, pdctype='line', coords=mousecoords)
- def DrawLines(self, pdc=None, polycoords=None):
- """Draw polyline in PseudoDC
- Set self.pline to wx.NEW_ID + 1
- polycoords - list of polyline vertices, geographical coordinates
- (if not given, self.polycoords is used)
- """
- if not pdc:
- pdc = self.pdcTmp
- if not polycoords:
- polycoords = self.polycoords
- if len(polycoords) > 0:
- self.plineid = wx.ID_NEW + 1
- # convert from EN to XY
- coords = []
- for p in polycoords:
- coords.append(self.Cell2Pixel(p))
- self.Draw(pdc, drawid=self.plineid, pdctype='polyline', coords=coords)
- Debug.msg (4, "BufferedWindow.DrawLines(): coords=%s, id=%s" % \
- (coords, self.plineid))
- return self.plineid
- return -1
- def DrawCross(self, pdc, coords, size, rotation=0,
- text=None, textAlign='lr', textOffset=(5, 5)):
- """Draw cross in PseudoDC
- @todo implement rotation
- @param pdc PseudoDC
- @param coord center coordinates
- @param rotation rotate symbol
- @param text draw also text (text, font, color, rotation)
- @param textAlign alignment (default 'lower-right')
- @textOffset offset for text (from center point)
- """
- Debug.msg(4, "BufferedWindow.DrawCross(): pdc=%s, coords=%s, size=%d" % \
- (pdc, coords, size))
- coordsCross = ((coords[0] - size, coords[1], coords[0] + size, coords[1]),
- (coords[0], coords[1] - size, coords[0], coords[1] + size))
- self.lineid = wx.NewId()
- for lineCoords in coordsCross:
- self.Draw(pdc, drawid=self.lineid, pdctype='line', coords=lineCoords)
- if not text:
- return self.lineid
- if textAlign == 'ul':
- coord = [coords[0] - textOffset[0], coords[1] - textOffset[1], 0, 0]
- elif textAlign == 'ur':
- coord = [coords[0] + textOffset[0], coords[1] - textOffset[1], 0, 0]
- elif textAlign == 'lr':
- coord = [coords[0] + textOffset[0], coords[1] + textOffset[1], 0, 0]
- else:
- coord = [coords[0] - textOffset[0], coords[1] + textOffset[1], 0, 0]
-
- self.Draw(pdc, img=text,
- pdctype='text', coords=coord)
- return self.lineid
- def MouseActions(self, event):
- """
- Mouse motion and button click notifier
- """
- if not self.processMouse:
- return
-
- if self.redrawAll is False:
- self.redrawAll = True
-
- wheel = event.GetWheelRotation()
- # zoom with mouse wheel
- if wheel != 0:
- self.processMouse = False
- current = event.GetPositionTuple()[:]
- Debug.msg (5, "BufferedWindow.MouseAction(): wheel=%d" % wheel)
- # zoom 1/2 of the screen, centered to current mouse position (TODO: settings)
- begin = (current[0] - self.Map.width / 4,
- current[1] - self.Map.height / 4)
- end = (current[0] + self.Map.width / 4,
- current[1] + self.Map.height / 4)
- if wheel > 0:
- zoomtype = 1
- else:
- zoomtype = -1
- # zoom
- self.Zoom(begin, end, zoomtype)
- # redraw map
- self.UpdateMap()
- self.OnPaint(None)
-
- # update statusbar
- self.parent.StatusbarUpdate()
- self.processMouse = True
-
- # left mouse button pressed
- elif event.LeftDown():
- self.OnLeftDown(event)
- # left mouse button released
- elif event.LeftUp():
- self.OnLeftUp(event)
- # dragging
- elif event.Dragging():
- Debug.msg (5, "BufferedWindow.MouseAction(): Dragging")
- current = event.GetPositionTuple()[:]
- previous = self.mouse['begin']
- move = (current[0] - previous[0],
- current[1] - previous[1])
- # dragging or drawing box with left button
- if self.mouse['use'] == 'pan':
- self.DragMap(move)
- # dragging decoration overlay item
- elif (self.mouse['use'] == 'pointer' and not self.parent.toolbars['vdigit']) and \
- self.dragid != None and \
- self.dragid != 99:
- self.DragItem(self.dragid, event)
- # dragging anything else - rubber band box or line
- else:
- self.mouse['end'] = event.GetPositionTuple()[:]
- if event.LeftIsDown():
- # draw box only when left mouse button is pressed
- self.MouseDraw(pdc=self.pdcTmp)
- # double click
- elif event.ButtonDClick():
- self.OnButtonDClick(event)
- # middle mouse button pressed
- elif event.MiddleDown():
- self.OnMiddleDown(event)
- # right mouse button pressed
- elif event.RightDown():
- self.OnRightDown(event)
- # right mouse button released
- elif event.RightUp():
- self.OnRightUp(event)
- elif event.Moving():
- self.OnMouseMoving(event)
- event.Skip()
- def OnLeftDown(self, event):
- """
- Left mouse button pressed
- """
- Debug.msg (5, "BufferedWindow.OnLeftDown(): use=%s" % \
- self.mouse["use"])
- self.mouse['begin'] = event.GetPositionTuple()[:]
- if self.mouse["use"] in ["measure", "profile"]:
- # measure or profile
- if len(self.polycoords) == 0:
- self.mouse['end'] = self.mouse['begin']
- self.polycoords.append(self.Pixel2Cell(self.mouse['begin']))
- self.ClearLines(pdc=self.pdcTmp)
- else:
- self.mouse['begin'] = self.mouse['end']
- elif self.mouse['use'] == 'zoom':
- pass
- elif self.mouse["use"] == "pointer" and self.parent.toolbars['vdigit']:
- # digitization
- digitToolbar = self.parent.toolbars['vdigit']
- digitClass = self.parent.digit
- east, north = self.Pixel2Cell(self.mouse['begin'])
- try:
- map = digitToolbar.GetLayer().GetName()
- except:
- map = None
- wx.MessageBox(parent=self,
- message=_("No vector map selected for editing."),
- caption=_("Error"), style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
- event.Skip()
- return
- # calculate position of 'update record' dialog
- position = self.mouse['begin']
- posWindow = self.ClientToScreen((position[0] + self.dialogOffset,
- position[1] + self.dialogOffset))
- if digitToolbar.GetAction() not in ("moveVertex", "addVertex",
- "removeVertex", "editLine"):
- # set pen
- self.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
- self.polypen = wx.Pen(colour='dark green', width=2, style=wx.SOLID)
- if digitToolbar.GetAction() in ("addVertex", "removeVertex"):
- # unselect
- digitClass.driver.SetSelected([])
- if digitToolbar.GetAction() == "addLine":
- if digitToolbar.GetAction('type') in ["point", "centroid"]:
- # add new point
- if digitToolbar.GetAction('type') == 'point':
- point = True
- else:
- point = False
- if digitClass.AddPoint(map, point, east, north) < 0:
- return
- self.UpdateMap(render=False) # redraw map
- # add new record into atribute table
- if UserSettings.Get(group='vdigit', key="addRecord", subkey='enabled') is True:
- # select attributes based on layer and category
- cats = {}
- cats[UserSettings.Get(group='vdigit', key="layer", subkey='value')] = \
- (UserSettings.Get(group='vdigit', key="category", subkey='value'), )
- addRecordDlg = dbm.DisplayAttributesDialog(parent=self, map=map,
- cats=cats,
- pos=posWindow,
- action="add")
- if addRecordDlg.mapDBInfo and \
- addRecordDlg.ShowModal() == wx.ID_OK:
- sqlfile = tempfile.NamedTemporaryFile(mode="w")
- for sql in addRecordDlg.GetSQLString():
- sqlfile.file.write(sql + ";\n")
- sqlfile.file.flush()
- executeCommand = gcmd.Command(cmd=["db.execute",
- "--q",
- "input=%s" % sqlfile.name])
- elif digitToolbar.GetAction('type') in ["line", "boundary"]:
- # add new point to the line
- self.polycoords.append(self.Pixel2Cell(event.GetPositionTuple()[:]))
- self.DrawLines(pdc=self.pdcTmp)
- elif digitToolbar.GetAction() == "editLine" and hasattr(self, "moveIds"):
- self.polycoords.append(self.Pixel2Cell(self.mouse['begin']))
- self.moveIds.append(wx.NewId())
- self.DrawLines(pdc=self.pdcTmp)
- elif digitToolbar.GetAction() == "deleteLine":
- pass
- elif digitToolbar.GetAction() in ["moveLine", "moveVertex", "editLine"] and \
- not hasattr(self, "moveBegin"):
- # incremental value
- self.moveBegin = [0, 0]
- # geographic coordinates of initial position (self.mouse['end'])
- self.moveCoords = []
- # list of ids to modify
- self.moveIds = []
- if digitToolbar.GetAction() in ["moveVertex", "editLine"]:
- # set pen
- pcolor = UserSettings.Get(group='vdigit', key="symbol",
- subkey=["highlight", "color"])
- self.pen = self.polypen = wx.Pen(colour=pcolor,
- width=2, style=wx.SHORT_DASH)
- self.pdcTmp.SetPen(self.polypen)
- elif digitToolbar.GetAction() == "splitLine":
- # unselect
- digitClass.driver.SetSelected([])
- elif digitToolbar.GetAction() in ["displayAttrs", "displayCats"]:
- qdist = digitClass.driver.GetThreshold(type='selectThresh')
- coords = (east, north)
- if digitToolbar.GetAction() == "displayAttrs":
- # select attributes based on coordinates (all layers)
- if self.parent.dialogs['attributes'] is None:
- if digitClass.type == 'vedit':
- self.parent.dialogs['attributes'] = dbm.DisplayAttributesDialog(parent=self, map=map,
- query=(coords, qdist),
- pos=posWindow,
- action="update")
- else:
- if digitClass.driver.SelectLineByPoint(coords,
- digitClass.GetSelectType()) is not None:
- self.parent.dialogs['attributes'] = dbm.DisplayAttributesDialog(parent=self, map=map,
- cats=digitClass.GetLineCats(),
- line=digitClass.driver.GetSelected()[0],
- action="update")
- else:
- # update currently open dialog
- if digitClass.type == 'vedit':
- self.parent.dialogs['attributes'].UpdateDialog(query=(coords, qdist))
- else:
- # unselect
- digitClass.driver.SetSelected([])
- # select new feature
- if digitClass.driver.SelectLineByPoint(coords,
- digitClass.GetSelectType()) is None:
- line = None
- else:
- line = digitClass.driver.GetSelected()[0]
- # upgrade dialog
- self.parent.dialogs['attributes'].UpdateDialog(cats=digitClass.GetLineCats(),
- line=line)
- if self.parent.dialogs['attributes']:
- line = self.parent.dialogs['attributes'].GetLine()
- if self.parent.dialogs['attributes'].mapDBInfo and line:
- # highlight feature & re-draw map
- digitClass.driver.SetSelected([line])
- if not self.parent.dialogs['attributes'].IsShown():
- self.parent.dialogs['attributes'].Show()
- else:
- digitClass.driver.SetSelected([])
- if self.parent.dialogs['attributes'] and \
- self.parent.dialogs['attributes'].IsShown():
- self.parent.dialogs['attributes'].Hide()
- else: # displayCats
- if self.parent.dialogs['category'] is None:
- # open new dialog
- if digitClass.type == 'vedit':
- self.parent.dialogs['category'] = VDigitCategoryDialog(parent=self,
- map=map,
- query=(coords, qdist),
- pos=posWindow,
- title=_("Update categories"))
- else:
- if digitClass.driver.SelectLineByPoint(coords,
- digitClass.GetSelectType()) is not None:
- self.parent.dialogs['category'] = VDigitCategoryDialog(parent=self,
- map=map,
- cats=digitClass.GetLineCats(),
- line=digitClass.driver.GetSelected()[0],
- pos=posWindow,
- title=_("Update categories"))
-
- else:
- # update currently open dialog
- if digitClass.type == 'vedit':
- self.parent.dialogs['category'].UpdateDialog(query=(coords, qdist))
- else:
- # unselect
- digitClass.driver.SetSelected([])
- # select new feature
- if digitClass.driver.SelectLineByPoint(coords,
- digitClass.GetSelectType()) is None:
- line = None
- else:
- line = digitClass.driver.GetSelected()[0]
- # upgrade dialog
- self.parent.dialogs['category'].UpdateDialog(cats=digitClass.GetLineCats(),
- line=line)
-
- if self.parent.dialogs['category']:
- line = self.parent.dialogs['category'].GetLine()
- if line:
- # highlight feature & re-draw map
- digitClass.driver.SetSelected([line])
- if not self.parent.dialogs['category'].IsShown():
- self.parent.dialogs['category'].Show()
- else:
- digitClass.driver.SetSelected([])
- if self.parent.dialogs['category'].IsShown():
- self.parent.dialogs['category'].Hide()
- self.UpdateMap(render=False)
- elif digitToolbar.GetAction() in ("copyCats", "copyAttrs"):
- if not hasattr(self, "copyCatsList"):
- self.copyCatsList = []
- else:
- self.copyCatsIds = []
- self.mouse['box'] = 'box'
- elif digitToolbar.GetAction() == "copyLine":
- self.copyIds = []
- self.layerTmp = None
- elif digitToolbar.GetAction() == "zbulkLine":
- if len(self.polycoords) > 1: # start new line
- self.polycoords = []
- self.ClearLines(pdc=self.pdcTmp)
- self.polycoords.append(self.Pixel2Cell(event.GetPositionTuple()[:]))
- if len(self.polycoords) == 1:
- begin = self.Pixel2Cell(self.polycoords[-1])
- end = self.Pixel2Cell(self.mouse['end'])
- else:
- end = self.Pixel2Cell(self.polycoords[-1])
- begin = self.Pixel2Cell(self.mouse['begin'])
- self.DrawLines(self.pdcTmp, begin, end)
- else:
- # get decoration id
- self.lastpos = self.mouse['begin']
- idlist = self.pdc.FindObjects(x=self.lastpos[0], y=self.lastpos[1],
- radius=self.hitradius)
- if idlist != []:
- self.dragid = idlist[0]
- event.Skip()
- def OnLeftUp(self, event):
- """
- Left mouse button released
- """
- Debug.msg (5, "BufferedWindow.OnLeftUp(): use=%s" % \
- self.mouse["use"])
- self.mouse['end'] = event.GetPositionTuple()[:]
- if self.mouse['use'] in ["zoom", "pan"]:
- # set region in zoom or pan
- begin = self.mouse['begin']
- end = self.mouse['end']
- if self.mouse['use'] == 'zoom':
- # set region for click (zero-width box)
- if begin[0] - end[0] == 0 or \
- begin[1] - end[1] == 0:
- # zoom 1/2 of the screen (TODO: settings)
- begin = (end[0] - self.Map.width / 4,
- end[1] - self.Map.height / 4)
- end = (end[0] + self.Map.width / 4,
- end[1] + self.Map.height / 4)
- self.Zoom(begin, end, self.zoomtype)
- # redraw map
- self.UpdateMap(render=True)
- # update statusbar
- self.parent.StatusbarUpdate()
- elif self.mouse["use"] == "query":
- # querying
- self.parent.QueryMap(self.mouse['begin'][0],self.mouse['begin'][1])
- elif self.mouse["use"] == "queryVector":
- # editable mode for vector map layers
- self.parent.QueryVector(self.mouse['begin'][0],self.mouse['begin'][1])
-
- elif self.mouse["use"] in ["measure", "profile"]:
- # measure or profile
- if self.mouse["use"] == "measure":
- self.parent.MeasureDist(self.mouse['begin'], self.mouse['end'])
- try:
- self.polycoords.append(self.Pixel2Cell(self.mouse['end']))
- self.pdcTmp.ClearId(self.lineid)
- self.DrawLines(pdc=self.pdcTmp)
- except:
- pass
- elif self.mouse["use"] == "pointer" and self.parent.gismanager.georectifying:
- # -> georectifying
- coord = self.Pixel2Cell(self.mouse['end'])
- if self.parent.toolbars['georect']:
- coordtype = 'gcpcoord'
- else:
- coordtype = 'mapcoord'
- self.parent.gismanager.georectifying.SetGCPData(coordtype, coord, self)
- self.UpdateMap(render=False, renderVector=False)
- elif self.mouse["use"] == "pointer" and self.parent.toolbars['vdigit']:
- # digitization tool active
- digitToolbar = self.parent.toolbars['vdigit']
- digitClass = self.parent.digit
- pos1 = self.Pixel2Cell(self.mouse['begin'])
- pos2 = self.Pixel2Cell(self.mouse['end'])
- if hasattr(self, "moveBegin"):
- if len(digitClass.driver.GetSelected()) == 0:
- self.moveCoords = pos2
- else:
- dx = pos2[0] - pos1[0]
- dy = pos2[1] - pos1[1]
- self.moveCoords = (self.moveCoords[0] + dx,
- self.moveCoords[1] + dy)
- # eliminate initial mouse moving efect
- self.mouse['begin'] = self.mouse['end']
- if digitToolbar.GetAction() in ["deleteLine", "moveLine", "moveVertex",
- "copyCats", "copyAttrs", "editLine", "flipLine",
- "mergeLine", "snapLine",
- "queryLine", "breakLine", "typeConv", "connectLine"]:
- nselected = 0
- # -> delete line || move line || move vertex
- if digitToolbar.GetAction() in ["moveVertex", "editLine"]:
- if len(digitClass.driver.GetSelected()) == 0:
- nselected = digitClass.driver.SelectLineByPoint(pos1, type=VDigit_Lines_Type)
- if digitToolbar.GetAction() == "editLine":
- try:
- selVertex = digitClass.driver.GetSelectedVertex(pos1)[0]
- except IndexError:
- selVertex = None
- if selVertex:
- # self.UpdateMap(render=False)
- ids = digitClass.driver.GetSelected(grassId=False)
- # move this line to tmp layer
- self.polycoords = []
- for id in ids:
- if id % 2: # register only vertices
- self.moveIds.append(id)
- e, n = self.Pixel2Cell(self.pdcVector.GetIdBounds(id)[0:2])
- self.polycoords.append((e, n))
- # self.pdcVector.RemoveId(id)
- digitClass.driver.DrawSelected(False)
-
- if selVertex < ids[-1] / 2:
- # choose first or last node of line
- self.moveIds.reverse()
- self.polycoords.reverse()
- else:
- # unselect
- digitClass.driver.SetSelected([])
- del self.moveBegin
- del self.moveCoords
- del self.moveIds
- self.UpdateMap(render=False)
- elif digitToolbar.GetAction() in ("copyCats", "copyAttrs"):
- if not hasattr(self, "copyCatsIds"):
- # 'from' -> select by point
- nselected = digitClass.driver.SelectLineByPoint(pos1, type=VDigit_Lines_Type)
- if nselected:
- if UserSettings.Get(group='advanced', key='digitInterface', subkey='type') == 'vedit':
- qdist = 10.0 * ((self.Map.region['e'] - self.Map.region['w']) / \
- self.Map.width)
- vWhat = gcmd.Command(['v.what',
- '--q',
- 'map=%s' % digitClass.map,
- 'east_north=%f,%f' % \
- (float(pos1[0]), float(pos1[1])),
- 'distance=%f' % qdist])
-
- for line in vWhat.ReadStdOutput():
- if "Category:" in line:
- cat = int(line.split(':')[1].strip())
- self.copyCatsList.append(cat)
- else:
- self.copyCatsList = digitClass.driver.GetSelected()
- else:
- # -> 'to' -> select by bbox
- digitClass.driver.SetSelected([])
- # return number of selected features (by box/point)
- nselected = digitClass.driver.SelectLinesByBox(pos1, pos2,
- digitClass.GetSelectType())
- if nselected == 0:
- if digitClass.driver.SelectLineByPoint(pos1,
- digitClass.GetSelectType()) is not None:
- nselected = 1
- if nselected > 0:
- self.copyCatsIds = digitClass.driver.GetSelected()
- elif digitToolbar.GetAction() == "queryLine":
- selected = digitClass.SelectLinesByQuery(pos1, pos2)
- nselected = len(selected)
- if nselected > 0:
- digitClass.driver.SetSelected(selected)
- else:
- # -> moveLine || deleteLine, etc. (select by point/box)
- if digitToolbar.GetAction() == 'moveLine' and \
- len(digitClass.driver.GetSelected()) > 0:
- nselected = 0
- else:
- nselected = digitClass.driver.SelectLinesByBox(pos1, pos2,
- digitClass.GetSelectType())
-
- if nselected == 0:
- if digitClass.driver.SelectLineByPoint(pos1,
- digitClass.GetSelectType()) is not None:
- nselected = 1
-
- if nselected > 0:
- if digitToolbar.GetAction() in ["moveLine", "moveVertex"]:
- # get pseudoDC id of objects which should be redrawn
- if digitToolbar.GetAction() == "moveLine":
- # -> move line
- self.moveIds = digitClass.driver.GetSelected(grassId=False)
- elif digitToolbar.GetAction() == "moveVertex":
- # -> move vertex
- self.moveIds = digitClass.driver.GetSelectedVertex(pos1)
- if len(self.moveIds) == 0: # no vertex found
- digitClass.driver.SetSelected([])
-
- #
- # check for duplicates
- #
- if UserSettings.Get(group='vdigit', key='checkForDupl', subkey='enabled') is True:
- dupl = digitClass.driver.GetDuplicates()
- self.UpdateMap(render=False)
- if dupl:
- posWindow = self.ClientToScreen((self.mouse['end'][0] + self.dialogOffset,
- self.mouse['end'][1] + self.dialogOffset))
-
- dlg = VDigitDuplicatesDialog(parent=self, data=dupl, pos=posWindow)
- if dlg.ShowModal() == wx.ID_OK:
- digitClass.driver.UnSelect(dlg.GetUnSelected())
- # update selected
- self.UpdateMap(render=False)
- if digitToolbar.GetAction() != "editLine":
- # -> move line || move vertex
- self.UpdateMap(render=False)
- else: # no vector object found
- self.UpdateMap(render=False, renderVector=False)
- elif digitToolbar.GetAction() in ["splitLine", "addVertex", "removeVertex"]:
- pointOnLine = digitClass.driver.SelectLineByPoint(pos1,
- type=VDigit_Lines_Type)
- if pointOnLine:
- if digitToolbar.GetAction() in ["splitLine", "addVertex"]:
- self.UpdateMap(render=False) # highlight object
- self.DrawCross(pdc=self.pdcTmp, coords=self.Cell2Pixel(pointOnLine),
- size=5)
- elif digitToolbar.GetAction() == "removeVertex":
- # get only id of vertex
- try:
- id = digitClass.driver.GetSelectedVertex(pos1)[0]
- except IndexError:
- id = None
- if id:
- x, y = self.pdcVector.GetIdBounds(id)[0:2]
- self.pdcVector.RemoveId(id)
- self.UpdateMap(render=False) # highlight object
- self.DrawCross(pdc=self.pdcTmp, coords=(x, y),
- size=5)
- else:
- # unselect
- digitClass.driver.SetSelected([])
- self.UpdateMap(render=False)
-
- elif digitToolbar.GetAction() == "copyLine":
- if UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value') == '':
- # no background map -> copy from current vector map layer
- nselected = digitClass.driver.SelectLinesByBox(pos1, pos2,
- digitClass.GetSelectType())
- if nselected > 0:
- # highlight selected features
- self.UpdateMap(render=False)
- else:
- self.UpdateMap(render=False, renderVector=False)
- else:
- # copy features from background map
- self.copyIds = digitClass.SelectLinesFromBackgroundMap(pos1, pos2)
- if len(self.copyIds) > 0:
- color = UserSettings.Get(group='vdigit', key='symbol',
- subkey=['highlight', 'color'])
- colorStr = str(color[0]) + ":" + \
- str(color[1]) + ":" + \
- str(color[2]) + ":"
- dVectTmp = ['d.vect',
- 'map=%s' % UserSettings.Get(group='vdigit', key='backgroundMap', subkey='value'),
- 'cats=%s' % utils.ListOfCatsToRange(self.copyIds),
- '-i',
- 'color=%s' % colorStr,
- 'fcolor=%s' % colorStr,
- 'type=point,line,boundary,centroid',
- 'width=2']
- self.layerTmp = self.Map.AddLayer(type='vector',
- name=globalvar.QUERYLAYER,
- command=dVectTmp)
- self.UpdateMap(render=True, renderVector=False)
- else:
- self.UpdateMap(render=False, renderVector=False)
- elif digitToolbar.GetAction() == "zbulkLine" and len(self.polycoords) == 2:
- # select lines to be labeled
- pos1 = self.polycoords[0]
- pos2 = self.polycoords[1]
- nselected = digitClass.driver.SelectLinesByBox(pos1, pos2,
- digitClass.GetSelectType())
- if nselected > 0:
- # highlight selected features
- self.UpdateMap(render=False)
- self.DrawLines(pdc=self.pdcTmp) # redraw temp line
- else:
- self.UpdateMap(render=False, renderVector=False)
- elif digitToolbar.GetAction() == "connectLine":
- if len(digitClass.driver.GetSelected()) > 0:
- self.UpdateMap(render=False)
- elif self.dragid != None:
- # end drag of overlay decoration
- if self.overlays.has_key(self.dragid):
- self.overlays[self.dragid]['coords'] = self.pdc.GetIdBounds(self.dragid)
- self.dragid = None
- self.currtxtid = None
- self.Update()
- event.Skip()
- def OnButtonDClick(self, event):
- """
- Mouse button double click
- """
- Debug.msg (5, "BufferedWindow.OnButtonDClick(): use=%s" % \
- self.mouse["use"])
- if self.mouse["use"] == "measure":
- # measure
- self.ClearLines(pdc=self.pdcTmp)
- self.polycoords = []
- self.mouse['use'] = 'pointer'
- self.mouse['box'] = 'point'
- self.mouse['end'] = [0, 0]
- self.Refresh()
- self.SetCursor(self.parent.cursors["default"])
- elif self.mouse["use"] == "profile":
- # profile
- pass
- # self.pdc.ClearId(self.lineid)
- # self.pdc.ClearId(self.plineid)
- # print 'coordinates: ',self.polycoords
- # self.polycoords = []
- # self.mouse['begin'] = self.mouse['end'] = [0, 0]
- # self.Refresh()
- elif self.mouse['use'] == 'pointer' and self.parent.toolbars['vdigit']:
- # digitization tool
- pass
- else:
- # select overlay decoration options dialog
- clickposition = event.GetPositionTuple()[:]
- idlist = self.pdc.FindObjects(clickposition[0], clickposition[1], self.hitradius)
- if idlist == []:
- return
- self.dragid = idlist[0]
- # self.ovlcoords[self.dragid] = self.pdc.GetIdBounds(self.dragid)
- if self.dragid > 100:
- self.currtxtid = self.dragid
- self.parent.OnAddText(None)
- elif self.dragid == 0:
- self.parent.OnAddBarscale(None)
- elif self.dragid == 1:
- self.parent.OnAddLegend(None)
- event.Skip()
- def OnRightDown(self, event):
- """
- Right mouse button pressed
- """
- Debug.msg (5, "BufferedWindow.OnRightDown(): use=%s" % \
- self.mouse["use"])
- x,y = event.GetPositionTuple()[:]
- l = self.pdc.FindObjects(x=x, y=y, radius=self.hitradius)
- if not l:
- return
- id = l[0]
- if id != 99:
- if self.pdc.GetIdGreyedOut(id) == True:
- self.pdc.SetIdGreyedOut(id, False)
- else:
- self.pdc.SetIdGreyedOut(id, True)
- r = self.pdc.GetIdBounds(id)
- r.Inflate(4,4)
- self.RefreshRect(r, False)
- digitToolbar = self.parent.toolbars['vdigit']
- if digitToolbar:
- digitClass = self.parent.digit
- # digitization tool (confirm action)
- if digitToolbar.GetAction() in ["moveLine", "moveVertex"] and \
- hasattr(self, "moveBegin"):
- pTo = self.Pixel2Cell(event.GetPositionTuple())
- pFrom = self.moveCoords
- move = (pTo[0]-pFrom[0], pTo[1]-pFrom[1])
-
- if digitToolbar.GetAction() == "moveLine":
- # move line
- digitClass.MoveSelectedLines(move)
- elif digitToolbar.GetAction() == "moveVertex":
- # move vertex
- digitClass.MoveSelectedVertex(pFrom,
- move)
- del self.moveBegin
- del self.moveCoords
- del self.moveIds
- event.Skip()
- def OnRightUp(self, event):
- """
- Right mouse button released
- """
- Debug.msg (5, "BufferedWindow.OnRightUp(): use=%s" % \
- self.mouse["use"])
- digitToolbar = self.parent.toolbars['vdigit']
- if digitToolbar:
- digitClass = self.parent.digit
- # digitization tool (confirm action)
- if digitToolbar.GetAction() == "addLine" and \
- digitToolbar.GetAction('type') in ["line", "boundary"]:
- # -> add new line / boundary
- try:
- map = digitToolbar.GetLayer().GetName()
- except:
- map = None
- wx.MessageBox(parent=self,
- message=_("No vector map selected for editing."),
- caption=_("Error"), style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
-
- if map:
- # mapcoords = []
- # xy -> EN
- # for coord in self.polycoords:
- # mapcoords.append(self.Pixel2Cell(coord))
- if digitToolbar.GetAction('type') == 'line':
- line = True
- else:
- line = False
- if len(self.polycoords) < 2: # ignore 'one-point' lines
- return
-
- if digitClass.AddLine(map, line, self.polycoords) < 0:
- return
-
- position = self.Cell2Pixel(self.polycoords[-1])
- self.polycoords = []
- self.UpdateMap(render=False)
- # add new record into atribute table
- if UserSettings.Get(group='vdigit', key="addRecord", subkey='enabled') is True:
- posWindow = self.ClientToScreen((position[0] + self.dialogOffset,
- position[1] + self.dialogOffset))
- # select attributes based on layer and category
- cats = {}
- cats[UserSettings.Get(group='vdigit', key="layer", subkey='value')] = \
- (UserSettings.Get(group='vdigit', key="category", subkey='value'), )
- addRecordDlg = dbm.DisplayAttributesDialog(parent=self, map=map,
- cats=cats,
- pos=posWindow,
- action="add")
- if addRecordDlg.mapDBInfo and \
- addRecordDlg.ShowModal() == wx.ID_OK:
- sqlfile = tempfile.NamedTemporaryFile(mode="w")
- for sql in addRecordDlg.GetSQLString():
- sqlfile.file.write(sql + ";\n")
- sqlfile.file.flush()
- executeCommand = gcmd.Command(cmd=["db.execute",
- "--q",
- "input=%s" % sqlfile.name])
- elif digitToolbar.GetAction() == "deleteLine":
- # -> delete selected vector features
- digitClass.DeleteSelectedLines()
- elif digitToolbar.GetAction() == "splitLine":
- # split line
- digitClass.SplitLine(self.Pixel2Cell(self.mouse['begin']))
- elif digitToolbar.GetAction() == "addVertex":
- # add vertex
- digitClass.AddVertex(self.Pixel2Cell(self.mouse['begin']))
- elif digitToolbar.GetAction() == "removeVertex":
- # remove vertex
- digitClass.RemoveVertex(self.Pixel2Cell(self.mouse['begin']))
- elif digitToolbar.GetAction() in ("copyCats", "copyAttrs"):
- try:
- if digitToolbar.GetAction() == 'copyCats':
- digitClass.CopyCats(self.copyCatsList,
- self.copyCatsIds, copyAttrb=False)
- else:
- digitClass.CopyCats(self.copyCatsList,
- self.copyCatsIds, copyAttrb=True)
-
- del self.copyCatsList
- del self.copyCatsIds
- except AttributeError:
- pass
- elif digitToolbar.GetAction() == "editLine" and hasattr(self, "moveBegin"):
- line = digitClass.driver.GetSelected()
- digitClass.EditLine(line, self.polycoords)
- del self.moveBegin
- del self.moveCoords
- del self.moveIds
- elif digitToolbar.GetAction() == "flipLine":
- digitClass.FlipLine()
- elif digitToolbar.GetAction() == "mergeLine":
- digitClass.MergeLine()
- elif digitToolbar.GetAction() == "breakLine":
- digitClass.BreakLine()
- elif digitToolbar.GetAction() == "snapLine":
- digitClass.SnapLine()
- elif digitToolbar.GetAction() == "connectLine":
- if len(digitClass.driver.GetSelected()) > 1:
- digitClass.ConnectLine()
- elif digitToolbar.GetAction() == "copyLine":
- digitClass.CopyLine(self.copyIds)
- del self.copyIds
- if self.layerTmp:
- self.Map.DeleteLayer(self.layerTmp)
- self.UpdateMap(render=True, renderVector=False)
- del self.layerTmp
- elif digitToolbar.GetAction() == "zbulkLine" and len(self.polycoords) == 2:
- pos1 = self.polycoords[0]
- pos2 = self.polycoords[1]
- selected = digitClass.driver.GetSelected()
- dlg = VDigitZBulkDialog(parent=self, title=_("Z bulk-labeling dialog"),
- nselected=len(selected))
- if dlg.ShowModal() == wx.ID_OK:
- digitClass.ZBulkLine(pos1, pos2, dlg.value.GetValue(), dlg.step.GetValue())
- self.UpdateMap(render=False, renderVector=True)
- elif digitToolbar.GetAction() == "typeConv":
- # -> feature type conversion
- # - point <-> centroid
- # - line <-> boundary
- digitClass.TypeConvForSelectedLines()
- if digitToolbar.GetAction() != "addLine":
- # unselect and re-render
- digitClass.driver.SetSelected([])
- self.polycoords = []
- self.UpdateMap(render=False)
- event.Skip()
- def OnMiddleDown(self, event):
- """Middle mouse button pressed"""
- digitToolbar = self.parent.toolbars['vdigit']
- # digitization tool
- if self.mouse["use"] == "pointer" and digitToolbar:
- digitClass = self.parent.digit
- if (digitToolbar.GetAction() == "addLine" and digitToolbar.GetAction('type') in ["line", "boundary"]) or \
- digitToolbar.GetAction() == "editLine":
- # add line or boundary -> remove last point from the line
- try:
- removed = self.polycoords.pop()
- Debug.msg(4, "BufferedWindow.OnMiddleDown(): polycoords_poped=%s" % \
- [removed,])
- self.mouse['begin'] = self.Cell2Pixel(self.polycoords[-1])
- except:
- pass
- if digitToolbar.GetAction() == "editLine":
- # remove last vertex & line
- if len(self.moveIds) > 1:
- self.moveIds.pop()
- self.UpdateMap(render=False, renderVector=False)
- elif digitToolbar.GetAction() in ["deleteLine", "moveLine", "splitLine",
- "addVertex", "removeVertex", "moveVertex",
- "copyCats", "flipLine", "mergeLine",
- "snapLine", "connectLine", "copyLine",
- "queryLine", "breakLine", "typeConv"]:
- # varios tools -> unselected selected features
- digitClass.driver.SetSelected([])
- if digitToolbar.GetAction() in ["moveLine", "moveVertex", "editLine"] and \
- hasattr(self, "moveBegin"):
- del self.moveBegin
- del self.moveCoords
- del self.moveIds
- elif digitToolbar.GetAction() == "copyCats":
- try:
- del self.copyCatsList
- del self.copyCatsIds
- except AttributeError:
- pass
-
- elif digitToolbar.GetAction() == "copyLine":
- del self.copyIds
- if self.layerTmp:
- self.Map.DeleteLayer(self.layerTmp)
- self.UpdateMap(render=True, renderVector=False)
- del self.layerTmp
- self.polycoords = []
- self.UpdateMap(render=False) # render vector
- elif digitToolbar.GetAction() == "zbulkLine":
- # reset polyline
- self.polycoords = []
- digitClass.driver.SetSelected([])
- self.UpdateMap(render=False)
- def OnMouseMoving(self, event):
- """Motion event and no mouse buttons were pressed"""
- digitToolbar = self.parent.toolbars['vdigit']
- if self.mouse["use"] == "pointer" and digitToolbar:
- digitClass = self.parent.digit
- self.mouse['end'] = event.GetPositionTuple()[:]
- Debug.msg (5, "BufferedWindow.OnMouseMoving(): coords=%f,%f" % \
- (self.mouse['end'][0], self.mouse['end'][1]))
- if digitToolbar.GetAction() == "addLine" and digitToolbar.GetAction('type') in ["line", "boundary"]:
- if len(self.polycoords) > 0:
- self.MouseDraw(pdc=self.pdcTmp, begin=self.Cell2Pixel(self.polycoords[-1]))
- elif digitToolbar.GetAction() in ["moveLine", "moveVertex", "editLine"] \
- and hasattr(self, "moveBegin"):
- dx = self.mouse['end'][0] - self.mouse['begin'][0]
- dy = self.mouse['end'][1] - self.mouse['begin'][1]
- self.moveBegin[0] += dx
- self.moveBegin[1] += dy
- if len(self.moveIds) > 0:
- # draw lines on new position
- if digitToolbar.GetAction() == "moveLine":
- # move line
- for id in self.moveIds:
- self.pdcVector.TranslateId(id, dx, dy)
- elif digitToolbar.GetAction() in ["moveVertex", "editLine"]:
- # move vertex ->
- # (vertex, left vertex, left line,
- # right vertex, right line)
- # do not draw static lines
- if digitToolbar.GetAction() == "moveVertex":
- self.polycoords = []
- self.pdcVector.TranslateId(self.moveIds[0], dx, dy)
- if self.moveIds[1] > 0: # previous vertex
- x, y = self.Pixel2Cell(self.pdcVector.GetIdBounds(self.moveIds[1])[0:2])
- self.pdcVector.RemoveId(self.moveIds[1]+1)
- self.polycoords.append((x, y))
- x, y = self.Pixel2Cell(self.pdcVector.GetIdBounds(self.moveIds[0])[0:2])
- self.polycoords.append((x, y))
- if self.moveIds[2] > 0: # next vertex
- x, y = self.Pixel2Cell(self.pdcVector.GetIdBounds(self.moveIds[2])[0:2])
- self.pdcVector.RemoveId(self.moveIds[2]-1)
- self.polycoords.append((x, y))
- self.ClearLines(pdc=self.pdcTmp)
- self.DrawLines(pdc=self.pdcTmp)
- else: # edit line
- try:
- if self.moveIds[-1] > 0: # previous vertex
- self.MouseDraw(pdc=self.pdcTmp,
- begin=self.Cell2Pixel(self.polycoords[-1]))
- except: # no line
- self.moveIds = []
- self.polycoords = []
- self.Refresh() # TODO: use RefreshRect()
- self.mouse['begin'] = self.mouse['end']
- elif digitToolbar.GetAction() == "zbulkLine":
- if len(self.polycoords) == 1:
- # draw mouse moving
- self.MouseDraw(self.pdcTmp)
- event.Skip()
- def ClearLines(self, pdc=None):
- """
- Clears temporary drawn lines from PseudoDC
- """
- if not pdc:
- return
- exit = True
- try:
- pdc.ClearId(self.lineid)
- pdc.RemoveId(self.lineid)
- except:
- exit = False
- try:
- pdc.ClearId(self.plineid)
- pdc.RemoveId(self.plineid)
- except:
- exit = False
- Debug.msg(4, "BufferedWindow.ClearLines(): lineid=%s, plineid=%s" %
- (self.lineid, self.plineid))
- self.Refresh()
- return exit
- def Pixel2Cell(self, (x, y)):
- """
- Convert image coordinates to real word coordinates
- Input : int x, int y
- Output: float x, float y
- """
- try:
- x = int(x)
- y = int(y)
- except:
- return None
- if self.Map.region["ewres"] > self.Map.region["nsres"]:
- res = self.Map.region["ewres"]
- else:
- res = self.Map.region["nsres"]
- w = self.Map.region["center_easting"] - (self.Map.width / 2) * res
- n = self.Map.region["center_northing"] + (self.Map.height / 2) * res
- east = w + x * res
- north = n - y * res
- # extent does not correspond with whole map canvas area...
- # east = self.Map.region['w'] + x * self.Map.region["ewres"]
- # north = self.Map.region['n'] - y * self.Map.region["nsres"]
- return (east, north)
- def Cell2Pixel(self, (east, north)):
- """
- Convert real word coordinates to image coordinates
- """
- try:
- east = float(east)
- north = float(north)
- except:
- return None
- if self.Map.region["ewres"] > self.Map.region["nsres"]:
- res = self.Map.region["ewres"]
- else:
- res = self.Map.region["nsres"]
- w = self.Map.region["center_easting"] - (self.Map.width / 2) * res
- n = self.Map.region["center_northing"] + (self.Map.height / 2) * res
- # x = int((east - w) / res)
- # y = int((n - north) / res)
- x = (east - w) / res
- y = (n - north) / res
- return (x, y)
- def Zoom(self, begin, end, zoomtype):
- """
- Calculates new region while (un)zoom/pan-ing
- """
- x1, y1 = begin
- x2, y2 = end
- newreg = {}
- # threshold - too small squares do not make sense
- # can only zoom to windows of > 5x5 screen pixels
- if abs(x2-x1) > 5 and abs(y2-y1) > 5 and zoomtype != 0:
- if x1 > x2:
- x1, x2 = x2, x1
- if y1 > y2:
- y1, y2 = y2, y1
- # zoom in
- if zoomtype > 0:
- newreg['w'], newreg['n'] = self.Pixel2Cell((x1, y1))
- newreg['e'], newreg['s'] = self.Pixel2Cell((x2, y2))
- # zoom out
- elif zoomtype < 0:
- newreg['w'], newreg['n'] = self.Pixel2Cell((-x1 * 2, -y1 * 2))
- newreg['e'], newreg['s'] = self.Pixel2Cell((self.Map.width + 2 * \
- (self.Map.width - x2),
- self.Map.height + 2 * \
- (self.Map.height - y2)))
- # pan
- elif zoomtype == 0:
- dx = x1 - x2
- dy = y1 - y2
- newreg['w'], newreg['n'] = self.Pixel2Cell((dx, dy))
- newreg['e'], newreg['s'] = self.Pixel2Cell((self.Map.width + dx,
- self.Map.height + dy))
- # if new region has been calculated, set the values
- if newreg != {}:
- # calculate new center point and display resolution
- self.Map.region['center_easting'] = newreg['w'] + \
- (newreg['e'] - newreg['w']) / 2
- self.Map.region['center_northing'] = newreg['s'] + \
- (newreg['n'] - newreg['s']) / 2
- self.Map.region["ewres"] = (newreg['e'] - newreg['w']) / self.Map.width
- self.Map.region["nsres"] = (newreg['n'] - newreg['s']) / self.Map.height
- self.Map.AlignExtentFromDisplay()
- self.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
- self.Map.region['e'], self.Map.region['w'])
- if self.redrawAll is False:
- self.redrawAll = True
- def ZoomBack(self):
- """
- Zoom to previous extents in zoomhistory list
- """
- zoom = []
- if len(self.zoomhistory) > 1:
- self.zoomhistory.pop()
- zoom = self.zoomhistory[len(self.zoomhistory)-1]
- # (n, s, e, w)
- if zoom:
- # zoom to selected region
- self.Map.region['center_easting'] = zoom[3] + \
- (zoom[2] - zoom[3]) / 2
- self.Map.region['center_northing'] = zoom[1] + \
- (zoom[0] - zoom[1]) / 2
- self.Map.region["ewres"] = (zoom[2] - zoom[3]) / self.Map.width
- self.Map.region["nsres"] = (zoom[0] - zoom[1]) / self.Map.height
- self.Map.AlignExtentFromDisplay()
- # update map
- self.UpdateMap()
- # update statusbar
- self.parent.StatusbarUpdate()
- def ZoomHistory(self, n, s, e, w):
- """
- Manages a list of last 10 zoom extents
- Return removed history item if exists
- """
- removed = None
- self.zoomhistory.append((n,s,e,w))
- if len(self.zoomhistory) > 10:
- removed = self.zoomhistory.pop(0)
- if removed:
- Debug.msg(4, "BufferedWindow.ZoomHistory(): hist=%s, removed=%s" %
- (self.zoomhistory, removed))
- else:
- Debug.msg(4, "BufferedWindow.ZoomHistory(): hist=%s" %
- (self.zoomhistory))
- return removed
- def OnZoomToMap(self, event):
- """
- Set display extents to match selected raster (including NULLs)
- or vector map.
- """
- self.ZoomToMap()
- def OnZoomToRaster(self, event):
- """
- Set display extents to match selected raster map (ignore NULLs)
- """
- self.ZoomToMap(zoom=True)
-
- def ZoomToMap(self, zoom=False):
- """
- Set display extents to match selected raster
- or vector map.
- """
- zoomreg = {}
- layer = self.GetSelectedLayer()
-
- if layer is None:
- return
- Debug.msg (3, "BufferedWindow.ZoomToMap(): layer=%s, type=%s" % \
- (layer.name, layer.type))
- # selected layer must be a valid map
- if layer.type in ('raster', 'rgb', 'his', 'shaded', 'arrow'):
- if layer.type == 'raster':
- self.Map.region = self.Map.GetRegion(rast="%s" % layer.name)
- else:
- self.Map.region = self.Map.GetRegion(rast="%s" % layer.name)
- elif layer.type in ('vector', 'thememap', 'themechart'):
- if self.parent.digit and layer.name == self.parent.digit.map and \
- self.parent.digit.type == 'vdigit':
- w, s, b, e, n, t = self.parent.digit.driver.GetMapBoundingBox()
- self.Map.region = self.Map.GetRegion(n=n, s=s, w=w, e=e)
- else:
- self.Map.region = self.Map.GetRegion(vect="%s" % layer.name)
- else:
- return
- ### self.Map.SetRegion()
- ### self.Map.AlignExtentFromDisplay()
- self.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
- self.Map.region['e'], self.Map.region['w'])
- self.UpdateMap()
- self.parent.StatusbarUpdate()
- def ZoomToWind(self, event):
- """
- Set display geometry to match computational
- region settings (set with g.region)
- """
- self.Map.region = self.Map.GetRegion()
- ### self.Map.SetRegion(windres=True)
- self.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
- self.Map.region['e'], self.Map.region['w'])
- self.UpdateMap()
- self.parent.StatusbarUpdate()
- def ZoomToDefault(self, event):
- """Set display geometry to match default
- region settings"""
- self.Map.region = self.Map.GetRegion(default=True)
- self.Map.AdjustRegion() # aling region extent to the display
- self.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
- self.Map.region['e'], self.Map.region['w'])
- self.UpdateMap()
- self.parent.StatusbarUpdate()
-
- def DisplayToWind(self, event):
- """
- Set computational region (WIND file) to
- match display extents
- """
- tmpreg = os.getenv("GRASS_REGION")
- os.unsetenv("GRASS_REGION")
- # We ONLY want to set extents here. Don't mess with resolution. Leave that
- # for user to set explicitly with g.region
- new = self.Map.AlignResolution()
-
- cmdRegion = ["g.region", "--o",
- "n=%f" % new['n'],
- "s=%f" % new['s'],
- "e=%f" % new['e'],
- "w=%f" % new['w'],
- "rows=%f" % new['rows'],
- "cols=%f" % new['cols']]
- p = gcmd.Command(cmdRegion)
- if tmpreg:
- os.environ["GRASS_REGION"] = tmpreg
- def ZoomToSaved(self, event):
- """
- Set display geometry to match extents in
- saved region file
- """
- zoomreg = {}
- dlg = gdialogs.SavedRegion(self, wx.ID_ANY, _("Zoom to saved region extents"),
- pos=wx.DefaultPosition, size=wx.DefaultSize,
- style=wx.DEFAULT_DIALOG_STYLE,
- loadsave='load')
- if dlg.ShowModal() == wx.ID_CANCEL:
- dlg.Destroy()
- return
- wind = dlg.wind
- p = gcmd.Command (["g.region",
- "-ugp", "region=%s" % wind])
- if p.returncode == 0:
- output = p.ReadStdOutput()
- for line in output:
- line = line.strip()
- if '=' in line: key,val = line.split('=')
- zoomreg[key] = float(val)
- self.Map.region['n'] = zoomreg['n']
- self.Map.region['s'] = zoomreg['s']
- self.Map.region['e'] = zoomreg['e']
- self.Map.region['w'] = zoomreg['w']
- self.ZoomHistory(self.Map.region['n'],self.Map.region['s'],self.Map.region['e'],self.Map.region['w'])
- self.UpdateMap()
- dlg.Destroy()
- def SaveDisplayRegion(self, event):
- """
- Save display extents to named region file.
- """
- dlg = gdialogs.SavedRegion(self, wx.ID_ANY, "Save display extents to region file",
- pos=wx.DefaultPosition, size=wx.DefaultSize,
- style=wx.DEFAULT_DIALOG_STYLE,
- loadsave='save')
- if dlg.ShowModal() == wx.ID_CANCEL:
- dlg.Destroy()
- return
- wind = dlg.wind
- # test to see if it already exists and ask permission to overwrite
- windpath = os.path.join(self.Map.env["GISDBASE"], self.Map.env["LOCATION_NAME"],
- self.Map.env["MAPSET"],"windows",wind)
- if windpath and not os.path.exists(windpath):
- self.SaveRegion(wind)
- elif windpath and os.path.exists(windpath):
- overwrite = wx.MessageBox(_("Region file <%s> already exists. "
- "Do you want to overwrite it?") % (wind),
- _("Warning"), wx.YES_NO)
- if (overwrite == wx.YES):
- self.SaveRegion(wind)
- else:
- pass
- dlg.Destroy()
- def SaveRegion(self, wind):
- """Save region settings"""
- new = self.Map.AlignResolution()
- cmdRegion = ["g.region",
- "-u",
- "n=%f" % new['n'],
- "s=%f" % new['s'],
- "e=%f" % new['e'],
- "w=%f" % new['w'],
- "rows=%d" % new['rows'],
- "cols=%d" % new['cols'],
- "save=%s" % wind,
- "--o"]
- tmpreg = os.getenv("GRASS_REGION")
- os.unsetenv("GRASS_REGION")
- p = gcmd.Command(cmdRegion)
- if tmpreg:
- os.environ["GRASS_REGION"] = tmpreg
- def Distance(self, beginpt, endpt, screen=True):
- """Calculete distance
- LL-locations not supported
- @todo Use m.distance
- @param beginpt first point
- @param endpt second point
- @param screen True for screen coordinates otherwise EN
- """
- x1, y1 = beginpt
- x2, y2 = endpt
- if screen:
- dEast = (x2 - x1) * self.Map.region["ewres"]
- dNorth = (y2 - y1) * self.Map.region["nsres"]
- else:
- dEast = (x2 - x1)
- dNorth = (y2 - y1)
-
- return (math.sqrt(math.pow((dEast),2) + math.pow((dNorth),2)), (dEast, dNorth))
- class MapFrame(wx.Frame):
- """
- Main frame for map display window. Drawing takes place in child double buffered
- drawing window.
- """
- def __init__(self, parent=None, id=wx.ID_ANY, title=_("GRASS GIS - Map display"),
- pos=wx.DefaultPosition, size=wx.DefaultSize,
- style=wx.DEFAULT_FRAME_STYLE, toolbars=["map"],
- tree=None, notebook=None, gismgr=None, page=None,
- Map=None, auimgr=None):
- """
- Main map display window with toolbars, statusbar and
- DrawWindow
- @param toolbars array of activated toolbars, e.g. ['map', 'digit']
- @param tree reference to layer tree
- @param notebook control book ID in Layer Manager
- @param gismgr Layer Manager panel
- @param page notebook page with layer tree
- @param Map instance of render.Map
- """
- wx.Frame.__init__(self, parent, id, title, pos, size, style)
- self.gismanager = gismgr # GIS Manager object
- self.Map = Map # instance of render.Map
- self.tree = tree # GIS Manager layer tree object
- self.page = page # Notebook page holding the layer tree
- self.layerbook = notebook # GIS Manager layer tree notebook
- self.parent = parent
- #
- # available cursors
- #
- self.cursors = {
- # default: cross
- # "default" : wx.StockCursor(wx.CURSOR_DEFAULT),
- "default" : wx.StockCursor(wx.CURSOR_ARROW),
- "cross" : wx.StockCursor(wx.CURSOR_CROSS),
- "hand" : wx.StockCursor(wx.CURSOR_HAND),
- "pencil" : wx.StockCursor(wx.CURSOR_PENCIL),
- "sizenwse": wx.StockCursor(wx.CURSOR_SIZENWSE)
- }
- #
- # set the size & system icon
- #
- self.SetClientSize(size)
- self.iconsize = (16, 16)
- self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass_map.ico'), wx.BITMAP_TYPE_ICO))
- #
- # Fancy gui
- #
- # self._mgr = auimgr
- self._mgr = wx.aui.AuiManager(self)
- #
- # Add toolbars
- #
- self.toolbars = { 'map' : None,
- 'vdigit' : None,
- 'georect' : None,
- 'nviz' : None }
- for toolb in toolbars:
- self.AddToolbar(toolb)
- #
- # Add statusbar
- #
- self.statusbar = self.CreateStatusBar(number=3, style=0)
- self.statusbar.SetStatusWidths([-5, -2, -1])
- self.toggleStatus = wx.Choice(self.statusbar, wx.ID_ANY,
- choices = globalvar.MAP_DISPLAY_STATUSBAR_MODE)
- self.toggleStatus.SetSelection(UserSettings.Get(group='display', key='statusbarMode', subkey='selection'))
- self.statusbar.Bind(wx.EVT_CHOICE, self.OnToggleStatus, self.toggleStatus)
- # auto-rendering checkbox
- self.autoRender = wx.CheckBox(parent=self.statusbar, id=wx.ID_ANY,
- label=_("Render"))
- self.statusbar.Bind(wx.EVT_CHECKBOX, self.OnToggleRender, self.autoRender)
- self.autoRender.SetValue(UserSettings.Get(group='display', key='autoRendering', subkey='enabled'))
- self.autoRender.SetToolTip(wx.ToolTip (_("Enable/disable auto-rendering")))
- # show region
- self.showRegion = wx.CheckBox(parent=self.statusbar, id=wx.ID_ANY,
- label=_("Show computational extent"))
- self.statusbar.Bind(wx.EVT_CHECKBOX, self.OnToggleShowRegion, self.showRegion)
-
- self.showRegion.SetValue(False)
- self.showRegion.Hide()
- self.showRegion.SetToolTip(wx.ToolTip (_("Show/hide computational "
- "region extent (set with g.region). "
- "Display region drawn as a blue box inside the "
- "computational region, "
- "computational region inside a display region "
- "as a red box).")))
- # set resolution
- self.compResolution = wx.CheckBox(parent=self.statusbar, id=wx.ID_ANY,
- label=_("Constrain display resolution to computational settings"))
- self.statusbar.Bind(wx.EVT_CHECKBOX, self.OnToggleResolution, self.compResolution)
- self.compResolution.SetValue(UserSettings.Get(group='display', key='compResolution', subkey='enabled'))
- self.compResolution.Hide()
- self.compResolution.SetToolTip(wx.ToolTip (_("Constrain display resolution "
- "to computational region settings. "
- "Default value for new map displays can "
- "be set up in 'User GUI settings' dialog.")))
- # map scale
- self.mapScale = wx.TextCtrl(parent=self.statusbar, id=wx.ID_ANY,
- value="", style=wx.TE_PROCESS_ENTER,
- size=(150, -1))
- self.mapScale.Hide()
- self.statusbar.Bind(wx.EVT_TEXT_ENTER, self.OnChangeMapScale, self.mapScale)
- # on-render gauge
- self.onRenderGauge = wx.Gauge(parent=self.statusbar, id=wx.ID_ANY,
- range=0, style=wx.GA_HORIZONTAL)
- self.onRenderGauge.Hide()
-
- self.StatusbarReposition() # reposition statusbar
- #
- # Init map display (buffered DC & set default cursor)
- #
- self.MapWindow2D = BufferedWindow(self, id=wx.ID_ANY,
- Map=self.Map, tree=self.tree, gismgr=self.gismanager)
- # default is 2D display mode
- self.MapWindow = self.MapWindow2D
- self.MapWindow.Bind(wx.EVT_MOTION, self.OnMotion)
- self.MapWindow.SetCursor(self.cursors["default"])
- # used by Nviz (3D display mode)
- self.MapWindow3D = None
- #
- # initialize region values
- #
- self.__InitDisplay()
- #
- # Bind various events
- #
- self.Bind(wx.EVT_ACTIVATE, self.OnFocus)
- self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
- self.Bind(render.EVT_UPDATE_PRGBAR, self.OnUpdateProgress)
-
- #
- # Update fancy gui style
- #
- self._mgr.AddPane(self.MapWindow, wx.aui.AuiPaneInfo().CentrePane().
- Dockable(False).BestSize((-1,-1)).
- CloseButton(False).DestroyOnClose(True).
- Layer(0))
- self._mgr.Update()
- #
- # Init print module and classes
- #
- self.printopt = disp_print.PrintOptions(self, self.MapWindow)
- #
- # Current location information
- #
- self.projinfo = self.Map.ProjInfo()
- #
- # Initialization of digitization tool
- #
- self.digit = None
- #
- # Init zoom history
- #
- self.MapWindow.ZoomHistory(self.Map.region['n'],
- self.Map.region['s'],
- self.Map.region['e'],
- self.Map.region['w'])
- #
- # Re-use dialogs
- #
- self.dialogs = {}
- self.dialogs['attributes'] = None
- self.dialogs['category'] = None
- self.dialogs['barscale'] = None
- self.dialogs['legend'] = None
- self.decorationDialog = None # decoration/overlays
- def AddToolbar(self, name):
- """
- Add defined toolbar to the window
- Currently known toolbars are:
- - map basic map toolbar
- - digit vector digitizer
- - georect georectifier
- """
- # default toolbar
- if name == "map":
- self.toolbars['map'] = toolbars.MapToolbar(self, self.Map)
- self._mgr.AddPane(self.toolbars['map'].toolbar,
- wx.aui.AuiPaneInfo().
- Name("maptoolbar").Caption(_("Map Toolbar")).
- ToolbarPane().Top().
- LeftDockable(False).RightDockable(False).
- BottomDockable(False).TopDockable(True).
- CloseButton(False).Layer(2))
- # vector digitizer
- elif name == "vdigit":
- self.toolbars['vdigit'] = toolbars.VDigitToolbar(parent=self, map=self.Map,
- layerTree=self.tree,
- log=self.gismanager.goutput)
- for toolRow in range(0, self.toolbars['vdigit'].numOfRows):
- self._mgr.AddPane(self.toolbars['vdigit'].toolbar[toolRow],
- wx.aui.AuiPaneInfo().
- Name("vdigittoolbar" + str(toolRow)).Caption(_("Vector digitizer toolbar")).
- ToolbarPane().Top().Row(toolRow + 1).
- LeftDockable(False).RightDockable(False).
- BottomDockable(False).TopDockable(True).
- CloseButton(False).Layer(2))
- # change mouse to draw digitized line
- self.MapWindow.mouse['box'] = "point"
- self.MapWindow.zoomtype = 0
- self.MapWindow.pen = wx.Pen(colour='red', width=2, style=wx.SOLID)
- self.MapWindow.polypen = wx.Pen(colour='green', width=2, style=wx.SOLID)
- # georectifier
- elif name == "georect":
- self.toolbars['georect'] = toolbars.GRToolbar(self, self.Map)
- self._mgr.AddPane(self.toolbars['georect'].toolbar,
- wx.aui.AuiPaneInfo().
- Name("georecttoolbar").Caption(_("Georectification toolbar")).
- ToolbarPane().Top().
- LeftDockable(False).RightDockable(False).
- BottomDockable(False).TopDockable(True).
- CloseButton(False).Layer(2))
- # nviz
- elif name == "nviz":
- import nviz
- # check for GLCanvas and OpenGL
- msg = None
- if not nviz.haveGLCanvas:
- msg = _("Unable to start Nviz. The GLCanvas class has not been "
- "included with this build "
- "of wxPython! Switching back to "
- "2D display mode.\n\nDetails: %s" % nviz.errorMsg)
- if not nviz.haveOpenGL:
- msg = _("Unable to start Nviz. The OpenGL package "
- "was not found. You can get it "
- "at http://PyOpenGL.sourceforge.net. "
- "Switching back to 2D display mode.\n\nDetails: %s" % nviz.errorMsg)
- if not nviz.haveNviz:
- msg = _("Unable to start Nviz. Python extension "
- "for Nviz was not found or loaded properly. "
- "Switching back to 2D display mode.\n\nDetails: %s" % nviz.errorMsg)
- if msg:
- wx.MessageBox(parent=self,
- message=msg,
- caption=_("Error"))
- return
- #
- # add Nviz toolbar and disable 2D display mode tools
- #
- self.toolbars['nviz'] = toolbars.NvizToolbar(self, self.Map)
- self.toolbars['map'].Enable2D(False)
- #
- # update layer tree (-> enable 3d-rasters)
- #
- self.tree.EnableItemType(type='3d-raster', enable=True)
-
- #
- # update status bar
- #
- self.toggleStatus.Enable(False)
- #
- # erase map window
- #
- self.MapWindow.EraseMap()
- busy = wx.BusyInfo(message=_("Please wait, loading data..."),
- parent=self)
- wx.Yield()
-
- #
- # create GL window & NVIZ toolbar
- #
- if not self.MapWindow3D:
- self.MapWindow3D = nviz.GLWindow(self, id=wx.ID_ANY,
- Map=self.Map, tree=self.tree, gismgr=self.gismanager)
- self.nvizToolWin = nviz.NvizToolWindow(self, id=wx.ID_ANY,
- mapWindow=self.MapWindow3D)
- self.MapWindow3D.OnPaint(None) # -> LoadData
- busy.Destroy()
- self.nvizToolWin.Show()
- #
- # switch from MapWindow to MapWindowGL
- # add nviz toolbar
- #
- self._mgr.DetachPane(self.MapWindow2D)
- self.MapWindow2D.Hide()
- self._mgr.AddPane(self.MapWindow3D, wx.aui.AuiPaneInfo().CentrePane().
- Dockable(False).BestSize((-1,-1)).
- CloseButton(False).DestroyOnClose(True).
- Layer(0))
- self._mgr.AddPane(self.toolbars['nviz'].toolbar,
- wx.aui.AuiPaneInfo().
- Name("nviztoolbar").Caption(_("Nviz toolbar")).
- ToolbarPane().Top().Row(1).
- LeftDockable(False).RightDockable(False).
- BottomDockable(False).TopDockable(True).
- CloseButton(False).Layer(2))
- self.MapWindow = self.MapWindow3D
- self.SetStatusText("", 0)
-
- self._mgr.Update()
- def RemoveToolbar (self, name):
- """
- Removes toolbar from the window
- TODO: Only hide, activate by calling AddToolbar()
- """
- # cannot hide main toolbar
- if name == "map":
- return
- elif name == "vdigit":
- # TODO: not destroy only hide
- for toolRow in range(0, self.toolbars['vdigit'].numOfRows):
- self._mgr.DetachPane (self.toolbars['vdigit'].toolbar[toolRow])
- self.toolbars['vdigit'].toolbar[toolRow].Destroy()
- else:
- self._mgr.DetachPane (self.toolbars[name].toolbar)
- self.toolbars[name].toolbar.Destroy()
- self.toolbars[name] = None
- if name == 'nviz':
- # hide nviz tools
- self.nvizToolWin.Hide()
- # unload data
- self.MapWindow3D.Reset()
- # switch from MapWindowGL to MapWindow
- self._mgr.DetachPane(self.MapWindow3D)
- self.MapWindow3D.Hide()
- self.MapWindow2D.Show()
- self._mgr.AddPane(self.MapWindow2D, wx.aui.AuiPaneInfo().CentrePane().
- Dockable(False).BestSize((-1,-1)).
- CloseButton(False).DestroyOnClose(True).
- Layer(0))
- self.MapWindow = self.MapWindow2D
-
- #
- # update layer tree (-> disable 3d-rasters)
- #
- self.tree.EnableItemType(type='3d-raster', enable=False)
-
- self.toolbars['map'].combo.SetValue ("Tools")
- self.toolbars['map'].Enable2D(True)
- self.toggleStatus.Enable(True)
- self._mgr.Update()
- def __InitDisplay(self):
- """
- Initialize map display, set dimensions and map region
- """
- self.width, self.height = self.GetClientSize()
- Debug.msg(2, "MapFrame.__InitDisplay():")
- self.Map.ChangeMapSize(self.GetClientSize())
- self.Map.region = self.Map.GetRegion() # g.region -upgc
- # self.Map.SetRegion() # adjust region to match display window
- def OnUpdateProgress(self, event):
- """Update progress bar info"""
- self.onRenderGauge.SetValue(event.value)
-
- event.Skip()
-
- def OnFocus(self, event):
- """
- Change choicebook page to match display.
- Or set display for georectifying
- """
- if self.gismanager.georectifying:
- # in georectifying session; display used to get get geographic
- # coordinates for GCPs
- self.OnPointer(event)
- else:
- # change bookcontrol page to page associated with display
- if self.page:
- pgnum = self.layerbook.GetPageIndex(self.page)
- if pgnum > -1:
- self.layerbook.SetSelection(pgnum)
-
- event.Skip()
- def OnMotion(self, event):
- """
- Mouse moved
- Track mouse motion and update status bar
- """
- # update statusbar if required
- if self.toggleStatus.GetSelection() == 0: # Coordinates
- e, n = self.MapWindow.Pixel2Cell(event.GetPositionTuple())
- if self.toolbars['vdigit'] and \
- self.toolbars['vdigit'].GetAction() == 'addLine' and \
- self.toolbars['vdigit'].GetAction('type') in ('line', 'boundary') and \
- len(self.MapWindow.polycoords) > 0:
- # for linear feature show segment and total length
- distance_seg = self.MapWindow.Distance(self.MapWindow.polycoords[-1],
- (e, n), screen=False)[0]
- distance_tot = distance_seg
- for idx in range(1, len(self.MapWindow.polycoords)):
- distance_tot += self.MapWindow.Distance(self.MapWindow.polycoords[idx-1],
- self.MapWindow.polycoords[idx],
- screen=False )[0]
- self.statusbar.SetStatusText("%.2f, %.2f (seg: %.2f; tot: %.2f)" % \
- (e, n, distance_seg, distance_tot), 0)
- else:
- self.statusbar.SetStatusText("%.2f, %.2f" % (e, n), 0)
- event.Skip()
- def OnDraw(self, event):
- """
- Re-display current map composition
- """
- self.MapWindow.UpdateMap(render=False)
-
- def OnRender(self, event):
- """
- Re-render map composition (each map layer)
- """
- # delete tmp map layers (queries)
- qlayer = self.Map.GetListOfLayers(l_name=globalvar.QUERYLAYER)
- for layer in qlayer:
- self.Map.DeleteLayer(layer)
- # deselect features in vdigit
- if self.toolbars['vdigit']:
- self.digit.driver.SetSelected([])
- self.MapWindow.UpdateMap(render=True, renderVector=True)
- else:
- self.MapWindow.UpdateMap(render=True)
-
- # update statusbar
- self.StatusbarUpdate()
- def OnPointer(self, event):
- """Pointer button clicked"""
- self.toolbars['map'].OnTool(event)
- self.toolbars['map'].action['desc'] = ''
-
- self.MapWindow.mouse['use'] = "pointer"
- self.MapWindow.mouse['box'] = "point"
-
- # change the cursor
- if self.toolbars['vdigit']:
- # digitization tool activated
- self.MapWindow.SetCursor(self.cursors["cross"])
- # reset mouse['box'] if needed
- if self.toolbars['vdigit'].GetAction() in ['addLine']:
- if self.toolbars['vdigit'].GetAction('type') in ['point', 'centroid']:
- self.MapWindow.mouse['box'] = 'point'
- else: # line, boundary
- self.MapWindow.mouse['box'] = 'line'
- elif self.toolbars['vdigit'].GetAction() in ['addVertex', 'removeVertex', 'splitLine',
- 'editLine', 'displayCats', 'displayAttrs',
- 'copyCats']:
- self.MapWindow.mouse['box'] = 'point'
- else: # moveLine, deleteLine
- self.MapWindow.mouse['box'] = 'box'
- elif self.gismanager.georectifying:
- self.MapWindow.SetCursor(self.cursors["cross"])
- else:
- self.MapWindow.SetCursor(self.cursors["default"])
- def OnZoomIn(self, event):
- """
- Zoom in the map.
- Set mouse cursor, zoombox attributes, and zoom direction
- """
- self.toolbars['map'].OnTool(event)
- self.toolbars['map'].action['desc'] = ''
-
- self.MapWindow.mouse['use'] = "zoom"
- self.MapWindow.mouse['box'] = "box"
- self.MapWindow.zoomtype = 1
- self.MapWindow.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
-
- # change the cursor
- self.MapWindow.SetCursor(self.cursors["cross"])
- def OnZoomOut(self, event):
- """
- Zoom out the map.
- Set mouse cursor, zoombox attributes, and zoom direction
- """
- self.toolbars['map'].OnTool(event)
- self.toolbars['map'].action['desc'] = ''
-
- self.MapWindow.mouse['use'] = "zoom"
- self.MapWindow.mouse['box'] = "box"
- self.MapWindow.zoomtype = -1
- self.MapWindow.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
-
- # change the cursor
- self.MapWindow.SetCursor(self.cursors["cross"])
- def OnZoomBack(self, event):
- """
- Zoom last (previously stored position)
- """
- self.MapWindow.ZoomBack()
- def OnPan(self, event):
- """
- Panning, set mouse to drag
- """
- self.toolbars['map'].OnTool(event)
- self.toolbars['map'].action['desc'] = ''
-
- self.MapWindow.mouse['use'] = "pan"
- self.MapWindow.mouse['box'] = "pan"
- self.MapWindow.zoomtype = 0
-
- # change the cursor
- self.MapWindow.SetCursor(self.cursors["hand"])
- def OnErase(self, event):
- """
- Erase the canvas
- """
- self.MapWindow.EraseMap()
- def OnZoomRegion(self, event):
- """
- Zoom to region
- """
- self.Map.getRegion()
- self.Map.getResolution()
- self.UpdateMap()
- # event.Skip()
- def OnAlignRegion(self, event):
- """
- Align region
- """
- if not self.Map.alignRegion:
- self.Map.alignRegion = True
- else:
- self.Map.alignRegion = False
- # event.Skip()
- def OnToggleRender(self, event):
- """Enable/disable auto-rendering"""
- if self.autoRender.GetValue():
- self.OnRender(None)
- def OnToggleShowRegion(self, event):
- """Show/Hide extent in map canvas"""
- if self.showRegion.GetValue():
- # show extent
- self.MapWindow.regionCoords = []
- else:
- del self.MapWindow.regionCoords
- # redraw map if auto-rendering is enabled
- if self.autoRender.GetValue():
- self.OnRender(None)
- def OnToggleResolution(self, event):
- """Use resolution of computation region settings
- for redering image instead of display resolution"""
- # redraw map if auto-rendering is enabled
- if self.autoRender.GetValue():
- self.OnRender(None)
-
- def OnToggleStatus(self, event):
- """Toggle status text"""
- self.StatusbarUpdate()
- def OnChangeMapScale(self, event):
- """Map scale changed by user"""
- scale = event.GetString()
- try:
- if scale[:2] != '1:':
- raise ValueError
- value = int(scale[2:])
- except ValueError:
- self.mapScale.SetValue('1:' + str(int(self.mapScaleValue)))
- return
- dEW = value * (self.Map.region['cols'] / self.ppm[0])
- dNS = value * (self.Map.region['rows'] / self.ppm[1])
- self.Map.region['n'] = self.Map.region['center_northing'] + dNS / 2
- self.Map.region['s'] = self.Map.region['center_northing'] - dNS / 2
- self.Map.region['w'] = self.Map.region['center_easting'] - dEW / 2
- self.Map.region['e'] = self.Map.region['center_easting'] + dEW / 2
- # add to zoom history
- self.MapWindow.ZoomHistory(self.Map.region['n'], self.Map.region['s'],
- self.Map.region['e'], self.Map.region['w'])
- # redraw a map
- self.MapWindow.UpdateMap()
- def StatusbarUpdate(self):
- """Update statusbar content"""
- self.showRegion.Hide()
- self.compResolution.Hide()
- self.mapScale.Hide()
- self.mapScaleValue = self.ppm = None
- if self.toggleStatus.GetSelection() == 0: # Coordinates
- self.statusbar.SetStatusText("", 0)
- # enable long help
- self.StatusbarEnableLongHelp()
- elif self.toggleStatus.GetSelection() == 1: # Extent
- self.statusbar.SetStatusText("%.2f - %.2f, %.2f - %.2f" %
- (self.Map.region["w"], self.Map.region["e"],
- self.Map.region["s"], self.Map.region["n"]), 0)
- # enable long help
- self.StatusbarEnableLongHelp()
- elif self.toggleStatus.GetSelection() == 2: # Comp. region
- compregion = self.Map.GetRegion()
- self.statusbar.SetStatusText("%.2f - %.2f, %.2f - %.2f (%.2f, %.2f)" %
- (compregion["w"], compregion["e"],
- compregion["s"], compregion["n"],
- compregion["ewres"], compregion["nsres"]), 0)
- # enable long help
- self.StatusbarEnableLongHelp()
- elif self.toggleStatus.GetSelection() == 3: # Show comp. extent
- self.statusbar.SetStatusText("", 0)
- self.showRegion.Show()
- # disable long help
- self.StatusbarEnableLongHelp(False)
- elif self.toggleStatus.GetSelection() == 4: # Display mode
- self.statusbar.SetStatusText("", 0)
- self.compResolution.Show()
- # disable long help
- self.StatusbarEnableLongHelp(False)
- elif self.toggleStatus.GetSelection() == 5: # Display geometry
- self.statusbar.SetStatusText("rows=%d; cols=%d; nsres=%.2f; ewres=%.2f" %
- (self.Map.region["rows"], self.Map.region["cols"],
- self.Map.region["nsres"], self.Map.region["ewres"]), 0)
- # enable long help
- self.StatusbarEnableLongHelp()
- elif self.toggleStatus.GetSelection() == 6: # Map scale
- # TODO: need to be fixed...
- ### screen X region problem
- ### user should specify ppm
- dc = wx.ScreenDC()
- dpSizePx = wx.DisplaySize() # display size in pixels
- dpSizeMM = wx.DisplaySizeMM() # display size in mm (system)
- dpSizeIn = (dpSizeMM[0] / 25.4, dpSizeMM[1] / 25.4) # inches
- sysPpi = dc.GetPPI()
- comPpi = (dpSizePx[0] / dpSizeIn[0],
- dpSizePx[1] / dpSizeIn[1])
- ppi = comPpi # pixel per inch
- self.ppm = ((ppi[0] / 2.54) * 100, # pixel per meter
- (ppi[1] / 2.54) * 100)
- Debug.msg(4, "MapFrame.StatusbarUpdate(mapscale): size: px=%d,%d mm=%f,%f "
- "in=%f,%f ppi: sys=%d,%d com=%d,%d; ppm=%f,%f" % \
- (dpSizePx[0], dpSizePx[1], dpSizeMM[0], dpSizeMM[1],
- dpSizeIn[0], dpSizeIn[1],
- sysPpi[0], sysPpi[1], comPpi[0], comPpi[1],
- self.ppm[0], self.ppm[1]))
- region = self.Map.region
- heightCm = region['rows'] / self.ppm[1] * 100
- widthCm = region['cols'] / self.ppm[0] * 100
- Debug.msg(4, "MapFrame.StatusbarUpdate(mapscale): width_cm=%f, height_cm=%f" %
- (widthCm, heightCm))
- xscale = (region['e'] - region['w']) / (region['cols'] / self.ppm[0])
- yscale = (region['n'] - region['s']) / (region['rows'] / self.ppm[1])
- scale = (xscale + yscale) / 2
- Debug.msg(3, "MapFrame.StatusbarUpdate(mapscale): xscale=%f, yscale=%f -> scale=%f" % \
- (xscale, yscale, scale))
- self.statusbar.SetStatusText("")
- self.mapScale.SetValue("1:%ld" % scale)
- self.mapScaleValue = scale
- self.mapScale.Show()
- # disable long help
- self.StatusbarEnableLongHelp(False)
- else:
- self.statusbar.SetStatusText("", 1)
- def StatusbarEnableLongHelp(self, enable=True):
- """Enable/disable toolbars long help"""
- for toolbar in self.toolbars.itervalues():
- if toolbar:
- toolbar.EnableLongHelp(enable)
-
- def StatusbarReposition(self):
- """Reposition checkbox in statusbar"""
- # reposition checkbox
- widgets = [(0, self.showRegion),
- (0, self.compResolution),
- (0, self.mapScale),
- (0, self.onRenderGauge),
- (1, self.toggleStatus),
- (2, self.autoRender)]
- for idx, win in widgets:
- rect = self.statusbar.GetFieldRect(idx)
- if idx == 0: # show region / mapscale / process bar
- # -> size
- wWin, hWin = win.GetBestSize()
- if win == self.onRenderGauge:
- wWin = rect.width - 6
- # -> position
- # if win == self.showRegion:
- # x, y = rect.x + rect.width - wWin, rect.y - 1
- # align left
- # else:
- x, y = rect.x + 3, rect.y - 1
- w, h = wWin, rect.height + 2
- else: # choice || auto-rendering
- x, y = rect.x, rect.y - 1
- w, h = rect.width, rect.height + 2
- if idx == 2:
- x += 5
- win.SetPosition((x, y))
- win.SetSize((w, h))
- def SaveToFile(self, event):
- """
- Save image to file
- """
- filetype = "PNG file (*.png)|*.png|"\
- "TIF file (*.tif)|*.tif|"\
- "GIF file (*.gif)|*.gif"
- dlg = wx.FileDialog(self, "Choose a file name to save the image as a PNG to",
- defaultDir = "",
- defaultFile = "",
- wildcard = filetype,
- style=wx.SAVE|wx.FD_OVERWRITE_PROMPT)
- if dlg.ShowModal() == wx.ID_OK:
- base = os.path.splitext(dlg.GetPath())[0]
- ext = os.path.splitext(dlg.GetPath())[1]
- if dlg.GetFilterIndex() == 0:
- type = wx.BITMAP_TYPE_PNG
- path = dlg.GetPath()
- if ext != '.png': path = base+'.png'
- elif dlg.GetFilterIndex() == 1:
- type = wx.BITMAP_TYPE_TIF
- if ext != '.tif': path = base+'.tif'
- elif dlg.GetFilterIndex() == 2:
- type = wx.BITMAP_TYPE_TIF
- if ext != '.gif': path = base+'.gif'
- self.MapWindow.SaveToFile(path, type)
- dlg.Destroy()
- def PrintMenu(self, event):
- """
- Print map display
- """
- """
- Print options and output menu
- """
- point = wx.GetMousePosition()
- printmenu = wx.Menu()
- # Add items to the menu
- setup = wx.MenuItem(printmenu, wx.ID_ANY, _('Page setup'))
- printmenu.AppendItem(setup)
- self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
- preview = wx.MenuItem(printmenu, wx.ID_ANY, _('Print preview'))
- printmenu.AppendItem(preview)
- self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
- doprint = wx.MenuItem(printmenu, wx.ID_ANY, _('Print display'))
- printmenu.AppendItem(doprint)
- self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
- # Popup the menu. If an item is selected then its handler
- # will be called before PopupMenu returns.
- self.PopupMenu(printmenu)
- printmenu.Destroy()
- def OnCloseWindow(self, event):
- """
- Window closed
- Also close associated layer tree page
- """
- pgnum = None
- self.Map.Clean()
-
- # close edited map properly
- digitToolbar = self.toolbars['vdigit']
- if digitToolbar:
- maplayer = digitToolbar.GetLayer()
- if maplayer:
- self.toolbars['vdigit'].OnExit()
- self.imgVectorMap = None
-
- if self.page:
- pgnum = self.layerbook.GetPageIndex(self.page)
- if pgnum > -1:
- self.layerbook.DeletePage(pgnum)
-
- ### self.Destroy()
- # if event:
- # event.Skip() <- causes application crash
-
- def GetRender(self):
- """
- Returns the current instance of render.Map()
- """
- return self.Map
- def OnQueryDisplay(self, event):
- """
- Query currrent raster/vector map layers (display mode)
- """
- if self.toolbars['map'].GetAction() == 'displayAttrb': # select previous action
- self.toolbars['map'].SelectDefault(event)
- return
- self.toolbars['map'].action['desc'] = 'displayAttrb'
-
- # switch GIS Manager to output console to show query results
- self.gismanager.notebook.SetSelection(1)
- self.MapWindow.mouse['use'] = "query"
- self.MapWindow.mouse['box'] = "point"
- self.MapWindow.zoomtype = 0
- # change the cursor
- self.MapWindow.SetCursor(self.cursors["cross"])
- def OnQueryModify(self, event):
- """
- Query vector map layer (edit mode)
- """
- if self.toolbars['map'].GetAction() == 'modifyAttrb': # select previous action
- self.toolbars['map'].SelectDefault(event)
- return
-
- self.toolbars['map'].action['desc'] = 'modifyAttrb'
-
- self.MapWindow.mouse['use'] = "queryVector"
- self.MapWindow.mouse['box'] = "point"
- self.MapWindow.zoomtype = 0
- # change the cursor
- self.MapWindow.SetCursor(self.cursors["cross"])
-
- def QueryMap(self, x, y):
- """
- Query map layer features
- Currently only raster and vector map layers are supported
- """
- #set query snap distance for v.what at mapunit equivalent of 10 pixels
- qdist = 10.0 * ((self.Map.region['e'] - self.Map.region['w']) / self.Map.width)
- east, north = self.MapWindow.Pixel2Cell((x, y))
- if not self.tree.layer_selected:
- dlg = wx.MessageDialog(parent=self,
- message=_('No map layer selected for querying.'),
- caption=_('Message'),
- style=wx.OK | wx.ICON_INFORMATION)
- dlg.ShowModal()
- dlg.Destroy()
- return
- mapname = None
- raststr = ''
- vectstr = ''
- rcmd = []
- vcmd = []
- for layer in self.tree.GetSelections():
- type = self.tree.GetPyData(layer)[0]['maplayer'].type
- dcmd = self.tree.GetPyData(layer)[0]['cmd']
- name = utils.GetLayerNameFromCmd(dcmd)
- if name == '':
- continue
- if type in ('raster', 'rgb', 'his'):
- raststr += "%s," % name
- elif type in ('vector', 'thememap', 'themechart'):
- vectstr += "%s," % name
- # build query commands for any selected rasters and vectors
- if raststr != '':
- rcmd = ['r.what', '--q',
- '-f',
- 'input=%s' % raststr.rstrip(','),
- 'east_north=%f,%f' % (float(east), float(north))]
- if vectstr != '':
- # check for vector maps open to be edited
- digitToolbar = self.toolbars['vdigit']
- if digitToolbar:
- map = digitToolbar.GetLayer().GetName()
- vect = []
- for vector in vectstr.split(','):
- if map == vector:
- self.gismanager.goutput.WriteWarning("Vector map <%s> "
- "opened for editing - skipped." % map)
- continue
- vect.append(vector)
- vectstr = ','.join(vect)
-
- if len(vectstr) <= 1:
- self.gismanager.goutput.WriteCmdLog("Nothing to query.")
- return
-
- vcmd = ['v.what', '--q',
- '-a',
- 'map=%s' % vectstr.rstrip(','),
- 'east_north=%f,%f' % (float(east), float(north)),
- 'distance=%f' % qdist]
- # parse query command(s)
- if self.gismanager:
- if rcmd:
- self.gismanager.goutput.RunCmd(rcmd)
- if vcmd:
- self.gismanager.goutput.RunCmd(vcmd)
- else:
- if rcmd:
- gcmd.Command(rcmd)
- if vcmd:
- gcmd.Command(vcmd)
- def QueryVector(self, x, y):
- """
- Query vector map layer features
- Attribute data of selected vector object are displayed in GUI dialog.
- Data can be modified (On Submit)
- """
- if not self.tree.layer_selected or \
- self.tree.GetPyData(self.tree.layer_selected)[0]['type'] != 'vector':
- wx.MessageBox(parent=self,
- message=_("No vector map selected for querying."),
- caption=_("Message"),
- style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
- return
-
- posWindow = self.ClientToScreen((x + self.MapWindow.dialogOffset,
- y + self.MapWindow.dialogOffset))
- qdist = 10.0 * ((self.Map.region['e'] - self.Map.region['w']) / \
- self.Map.width)
- east, north = self.MapWindow.Pixel2Cell((x, y))
- mapName = self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].name
- if self.dialogs['attributes'] is None:
- self.dialogs['attributes'] = dbm.DisplayAttributesDialog(parent=self.MapWindow,
- map=mapName,
- query=((east, north), qdist),
- pos=posWindow,
- action="update")
- else:
- # update currently open dialog
- self.dialogs['attributes'].UpdateDialog(query=((east, north), qdist))
- line = self.dialogs['attributes'].GetLine()
- try:
- qlayer = self.Map.GetListOfLayers(l_name=globalvar.QUERYLAYER)[0]
- except IndexError:
- qlayer = None
-
- if self.dialogs['attributes'].mapDBInfo and line:
- # highlight feature & re-draw map
- if qlayer:
- qlayer.SetCmd(self.AddTmpVectorMapLayer(mapName, line,
- useId=True,
- addLayer=False))
- else:
- self.AddTmpVectorMapLayer(mapName, line, useId=True)
- self.MapWindow.UpdateMap(render=False, renderVector=False)
- # digitClass.driver.SetSelected([line])
- if not self.dialogs['attributes'].IsShown():
- self.dialogs['attributes'].Show()
- else:
- if qlayer:
- self.Map.DeleteLayer(qlayer)
- self.MapWindow.UpdateMap(render=False, renderVector=False)
- if self.dialogs['attributes'].IsShown():
- self.dialogs['attributes'].Hide()
- def OnQuery(self, event):
- """Query tools menu"""
- self.toolbars['map'].OnTool(event)
- action = self.toolbars['map'].GetAction()
-
- point = wx.GetMousePosition()
- toolsmenu = wx.Menu()
- # Add items to the menu
- display = wx.MenuItem(parentMenu=toolsmenu, id=wx.ID_ANY,
- text=Icons["queryDisplay"].GetLabel(),
- kind=wx.ITEM_CHECK)
- toolsmenu.AppendItem(display)
- self.Bind(wx.EVT_MENU, self.OnQueryDisplay, display)
- if action == "displayAttrb":
- display.Check(True)
-
- modify = wx.MenuItem(parentMenu=toolsmenu, id=wx.ID_ANY,
- text=Icons["queryModify"].GetLabel(),
- kind=wx.ITEM_CHECK)
- toolsmenu.AppendItem(modify)
- self.Bind(wx.EVT_MENU, self.OnQueryModify, modify)
- digitToolbar = self.toolbars['vdigit']
- layer_selected = self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer']
- if digitToolbar and \
- digitToolbar.GetLayer() == layer_selected:
- modify.Enable(False)
- else:
- if action == "modifyAttrb":
- modify.Check(True)
- self.PopupMenu(toolsmenu)
- toolsmenu.Destroy()
- def AddTmpVectorMapLayer(self, name, cats, useId=False, addLayer=True):
- """
- Add temporal vector map layer to map composition
- @param name name of map layer
- @param useId use feature id instead of category
- """
- # color settings from ATM
- color = UserSettings.Get(group='atm', key='highlight', subkey='color')
- colorStr = str(color[0]) + ":" + \
- str(color[1]) + ":" + \
- str(color[2])
- cmd = ["d.vect",
- "map=%s" % name,
- "color=%s" % colorStr,
- "fcolor=%s" % colorStr,
- "width=%d" % UserSettings.Get(group='atm', key='highlight', subkey='width')]
-
- if useId:
- cmd.append('-i')
- cmd.append('cats=%s' % str(cats))
- else:
- cmd.append("cats=%s" % utils.ListOfCatsToRange(cats))
- # if self.icon:
- # cmd.append("icon=%s" % (self.icon))
- # if self.pointsize:
- # cmd.append("size=%s" % (self.pointsize))
- if addLayer:
- return self.Map.AddLayer(type='vector', name=globalvar.QUERYLAYER, command=cmd,
- l_active=True, l_hidden=True, l_opacity=1.0)
- else:
- return cmd
- def OnAnalyze(self, event):
- """
- Analysis tools menu
- """
- point = wx.GetMousePosition()
- toolsmenu = wx.Menu()
- # Add items to the menu
- measure = wx.MenuItem(toolsmenu, wx.ID_ANY, Icons["measure"].GetLabel())
- measure.SetBitmap(Icons["measure"].GetBitmap(self.iconsize))
- toolsmenu.AppendItem(measure)
- self.Bind(wx.EVT_MENU, self.OnMeasure, measure)
- profile = wx.MenuItem(toolsmenu, wx.ID_ANY, Icons["profile"].GetLabel())
- profile.SetBitmap(Icons["profile"].GetBitmap(self.iconsize))
- toolsmenu.AppendItem(profile)
- self.Bind(wx.EVT_MENU, self.Profile, profile)
- histogram = wx.MenuItem(toolsmenu, wx.ID_ANY, Icons["histogram"].GetLabel())
- histogram.SetBitmap(Icons["histogram"].GetBitmap(self.iconsize))
- toolsmenu.AppendItem(histogram)
- self.Bind(wx.EVT_MENU, self.Histogram, histogram)
- # Popup the menu. If an item is selected then its handler
- # will be called before PopupMenu returns.
- self.PopupMenu(toolsmenu)
- toolsmenu.Destroy()
- def OnMeasure(self, event):
- """
- Init measurement routine that calculates
- map distance along transect drawn on
- map display
- """
- self.totaldist = 0.0 # total measured distance
- # switch GIS Manager to output console to show measure results
- self.gismanager.notebook.SetSelection(1)
- # change mouse to draw line for measurement
- self.MapWindow.mouse['use'] = "measure"
- self.MapWindow.mouse['box'] = "line"
- self.MapWindow.zoomtype = 0
- self.MapWindow.pen = wx.Pen(colour='red', width=2, style=wx.SHORT_DASH)
- self.MapWindow.polypen = wx.Pen(colour='green', width=2, style=wx.SHORT_DASH)
- # change the cursor
- self.MapWindow.SetCursor(self.cursors["pencil"])
- # initiating output
- style = self.gismanager.goutput.cmd_output.StyleWarning
- self.gismanager.goutput.WriteLog(_('Click and drag with left mouse button '
- 'to measure.%s'
- 'Double click with left button to clear.') % \
- (os.linesep), style)
- if self.projinfo['proj'] != 'xy':
- units = self.projinfo['units']
- style = self.gismanager.goutput.cmd_output.StyleCommand
- self.gismanager.goutput.WriteLog(_('Measuring distance') + ' ('
- + units + '):',
- style)
- else:
- self.gismanager.goutput.WriteLog(_('Measuring distance:'),
- style)
- def MeasureDist(self, beginpt, endpt):
- """
- Calculate map distance from screen distance
- and print to output window
- """
- if self.gismanager.notebook.GetSelection() != 1:
- self.gismanager.notebook.SetSelection(1)
- dist, (north, east) = self.MapWindow.Distance(beginpt, endpt)
- dist = round(dist, 3)
- d, dunits = self.FormatDist(dist)
- self.totaldist += dist
- td, tdunits = self.FormatDist(self.totaldist)
- 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)
- angle = angle+90
- if angle < 0: angle = 360+angle
- mstring = 'segment = %s %s\ttotal distance = %s %s\tbearing = %d deg' \
- % (strdist,dunits,strtotdist,tdunits,angle)
- else:
- mstring = 'segment = %s %s\ttotal distance = %s %s' \
- % (strdist,dunits,strtotdist,tdunits)
- self.gismanager.goutput.WriteLog(mstring)
- return dist
- def Profile(self, event):
- """
- Init profile canvas and tools
- """
- raster = []
- if self.tree.layer_selected and \
- self.tree.GetPyData(self.tree.layer_selected)[0]['type'] == 'raster':
- raster.append(self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].name)
- self.profile = profile.ProfileFrame(self,
- id=wx.ID_ANY, pos=wx.DefaultPosition, size=(700,300),
- style=wx.DEFAULT_FRAME_STYLE, rasterList=raster)
- self.profile.Show()
- def FormatDist(self, dist):
- """Format length numbers and units in a nice way,
- as a function of length. From code by Hamish Bowman
- Grass Development Team 2006"""
- mapunits = self.projinfo['units']
- if mapunits == 'metres': mapunits = 'meters'
- outunits = mapunits
- dist = float(dist)
- divisor = 1.0
- # figure out which units to use
- if mapunits == 'meters':
- if dist > 2500.0:
- outunits = 'km'
- divisor = 1000.0
- else: outunits = 'm'
- elif mapunits == 'feet':
- # nano-bug: we match any "feet", but US Survey feet is really
- # 5279.9894 per statute mile, or 10.6' per 1000 miles. As >1000
- # miles the tick markers are rounded to the nearest 10th of a
- # mile (528'), the difference in foot flavours is ignored.
- if dist > 5280.0:
- outunits = 'miles'
- divisor = 5280.0
- else:
- outunits = 'ft'
- elif 'degree' in mapunits:
- if dist < 1:
- outunits = 'min'
- divisor = (1/60.0)
- else:
- outunits = 'deg'
- # format numbers in a nice way
- if (dist/divisor) >= 2500.0:
- outdist = round(dist/divisor)
- elif (dist/divisor) >= 1000.0:
- outdist = round(dist/divisor,1)
- elif (dist/divisor) > 0.0:
- outdist = round(dist/divisor,int(math.ceil(3-math.log10(dist/divisor))))
- else:
- outdist = float(dist/divisor)
- return (outdist, outunits)
- def Histogram(self, event):
- """
- Init histogram display canvas and tools
- """
- self.histogram = histogram.HistFrame(self,
- id=wx.ID_ANY, size=globalvar.HIST_WINDOW_SIZE,
- style=wx.DEFAULT_FRAME_STYLE)
- #show new display
- self.histogram.Show()
- self.histogram.Refresh()
- self.histogram.Update()
- def OnDecoration(self, event):
- """
- Decorations overlay menu
- """
- point = wx.GetMousePosition()
- decmenu = wx.Menu()
- # Add items to the menu
- AddScale = wx.MenuItem(decmenu, wx.ID_ANY, Icons["addbarscale"].GetLabel())
- AddScale.SetBitmap(Icons["addbarscale"].GetBitmap(self.iconsize))
- decmenu.AppendItem(AddScale)
- self.Bind(wx.EVT_MENU, self.OnAddBarscale, AddScale)
- AddLegend = wx.MenuItem(decmenu, wx.ID_ANY, Icons["addlegend"].GetLabel())
- AddLegend.SetBitmap(Icons["addlegend"].GetBitmap(self.iconsize))
- decmenu.AppendItem(AddLegend)
- self.Bind(wx.EVT_MENU, self.OnAddLegend, AddLegend)
- AddText = wx.MenuItem(decmenu, wx.ID_ANY, Icons["addtext"].GetLabel())
- AddText.SetBitmap(Icons["addtext"].GetBitmap(self.iconsize))
- decmenu.AppendItem(AddText)
- self.Bind(wx.EVT_MENU, self.OnAddText, AddText)
- # Popup the menu. If an item is selected then its handler
- # will be called before PopupMenu returns.
- self.PopupMenu(decmenu)
- decmenu.Destroy()
- def OnAddBarscale(self, event):
- """
- Handler for scale/arrow map decoration menu selection.
- """
- if self.dialogs['barscale']:
- return
- id = 0 # unique index for overlay layer
- # If location is latlon, only display north arrow (scale won't work)
- # proj = self.projinfo['proj']
- # if proj == 'll':
- # barcmd = 'd.barscale -n'
- # else:
- # barcmd = 'd.barscale'
- # decoration overlay control dialog
- self.dialogs['barscale'] = \
- gdialogs.DecorationDialog(parent=self, title=_('Scale and North arrow'),
- size=(350, 200),
- style=wx.DEFAULT_DIALOG_STYLE | wx.CENTRE,
- cmd=['d.barscale'],
- ovlId=id,
- name='barscale',
- checktxt = _("Show/hide scale and North arrow"),
- ctrltxt = _("scale object"))
- self.dialogs['barscale'].Show()
- def OnAddLegend(self, event):
- """
- Handler for legend map decoration menu selection.
- """
- if self.dialogs['legend']:
- return
-
- id = 1 # index for overlay layer in render
- cmd = ['d.legend']
- if self.tree.layer_selected and \
- self.tree.GetPyData(self.tree.layer_selected)[0]['type'] == 'raster':
- cmd.append('map=%s' % self.tree.GetPyData(self.tree.layer_selected)[0]['maplayer'].name)
- # Decoration overlay control dialog
- self.dialogs['legend'] = \
- gdialogs.DecorationDialog(parent=self, title=('Legend'),
- size=(350, 200),
- style=wx.DEFAULT_DIALOG_STYLE | wx.CENTRE,
- cmd=cmd,
- ovlId=id,
- name='legend',
- checktxt = _("Show/hide legend"),
- ctrltxt = _("legend object"))
- self.dialogs['legend'].Show()
- def OnAddText(self, event):
- """
- Handler for text decoration menu selection.
- """
- id = 2 # index for overlay layer in render
- # default values
- text = ''
- font = self.GetFont()
- color = wx.BLACK
- coords = [10, 10, 10, 10]
- rotation = 0.0
- # if self.MapWindow.currtxtid == None: # text doesn't already exist
- # id = wx.NewId() + 100
- # else: # text already exists
- # id = self.MapWindow.currtxtid
- # textcoords = self.ovlcoords[id]
- dlg = gdialogs.TextLayerDialog(parent=self, ovlId=id, title=_('Add text layer'),
- size=(400, 200))
- dlg.CenterOnScreen()
- # If OK button pressed in decoration control dialog
- if dlg.ShowModal() == wx.ID_OK:
- text = dlg.GetValues()[0]
- coords, w, h = self.MapWindow.TextBounds(dlg.GetValues(),
- coords)
- # delete object if it has no text
- if text == '':
- try:
- self.MapWindow.pdc.ClearId(id)
- self.MapWindow.pdc.RemoveId(id)
- del self.MapWindow.textdict[id]
- # del self.ovlcoords[id]
- except:
- pass
- return
- self.MapWindow.pdc.ClearId(id)
- self.MapWindow.pdc.SetId(id)
- self.MapWindow.textdict[id] = (text, font, color, rotation)
- self.MapWindow.Draw(self.MapWindow.pdc, img=self.MapWindow.textdict[id],
- drawid=id, pdctype='text', coords=coords)
- self.MapWindow.UpdateMap(render=False, renderVector=False)
- def GetOptData(self, dcmd, type, params, propwin):
- """
- Callback method for decoration overlay command generated by
- dialog created in menuform.py
- """
- # Reset comand and rendering options in render.Map. Always render decoration.
- # Showing/hiding handled by PseudoDC
- self.Map.ChangeOverlay(ovltype=type, type='overlay', name='', command=dcmd,
- l_active=True, l_render=False)
- self.params[type] = params
- self.propwin[type] = propwin
- def OnZoomMenu(self, event):
- """
- Zoom menu
- """
- point = wx.GetMousePosition()
- zoommenu = wx.Menu()
- # Add items to the menu
- zoommap = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to selected map'))
- zoommenu.AppendItem(zoommap)
- self.Bind(wx.EVT_MENU, self.MapWindow.OnZoomToMap, zoommap)
- zoomwind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to computational region (set with g.region)'))
- zoommenu.AppendItem(zoomwind)
- self.Bind(wx.EVT_MENU, self.MapWindow.ZoomToWind, zoomwind)
- zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to default region'))
- zoommenu.AppendItem(zoomdefault)
- self.Bind(wx.EVT_MENU, self.MapWindow.ZoomToDefault, zoomdefault)
- zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to saved region'))
- zoommenu.AppendItem(zoomsaved)
- self.Bind(wx.EVT_MENU, self.MapWindow.ZoomToSaved, zoomsaved)
- savewind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Set computational region from display'))
- zoommenu.AppendItem(savewind)
- self.Bind(wx.EVT_MENU, self.MapWindow.DisplayToWind, savewind)
- savezoom = wx.MenuItem(zoommenu, wx.ID_ANY, _('Save display geometry to named region'))
- zoommenu.AppendItem(savezoom)
- self.Bind(wx.EVT_MENU, self.MapWindow.SaveDisplayRegion, savezoom)
- # Popup the menu. If an item is selected then its handler
- # will be called before PopupMenu returns.
- self.PopupMenu(zoommenu)
- zoommenu.Destroy()
- def SetProperties(self, render=False, mode=0, showCompExtent=False):
- """Set properies of map display window"""
- self.autoRender.SetValue(render)
- self.toggleStatus.SetSelection(mode)
- self.StatusbarUpdate()
- self.showRegion.SetValue(showCompExtent)
- if showCompExtent:
- self.MapWindow.regionCoords = []
-
- # end of class MapFrame
- class MapApp(wx.App):
- """
- MapApp class
- """
- def OnInit(self):
- wx.InitAllImageHandlers()
- if __name__ == "__main__":
- Map = render.Map() # instance of Map class to render GRASS display output to PPM file
- else:
- Map = None
- self.mapFrm = MapFrame(parent=None, id=wx.ID_ANY, Map=Map, size=(640,480))
- #self.SetTopWindow(Map)
- self.mapFrm.Show()
- if __name__ == "__main__":
- # redraw map, if new command appears
- self.redraw = False
- status = Command(self, Map)
- status.start()
- self.timer = wx.PyTimer(self.watcher)
- # check each 0.1s
- self.timer.Start(100)
- return 1
- def OnExit(self):
- if __name__ == "__main__":
- # stop the timer
- self.timer.Stop()
- # terminate thread (a bit ugly)
- os.system("""echo "quit" >> %s""" % (cmdfilename))
- def watcher(self):
- """Redraw, if new layer appears"""
- if self.redraw:
- self.mapFrm.OnDraw(None)
- self.redraw = False
- return
- # end of class MapApp
- if __name__ == "__main__":
- ###### SET command variable
- if len(sys.argv) != 3:
- print __doc__
- sys.exit()
- title = sys.argv[1]
- cmdfilename = sys.argv[2]
- import gettext
- gettext.install("gm_map") # replace with the appropriate catalog name
- print "Starting monitor <%s>" % (title)
- gm_map = MapApp(0)
- # set title
- gm_map.mapFrm.SetTitle ("GRASS GIS - Map Display: " + title + " - Location: " + \
- grassenv.GetGRASSVariable("LOCATION_NAME"))
- gm_map.MainLoop()
- os.remove(cmdfilename)
- os.system("""g.gisenv set="GRASS_PYCMDFILE" """)
- print "Stoping monitor <%s>" % (title)
- sys.exit()
|