使用 GIS C# 库将地图渲染为图像 SVG、PNG、JPG
地图渲染概述
使用 Aspose.GIS for .NET C# API,您可以将来自 Shapefile、FileGDB、GeoJSON、KML 或其他 支持的文件格式 的地图渲染为 SVG、PNG、JPEG 或 BMP。
以下是说明如何使用默认设置从 shapefile 渲染地图的 C# 代码:
// 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); | |
} |
这是结果:
让我们更详细地了解代码。
首先,我们实例化一个 Map 对象。它代表可以渲染的来自各种来源的图层集合。 Map 具有其打算显示的尺寸。在这里,我们将地图设置为宽度为 800 像素、高度为 400 像素。
请注意,Map 包含在 using 语句中。这是必要的,因为 map 会跟踪添加到其中的所有资源,并在完成渲染且 Map 对象被释放时释放它们。
接下来,我们将来自文件的图层添加到地图中。每个图层都渲染在之前的图层之上,按照其添加到地图中的顺序进行渲染。有关如何打开矢量图层的更多详细信息,请参阅 此处。
最后,我们调用 Map.Render 将地图渲染到文件。我们指定保存结果文件的路径和要使用的渲染器。类 Renderers 包含 Aspose.GIS 中包含的所有渲染器的引用。例如,您可以指定 Renderers.Png 而不是上述示例中的 Renderers.Svg 将地图渲染为 PNG 文件
高级样式设置
使用 Aspose.GIS API,您可以自定义渲染和特征样式以实现所需的外观。
// 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); | |
} |
在地图中绘制栅格
使用 Aspose.GIS for .NET,您可以从栅格格式渲染地图。
使用默认设置渲染
以下是如何使用默认设置将 GeoTIFF 渲染为 SVG 的方法:
// 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); | |
} |
渲染倾斜栅格
使用 Aspose.GIS,您可以渲染具有倾斜栅格单元的栅格。
// 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); | |
} |
在极坐标空间参考中渲染
Aspose.GIS 允许您在地图渲染过程中使用极坐标空间参考。
// 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); | |
} |