def label_change_cb(self, *args): if self.layer is None or self.updating: return font = self.label_font.entry.get_chars(0,-1) field_name = self.label_field.entry.get_chars(0,-1) color = self.label_color.current_color color = gvogrfs.gv_to_ogr_color( color ) ogrfs = self.layer.get_property('_gv_ogrfs_point') ogrfs_obj = gvogrfs.OGRFeatureStyle() try: ogrfs_obj.parse( ogrfs ) except: print 'an error occurred parsing the ogrfs property:\n', ogrfs #remove the old label ogrfs_obj.remove_part('LABEL') if field_name != 'disabled' and len(field_name) != 0: ogrfs_label = gvogrfs.OGRFeatureStylePart() ogrfs_label.parse( 'LABEL(t:{%s},f:"%s",c:%s)' % (field_name, font, color) ) ogrfs_obj.add_part( ogrfs_label ) self.layer.set_property( '_gv_ogrfs_point', ogrfs_obj.unparse() ) self.layer.display_change()
def label_change_cb(self, *args): if self.layer is None or self.updating: return font = self.label_font.get_font_name() field_name = self.label_field.get_active_text() text_value = '' if self.text_entry: text_value = self.text_entry.get_text() gv_color = self.label_color.current_color color = gvogrfs.gv_to_ogr_color(gv_color) x_off = '' y_off = '' if self.enable_offsets: #handle user editing of the values in the x and y offsets. #something odd happens when listening to the 'changed' signal #of the spin box, the text value is difference from the float #value, even if the values should match. This hack forces the #spin buttons to update the value and exits because this #will be called again right away x = self.x_offset.get_value() y = self.y_offset.get_value() sx = self.x_offset.get_text() sy = self.y_offset.get_text() try: if float(sx) != x: self.x_offset.set_value(float(sx)) return if float(sy) != y: self.y_offset.set_value(float(sy)) return except: return if 0.0 != x: x_off = 'dx:%s,' % x if 0.0 != y: y_off = 'dy:%s,' % -y if not field_name or field_name == "disabled": ogrfs = 'LABEL(%s%st:\"%s\",f:"%s",c:%s)' % (x_off, y_off, text_value, font, color) else: ogrfs = 'LABEL(%s%st:{%s},f:"%s",c:%s)' % (x_off, y_off, field_name, font, color) if ogrfs: self.ogrfs_obj = gvogrfs.OGRFeatureStylePart() self.ogrfs_obj.parse(ogrfs) else: self.ogrfs_obj = None self.gui_update() self.notif('ogrfs-changed')
def CreateNorthSymbol(ntype=GVNORTHSYM1,color1=(0.0,0.0,0.0,1.0), color2=(1.0,1.0,1.0,1.0),scale=1.0,symbol_manager=None): """ Create the North Symbol and put it in a symbol manager. Input: ntype- type of north arrow to create color1- first color color2- second color (if needed) scale- amount to scale size by. symbol_manager- symbol manager to inject symbol into (a new one will be created if this is set to None). """ if symbol_manager is None: sm=gview.GvSymbolManager() else: sm=symbol_manager cstr1=gvogrfs.gv_to_ogr_color(color1) if len(cstr1) < 9: cstr1=cstr1+"FF" cstr2=gvogrfs.gv_to_ogr_color(color2) if len(cstr2) < 9: cstr2=cstr2+"FF" sstr=string.replace(str(scale),'.','_') refname=ntype+cstr1[1:]+cstr2[1:]+sstr if ntype==GVNORTHSYM1: shape=gview.GvShape(type=gview.GVSHAPE_AREA) shape.set_node(1.0*scale,-2.6*scale,node=0) shape.set_node(0.0,-0.8*scale,node=1) shape.set_node(-1.0*scale,-2.6*scale,node=2) shape.set_node(0.0,2.6*scale,node=3) shape.set_node(1.0*scale,-2.6*scale,node=4) shape.set_property('_gv_ogrfs','PEN(c:'+cstr1+');BRUSH(c:'+\ cstr2+')') sm.inject_vector_symbol(refname,shape) return (refname,sm)
def __init__(self, interactive=False, default_ogrfs = None): gtk.Window.__init__(self) self.set_title('Label Edit Tool') gview.app.sel_manager.subscribe( 'active-layer-changed', self.gui_update ) gview.app.sel_manager.subscribe( 'selection-changed', self.gui_update ) gview.app.sel_manager.subscribe( 'subselection-changed', self.gui_update ) if default_ogrfs is None: font =pgufont.XLFDFontSpec() default_font = gview.get_preference('label-font') if default_font is None: font.set_font_part('Family', 'Sans') font.set_font_part('Size', 12) else: font.parse_font_spec(default_font) color = gview.get_preference('label-color') if color is None: color = "#88FF88" else: try: color = color.replace("(", "") color = color.replace(")", "") r, g, b, a = color.split(",") r = float(r) g = float(g) b = float(b) a = float(a) color = ( r, g, b, a ) color = gvogrfs.gv_to_ogr_color( color ) except: color = "#88FF88" default_ogrfs = 'LABEL(t:"",f:"%s",c:%s)' % (font, color) self.default_ogrfs = default_ogrfs self.selected_shape = None self.layer = None self.interactive = interactive self.create_gui() self.show() self.edit_mode = 0 self.view = gview.app.view_manager.get_active_view() self.key_sig = self.view.connect('key-press-event', self.key_press_cb) self.connect('delete-event', self.close) self.gui_update()
def SimpleNorthLayer(xoffset,yoffset,ntype=GVNORTHSYM1, color1=(0.0,0.0,0.0,1.0), color2=(1.0,1.0,1.0,1.0), scale=1.0, shapes_name="North Arrow"): """ Create a layer with a North arrow symbol, with the North arrow located at (xoffset,yoffset). The 'ntype' parameter will eventually be used for different types of north arrows. The layer will contain two shapes: an area or line, and label. Input: xoffset,yoffset- where to center North Arrow (in display coordinates). ntype- index of north arrow type (currently only one type). color1- First color and outline color for north arrow. A tuple of 4 values between 0 and 1. color2- Second color (not used yet). scale- amount to scale size of symbol by. shapes_name- name to give the shapes that form the north arrow. """ shps=gview.GvShapes(name=shapes_name) gview.undo_register( shps ) nshp=gview.GvShape(type=gview.GVSHAPE_POINT) nshp.set_node(xoffset,yoffset) cstr1=gvogrfs.gv_to_ogr_color(color1) if len(cstr1) < 9: cstr1=cstr1+"FF" refname,sm=CreateNorthSymbol(ntype,color1,color2,scale) dxstr=str(-1.5*scale) dystr=str(-15.0*scale) nshp.set_property('_gv_ogrfs', 'SYMBOL(c:'+cstr1+',s:4,id:"'+refname+'");'+\ 'LABEL(c:'+cstr1+',t:"N",dx:'+dxstr+\ ',dy:'+dystr+')' ) shps.append(nshp) layer=gview.GvShapesLayer(shps) # Set antialias property so that lines look nice # when rotated. layer.set_property('_gl_antialias','1') return layer
def symbol_cb( self, widget, *args ): """ update the symbol. This might have been called because the color changed also, so update the _point_color property too. """ if self.layer is None or self.updating: return symbol = self.point_symbol.entry.get_text() ogrfs = self.layer.get_property('_gv_ogrfs_point') ogrfs_obj = gvogrfs.OGRFeatureStyle() try: ogrfs_obj.parse( ogrfs ) except: print 'an error occurred parsing the ogrfs property:\n', ogrfs #remove the old symbol ogrfs_obj.remove_part('SYMBOL') point_sym_text = '"ogr-sym-%s"' % symbols.index(symbol) color = self.point_color.current_color #should this only be done on point layers? point_ogr_color = gvogrfs.gv_to_ogr_color(color) point_size = self.point_size.entry.get_text() ogr_part = 'SYMBOL(c:' + point_ogr_color + ',id:' + \ point_sym_text + ')' ogrfs_sym = gvogrfs.OGRFeatureStylePart() ogrfs_sym.parse( ogr_part ) ogrfs_obj.add_part( ogrfs_sym ) self.layer.set_property('_gv_ogrfs_point', ogrfs_obj.unparse()) prop = str(color[0]) + ' ' + str(color[1]) + \ ' ' + str(color[2]) + ' ' + str(color[3]) self.layer.set_property( '_point_color', prop ) self.layer.display_change()
def prepare_legend(self): if self.resizing: return class_cn = self.layer.get_property('Class_sn') if class_cn: self.Class_sn = int(class_cn) #remove any existing shapes self.shapes.delete_shapes(range(len(self.shapes))) # get preferences samp_x_size = pref('legend-sample-x-size', 20) samp_y_size = pref('legend-sample-y-size', 20) title_font = pref('legend-title-font','Serif 20') title_font_color = pref('legend-title-font-color', black) title_ogr_color = gv_to_ogr_color(color_string_to_tuple(title_font_color)) label_font = pref('legend-label-font','Serif 14') label_font_color = pref('legend-label-font-color', black) label_ogr_color = gv_to_ogr_color(color_string_to_tuple(label_font_color)) # utility layout for measuring text w/h layout = pango.Layout(self.viewarea.get_pango_context()) try: font = pango.FontDescription(title_font) except: font = pango.FontDescription('Serif 20') layout.set_font_description(font) #handle multi-line text for the title. lines = self.cls.title.split('\\n') layout.set_text(self.cls.title.replace('\\n', '\n')) title_width, title_height = layout.get_pixel_size() h = title_height/len(lines) x_offset = 10 #start title 10 pixels in from left edge col_offset = 30 #space columns apart y_offset = 35 #start title 35 pixels down from top edge max_height = 0 title_width += x_offset + 10 title_height += y_offset + 10 #handle large fonts in the sample text try: font = pango.FontDescription(label_font) except: # get a default font if preferred one # can't be loaded. font = pango.FontDescription('Serif 14') layout.set_font_description(font) layout.set_text(self.cls.name[0]) w,h = layout.get_pixel_size() samp_offset = max(samp_y_size, h) + 10 cols = int(self.cls.count / 8) samps = min(8, self.cls.count) samp_height = samps * (samp_offset) + 10 samp_width = x_offset for i in range(cols + 1): idx = 8 * i col_width = 0 while idx < self.cls.count and idx < 8 * (i + 1): name = self.cls.name[idx] layout.set_text(name) width = samp_x_size + 20 + layout.get_pixel_size()[0] col_width = max(col_width, width) idx += 1 samp_width += (col_width + col_offset) samp_width += 10 total_width = max(title_width, samp_width) total_height = title_height + samp_height #resize the window appropriately self.resizing = True x,y,width,height,bpp = self.window.get_geometry() if width < total_width or height < total_height: self.resize_count += 1 if self.resize_count < 2: self.set_size_request(total_width, total_height) self.resizing = False for line in lines: samp_text = gview.GvShape() samp_text.add_node(x_offset, y_offset) ogrfs = 'LABEL(c:%s,f:"%s",t:"%s")' % (title_ogr_color, title_font, line) samp_text.set_property('_gv_ogrfs', ogrfs) self.shapes.append(samp_text) y_offset += h txt = _('Legend') line = lines[0] if len(line) > 6 and line[:6] != txt and line[:6] != 'legend': self.set_title( '%s: %s...' % (txt, line) ) else: self.set_title(line + '...') y_offset += 10 title_offset = y_offset max_width = 0 max_height = 0 for class_id in range(self.cls.count): color = self.cls.get_color(class_id) symbol = self.cls.get_symbol(class_id) scale = self.cls.get_scale(class_id) name = self.cls.name[class_id] if symbol: samp = gview.GvShape(type=gview.GVSHAPE_POINT) samp.set_node(x_offset+samp_x_size/2, y_offset+samp_y_size/2) ogrfs_color = gv_to_ogr_color(color) ogrfs = 'SYMBOL(id:%s,c:%s,s:%s)' % (symbol, ogrfs_color, scale/2) samp.set_property('_gv_ogrfs', ogrfs) else: samp = gview.GvShape(type=gview.GVSHAPE_AREA) samp.add_node(x_offset, y_offset) samp.add_node(x_offset+samp_x_size, y_offset) samp.add_node(x_offset+samp_x_size, y_offset+samp_y_size) samp.add_node(x_offset, y_offset+samp_y_size) samp.add_node(x_offset, y_offset) color = '%f %f %f %f' % color samp.set_property('_gv_color', color) samp.set_property('_gv_fill_color', color) self.shapes.append(samp) samp_text = gview.GvShape() samp_text.add_node(x_offset+samp_x_size+10, y_offset+17) ogrfs = 'LABEL(c:%s,f:%s,t:%s)' % (label_ogr_color, label_font, name) samp_text.set_property('_gv_ogrfs', ogrfs) self.shapes.append(samp_text) layout.set_text(name) this_width = samp_x_size + 20 + layout.get_pixel_size()[0] if max_width < this_width: max_width = this_width y_offset += samp_offset if y_offset+samp_offset > self.viewarea.get_height(): max_height = max(max_height, y_offset + samp_offset) y_offset = title_offset x_offset += (col_offset + max_width) max_width = 0 self.vlayer.display_change()
def SimpleReferenceGrid(min_x,min_y,max_x,max_y,x_divisions,y_divisions, color=(0.5,1.0,0.5,1.0),xoff=-0.15,yoff=-0.04, label_type=None,shapes_name="Grid"): """ Create a reference grid for an unprojected raster. min_x, min_y- minimum lat/longs, in decimal degrees max_x, max_y- minimum lat/longs, in decimal degrees x_divisions- number of divisions in horizontal direction y_divisions- number of divisions in vertical direction xoff- horizontal offset of vertical labels, as a fraction of max_x-min_x. Offset is relative to min_x. yoff- vertical offset of horizontal labels, as a fraction of max_y-min_y. Offset is relative to min_y. color- start color for the grid label_type- not used yet; might be later for formatting. """ shps=gview.GvShapes(name=shapes_name) gview.undo_register( shps ) shps.add_field('position','string',20) if os.name == 'nt': font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" else: #font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" #font="-urw-helvetica-medium-r-normal-*-9-*-*-*-p-*-iso8859-2" font="-adobe-helvetica-medium-r-normal-*-8-*-*-*-p-*-iso10646-1" #font="-misc-fixed-medium-r-*-*-9-*-*-*-*-*-*-*" lxoff=(max_x-min_x)*xoff # horizontal label placement lyoff=(max_y-min_y)*yoff # vertical label placement hspc=(max_x-min_x)/x_divisions vspc=(max_y-min_y)/y_divisions for hval in Numeric.arange(min_x,max_x+hspc/100.0,hspc): nshp=gview.GvShape(type=gview.GVSHAPE_LINE) nshp.set_node(hval,max_y,0,0) nshp.set_node(hval,min_y,0,1) shps.append(nshp) pshp=gview.GvShape(type=gview.GVSHAPE_POINT) pshp.set_node(hval,min_y+lyoff) pshp.set_property('position',"%.1f" % hval) shps.append(pshp) for vval in Numeric.arange(min_y,max_y+vspc/100.0,vspc): nshp=gview.GvShape(type=gview.GVSHAPE_LINE) nshp.set_node(min_x,vval,0,0) nshp.set_node(max_x,vval,0,1) shps.append(nshp) pshp=gview.GvShape(type=gview.GVSHAPE_POINT) pshp.set_node(min_x+lxoff,vval) pshp.set_property('position',"%.1f" % vval) shps.append(pshp) cstr=gvogrfs.gv_to_ogr_color(color) if len(cstr) < 9: cstr=cstr+"FF" clstr=str(color[0])+' '+str(color[1])+' '+str(color[2])+' '+str(color[3]) layer=gview.GvShapesLayer(shps) layer.set_property('_line_color',clstr) layer.set_property('_point_color',clstr) # Set antialias property so that lines look nice # when rotated. layer.set_property('_gl_antialias','1') layer.set_property('_gv_ogrfs_point', 'LABEL(t:{position},f:"'+font+'",c:'+cstr+')') layer.set_read_only(gtk.TRUE) return layer
def SimpleScalebarLayer(xoffset,yoffset,dwidth,swidth, angle,units_label=None,stype=GVSCALE1, color1=(0.0,0.0,0.0,1.0), color2=(1.0,1.0,1.0,1.0), offset=-0.2, shapes_name="Scale Bar"): """ Create a layer with a Scale bar located at stretching from dmin to dmax on the display Input: xoffset,yoffset- where to center scale bar (in display coordinates) dwidth- width of scale bar in display coordinates swidth- width of scale bar in scale coordinates (same if scale units and geocoding units are the same; different if for example display is UTM- meters- and scale bar is in km) angle- angle of scale bar relative to display (in RADIANS) units_label- label for units (left out if None) stype- index of scale bar type (currently must be 0) color1- First color and outline color for scale bar. A tuple of 4 values between 0 and 1. color2- Second color (only used in alternating scale bars) offset- Vertical offset of labels for scale bar as a fraction of scale bar width. shapes_name- name to give the shapes that form the scalebar. """ shps=gview.GvShapes(name=shapes_name) gview.undo_register( shps ) shps.add_field('label','string',20) if os.name == 'nt': font="-adobe-helvetica-medium-r-*-*-15-*-*-*-*-*-*-*" else: #font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" #font="-urw-helvetica-medium-r-normal-*-9-*-*-*-p-*-iso8859-2" font="-adobe-helvetica-medium-r-normal-*-8-*-*-*-p-*-iso10646-1" #font="-misc-fixed-medium-r-*-*-9-*-*-*-*-*-*-*" sc=dwidth/swidth svals,labels=GetScaleBlocks(swidth) cstr1=str(color1[0])+' '+str(color1[1])+' '+str(color1[2])+\ ' '+str(color1[3]) cstr2=str(color2[0])+' '+str(color2[1])+' '+str(color2[2])+\ ' '+str(color2[3]) if stype == GVSCALE1: # Rectangle with alternating filled/unfilled # sections. smax=svals[len(svals)-1] # rectangle nodes before rotation hbr=(svals-(smax/2.0))*sc # horizontal- shift to -smax/2:+smax/2 # so that rectangle is centered about 0. # rectangle extends from -smax/20 (bbr) to +smax/20 (tbr) vertically, # labels are placed at + or - smax/5 tbr=smax/20.0*Numeric.ones(Numeric.shape(hbr)) bbr=-1*smax/20.0*Numeric.ones(Numeric.shape(hbr)) lbr=offset*smax*Numeric.ones(Numeric.shape(hbr)) # units label location before rotation uxbr=(hbr[len(hbr)-1]-hbr[0])*0.05+hbr[len(hbr)-1] # rotate ctheta=Numeric.cos(angle) stheta=Numeric.sin(angle) tx=hbr*ctheta-tbr*stheta + xoffset ty=hbr*stheta+tbr*ctheta + yoffset bx=hbr*ctheta-bbr*stheta + xoffset by=hbr*stheta+bbr*ctheta + yoffset lx=hbr*ctheta-lbr*stheta + xoffset ly=hbr*stheta+lbr*ctheta + yoffset ux=uxbr*ctheta + xoffset uy=uxbr*stheta + yoffset # LATER: once shape collections are working, use them instead # so that entire scale bar can be selected and shifted as # a whole rather than separate shapes... #shp=gview.GvShape(type=gview.GVSHAPE_COLLECTION) #shp.set_property('_gv_ogrfs_point', # 'LABEL(t:{label},f:"%s",c:#000000FF)' % font) #shps.append(shp) for idx in range(len(tx)-1): nshp=gview.GvShape(type=gview.GVSHAPE_AREA) nshp.add_node(tx[idx],ty[idx],0) nshp.add_node(tx[idx+1],ty[idx+1],0) nshp.add_node(bx[idx+1],by[idx+1],0) nshp.add_node(bx[idx],by[idx],0) nshp.add_node(tx[idx],ty[idx],0) if idx % 2: nshp.set_property('_gv_color',cstr1) nshp.set_property('_gv_fill_color',cstr2) else: nshp.set_property('_gv_color',cstr1) nshp.set_property('_gv_fill_color',cstr1) shps.append(nshp) for idx in range(len(lx)): if labels[idx] is not None: lshp=gview.GvShape(type=gview.GVSHAPE_POINT) lshp.set_node(lx[idx],ly[idx]) lshp.set_property('label',labels[idx]) shps.append(lshp) if units_label is not None: lshp=gview.GvShape(type=gview.GVSHAPE_POINT) lshp.set_node(ux,uy) lshp.set_property('label',units_label) shps.append(lshp) layer=gview.GvShapesLayer(shps) # Set antialias property so that lines look nice # when rotated. layer.set_property('_gl_antialias','1') cstr3=gvogrfs.gv_to_ogr_color(color1) if len(cstr3) < 9: cstr3=cstr3+"FF" layer.set_property('_gv_ogrfs_point', 'LABEL(t:{label},f:"%s",c:%s)' % (font,cstr3)) return layer
def SimpleLatLongGrid(min_x,min_y,max_x,max_y,hdeg,hmin,hsec,vdeg,vmin,vsec, color=(0.5,1.0,0.5,1.0),xoff=-0.18,yoff=1.04, label_type=None,shapes_name="Grid"): """ Create a reference graticule. min_x, min_y- minimum lat/longs, in decimal degrees max_x, max_y- minimum lat/longs, in decimal degrees hdeg/hmin/hsec- horizontal spacing (degrees/min/sec) vdeg/vmin/vsec- vertical spacing (degrees/min/sec) decimal degrees=degrees+(minutes/60.0)+(seconds/3600.0) xoff- horizontal offset of vertical labels, as a fraction of max_x-min_x. Offset is relative to min_x. yoff- vertical offset of horizontal labels, as a fraction of max_y-min_y. Offset is relative to min_y. color- start color for the grid label_type- not used yet; might be later for formatting. extents may be shifted slightly to generate 'nice' labels. Note that the min_x, min_y, max_x, max_y extents include a border 5% in from each side, and room for labels. """ shps=gview.GvShapes(name=shapes_name) gview.undo_register( shps ) shps.add_field('position','string',20) if os.name == 'nt': font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" else: #font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" #font="-urw-helvetica-medium-r-normal-*-9-*-*-*-p-*-iso8859-2" font="-adobe-helvetica-medium-r-normal-*-8-*-*-*-p-*-iso10646-1" #font="-misc-fixed-medium-r-*-*-9-*-*-*-*-*-*-*" x_spacing=float(hdeg)+(float(hmin)+(float(hsec)/60.0))/60.0 y_spacing=float(vdeg)+(float(vmin)+(float(vsec)/60.0))/60.0 # Round to nearest integer space max_x=min_x+Numeric.floor((max_x-min_x)/x_spacing)*x_spacing max_y=min_y+Numeric.floor((max_y-min_y)/y_spacing)*y_spacing lxoff=(max_x-min_x)*xoff # horizontal label placement lyoff=(max_y-min_y)*yoff # vertical label placement for hval in Numeric.arange(min_x, max_x+x_spacing/100.0, x_spacing): nshp=gview.GvShape(type=gview.GVSHAPE_LINE) nshp.set_node(hval,max_y,0,0) nshp.set_node(hval,min_y,0,1) shps.append(nshp) pshp=gview.GvShape(type=gview.GVSHAPE_POINT) pshp.set_node(hval,min_y+lyoff) hstr=GetLatLongString(hval,'longitude') pshp.set_property('position',hstr) shps.append(pshp) for vval in Numeric.arange(min_y, max_y+y_spacing/100.0, y_spacing): nshp=gview.GvShape(type=gview.GVSHAPE_LINE) nshp.set_node(min_x,vval,0,0) nshp.set_node(max_x,vval,0,1) shps.append(nshp) pshp=gview.GvShape(type=gview.GVSHAPE_POINT) pshp.set_node(min_x+lxoff,vval) vstr=GetLatLongString(vval,'latitude') pshp.set_property('position',vstr) shps.append(pshp) cstr=gvogrfs.gv_to_ogr_color(color) if len(cstr) < 9: cstr=cstr+"FF" clstr=str(color[0])+' '+str(color[1])+' '+str(color[2])+' '+str(color[3]) layer=gview.GvShapesLayer(shps) layer.set_property('_line_color',clstr) layer.set_property('_point_color',clstr) # Set antialias property so that lines look nice # when rotated. layer.set_property('_gl_antialias','1') layer.set_property('_gv_ogrfs_point', 'LABEL(t:{position},f:"'+font+'",c:'+cstr+')') layer.set_read_only(gtk.TRUE) return layer
def SimpleMeasuredGrid(min_x,min_y,max_x,max_y,x_spacing,y_spacing, color=(0.5,1.0,0.5,1.0),xoff=-0.14,yoff=1.04, label_type=None,shapes_name="Grid"): """ Create a reference grid for a utm-projected raster. min_x, min_y, max_x, max_y- extents that grid should cover x_spacing- line spacing in horizontal direction y_spacing- line spacing in vertical direction xoff- horizontal offset of vertical labels, as a fraction of max_x-min_x. Offset is relative to min_x. yoff- vertical offset of horizontal labels, as a fraction of max_y-min_y. Offset is relative to min_y. color- start color for the grid label_type- not used yet; might be later for formatting. shapes_name- name to give the shapes forming the layer. """ shps=gview.GvShapes(name=shapes_name) gview.undo_register( shps ) shps.add_field('position','string',20) if os.name == 'nt': font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" else: #font="-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" #font="-urw-helvetica-medium-r-normal-*-9-*-*-*-p-*-iso8859-2" font="-adobe-helvetica-medium-r-normal-*-8-*-*-*-p-*-iso10646-1" #font="-misc-fixed-medium-r-*-*-9-*-*-*-*-*-*-*" # Round to nearest integer space max_x=min_x+Numeric.floor((max_x-min_x)/x_spacing)*x_spacing max_y=min_y+Numeric.floor((max_y-min_y)/y_spacing)*y_spacing lxoff=(max_x-min_x)*xoff # horizontal label placement lyoff=(max_y-min_y)*yoff # vertical label placement for hval in Numeric.arange(min_x, max_x+x_spacing/100.0, x_spacing): nshp=gview.GvShape(type=gview.GVSHAPE_LINE) nshp.set_node(hval,max_y,0,0) nshp.set_node(hval,min_y,0,1) shps.append(nshp) pshp=gview.GvShape(type=gview.GVSHAPE_POINT) pshp.set_node(hval,min_y+lyoff) pshp.set_property('position',"%d" % int(hval+0.5)) shps.append(pshp) for vval in Numeric.arange(min_y, max_y+y_spacing/100.0, y_spacing): nshp=gview.GvShape(type=gview.GVSHAPE_LINE) nshp.set_node(min_x,vval,0,0) nshp.set_node(max_x,vval,0,1) shps.append(nshp) pshp=gview.GvShape(type=gview.GVSHAPE_POINT) pshp.set_node(min_x+lxoff,vval) pshp.set_property('position',"%d" % int(vval+0.5)) shps.append(pshp) cstr=gvogrfs.gv_to_ogr_color(color) if len(cstr) < 9: cstr=cstr+"FF" clstr=str(color[0])+' '+str(color[1])+' '+str(color[2])+' '+str(color[3]) layer=gview.GvShapesLayer(shps) layer.set_property('_line_color',clstr) layer.set_property('_point_color',clstr) # Set antialias property so that lines look nice # when rotated. layer.set_property('_gl_antialias','1') layer.set_property('_gv_ogrfs_point', 'LABEL(t:{position},f:"'+font+'",c:'+cstr+')') layer.set_read_only(gtk.TRUE) return layer
def prepare_legend(self): if self.resizing: return if self.layer.get_property('Class_sn') is not None: self.Class_sn = int(self.layer.get_property('Class_sn')) #remove any existing shapes self.shapes.delete_shapes(range(len(self.shapes))) samp_x_size = gview.get_preference('legend-sample-x-size') if samp_x_size is None: samp_x_size = 20 else: samp_x_size = int(samp_x_size) samp_y_size = gview.get_preference('legend-sample-y-size') if samp_y_size is None: samp_y_size = 20 else: samp_y_size = int(samp_y_size) title_font = pgufont.XLFDFontSpec() font_spec = gview.get_preference('legend-title-font') if font_spec is None: title_font.set_font_part('Family', 'times') title_font.set_font_part('Pixel Size', '20') else: title_font.parse_font_spec(font_spec) title_font_color = gview.get_preference('legend-title-font-color') if title_font_color is None: title_font_color = (0.0, 0.0, 0.0, 1.0) else: title_font_color = color_string_to_tuple(title_font_color) title_ogr_color = gv_to_ogr_color(title_font_color) label_font = pgufont.XLFDFontSpec() font_spec = gview.get_preference('legend-label-font') if font_spec is None: label_font.set_font_part('Family', 'times') label_font.set_font_part('Pixel Size', '14') else: label_font.parse_font_spec(font_spec) label_font_color = gview.get_preference('legend-label-font-color') if label_font_color is None: label_font_color = (0.0, 0.0, 0.0, 1.0) else: label_font_color = color_string_to_tuple(label_font_color) label_ogr_color = gv_to_ogr_color(label_font_color) #handle large fonts in the sample text try: gdk_font = load_font(str(label_font)) except: # get a default font if preferred one # can't be loaded. gdk_font = load_font('*') h = gdk_font.height("Wj") samp_offset = max(samp_y_size, h) + 10 #handle multi-line text for the title. try: gdk_title_font = load_font(str(title_font)) except: gdk_title_font = load_font('*') lines = string.split(self.classification.title, '\\n') x_offset = 10 #start title 10 pixels in from left edge col_offset = 30 #space columns apart y_offset = 35 #start title 35 pixels down from top edge title_width = 0 max_height = 0 #resize the window appropriately title_height = y_offset title_width = 0 for idx in range(len(lines)): line = lines[idx] title_height = title_height + gdk_title_font.height(line) title_width = max(title_width, gdk_title_font.width(line)) title_height = title_height + 10 title_width = x_offset + title_width + 10 cols = int(self.classification.count / 8) samps = min(8, self.classification.count) samp_height = samps * (samp_offset) + 10 samp_width = x_offset for i in range(cols + 1): idx = 8 * i col_width = 0 while idx < self.classification.count and idx < 8 * (i + 1): name = self.classification.name[idx] width = samp_x_size + 20 + gdk_font.width(name) col_width = max(col_width, width) idx = idx + 1 samp_width = samp_width + col_width + col_offset samp_width = samp_width + 10 total_width = max(title_width, samp_width) total_height = title_height + samp_height self.resizing = TRUE if (self.get_window().width < total_width) or \ (self.get_window().height < total_height): self.resize_count = self.resize_count + 1 if self.resize_count < 2: self.set_usize(total_width, total_height) self.resizing = FALSE for idx in range(len(lines)): line = lines[idx] w = gdk_title_font.width(line) h = gdk_title_font.height(line) title_width = max(title_width, w) samp_text = gview.GvShape() samp_text.add_node(x_offset, y_offset) samp_text.set_property( '_gv_ogrfs', 'LABEL(c:' + title_ogr_color + \ ',f:"' + str(title_font) + '",' \ + 't:"' + line + '")' ) self.shapes.append(samp_text) y_offset = y_offset + h if ((len(lines[0]) > 6) and (lines[0][:6] != 'Legend') and (lines[0][:6] != 'legend')): self.set_title('Legend: ' + lines[0] + '...') else: self.set_title(lines[0] + '...') y_offset = y_offset + 10 title_offset = y_offset max_width = 0 max_height = 0 for class_id in range(self.classification.count): color = self.classification.get_color(class_id) symbol = self.classification.get_symbol(class_id) scale = self.classification.get_scale(class_id) if symbol is not None: samp = gview.GvShape(type=gview.GVSHAPE_POINT) samp.add_node(x_offset + (samp_x_size / 2), y_offset + (samp_y_size / 2)) ogrfs_color = '#%02x%02x%02x%02x' % ( int(color[0] * 255.999), int(color[1] * 255.999), int(color[2] * 255.999), int(color[3] * 255.999)) ogrfs = "SYMBOL(id:%s,c:%s,s:%s)" % (symbol, ogrfs_color, scale) samp.set_property("_gv_ogrfs", ogrfs) else: samp = gview.GvShape(type=gview.GVSHAPE_AREA) samp.add_node(x_offset, y_offset) samp.add_node(x_offset + samp_x_size, y_offset) samp.add_node(x_offset + samp_x_size, y_offset + samp_y_size) samp.add_node(x_offset, y_offset + samp_y_size) samp.add_node(x_offset, y_offset) color = '%f %f %f %f' % color samp.set_property('_gv_color', color) samp.set_property('_gv_fill_color', color) self.shapes.append(samp) name = self.classification.name[class_id] samp_text = gview.GvShape() samp_text.add_node(x_offset + samp_x_size + 10, y_offset + 17) font = str(label_font) samp_text.set_property( '_gv_ogrfs', 'LABEL(c:' + label_ogr_color + \ ',f:"'+font+'",t:"'+name+'")' ) self.shapes.append(samp_text) this_width = samp_x_size + 20 + gdk_font.width(name) if max_width < this_width: max_width = this_width y_offset = y_offset + samp_offset if y_offset + samp_offset > self.viewarea.get_height(): max_height = max(max_height, y_offset + samp_offset) y_offset = title_offset x_offset = x_offset + col_offset + max_width max_width = 0 self.vlayer.changed()
def prepare_legend(self): if self.resizing: return if self.layer.get_property('Class_sn') is not None: self.Class_sn = int(self.layer.get_property('Class_sn')) #remove any existing shapes self.shapes.delete_shapes(range(len(self.shapes))) samp_x_size = gview.get_preference('legend-sample-x-size') if samp_x_size is None: samp_x_size = 20 else: samp_x_size = int(samp_x_size) samp_y_size = gview.get_preference('legend-sample-y-size') if samp_y_size is None: samp_y_size = 20 else: samp_y_size = int(samp_y_size) title_font = pgufont.XLFDFontSpec() font_spec = gview.get_preference('legend-title-font') if font_spec is None: title_font.set_font_part('Family', 'times') title_font.set_font_part('Pixel Size', '20') else: title_font.parse_font_spec(font_spec) title_font_color = gview.get_preference('legend-title-font-color') if title_font_color is None: title_font_color = (0.0, 0.0, 0.0, 1.0) else: title_font_color = color_string_to_tuple(title_font_color) title_ogr_color = gv_to_ogr_color(title_font_color) label_font = pgufont.XLFDFontSpec() font_spec = gview.get_preference('legend-label-font') if font_spec is None: label_font.set_font_part('Family', 'times') label_font.set_font_part('Pixel Size', '14') else: label_font.parse_font_spec(font_spec) label_font_color = gview.get_preference('legend-label-font-color') if label_font_color is None: label_font_color = (0.0, 0.0, 0.0, 1.0) else: label_font_color = color_string_to_tuple(label_font_color) label_ogr_color = gv_to_ogr_color(label_font_color) #handle large fonts in the sample text try: gdk_font = load_font(str(label_font)) except: # get a default font if preferred one # can't be loaded. gdk_font = load_font('*') h = gdk_font.height("Wj") samp_offset = max(samp_y_size, h) + 10 #handle multi-line text for the title. try: gdk_title_font = load_font(str(title_font)) except: gdk_title_font = load_font('*') lines = string.split(self.classification.title, '\\n') x_offset = 10 #start title 10 pixels in from left edge col_offset = 30 #space columns apart y_offset = 35 #start title 35 pixels down from top edge title_width = 0 max_height = 0 #resize the window appropriately title_height = y_offset title_width = 0 for idx in range(len(lines)): line = lines[idx] title_height = title_height + gdk_title_font.height(line) title_width = max(title_width, gdk_title_font.width(line)) title_height = title_height + 10 title_width = x_offset + title_width + 10 cols = int (self.classification.count / 8) samps = min( 8, self.classification.count ) samp_height = samps * (samp_offset) + 10 samp_width = x_offset for i in range( cols + 1): idx = 8 * i col_width = 0 while idx < self.classification.count and idx < 8 * (i + 1): name = self.classification.name[idx] width = samp_x_size + 20 + gdk_font.width(name) col_width = max(col_width, width) idx = idx + 1 samp_width = samp_width + col_width + col_offset samp_width = samp_width + 10 total_width = max(title_width, samp_width) total_height = title_height + samp_height self.resizing = TRUE if (self.get_window().width < total_width) or \ (self.get_window().height < total_height): self.resize_count = self.resize_count + 1 if self.resize_count < 2: self.set_usize(total_width, total_height) self.resizing = FALSE for idx in range(len(lines)): line = lines[idx] w = gdk_title_font.width(line) h = gdk_title_font.height(line) title_width = max(title_width, w) samp_text = gview.GvShape() samp_text.add_node( x_offset, y_offset ) samp_text.set_property( '_gv_ogrfs', 'LABEL(c:' + title_ogr_color + \ ',f:"' + str(title_font) + '",' \ + 't:"' + line + '")' ) self.shapes.append(samp_text) y_offset = y_offset + h if ((len(lines[0]) > 6) and (lines[0][:6] != 'Legend') and (lines[0][:6] != 'legend')): self.set_title('Legend: ' + lines[0] + '...') else: self.set_title(lines[0] + '...') y_offset = y_offset + 10 title_offset = y_offset max_width = 0 max_height = 0 for class_id in range(self.classification.count): color = self.classification.get_color( class_id ) symbol = self.classification.get_symbol( class_id ) scale = self.classification.get_scale( class_id ) if symbol is not None: samp = gview.GvShape( type = gview.GVSHAPE_POINT ) samp.add_node( x_offset + (samp_x_size/2), y_offset + (samp_y_size/2) ) ogrfs_color = '#%02x%02x%02x%02x' % (int(color[0] * 255.999), int(color[1] * 255.999), int(color[2] * 255.999), int(color[3] * 255.999)) ogrfs = "SYMBOL(id:%s,c:%s,s:%s)" % (symbol, ogrfs_color, scale) samp.set_property( "_gv_ogrfs", ogrfs ) else: samp = gview.GvShape( type = gview.GVSHAPE_AREA ) samp.add_node( x_offset, y_offset ) samp.add_node( x_offset+samp_x_size, y_offset ) samp.add_node( x_offset+samp_x_size, y_offset+samp_y_size ) samp.add_node( x_offset, y_offset+samp_y_size ) samp.add_node( x_offset, y_offset ) color = '%f %f %f %f' % color samp.set_property( '_gv_color', color ) samp.set_property( '_gv_fill_color', color ) self.shapes.append( samp ) name = self.classification.name[class_id] samp_text = gview.GvShape() samp_text.add_node( x_offset+samp_x_size+10, y_offset + 17 ) font = str(label_font) samp_text.set_property( '_gv_ogrfs', 'LABEL(c:' + label_ogr_color + \ ',f:"'+font+'",t:"'+name+'")' ) self.shapes.append( samp_text ) this_width = samp_x_size + 20 + gdk_font.width(name) if max_width < this_width: max_width = this_width y_offset = y_offset + samp_offset if y_offset+samp_offset > self.viewarea.get_height(): max_height = max(max_height, y_offset + samp_offset) y_offset = title_offset x_offset = x_offset + col_offset + max_width max_width = 0 self.vlayer.changed()
def color_change(self, widget, *args): gv_color = widget.get_color() parm = gvogrfs.OGRFeatureStyleParam() parm.parse('c:%s' % gvogrfs.gv_to_ogr_color(gv_color)) self.ogrfs_obj.set_parm(parm) self.notif('ogrfs-changed')