ArcGIS⇒GeoJson


GeoJsonとして使用可能かは見てないが、こんな感じかな。

GeoJson⇒ArcGIS
の時と同様ArcGIS.PCL関連に加えて、下記コードはDynamicJsonを入れとく必要あり。
別に使わなくても切り出せそうだが楽だったので。

using ArcGIS.ServiceModel;
using ArcGIS.ServiceModel.Common;
using Codeplex.Data;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using (ComReleaser com = new ComReleaser())
{
  string testShpPath = @"C:\japan_ver71\japan_ver71.shp";
  string dir = System.IO.Path.GetDirectoryName(testShpPath);
  string file = System.IO.Path.GetFileNameWithoutExtension(testShpPath);

  Type t = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
  IWorkspaceFactory wsf = (IWorkspaceFactory)Activator.CreateInstance(t);
  com.ManageLifetime(wsf);
  IFeatureWorkspace fw = (IFeatureWorkspace)wsf.OpenFromFile(dir, 0);
  com.ManageLifetime(fw);

  IFeatureClass fc = fw.OpenFeatureClass(file);
  com.ManageLifetime(fc);

  IRecordSetInit rcInit = new RecordSetClass();
  com.ManageLifetime(rcInit);

  //確認用条件
  IQueryFilter qFilter = new QueryFilterClass();
  com.ManageLifetime(qFilter);
  qFilter.WhereClause = fc.OIDFieldName + " = 1";

  rcInit.SetSourceTable((ITable)fc, qFilter);
  IRecordSet recordset = (IRecordSet)rcInit;

  IJSONConverterGdb jsonConverterGdb = new JSONConverterGdbClass();
  com.ManageLifetime(jsonConverterGdb);

  IGeometryResultOptions geomOption = new GeometryResultOptionsClass();
  com.ManageLifetime(geomOption);
  geomOption.GeneralizeGeometries = false;
  geomOption.DensifyGeometries = false;

  IPropertySet propSet = new PropertySetClass();
  com.ManageLifetime(propSet);
  propSet.SetProperty("GeometryResultOptions", geomOption);

  IJSONWriter jsonWriter = new JSONWriterClass();
  com.ManageLifetime(jsonWriter);
  jsonWriter.WriteToString();

  IJSONSerializer jsonSerializer = new JSONSerializerGdbClass();
  com.ManageLifetime(jsonSerializer);

  jsonSerializer.InitSerializer(jsonWriter, null);
  jsonSerializer.WriteObject(recordset, propSet);
  string esriJson = Encoding.UTF8.GetString(jsonWriter.GetStringBuffer());

  dynamic esriJsonObj = DynamicJson.Parse(esriJson);
  if (!esriJsonObj.IsDefined("features"))
    return;

  List<Feature<Polygon>> esriFeatureList = JsonSerializer.DeserializeFromString<List<Feature<Polygon>>>(esriJsonObj.features.ToString());
  var geoJsonObj = esriFeatureList.ToFeatureCollection<Polygon>();

  string geoJson = JsonSerializer.SerializeToString(geoJsonObj);

  Debug.WriteLine(geoJson);
                
}//end com
カテゴリー: 開発 タグ: パーマリンク