ストリームとリモートストレージ
マルチファイル形式での作業
一部のGISデータ形式は、コンテンツを複数のファイルに分割します。たとえば、シェープファイルには、.shp、.shx、および*.dbfの少なくとも3つのファイルが必要です。これらの形式では、すべてのファイルを特定のディレクトリ構造内に、定義済みのファイル名パターンで保存する必要があります。
同時に、ファイルはリモートサーバーまたはストリーム経由でのみアクセス可能な別の場所に格納されている場合があります。ストリームにはファイル名とディレクトリ構造の情報がないため、ファイル形式ドライバーがデータをどのように処理するかを判断することはできません。Aspose.GISはこの問題を抽象パスメカニズムを提供することで解決します。
抽象パスは、ある種のファイルシステムのようなストレージ内のファイル(またはディレクトリ)へのパスを表します。ストレージは、アーカイブからFTPサーバーまで、ファイルとディレクトリの概念を持つものであれば何でも構いません。ファイルを開いたりディレクトリをリストしたりするなど、典型的なファイル操作を実行する方法を定義します。
AbstractPath を継承し、その抽象メソッドの実装を提供することで、ストレージのファイル操作の方法を指定できます。
ショーケース:Azure Blob Storage
Aspose.GIS Examplesリポジトリには、Azure Blob Storage用のカスタム抽象パスの完全な実装例が含まれています。このショーケースでは、Azure Blob Storageから直接シェープファイルを読み取る方法を示します。こちらで見つけることができます:https://github.com/aspose-gis/Aspose.GIS-for-.NET/tree/master/Showcases/Azure_Blob_Integration_by_Aspose_Gis_for_NET。
単一ファイル形式(GeoJSON、KML)
GeoJSONやKMLなどのGISデータ形式は、レイヤーのすべてのデータを1つのファイルに保存できます。ファイルのストリームを取得できる場合は、カスタム抽象パスの実装をスキップし、メソッドAbstractPath.FromStream()を使用してストリーム用の抽象パスをインスタンス化できます。
ストリームからファイルを読み込む
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
const string geoJson = @"{""type"":""FeatureCollection"",""features"":[ | |
{""type"":""Feature"",""geometry"":{""type"":""Point"",""coordinates"":[0, 1]},""properties"":{""name"":""John""}}, | |
{""type"":""Feature"",""geometry"":{""type"":""Point"",""coordinates"":[2, 3]},""properties"":{""name"":""Mary""}} | |
]}"; | |
using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(geoJson))) | |
using (var layer = VectorLayer.Open(AbstractPath.FromStream(memoryStream), Drivers.GeoJson)) | |
{ | |
Console.WriteLine(layer.Count); // 2 | |
Console.WriteLine(layer[1].GetValue<string>("name")); // Mary | |
} |
ストリームにファイルを書き込む
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var memoryStream = new MemoryStream()) | |
{ | |
using (var layer = VectorLayer.Create(AbstractPath.FromStream(memoryStream), Drivers.GeoJson)) | |
{ | |
layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String)); | |
layer.Attributes.Add(new FeatureAttribute("age", AttributeDataType.Integer)); | |
Feature firstFeature = layer.ConstructFeature(); | |
firstFeature.Geometry = new Point(33.97, -118.25); | |
firstFeature.SetValue("name", "John"); | |
firstFeature.SetValue("age", 23); | |
layer.Add(firstFeature); | |
Feature secondFeature = layer.ConstructFeature(); | |
secondFeature.Geometry = new Point(35.81, -96.28); | |
secondFeature.SetValue("name", "Mary"); | |
secondFeature.SetValue("age", 54); | |
layer.Add(secondFeature); | |
} | |
Console.WriteLine(Encoding.UTF8.GetString(memoryStream.ToArray())); | |
} |