ホーム » QGIS (ページ 3)

QGIS」カテゴリーアーカイブ

QgsMessageBarを表示しない

CRSが未設定のレイヤを表示するとQgsMessageBarが表示される

「新しいレイヤの投影座標系」を「プロジェクトのCRSを使用」に設定してもこれは避けられない

QgisAppクラスのvalidateCrsを確認すると
[code]
void QgisApp::validateCrs( QgsCoordinateReferenceSystem &srs )
{
static QString sAuthId = QString();
QgsSettings mySettings;
QString myDefaultProjectionOption = mySettings.value( QStringLiteral( "Projections/defaultBehavior" ), "prompt" ).toString();
if ( myDefaultProjectionOption == QLatin1String( "prompt" ) )
{
  省略
}
else if ( myDefaultProjectionOption == QLatin1String( "useProject" ) )
{
// XXX TODO: Change project to store selected CS as ‘projectCRS’ not ‘selectedWkt’
sAuthId = QgsProject::instance()->crs().authid();
srs.createFromOgcWmsCrs( sAuthId );
QgsDebugMsg( "Layer srs set from project: " + sAuthId );
messageBar()->pushMessage( tr( "CRS was undefined" ), tr( "defaulting to project CRS %1 – %2" ).arg( sAuthId, srs.description() ), Qgis::Warning, messageTimeout() );
}
else ///Projections/defaultBehavior==useGlobal
{
  省略
}
}
[/code]

「プロジェクトのCRSを使用」に設定するとかならず「messageBar()->pushMessage」が呼ばれてしまう。
フラグなどで対応することはできそうもない。

QgsMessageBarクラス参考

とりあえず以下のコードで表示される時間を1秒に設定
[code]
QSettings().setValue( "qgis/messageTimeout", 1 )
[/code]

QgsFillSymbolの設定

ポリゴンのシンボルQgsFillSymbolの設定(構築)はQgsFillSymbolクラスのメソッドcreateSimpleにスタイルを渡して設定する。

内部の色、外周の色を設定する時は
[code]
QgsFillSymbol.createSimple({‘color’: ‘0,0,0,0’,’outline_color’: ‘255,0,0,255’})
[/code]

更に内部のパターンを設定する時は
[code]
QgsFillSymbol.createSimple({‘color’: ‘0,0,0,0’,’outline_color’: ‘255,0,0,255’,’style’ : ‘diagonal_x’})
[/code]

設定のキーはQgsSymbolLayer::createを参考
[code]
QgsSymbolLayer *QgsSimpleFillSymbolLayer::create( const QgsStringMap &props )
{
QColor color = DEFAULT_SIMPLEFILL_COLOR;
Qt::BrushStyle style = DEFAULT_SIMPLEFILL_STYLE;
QColor strokeColor = DEFAULT_SIMPLEFILL_BORDERCOLOR;
Qt::PenStyle strokeStyle = DEFAULT_SIMPLEFILL_BORDERSTYLE;
double strokeWidth = DEFAULT_SIMPLEFILL_BORDERWIDTH;
Qt::PenJoinStyle penJoinStyle = DEFAULT_SIMPLEFILL_JOINSTYLE;
QPointF offset;

if ( props.contains( QStringLiteral( "color" ) ) )
color = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color" )] );
if ( props.contains( QStringLiteral( "style" ) ) )
style = QgsSymbolLayerUtils::decodeBrushStyle( props[QStringLiteral( "style" )] );
if ( props.contains( QStringLiteral( "color_border" ) ) )
{
//pre 2.5 projects used "color_border"
strokeColor = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color_border" )] );
}
else if ( props.contains( QStringLiteral( "outline_color" ) ) )
{
strokeColor = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline_color" )] );
}
else if ( props.contains( QStringLiteral( "line_color" ) ) )
{
strokeColor = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "line_color" )] );
}

if ( props.contains( QStringLiteral( "style_border" ) ) )
{
//pre 2.5 projects used "style_border"
strokeStyle = QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "style_border" )] );
}
else if ( props.contains( QStringLiteral( "outline_style" ) ) )
{
strokeStyle = QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "outline_style" )] );
}
else if ( props.contains( QStringLiteral( "line_style" ) ) )
{
strokeStyle = QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "line_style" )] );
}
if ( props.contains( QStringLiteral( "width_border" ) ) )
{
//pre 2.5 projects used "width_border"
strokeWidth = props[QStringLiteral( "width_border" )].toDouble();
}
else if ( props.contains( QStringLiteral( "outline_width" ) ) )
{
strokeWidth = props[QStringLiteral( "outline_width" )].toDouble();
}
else if ( props.contains( QStringLiteral( "line_width" ) ) )
{
strokeWidth = props[QStringLiteral( "line_width" )].toDouble();
}
if ( props.contains( QStringLiteral( "offset" ) ) )
offset = QgsSymbolLayerUtils::decodePoint( props[QStringLiteral( "offset" )] );
if ( props.contains( QStringLiteral( "joinstyle" ) ) )
penJoinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( props[QStringLiteral( "joinstyle" )] );

