【C#】Proj.Net ~ あれこれ編 ~


■ 変換を纏めて行う

 * TransformList() を使用する

サンプル

using GeoAPI.CoordinateSystems;
using GeoAPI.CoordinateSystems.Transformations;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SampleForm
{
  public partial class Form1 : Form
  {
    public Form10()
    {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      List<double[]> points = new List<double[]>
      {
        new[] {290586.087, 6714000 },
        new[] {90586.392, 6713996.224},
        new[] {290590.133, 6713973.772},
        new[] {290594.111, 6713957.416},
        new[] {290596.615, 6713943.567},
        new[] {290596.701, 6713939.485}
      };
      var result = ToUtmPoints(points, true, 33);
      this.label1.Text = result;
    }

    public static string ToUtmPoints(List<double[]> points, bool isNorthZone, int utmZone)
    {
      // Transform to UTM
      CoordinateTransformationFactory factory = new CoordinateTransformationFactory();
      ICoordinateSystem wgs84geo = GeographicCoordinateSystem.WGS84;
      ICoordinateSystem utm = ProjectedCoordinateSystem.WGS84_UTM(utmZone, isNorthZone);
      ICoordinateTransformation transformation =
         factory.CreateFromCoordinateSystems(wgs84geo, utm);

      var utmPoints = transformation.MathTransform.TransformList(points);

      StringBuilder returnValue = new StringBuilder();
      for (int i = 0; i < points.Count; i++)
      {
        returnValue.AppendLine(
          string.Format("({0}, {1}) => ({2}, {3})",
          points[i][0], points[i][1], utmPoints[i][0], utmPoints[i][1]));
      }

      return returnValue.ToString();
    }
}

WKT でICoordinateSystemインスタンスを作成する

 * CreateFromWkt() を使用する
使用上の注意
 * 適当な文字列を指定すると、例外 NotImplementedException が発生する

サンプル

例1
https://github.com/fivepmtechnology/ProjNET/blob/master/ProjNet.CoordinateSystems.Tests/WKT/WKTCoordSysParserTests.cs
string wkt = "PROJCS[\"NAD83(HARN) / Texas Central (ftUS)\", GEOGCS[\"NAD83(HARN)\", DATUM[\"NAD83_High_Accuracy_Regional_Network\", SPHEROID[\"GRS 1980\", 6378137, 298.257222101, AUTHORITY[\"EPSG\", \"7019\"]], TOWGS84[725, 685, 536, 0, 0, 0, 0], AUTHORITY[\"EPSG\", \"6152\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9122\"]], AUTHORITY[\"EPSG\", \"4152\"]], UNIT[\"US survey foot\", 0.304800609601219, AUTHORITY[\"EPSG\", \"9003\"]], PROJECTION[\"Lambert_Conformal_Conic_2SP\"], PARAMETER[\"standard_parallel_1\", 31.883333333333], PARAMETER[\"standard_parallel_2\", 30.1166666667], PARAMETER[\"latitude_of_origin\", 29.6666666667], PARAMETER[\"central_meridian\", -100.333333333333], PARAMETER[\"false_easting\", 2296583.333], PARAMETER[\"false_northing\", 9842500], AUTHORITY[\"EPSG\", \"2918\"]]";
ProjectedCoordinateSystem projectedCoordinateSystem =
  CoordinateSystemWktReader.Parse(wkt) as ProjectedCoordinateSystem;
this.label1.Text = projectedCoordinateSystem.GeographicCoordinateSystem.Name;
例2
CoordinateSystemFactory factory = new CoordinateSystemFactory();
CoordinateTransformationFactory transformationFactory = new CoordinateTransformationFactory();

ICoordinateSystem sourceCoordinateSystem = factory.CreateFromWkt("【変換元のWKT文字列】");
ICoordinateSystem targetCoordinateSystem = factory.CreateFromWkt("【変換先のWKT文字列】");

ICoordinateTransformation transformation = 
  transformationFactory.CreateFromCoordinateSystems(
    sourceCoordinateSystem, targetCoordinateSystem);

Coordinate sourceCoordinate = new Coordinate(4.3593204, 52.0088095);
Coordinate targetCoordinate = transformation.MathTransform.Transform(sourceCoordinate);

this.label1.Text =
  string.Format("({0:#.####}, {1:#.####}) => ({2:#}, {3:#})",
   sourceCoordinate.Y, sourceCoordinate.X, targetCoordinate.X, targetCoordinate.Y);

補足:WKT / SRID

WKT (Well-Known Text) とは?
 * 点、線、平面などの幾何学オブジェクトを、投影法を基に変換し、地図上に表現するマークアップ言語

【幾何学オブジェクト(ジオメトリオブジェクト)】
 * 点:ポイント(Point)
 * 線:ライン(LineString)
 * 面:ポリゴン(Polygon)

etc... の 計18種類
https://ja.wikipedia.org/wiki/Well-known_text
SRID(Spatial Reference ID, 空間参照系識別子) とは?
 * 空間参照系(Spatial Reference System, SRS)の識別コード(整数)
https://qiita.com/boiledorange73/items/b98d3d1ef3abf7299aba
http://pucyan.blog77.fc2.com/blog-entry-109.html
https://homata.gitbook.io/geodjango/hajimeteno/coordinate

関連記事

PROJ

Proj.Net ~ 環境構築編 / .NET で PROJ を使うには... ~
https://blogs.yahoo.co.jp/dk521123/38088151.html
Proj.Net ~ 基本編 ~
https://blogs.yahoo.co.jp/dk521123/38104577.html
GISツール】PROJ / PROJ.4 ~ 入門編 ~
https://blogs.yahoo.co.jp/dk521123/38095650.html

その他

地図 / 緯度経度 について
https://blogs.yahoo.co.jp/dk521123/38081789.html