• 公図のXMLデータをgeojsonに変換したものについて、画像に変換する。
  • 1つのXMLデータから1つの画像ファイルを作成するときには、どの字かもわかるように作成する。
  • QGISのprocessingツールから実行
  • あらかじめ字と地番のスタイルファイルを作成しておく必要がある。
  • 字が青色、地番が黒色。

公図の画像化

# 公図のgeojsonを画像にする。PDFは不可。
# 地籍調査未済の箇所用。CRSはとりあえず平面直角座標系で利用。地籍調査済みはgeojsonのまま活用。
# aza.qmlとkouzu.qmlをこのpyと同じ場所に保存。
# QGIS上で実行
import os
import glob
import processing

from qgis.core import (
    QgsGeometry,
    QgsMapSettings,
    QgsPrintLayout,
    QgsMapSettings,
    QgsMapRendererParallelJob,
    QgsLayoutItemLabel,
    QgsLayoutItemLegend,
    QgsLayoutItemMap,
    QgsLayoutItemPolygon,
    QgsLayoutItemScaleBar,
    QgsLayoutExporter,
    QgsLayoutItem,
    QgsLayoutPoint,
    QgsLayoutSize,
    QgsUnitTypes,
    QgsProject,
    QgsFillSymbol,
)

from qgis.PyQt.QtGui import (
    QPolygonF,
    QColor,
)

from qgis.PyQt.QtCore import (
    QPointF,
    QRectF,
    QSize,
)

from qgis.PyQt.QtCore import QEventLoop

# ファイルの読み込み \は2個重ねる
# 公図ファイルの読み込み
kouzu_path_locate = "Z:\\src\\gis"
all_kouzu_files = glob.glob(os.path.join(kouzu_path_locate,"*.geojson"))
total_number_kouzu = len(all_kouzu_files)

# qmlファイル(表示スタイル)の読み込み
qml_path_locate = "Z:\\src\\gis"
kouzu_qml = os.path.join(qml_path_locate,"kouzu.qml")
aza_qml = os.path.join(qml_path_locate,"aza.qml")

# CRSの設定。EPSGで入力。平面直角座標系ならなんでも可。
temp_epsg = 2449

# 出力ファイルの拡張子の設定
extension_output_file = "png"

# 出力ファイルのカウント
file_number = 1

# 一時的に字ファイルを保存するフォルダを作成する
aza_area = os.path.join(kouzu_path_locate,"aza_area")

if not os.path.isdir(aza_area):
    os.makedirs(aza_area)

for file in all_kouzu_files:
    #入力ファイルの設定
    temp_filename1 = file.split("\\")
    input_filename = temp_filename1[-1]
    #出力ファイルの設定。字も一緒に。
    output_tiban_filename = input_filename.split(".")[0]+"."+extension_output_file
    output_aza_filename = input_filename.split(".")[0]+".geojson"
    
    tiban_layer = QgsVectorLayer(file,input_filename,"ogr")
    tiban_layer.loadNamedStyle(kouzu_qml) 
    if not tiban_layer.isValid():
        print("Tiban layer failed to load!")
    else:
        QgsProject.instance().addMapLayer(tiban_layer)
    
    # CRSを再投影
    tiban_layer.setCrs(QgsCoordinateReferenceSystem(temp_epsg, QgsCoordinateReferenceSystem.EpsgCrsId))

    # 字界の作成
    f_aza = os.path.join(aza_area,output_aza_filename)

    alg_params = {
        'FIELD': '小字名',
        'INPUT': tiban_layer,
        'OUTPUT': f_aza,
    }

    processing.run("gdal:dissolve",alg_params)

    # 字界の表示
    aza_layer = QgsVectorLayer(f_aza,"aza","ogr")
    aza_layer.loadNamedStyle(aza_qml) 

    if not aza_layer.isValid():
        print("Aza layer failed to load!")
    else:
        QgsProject.instance().addMapLayer(aza_layer)

    aza_layer.setCrs(QgsCoordinateReferenceSystem(2449, QgsCoordinateReferenceSystem.EpsgCrsId))

    #地番のバウンディングボックスの取得
    box = tiban_layer.extent()
    xlength = box.xMaximum() - box.xMinimum()
    ylength = box.yMaximum() - box.yMinimum()

    #印刷設定
    settings = QgsMapSettings()
    settings.setLayers([tiban_layer,aza_layer])

    settings.setBackgroundColor(QColor(255, 255, 255))
    # 公図の縦幅と横幅の5倍の大きさで図化
    settings.setOutputSize(QSize(xlength*5, ylength*5))
    settings.setExtent(tiban_layer.extent())
    
    render = QgsMapRendererParallelJob(settings)
    
    def finished():
        img = render.renderedImage()
        # save the image; e.g. img.save("/Users/myuser/render.png","png")
        img.save(os.path.join(kouzu_path_locate,output_tiban_filename),extension_output_file) 
        
    render.finished.connect(finished)
    
    # Start the rendering
    render.start()
    
   # レイヤーの削除
    QgsProject.instance().removeMapLayer(tiban_layer)
    QgsProject.instance().removeMapLayer(aza_layer)
    
    # The following loop is not normally required, we
    # are using it here because this is a standalone example.
    loop = QEventLoop()
    render.finished.connect(loop.quit)
    loop.exec_()

    print(file_number,"/",total_number_kouzu,"個のファイルを出力しました。")
    file_number += 1