QgsSimpleFillSymbolLayer *sl = new QgsSimpleFillSymbolLayer( color, style, strokeColor, strokeStyle, strokeWidth, penJoinStyle );
sl->setOffset( offset );
if ( props.contains( QStringLiteral( "border_width_unit" ) ) )
{
sl->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "border_width_unit" )] ) );
}
else if ( props.contains( QStringLiteral( "outline_width_unit" ) ) )
{
sl->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "outline_width_unit" )] ) );
}
else if ( props.contains( QStringLiteral( "line_width_unit" ) ) )
{
sl->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "line_width_unit" )] ) );
}
if ( props.contains( QStringLiteral( "offset_unit" ) ) )
sl->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "offset_unit" )] ) );

if ( props.contains( QStringLiteral( "border_width_map_unit_scale" ) ) )
sl->setStrokeWidthMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "border_width_map_unit_scale" )] ) );
if ( props.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
sl->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "offset_map_unit_scale" )] ) );

sl->restoreOldDataDefinedProperties( props );

return sl;
}
[/code]

設定値はQgsSymbolLayerUtilsを参照

[code]
Qt::BrushStyle QgsSymbolLayerUtils::decodeBrushStyle( const QString &str )
{
if ( str == QLatin1String( "solid" ) ) return Qt::SolidPattern;
if ( str == QLatin1String( "horizontal" ) ) return Qt::HorPattern;
if ( str == QLatin1String( "vertical" ) ) return Qt::VerPattern;
if ( str == QLatin1String( "cross" ) ) return Qt::CrossPattern;
if ( str == QLatin1String( "b_diagonal" ) ) return Qt::BDiagPattern;
if ( str == QLatin1String( "f_diagonal" ) ) return Qt::FDiagPattern;
if ( str == QLatin1String( "diagonal_x" ) ) return Qt::DiagCrossPattern;
if ( str == QLatin1String( "dense1" ) ) return Qt::Dense1Pattern;
if ( str == QLatin1String( "dense2" ) ) return Qt::Dense2Pattern;
if ( str == QLatin1String( "dense3" ) ) return Qt::Dense3Pattern;
if ( str == QLatin1String( "dense4" ) ) return Qt::Dense4Pattern;
if ( str == QLatin1String( "dense5" ) ) return Qt::Dense5Pattern;
if ( str == QLatin1String( "dense6" ) ) return Qt::Dense6Pattern;
if ( str == QLatin1String( "dense7" ) ) return Qt::Dense7Pattern;
if ( str == QLatin1String( "no" ) ) return Qt::NoBrush;
return Qt::SolidPattern;
}

QString QgsSymbolLayerUtils::encodeSldBrushStyle( Qt::BrushStyle style )
{
switch ( style )
{
case Qt::CrossPattern:
return QStringLiteral( "cross" );
case Qt::DiagCrossPattern:
return QStringLiteral( "x" );

/* The following names are taken from the presentation "GeoServer
* Cartographic Rendering" by Andrea Aime at the FOSS4G 2010.
* (see http://2010.foss4g.org/presentations/3588.pdf)
*/
case Qt::HorPattern:
return QStringLiteral( "horline" );
case Qt::VerPattern:
return QStringLiteral( "line" );
case Qt::BDiagPattern:
return QStringLiteral( "slash" );
case Qt::FDiagPattern:
return QStringLiteral( "backslash" );

/* define the other names following the same pattern used above */
case Qt::Dense1Pattern:
case Qt::Dense2Pattern:
case Qt::Dense3Pattern:
case Qt::Dense4Pattern:
case Qt::Dense5Pattern:
case Qt::Dense6Pattern:
case Qt::Dense7Pattern:
return QStringLiteral( "brush://%1" ).arg( encodeBrushStyle( style ) );

default:
return QString();
}
}

[/code]

QGISのプロジェクトをクリア

QgsProjectのclearメソッドで読み込んでいたレイヤなどが解放される

[code]
QgsProject.instance().clear()
[/code]

Polygon Featureを登録

参考サイト

最初にQgsGeometryを作成

[code]
points = []

point1=QgsPoint()
point1.setX(-31446.8)
point1.setY(-51366.4)
points.append(point1)

point2=QgsPoint()
point2.setX(-31446.8)
point2.setY(-50366.4)
points.append(point2)

