C#에서 Esri Gdb 파일 형식으로 작업하기
Contents
[
Hide
]
ESRI File GeoDatabases (FileGDB)
ESRI File GeoDatabases (FileGDB)는 GIS 소프트웨어에서 가장 널리 사용되는 기본 형식 중 하나입니다. Aspose.GIS를 사용하면 FileGDB 파일 형식을 작업하고 해당 기능을 반복할 수 있습니다.
FileGDB 파일의 레이어 반복
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
//File GDB is a multi layer format. This example shows how to open File GDB as a dataset (collection of layers) and access layers in it. | |
using (var dataset = Dataset.Open(dataDir + "ThreeLayers.gdb", Drivers.FileGdb)) | |
{ | |
Console.WriteLine("FileGDB has {0} layers", dataset.LayersCount); | |
for (int i = 0; i < dataset.LayersCount; ++i) | |
{ | |
Console.WriteLine("Layer {0} name: {1}", i, dataset.GetLayerName(i)); | |
using (var layer = dataset.OpenLayerAt(i)) | |
{ | |
Console.WriteLine("Layer has {0} features", layer.Count); | |
foreach (var feature in layer) | |
{ | |
Console.WriteLine(feature.Geometry.AsText()); | |
} | |
} | |
Console.WriteLine(""); | |
} | |
} |
FileGDB를 GeoJSON으로 변환
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
//This will convert FileGDB dataset with three layers into single layered GeoJSON. | |
VectorLayer.Convert(dataDir + "ThreeLayers.gdb", Drivers.FileGdb, dataDir + "ThreeLayers_out.json", Drivers.GeoJson); |
레이어로 FileGDB 읽기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
//FileGDB can still be opened as a single layer. In this case, the opened layer will merge all layers inside FileGDB. | |
using (var layer = VectorLayer.Open(dataDir + "ThreeLayers.gdb", Drivers.FileGdb)) | |
{ | |
Console.WriteLine("All layers in FileGDB has {0} features", layer.Count); | |
foreach (var feature in layer) | |
{ | |
Console.WriteLine(feature.Geometry.AsText()); | |
} | |
} | |
ShapeFile을 데이터 세트로 열기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
//FileGDB can still be opened as a single layer. In this case, the opened layer will merge all layers inside FileGDB. | |
using (var layer = VectorLayer.Open(dataDir + "ThreeLayers.gdb", Drivers.FileGdb)) | |
{ | |
Console.WriteLine("All layers in FileGDB has {0} features", layer.Count); | |
foreach (var feature in layer) | |
{ | |
Console.WriteLine(feature.Geometry.AsText()); | |
} | |
} | |
FileGDB 데이터 세트에 레이어 추가
기존 FileGDB 데이터 세트를 열고 새 레이어를 추가합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
// -- copy test dataset, to avoid modification of test data. | |
var path = RunExamples.GetDataDir() + "ThreeLayers.gdb"; | |
var datasetPath = RunExamples.GetDataDir() + "AddLayerToFileGdbDataset_out.gdb"; | |
RunExamples.CopyDirectory(path, datasetPath); | |
// -- | |
using (var dataset = Dataset.Open(datasetPath, Drivers.FileGdb)) | |
{ | |
Console.WriteLine(dataset.CanCreateLayers); // True | |
using (var layer = dataset.CreateLayer("data", SpatialReferenceSystem.Wgs84)) | |
{ | |
layer.Attributes.Add(new FeatureAttribute("Name", AttributeDataType.String)); | |
var feature = layer.ConstructFeature(); | |
feature.SetValue("Name", "Name_1"); | |
feature.Geometry = new Point(12.21, 23.123, 20, -200); | |
layer.Add(feature); | |
} | |
using (var layer = dataset.OpenLayer("data")) | |
{ | |
Console.WriteLine(layer.Count); // 1 | |
Console.WriteLine(layer[0].GetValue<string>("Name")); // "Name_1" | |
} | |
} |
FileGDB 데이터 세트에서 레이어 제거
기존 FileGDB 데이터 세트를 열고 해당 레이어를 제거합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
// -- copy test dataset, to avoid modification of test data. | |
//var datasetPath = GetOutputPath(".gdb"); | |
var path = RunExamples.GetDataDir() + "ThreeLayers.gdb"; | |
var datasetPath = RunExamples.GetDataDir() + "RemoveLayersFromFileGdbDataset_out.gdb"; | |
RunExamples.CopyDirectory(path, datasetPath); | |
// -- | |
using (var dataset = Dataset.Open(datasetPath, Drivers.FileGdb)) | |
{ | |
Console.WriteLine(dataset.CanRemoveLayers); // True | |
Console.WriteLine(dataset.LayersCount); // 3 | |
// remove layer by index | |
dataset.RemoveLayerAt(2); | |
Console.WriteLine(dataset.LayersCount); // 2 | |
// remove layer by name | |
dataset.RemoveLayer("layer1"); | |
Console.WriteLine(dataset.LayersCount); // 1 | |
} |
FileGDB 데이터 세트 만들기
새 FileGDB 데이터 세트를 만들고 레이어를 추가합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
Console.WriteLine(Drivers.FileGdb.CanCreateDatasets); // True | |
var path = RunExamples.GetDataDir() + "CreateFileGdbDataset_out.gdb"; | |
using (var dataset = Dataset.Create(path, Drivers.FileGdb)) | |
{ | |
Console.WriteLine(dataset.LayersCount); // 0 | |
using (var layer = dataset.CreateLayer("layer_1")) | |
{ | |
layer.Attributes.Add(new FeatureAttribute("value", AttributeDataType.Integer)); | |
for (int i = 0; i < 10; ++i) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.SetValue("value", i); | |
feature.Geometry = new Point(i, i); | |
layer.Add(feature); | |
} | |
} | |
using (var layer = dataset.CreateLayer("layer_2")) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.Geometry = new LineString(new[] | |
{ | |
new Point(1, 2), | |
new Point(3, 4), | |
}); | |
layer.Add(feature); | |
} | |
Console.WriteLine(dataset.LayersCount); // 2 | |
} |
VectorLayer API를 사용하여 단일 레이어가 있는 FileGDB 데이터 세트 만들기
이 예제는 VectorLayer API를 사용하여 단일 레이어가 있는 FileGDB 데이터 세트를 만듭니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
var path = RunExamples.GetDataDir() + "CreateFileGdbDatasetWithSingleLayer_out.gdb"; | |
var options = new FileGdbOptions(); | |
using (var layer = VectorLayer.Create(path, Drivers.FileGdb, options, SpatialReferenceSystem.Wgs84)) | |
// this 'using' is equivalent to | |
// using (var dataset = Dataset.Create(path, Drivers.FileGdb)) | |
// using (var layer = Dataset.CreateLayer("layer")) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.Geometry = new LineString(new[] | |
{ | |
new Point(1, 2), | |
new Point(3, 4), | |
}); | |
layer.Add(feature); | |
} | |
using (var dataset = Dataset.Open(path, Drivers.FileGdb)) | |
using (var layer = dataset.OpenLayer("layer")) | |
{ | |
Console.WriteLine("Features count: {0}", layer.Count); // 1 | |
} |
GeoJSON 레이어를 FileGDB 데이터 세트 레이어로 변환
FileGDB에 새 레이어를 만들고 GeoJSON 레이어의 데이터를 추가합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
var geoJsonPath = RunExamples.GetDataDir() + "ConvertGeoJsonLayerToLayerInFileGdbDataset_out.json"; | |
using (VectorLayer layer = VectorLayer.Create(geoJsonPath, 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); | |
} | |
// -- | |
// -- copy test dataset, to avoid modification of test data. | |
var sourceFile = RunExamples.GetDataDir() + "ThreeLayers.gdb"; | |
var destinationFile = RunExamples.GetDataDir() + "ThreeLayersCopy_out.gdb"; | |
RunExamples.CopyDirectory(sourceFile, destinationFile); | |
// -- | |
using (var geoJsonLayer = VectorLayer.Open(geoJsonPath, Drivers.GeoJson)) | |
{ | |
using (var fileGdbDataset = Dataset.Open(destinationFile, Drivers.FileGdb)) | |
using (var fileGdbLayer = fileGdbDataset.CreateLayer("new_layer", SpatialReferenceSystem.Wgs84)) | |
{ | |
fileGdbLayer.CopyAttributes(geoJsonLayer); | |
foreach (var feature in geoJsonLayer) | |
{ | |
fileGdbLayer.Add(feature); | |
} | |
} | |
} |
FileGDB 데이터 세트 레이어에서 OBJECTID 읽기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var dataset = Dataset.Open(path, Drivers.FileGdb)) | |
using (var layer = dataset.OpenLayer("layer")) | |
{ | |
foreach (var feature in layer) | |
{ | |
Console.WriteLine(feature.GetValue<int>("OBJECTID")); | |
} | |
} |
FileGDB 레이어에 대한 정밀도 그리드 지정
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
var path = RunExamples.GetDataDir() + "PrecisionGrid_out.gdb"; | |
using (var dataset = Dataset.Create(path, Drivers.FileGdb)) | |
{ | |
var options = new FileGdbOptions | |
{ | |
// specify coordinate precision grid parameters (origins and scales for coordinates) | |
CoordinatePrecisionGrid = new FileGdbCoordinatePrecisionGrid | |
{ | |
// all our coordinates must be more than (-400, -400) point | |
XOrigin = -400, | |
YOrigin = -400, | |
// the write precision is 10 digits after the decimal point | |
XYScale = 1e10, | |
// M values are started at 0 and precision is 4 digits after the decimal point | |
MOrigin = 0, | |
MScale = 1e4, | |
}, | |
// throw whenever an attempt to write coordinate that does not fit precision grid is detected | |
EnsureValidCoordinatesRange = true, | |
}; | |
using (var layer = dataset.CreateLayer("layer_name", options, SpatialReferenceSystem.Wgs84)) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.Geometry = new Point(10, 20) { M = 10.1282 }; | |
layer.Add(feature); | |
feature = layer.ConstructFeature(); | |
// X == -410 is less than XOrigin, so an exception is thrown | |
feature.Geometry = new Point(-410, 0) { M = 20.2343 }; | |
try | |
{ | |
layer.Add(feature); | |
} | |
catch (GisException e) | |
{ | |
Console.WriteLine(e.Message); // X value -410 is out of valid range. | |
} | |
} | |
} |
FileGDB 레이어에 대한 허용 오차 지정
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
var path = RunExamples.GetDataDir() + "TolerancesForFileGdbLayer_out.gdb"; | |
using (var dataset = Dataset.Create(path, Drivers.FileGdb)) | |
{ | |
var options = new FileGdbOptions | |
{ | |
XYTolerance = 0.001, | |
ZTolerance = 0.1, | |
MTolerance = 0.1, | |
}; | |
using (var layer = dataset.CreateLayer("layer_name", options)) | |
{ | |
// layer is created with the provided tolerances and some ArcGIS features/tools will use it | |
} | |
} |
ObjectId 및 Geometry 필드 이름 지정
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
var path = RunExamples.GetDataDir() + "NamesOfObjectIdAndGeometryFields_out.gdb"; | |
using (var dataset = Dataset.Create(path, Drivers.FileGdb)) | |
{ | |
var options = new FileGdbOptions | |
{ | |
// name object ID field 'OID' rather than the default 'OBJECTID'. | |
ObjectIdFieldName = "OID", | |
// name geometry field 'POINT' rather than the default 'SHAPE'. | |
GeometryFieldName = "POINT", | |
}; | |
using (var layer = dataset.CreateLayer("layer_name", options, SpatialReferenceSystem.Wgs84)) | |
{ | |
var feature = layer.ConstructFeature(); | |
feature.Geometry = new Point(12.32, 34.21); | |
layer.Add(feature); | |
} | |
using (var layer = dataset.OpenLayer("layer_name")) | |
{ | |
var feature = layer[0]; | |
Console.WriteLine(feature.GetValue<int>("OID")); // 1 | |
} | |
} |