Kết xuất Bản đồ thành Hình ảnh SVG, PNG, JPG bằng Thư viện GIS C#

Tổng quan về Kết xuất Bản đồ

Với Aspose.GIS for .NET C# API, bạn có thể kết xuất bản đồ từ Shapefile, FileGDB, GeoJSON, KML hoặc các định dạng tệp được hỗ trợ khác thành SVG, PNG, JPEG hoặc BMP.

Đây là mã C# minh họa cách kết xuất bản đồ từ một shapefile sang SVG bằng cài đặt mặc định:

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(800, 400))
{
map.Add(VectorLayer.Open(dataDir + "land.shp", Drivers.Shapefile));
map.Render(dataDir + "land_out.svg", Renderers.Svg);
}

Đây là kết quả:

map rendering

Hãy xem xét kỹ hơn về mã này.

Trước tiên, chúng ta khởi tạo một đối tượng Map . Nó đại diện cho một tập hợp các lớp từ nhiều nguồn khác nhau có thể được kết xuất. Bản đồ có kích thước mà nó dự định hiển thị. Ở đây, chúng ta đặt bản đồ rộng 800 pixel và cao 400 pixel.

Lưu ý rằng Map được bao bọc trong câu lệnh using. Điều này là cần thiết vì bản đồ theo dõi tất cả các tài nguyên được thêm vào nó và giải phóng chúng khi chúng ta hoàn thành kết xuất và đối tượng Map bị giải phóng.

Tiếp theo, chúng ta thêm một lớp từ tệp vào bản đồ. Mỗi lớp được kết xuất trên lớp trước đó, theo thứ tự mà chúng được thêm vào bản đồ. Xem thêm chi tiết về cách mở các lớp vector tại đây.

Cuối cùng, chúng ta gọi Map.Render để kết xuất bản đồ thành một tệp. Chúng ta chỉ định đường dẫn đến nơi lưu trữ tệp kết quả và trình kết xuất sẽ sử dụng. Lớp Renderers chứa các tham chiếu đến tất cả các trình kết xuất được bao gồm trong Aspose.GIS. Ví dụ: bạn có thể chỉ định Renderers.Png thay vì Renderers.Svg trong ví dụ trên để kết xuất bản đồ thành tệp PNG

Tạo kiểu Nâng cao

Với API Aspose.GIS, bạn có thể tùy chỉnh kết xuất và kiểu dáng tính năng để đạt được giao diện mong muốn.

advanced styling

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(800, 476))
{
var baseMapSymbolizer = new SimpleFill { FillColor = Color.Salmon, StrokeWidth = 0.75 };
map.Add(VectorLayer.Open(dataDir + "basemap.shp", Drivers.Shapefile), baseMapSymbolizer);
var citiesSymbolizer = new SimpleMarker() { FillColor = Color.LightBlue };
citiesSymbolizer.FeatureBasedConfiguration = (feature, symbolizer) =>
{
var population = feature.GetValue<int>("population");
symbolizer.Size = 10 * population / 1000;
if (population < 2500)
{
symbolizer.FillColor = Color.GreenYellow;
}
};
map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), citiesSymbolizer);
map.Render(dataDir + "cities_out.svg", Renderers.Svg);
}

Vẽ raster trong bản đồ

Với Aspose.GIS for .NET, bạn có thể kết xuất một bản đồ từ các định dạng raster.

Kết xuất với cài đặt mặc định

Đây là cách kết xuất bản đồ từ GeoTIFF sang SVG bằng cài đặt mặc định:

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
string filesPath = RunExamples.GetDataDir();
using (var map = new Map(500, 500))
{
var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_float32.tif"));
// Conversion to colors is detected automatically.
// The maximum and minimum values are calculated and linear interpolation is used.
map.Add(layer);
map.Render(filesPath + "raster_float32_out.svg", Renderers.Svg);
}

default raster

Kết xuất raster bị lệch

Với Aspose.GIS, bạn có thể kết xuất một raster với các ô raster bị lệch.

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
string filesPath = RunExamples.GetDataDir();
using (var map = new Map(500, 500))
{
// use background color
map.BackgroundColor = Color.Azure;
var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_skew.tif"));
// Conversion to colors is detected automatically.
// The maximum and minimum values are calculated and linear interpolation is used.
map.Add(layer);
map.Render(filesPath + "raster_skew_out.svg", Renderers.Svg);
}

skew raster

Kết xuất trong tham chiếu không gian cực

Aspose.GIS cho phép bạn sử dụng các tham chiếu không gian cực trên quy trình kết xuất bản đồ.

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
string filesPath = RunExamples.GetDataDir();
// make own multi colorizer it works faster than auto-detection
var colorizer = new MultiBandColor()
{
RedBand = new BandColor() { BandIndex = 0, Min = 0, Max = 255 },
GreenBand = new BandColor() { BandIndex = 1, Min = 0, Max = 255 },
BlueBand = new BandColor() { BandIndex = 2, Min = 0, Max = 255 }
};
using (var map = new Map(500, 500))
{
// setup the polar extent and coordinate system (gnomonic spatial reference)
map.SpatialReferenceSystem = SpatialReferenceSystem.CreateFromEpsg(102034);
map.Extent = new Extent(-180, 60, 180, 90) { SpatialReferenceSystem = SpatialReferenceSystem.Wgs84 };
map.BackgroundColor = Color.Azure;
// open geo-tiff
var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_countries.tif"));
// draw
map.Add(layer, colorizer);
map.Render(filesPath + "raster_countries_gnomonic_out.png", Renderers.Png);
}

gnomonic countries