如何在C#中将地图渲染成SVG、JPG或PNG图像?Aspose快速搞定

KML是Keyhole标记语言的缩写,它是XML表示法的扩展。它是GIS文件格式,用于显示地理信息。可以以编程方式创建KML文件,也可以使用C#语言从其中读取信息。

让我们探索以下与KML文件格式有关的部分:

  • 使用C#以编程方式将地图从Shapefile渲染到SVG
  • 在C#中以编程方式将地图从Shapefile渲染到PNG图像
  • 使用C#使用高级样式自定义地图渲染
  • 使用C#以编程方式将地图从GeoTIFF格式渲染为SVG

Aspose.GIS for .NET API可让您使用不同的地理和地理空间文件格式。您可以根据需要将地图从受支持的文件格式渲染为SVG,PNG和其他文件格式。

>>你可以下载Aspose.GIS最新版测试体验。


使用C#以编程方式将Map从Shapefile渲染到SVG

可以按照以下步骤将地图从shapefile渲染到SVG文件:

  • 创建一个新的Map类实例
  • 创建矢量地图图层并将其添加到地图
  • 将地图渲染到文件中

下面的代码详细说明了如何使用C#以编程方式将地图从Shapefile渲染到SVG。

// Create new instance of Map class.
using (var map = new Rendering.Map(800, 400))
{
    // Create vector map layer and add it to map.
    map.Add(VectorLayer.Open("land.shp", Drivers.Shapefile));

    // Render the map into a file.
    map.Render("land_out.svg", Renderers.Svg);
}

在C#中以编程方式将地图从Shapefile渲染到PNG图像

可以将地图文件从shapefile渲染为光栅图像格式,如PNG,JPG,BMP等。以下是将地图文件从shapefile渲染为PNG图像的步骤:

  • 使用尺寸初始化Map类对象
  • 创建并添加矢量地图图层
  • 将地图渲染为PNG图像

以下代码显示了使用C#以编程方式将地图从Shapefile渲染到PNG图像的过程:

// Initialize Map class object with dimensions.
using (var map = new Rendering.Map(800, 400))
{
    // Create and add vector map layer.
    map.Add(VectorLayer.Open("land.shp", Drivers.Shapefile));

    // Render the map to a PNG image.
    map.Render("land_out.png", Renderers.Png);
}

使用C#使用高级样式自定义地图渲染

可以使用高级功能样式自定义地图的外观。请按照以下步骤进行自定义地图渲染:

  • 初始化Map类对象
  • 为简单的多边形符号器初始化SimpleFill
  • 从输入shapefile中打开图层以进行读取
  • 将地图渲染到文件中

以下代码说明了如何使用C#使用高级样式进行自定义地图渲染:

// Initialize Map class object.
using (var map = new Rendering.Map(800, 476))
{
    // Initilize SimpleFill for simple polygon symbolizer.
    var baseMapSymbolizer = new Rendering.Symbolizers.SimpleFill { FillColor = Color.Salmon, StrokeWidth = 0.75 };

    // Open the layer from input shapefile for reading.
    map.Add(VectorLayer.Open("basemap.shp", Drivers.Shapefile), baseMapSymbolizer);

    // Initialize a new instance of SimpleMarker class.
    var citiesSymbolizer = new Rendering.Symbolizers.SimpleMarker() { FillColor = Color.LightBlue };
    citiesSymbolizer.FeatureBasedConfiguration = (feature, symbolizer) =>
    {
        var population = feature.GetValue("population");
        symbolizer.Size = 10 * population / 1000;
        if (population < 2500) { symbolizer.FillColor = Color.GreenYellow; } }; // Add a VectorMapLayer to the map. map.Add(VectorLayer.Open("points.geojson", Drivers.GeoJson), citiesSymbolizer); // Render map into a file. map.Render("cities_out.svg", Renderers.Svg); }

使用C#以编程方式将地图从GeoTIFF格式渲染为SVG

可以按照以下步骤将地图从GeoTIFF渲染到SVG文件:

  • 实例化Map类对象
  • 使用GeoTIFF或TIFF格式的驱动程序打开要读取的层
  • 将地图渲染到文件中

以下代码段演示了如何使用C#以编程方式将地图从GeoTIFF格式呈现为SVG:

// Initialize Map class object.
using (var map = new Rendering.Map(500, 500))
{
    // Open the layer for reading using driver for the GeoTIFF or TIFF format.
    var layer = GeoTiff.OpenLayer("raster_float32.tif");
                
    // Conversion to colors is detected automatically.
    // The maximum and minimum values are calculated and linear interpolation is used.
    map.Add(layer);

    // Render map into a file.
    map.Render("raster_float32_out.svg", Renderers.Svg);
}

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章