PostGIS - Работа с ГИС слоями в PostgreSQL базах данных используя C#
Contents
[
Hide
]
PostgreSQL (расширение PostGIS) являются одной из наиболее широко используемых баз данных среди ГИС программного обеспечения. Aspose.GIS позволяет вам работать с базами данных PostgreSQL.
Перебор слоев в базе данных PostGis
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 | |
// First, we create the connection to the PostgreSQL with Npgsql library (https://www.nuget.org/packages/Npgsql/) | |
// Then, we pass this connection to Dataset.Open method in order to access tables in the PostgreSQL. | |
using (var connection = new NpgsqlConnection(postgreSqlConnectionString)) | |
{ | |
connection.Open(); | |
using (var ds = Dataset.Open(connection, Drivers.PostGis)) | |
{ | |
// Only spatial tables are exported as layers, so the following code will list all tables | |
// that have geometry column. | |
for (int i = 0; i < ds.LayersCount; ++i) | |
{ | |
Console.WriteLine(ds.GetLayerName(i)); | |
} | |
} | |
} |
Добавление нового слоя в базу данных PostGis
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 | |
// First, we create the connection to the PostgreSQL with Npgsql library (https://www.nuget.org/packages/Npgsql/) | |
using (var connection = new NpgsqlConnection(postgreSqlConnectionString)) | |
{ | |
connection.Open(); | |
// Then, we pass this connection to Dataset.Open method in order to access tables in the PostgreSQL. | |
using (var ds = Dataset.Open(connection, Drivers.PostGis)) | |
{ | |
// create table with the name "features_table" and fill it with data. | |
using (var layer = ds.CreateLayer("features_table")) | |
{ | |
layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String) { Width = 50 }); | |
var feature = layer.ConstructFeature(); | |
feature.SetValue("name", "Name1"); | |
feature.Geometry = Geometry.FromText("POINT (10 20 30)"); | |
layer.Add(feature); | |
feature = layer.ConstructFeature(); | |
feature.SetValue("name", "Name2"); | |
feature.Geometry = Geometry.FromText("POINT (-10 -20 -30)"); | |
layer.Add(feature); | |
} | |
} | |
} |
Экспорт слоя из базы данных PostGis в файловый формат
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 outputPath = Path.Combine(RunExamples.GetDataDir(), "postgres_out.shp"); | |
// First, we create the connection to the PostgreSQL with Npgsql library (https://www.nuget.org/packages/Npgsql/) | |
// Then, we pass this connection to Dataset.Open method in order to access tables in the PostgreSQL. | |
using (var connection = new NpgsqlConnection(postgreSqlConnectionString)) | |
{ | |
connection.Open(); | |
using (var ds = Dataset.Open(connection, Drivers.PostGis)) | |
{ | |
// open table with the name "features_table" in Postgres database and save it to the shapefile. | |
using (var table = ds.OpenLayer("features_table")) | |
{ | |
table.SaveTo(outputPath, Drivers.Shapefile); | |
} | |
} | |
} |