Map Rendering to Image SVG, PNG, JPG using GIS C# Library
Map Rendering Overview
With Aspose.GIS for .NET C# API you can render a map from a Shapefile, FileGDB, GeoJSON, KML or other supported file formats to SVG, PNG, JPEG, or BMP.
Here is C# code illustrating how to render a map from a shapefile to SVG using default settings:
// 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); | |
} |
Here’s the result:
Let’s take a closer look at the code.
First, we instantiate a Map object. It represents a collection of layers from various sources that can be rendered. A Map has a size it’s intended to be displayed at. Here we set the map to be 800 pixels wide and 400 pixels tall.
Notice that the Map is enclosed into the using statement. This is necessary because the map keeps track of all resources added to it, and disposes them when we’re done with rendering and Map object is disposed.
Next, we add a layer from a file to the map. Each layer is rendered on top of the previous layer, in the order in which they were added to the map. See more details about how to open vector layers here.
Finally, we call Map.Render to render the map into a file. We specify a path to where to save the result file and a renderer to use. Class Renderers contains references to all renderers included with Aspose.GIS. For example, you can specify Renderers.Png instead of Renderers.Svg in the example above to render the map into a PNG file
Advanced Styling
With Aspose.GIS API, you can customize rendering and feature styles in order to achieve the look you want.
// 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); | |
} |
Draw raster in map
With Aspose.GIS for .NET you can render a map from raster formats.
Render with default settings
Here’s how to render a map from a GeoTIFF to SVG using default settings:
// 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); | |
} |
Render skew rasters
With Aspose.GIS you can render a raster with skew raster cells.
// 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); | |
} |
Render in polar spatial reference
Aspose.GIS lets you using polar spatial references on a map rendering process.
// 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); | |
} |