Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import os.path
  2. import sys
  3. #setting the environment variables
  4. os.environ['QGIS_PREFIX_PATH'] = r'C:/OSGeo4W64/apps/qgis'
  5. os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:/OSGEO4~1/apps/Qt5/plugins'
  6. os.environ['PATH'] += r';C:/OSGeo4W64/apps/qgis/bin;C:/OSGeo4W64/apps/Qt5/bin;C:/OSGeo4W64/apps/Python37'
  7. sys.path.extend([r'C:/OSGeo4W64/apps/qgis/python',r'C:/OSGeo4W64/apps/qgis/Python37/Lib/site-packages'])
  8. from qgis.core import *
  9. from PyQt5.QtCore import QRectF
  10.  
  11. qgs = QgsApplication([], False)
  12. qgs.initQgis()
  13. shp_file = 'C:/Users/y.saban/Desktop/zonning/rab.shp'
  14. vlayer = QgsVectorLayer(shp_file, 'vlayer')
  15. QgsProject.instance().addMapLayer(vlayer,False)
  16.  
  17. l = QgsPrintLayout(QgsProject.instance())
  18. l.initializeDefaults()
  19. l.setUnits(QgsUnitTypes.LayoutMillimeters)
  20. page = l.pageCollection().pages()[0]
  21. lm = 20 # left margin
  22. tm = 32 # upper margin
  23. w, h = 900, 600 #width and height
  24.  
  25. page.setPageSize(QgsLayoutSize(1189, 841))
  26. #setting the layer style
  27. blackSymbol = QgsFillSymbol.createSimple({'color': '255,0,0,100',
  28. 'color_border': 'BLACK',
  29. 'width_border': '0.1'})
  30. vlayer.setRenderer(QgsSingleSymbolRenderer(blackSymbol))
  31. #adding the map to the layout
  32. theMap = QgsLayoutItemMap(l)
  33. theMap.updateBoundingRect()
  34. theMap.setRect(QRectF(lm,tm, w, h))
  35. theMap.setPos(lm,tm)
  36. theMap.updateBoundingRect()
  37. theMap.setLayers([vlayer])
  38. # setting the map extent
  39. theMap.setExtent(QgsRectangle(363600.0, 376800.0, 364500.0 ,377400.0))
  40. theMap.attemptSetSceneRect(QRectF(lm,tm, w, h))
  41. l.addItem(theMap)
  42. l.updateBounds()
  43. #exporting to pdf
  44. exporter = QgsLayoutExporter(l)
  45. pdf_settings = exporter.PdfExportSettings()
  46. exporter.exportToPdf('C:/map.pdf', pdf_settings)
  47. qgs.exitQgis()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement