1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Shapely+PyprojでLineString座標列を一括変換(WGS84->UTM)する

Last updated at Posted at 2021-01-14

PythonでのWKT形式LineStringの座標変換処理(WGS84->UTM)の実装に関しての備忘録

実行環境

Python: v3.8.5
利用ライブラリ:Shapely=1.7.0, Pyproj=3.0.0.post1

処理内容・実装コード

Shapelyで読み込んだLineStringの座標列について、指定EPSGコードにてPyprojのTransformer.itransformerで一括座標変換

transform_linestr.py
from pyproj import Transformer
from shapely import wkt
from shapely.geometry import LineString

WGS84_EPSG = 4326
UTM_EPSG = 6691


def main():
    # WKT形式LineStringロード
    linestr_wkt = "LINESTRING (138.385554841666 34.9748902437125, 138.385886903329 34.9752925491883, 138.38621257919 34.9757140120678, 138.386397767425 34.97612270334488)"
    linestr = wkt.loads(linestr_wkt)

    # WGS84からUTMへ座標変換
    trans_linestr = transform_linestr(linestr, WGS84_EPSG, UTM_EPSG)

    # UTMからWGS84へ座標再変換(元座標の精度が高い場合に若干値が変化する可能性有)
    re_trans_linestr = transform_linestr(trans_linestr, UTM_EPSG, WGS84_EPSG)

    print("Load WKT:  ", linestr)
    print("WGS84->UTM:", trans_linestr)
    print("UTM->WGS84:", re_trans_linestr)


def transform_linestr(linestr: LineString, from_epsg: int, to_epsg: int) -> LineString:
    """指定EPSGコードにおけるLineString座標変換処理

    Args:
        linestr (LineString): 座標変換元LineString
        from_epsg (int): 変換元EPSGコード
        to_epsg (int): 変換先EPSGコード

    Returns:
        LineString: 座標変換後LineString
    """
    transformer = Transformer.from_crs(from_epsg, to_epsg)
    trans_coords = transformer.itransform(linestr.coords, switch=True)
    return LineString(trans_coords)


if __name__ == "__main__":
    main()

実行出力結果

Load WKT:   LINESTRING (138.385554841666 34.9748902437125, 138.385886903329 34.9752925491883, 138.38621257919 34.9757140120678, 138.386397767425 34.97612270334488)
WGS84->UTM: LINESTRING (3873381.187880124 261326.0666616608, 3873425.024292707 261357.5560269007, 3873471.001297946 261388.5176213919, 3873515.897231507 261406.6137873351)
UTM->WGS84: LINESTRING (138.385554841666 34.9748902437125, 138.385886903329 34.9752925491883, 138.38621257919 34.9757140120678, 138.386397767425 34.97612270334487)

Shapely+Pyprojでとても簡単に座標列変換処理を実装できた。Pyproj便利。

参考

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?