วิธีใช้โปรแกรมแต่งแผนที่ในสคริปต์แบบสแตนด์อโลน


9

ฉันกำลังพยายามติดตามส่วนการแสดงแผนที่จากตำรา pyqgis แต่ฉันต้องการทดสอบนี้เป็นแอปพลิเคชันแบบสแตนด์อโลน ฉันสามารถทำส่วนแรกโดยใช้การเรนเดอร์แบบง่าย ๆ แต่ฉันก็ยังคงทำตัวอย่างที่สองโดยใช้ตัวแต่งแผนที่เป็นสคริปต์แบบสแตนด์อโลน

นี่เป็นตัวอย่างสำหรับบิตที่ฉันสามารถทำได้:

from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *

QgsApplication.setPrefixPath("/usr/", True)
QgsApplication.initQgis()

fh = open("eg.csv","w")
fh.write("""
x,y,name
153.0278, -27.4679, Brisbane
144.2500, -23.4500, Longreach
145.7753, -16.9256, Cairns
""")
fh.close()

uri = "eg.csv?delimiter=%s&xField=%s&yField=%s" % (",", "x", "y")
layer = QgsVectorLayer(uri, "eglayer", "delimitedtext")
QgsMapLayerRegistry.instance().addMapLayer(layer)
img = QImage(QSize(800,600), QImage.Format_ARGB32_Premultiplied)
color = QColor(255,255,255)
img.fill(color.rgb())
p = QPainter()
p.begin(img)
render = QgsMapRenderer()
lst = [ layer.getLayerID() ]  # add ID of every layer
render.setLayerSet(lst)
rect = QgsRectangle(render.fullExtent())
rect.scale(1.1)
render.setExtent(rect)
render.setOutputSize(img.size(), img.logicalDpiX())
render.render(p)
p.end()
img.save("render.png","png")

สิ่งที่ฉันต้องการจะทำคือเหมือนกัน แต่ใช้QgsCompositionและบันทึกเป็น pdf ตัวอย่าง ตำราพูดว่า:

เมื่อใช้ผู้แต่งในแอปพลิเคชันแบบสแตนด์อโลนคุณสามารถสร้างอินสแตนซ์ตัวสร้างแผนที่ของคุณเองในแบบเดียวกับที่แสดงในส่วนด้านบนและส่งต่อไปยังองค์ประกอบ

บิตนี้ฉันทำไม่ได้ความพยายามทั้งหมดของฉันมีแผนที่ว่างเปล่าหรือเป็นเซกฟอลต์ ฉันใช้ linux mint 13 โดยใช้ qgis 1.8.0 มันจะดีถ้ามีใครสามารถแสดงให้ฉันเห็นถึงวิธีการแก้ไขตัวอย่างง่ายๆให้กับผู้ที่ใช้นักแต่งเพลง

คำตอบ:


8

จากความคิดเห็นคำตอบนี้ใช้ได้กับเวอร์ชันก่อนหน้า2.4
สำหรับการอ้างอิงในอนาคตนี่คือตัวอย่างแบบสแตนด์อโลนที่ใช้งานได้

from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *

QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
app = QgsApplication([], True)

fh = open("eg.csv","w")
fh.write("""
x,y,name
153.0278, -27.4679, Brisbane
144.2500, -23.4500, Longreach
145.7753, -16.9256, Cairns
""")
fh.close()

uri = "eg.csv?delimiter=%s&xField=%s&yField=%s" % (",", "x", "y")
layer = QgsVectorLayer(uri, "eglayer", "delimitedtext")
print layer.isValid()
layerset = []
QgsMapLayerRegistry.instance().addMapLayer(layer)
layerset.append(layer.getLayerID())

myMapRenderer = QgsMapRenderer()
myMapRenderer.setLayerSet(layerset)
mapRectangle = QgsRectangle(140,-28,155,-15)
myMapRenderer.setExtent(mapRectangle)

comp = QgsComposition(myMapRenderer)
comp.setPlotStyle(QgsComposition.Print)
composerMap = QgsComposerMap(comp, 5,5,200,200)
composerMap.setNewExtent(mapRectangle)
comp.addItem(composerMap)
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("out.pdf")
printer.setPaperSize(QSizeF(comp.paperWidth(), comp.paperHeight()),    QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QPrinter.Color)
printer.setResolution(comp.printResolution())

pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
comp.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()
app.exitQgis()

เมื่อฉันทำสิ่งนี้ฉันจะได้รับ pdf แต่มันว่างเปล่า ฉันใช้ 2.10 (ฉันต้องเปลี่ยน. getLayerID () เป็น. id ())
— Conley Owens

ใช่ขอโทษมันไม่ได้ผลสำหรับฉันอีกต่อไป ใช้งานได้กับ 1.8.0 แต่ฉันเพิ่งทดสอบใน 2.4.0 และดูเหมือนจะไม่ทำงานอีกต่อไป
— rjad

