本文整理汇总了Python中qgis.core.QgsDistanceArea.setProjectionsEnabled方法的典型用法代码示例。如果您正苦于以下问题:Python QgsDistanceArea.setProjectionsEnabled方法的具体用法?Python QgsDistanceArea.setProjectionsEnabled怎么用?Python QgsDistanceArea.setProjectionsEnabled使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsDistanceArea
的用法示例。
在下文中一共展示了QgsDistanceArea.setProjectionsEnabled方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_scalebar
# 需要导入模块: from qgis.core import QgsDistanceArea [as 别名]
# 或者: from qgis.core.QgsDistanceArea import setProjectionsEnabled [as 别名]
def draw_scalebar(self, composer_map, top_offset):
"""Add a numeric scale to the bottom left of the map.
We draw the scale bar manually because QGIS does not yet support
rendering a scale bar for a geographic map in km.
.. seealso:: :meth:`drawNativeScaleBar`
:param composer_map: Composer map on which to draw the scalebar.
:type composer_map: QgsComposerMap
:param top_offset: Vertical offset at which the logo should be drawn.
:type top_offset: int
"""
LOGGER.debug('InaSAFE Map drawScaleBar called')
myCanvas = self.iface.mapCanvas()
myRenderer = myCanvas.mapRenderer()
#
# Add a linear map scale
#
myDistanceArea = QgsDistanceArea()
myDistanceArea.setSourceCrs(myRenderer.destinationCrs().srsid())
myDistanceArea.setProjectionsEnabled(True)
# Determine how wide our map is in km/m
# Starting point at BL corner
myComposerExtent = composer_map.extent()
myStartPoint = QgsPoint(myComposerExtent.xMinimum(),
myComposerExtent.yMinimum())
# Ending point at BR corner
myEndPoint = QgsPoint(myComposerExtent.xMaximum(),
myComposerExtent.yMinimum())
myGroundDistance = myDistanceArea.measureLine(myStartPoint, myEndPoint)
# Get the equivalent map distance per page mm
myMapWidth = self.mapWidth
# How far is 1mm on map on the ground in meters?
myMMToGroundDistance = myGroundDistance / myMapWidth
#print 'MM:', myMMDistance
# How long we want the scale bar to be in relation to the map
myScaleBarToMapRatio = 0.5
# How many divisions the scale bar should have
myTickCount = 5
myScaleBarWidthMM = myMapWidth * myScaleBarToMapRatio
myPrintSegmentWidthMM = myScaleBarWidthMM / myTickCount
# Segment width in real world (m)
# We apply some logic here so that segments are displayed in meters
# if each segment is less that 1000m otherwise km. Also the segment
# lengths are rounded down to human looking numbers e.g. 1km not 1.1km
myUnits = ''
myGroundSegmentWidth = myPrintSegmentWidthMM * myMMToGroundDistance
if myGroundSegmentWidth < 1000:
myUnits = 'm'
myGroundSegmentWidth = round(myGroundSegmentWidth)
# adjust the segment width now to account for rounding
myPrintSegmentWidthMM = myGroundSegmentWidth / myMMToGroundDistance
else:
myUnits = 'km'
# Segment with in real world (km)
myGroundSegmentWidth = round(myGroundSegmentWidth / 1000)
myPrintSegmentWidthMM = ((myGroundSegmentWidth * 1000) /
myMMToGroundDistance)
# Now adjust the scalebar width to account for rounding
myScaleBarWidthMM = myTickCount * myPrintSegmentWidthMM
#print "SBWMM:", myScaleBarWidthMM
#print "SWMM:", myPrintSegmentWidthMM
#print "SWM:", myGroundSegmentWidthM
#print "SWKM:", myGroundSegmentWidthKM
# start drawing in line segments
myScaleBarHeight = 5 # mm
myLineWidth = 0.3 # mm
myInsetDistance = 7 # how much to inset the scalebar into the map by
myScaleBarX = self.pageMargin + myInsetDistance
myScaleBarY = (
top_offset + self.mapHeight - myInsetDistance -
myScaleBarHeight) # mm
# Draw an outer background box - shamelessly hardcoded buffer
myRect = QgsComposerShape(myScaleBarX - 4, # left edge
myScaleBarY - 3, # top edge
myScaleBarWidthMM + 13, # right edge
myScaleBarHeight + 6, # bottom edge
self.composition)
myRect.setShapeType(QgsComposerShape.Rectangle)
myRect.setLineWidth(myLineWidth)
myRect.setFrame(False)
myBrush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
# workaround for missing setTransparentFill missing from python api
myRect.setBrush(myBrush)
self.composition.addItem(myRect)
# Set up the tick label font
myFontWeight = QtGui.QFont.Normal
myFontSize = 6
myItalicsFlag = False
myFont = QtGui.QFont('verdana',
myFontSize,
myFontWeight,
myItalicsFlag)
# Draw the bottom line
myUpshift = 0.3 # shift the bottom line up for better rendering
#.........这里部分代码省略.........