point3=QgsPoint()
point3.setX(-30446.8)
point3.setY(-50366.4)
points.append(point3)

point4=QgsPoint()
point4.setX(-31446.8)
point4.setY(-51366.4)
points.append(point4)

line = QgsLineString()
line.setPoints(points)
poly = QgsPolygon()
poly.setExteriorRing(line)
geom = QgsGeometry(poly)
[/code]

QgsGeometryをWKTから作成する時は

[code]
geom = QgsGeometry.fromWkt(‘Polygon ((-31446.7999999999992724 -51366.40000000000145519, -31446.7999999999992724 -50366.40000000000145519, -30446.7999999999992724 -50366.40000000000145519, -31446.7999999999992724 -51366.40000000000145519))’)
[/code]

Featureを作成

[code]
features = []
feature = QgsFeature(l.fields())
feature.setAttribute(0, ‘HELLO100’)
feature.setAttribute(1, 2)
feature.setGeometry(geom)
features.append(feature)
iface.activeLayer().dataProvider().addFeatures(features)
iface.activeLayer().triggerRepaint()
[/code]

2019/01/15 修正
iface.activeLayer().addFeatures([feature])

iface.activeLayer().dataProvider().addFeatures(features)
に修正

カテゴリシンボルの設定

最初にカテゴリシンボルの対象となる項目「myint」を持つメモリレイヤを追加

[code]
layer=QgsVectorLayer(‘polygon?crs=epsg:2451′,’mypolygon’,’memory’)
layer.dataProvider().addAttributes([QgsField("myint", QVariant.Int)])
layer.updateFields()
QgsProject.instance().addMapLayer(layer)
[/code]

次にカテゴリ項目の設定

[code]

cats=[]

# 項目値=1の時のシンボル
val1 = 1
symbol1 = QgsFillSymbol.createSimple({‘color’: ‘0,0,255,255’,’outline_color’: ‘0,0,255,255’})
label1 = ‘label1’
cat1 = QgsRendererCategory(val1, symbol1 , label1 )

cats.append(cat1)

# 項目値=2の時のシンボル
val2 = 2
symbol2 = QgsFillSymbol.createSimple({‘color’: ‘255,0,0,255’,’outline_color’: ‘255,0,0,255’})
label2 = ‘label2’
cat2 = QgsRendererCategory(val2, symbol2 , label2 )

cats.append(cat2)

renderer=QgsCategorizedSymbolRenderer(‘myint’, cats)

layer.setRenderer(renderer)

[/code]

ポリゴンレイヤのシンボル設定

ポリゴンレイヤのシンボルの設定

ラインの色を赤に内部を透明に設定

[code]
symbol = QgsFillSymbol.createSimple({‘color’: ‘0,0,0,0’,’outline_color’: ‘255,0,0,255’})
iface.activeLayer().renderer().setSymbol(symbol)
iface.activeLayer().triggerRepaint()
[/code]

レイヤのCRSを設定

投影情報を持たないShapeファイルを読み込むと投影情報の確認ダイアログが表示される
これはオプションで新しいレイヤの投影座標系に「CRSダイアログを表示」が指定されているため

これを「プロジェクトのCRSを使用する」に変更するためにはQSettingで設定

[code]
QSettings().setValue( "/Projections/defaultBehavior", "useProject" )
[/code]

※オプション画面のuiはqgsoptionbase.ui

プロジェクトのCRSを設定

[code]
from qgis.core import QgsProject
crs = QgsCoordinateReferenceSystem("EPSG:2541")
prj = QgsProject.instance()
prj.setCrs(crs)
[/code]

設定した投影がステータスバーに反映される

設定した内容を確認

[code]
print("CRS Description: {}".format(crs.description()))
CRS Description: JGD2000 / Japan Plane Rectangular CS IX
[/code]

Pythonでプラグインをロードする

どこかのサイトで以下のコードでプラグインをロードできるとあった。
試してみるとプラグインの選択画面ではチェックが付くが実際にはプラグインの中の関数は呼ばれない
[code]
qgis.utils.loadPlugin(‘myplugin’)
[/code]

以下も同時にやるといいみたい。
でもチェックが付かない?
[code]
qgis.utils.startPlugin(‘myplugin’)
[/code]

起動時に実行するPythonスクリプトではPluginの設定はできない

QGISは実行パラメータ–codeで起動時にPythonのスクリプトを実行できる。

どこかのサイトでこのスクリプトの中で以下のようにQgsSettingsを設定すれば起動時にプラグインをロードできると記載があったが

[code]
QSettings().setValue( "/PythonPlugins/myplugin", True )
[/code]

これはダメ!

起動時に指定したスクリプトはプラグインのロード終了後実行されるので、影響がない