ดูเหมือนว่าการเพิ่ม composerMap.setNewExtent (mapRectangle)
— rjad

น่าเสียดายที่นี่ใช้งานไม่ได้กับ 2.8.3 อีกต่อไป ฉันเปลี่ยน getLayerID () เป็น. id () และยังคงได้รับหน้าว่างเท่านั้น การแสดงผลข้อความแบบคงที่ ฯลฯ ทำงานได้ ความคิดเห็นเกี่ยวกับปัญหาที่อาจเกิดขึ้น?
— chriserik

QgsMapRenderer เลิกใช้แล้ว2.4ขึ้นไปดูคำตอบนี้ตามตัวอย่างเดียวกันซึ่งน่าจะใช้งานได้gis.stackexchange.com/a/223127/36886
— raphael

3

QgsMapRenderer จะเลิกใน 2.4 และสูงกว่าผมได้ปรับปรุงส่วนที่เลิกจากคำตอบนี้กับสิ่งที่ควรจะทำงานจากไป2.42.18.2

from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *

QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
app = QgsApplication([], True)

fh = open("eg.csv","w")
fh.write("""
x,y,name
153.0278, -27.4679, Brisbane
144.2500, -23.4500, Longreach
145.7753, -16.9256, Cairns
""")
fh.close()

uri = "eg.csv?delimiter=%s&xField=%s&yField=%s" % (",", "x", "y")
layer = QgsVectorLayer(uri, "eglayer", "delimitedtext")
print layer.isValid()
layerset = []
QgsMapLayerRegistry.instance().addMapLayer(layer)
layerset.append(layer.getLayerID())

def create_composition(layer_list, extent):
#New code for versions 2.4 and above
    ms = QgsMapSettings()
    ms.setLayers(layer_list)
    ms.setExtent(extent)
    comp = QgsComposition(ms)
    return comp, ms

comp, ms = create_composition(layerset, QgsRectangle(140,-28,155,-15))

comp.setPlotStyle(QgsComposition.Print)
composerMap = QgsComposerMap(comp, 5,5,200,200)

#Uses mapsettings value
composerMap.setNewExtent(ms.extent())

comp.addItem(composerMap)
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("out.pdf")
printer.setPaperSize(QSizeF(comp.paperWidth(), comp.paperHeight()),    QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QPrinter.Color)
printer.setResolution(comp.printResolution())

pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
comp.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()
app.exitQgis()

layer.getLayerID()ใช้งานไม่ได้และจะต้องเปลี่ยนเป็น:layer.id()
— Che

@ Mr.Che โปรดรวมระบบปฏิบัติการและรุ่น QGIS ของคุณด้วย
— ฟาเอล

รุ่น Win 7 และ QGIS: i.stack.imgur.com/8u8Ed.png
— สหาย Che

2

บางทีรหัสนี้อาจมีประโยชน์แม้ว่าจะไม่ใช่แอปพลิเคชันแบบสแตนด์อโลน:

from qgis.core import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os   
# Clear map canvas
QgsMapLayerRegistry.instance().removeAllMapLayers()
iface.mapCanvas().refresh()
# Open QGIS project
QgsProject.instance().setFileName('composerimage_demo.qgs')
QgsProject.instance().read()
# Set up composition
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
# Set dimensions and resolution
c.setPaperSize(160,185)
dpi = c.printResolution()
dpmm = (dpi / 25.4)
width = int(dpmm * c.paperWidth())
height = int(dpmm * c.paperHeight())
# Add map to composition
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x,y,w,h)
composerMap.setFrame(True) # Does not work with QGIS 1.9-Master. Use hasFrame() instead.
c.addItem(composerMap)
# Create output image and initialize it
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
image.fill(0)
# Render composition
imagePainter = QPainter(image)
sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())
targetArea = QRectF(0, 0, width, height)
c.render(imagePainter, targetArea, sourceArea)
imagePainter.end()
# Save image to disk (other extensions possible)
image.save('composerimage_demo.jpg')
# Clear map canvas
QgsMapLayerRegistry.instance().removeAllMapLayers()
iface.mapCanvas().refresh()

แผนที่ตั้งอยู่บนพื้นฐานของโครงการ QGIS คุณสามารถหาตัวอย่างที่สมบูรณ์ได้ที่นี่: http://www.qgis.nl/media/2013/08/composerimage_demo.zip


ขอบคุณ แต่ปัญหาของผมก็คือผมไม่ทราบวิธีการที่จะได้รับวัตถุ mapRenderer ที่ถูกต้องจะส่งผ่านไปโดยไม่ต้องโทรQgsComposition iface.mapCanvas().mapRenderer()
— rjad
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.