Renderizado de Mapas a Imagen SVG, PNG, JPG usando la Biblioteca GIS C#
Descripción general del renderizado de mapas
Con la API Aspose.GIS para .NET C#, puede renderizar un mapa desde un Shapefile, FileGDB, GeoJSON, KML u otros formatos de archivo compatibles a SVG, PNG, JPEG o BMP.
Aquí hay código C# que ilustra cómo renderizar un mapa desde un shapefile a SVG usando la configuración predeterminada:
// 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); | |
} |
Aquí está el resultado:
Echemos un vistazo más de cerca al código.
Primero, instanciamos un objeto Map . Representa una colección de capas de diversas fuentes que se pueden renderizar. Un mapa tiene un tamaño en el que pretende mostrarse. Aquí configuramos el mapa para que tenga 800 píxeles de ancho y 400 píxeles de alto.
Tenga en cuenta que el Mapa está encerrado en la instrucción using. Esto es necesario porque el mapa realiza un seguimiento de todos los recursos agregados a él y los elimina cuando terminamos con el renderizado y se elimina el objeto Map.
A continuación, agregamos una capa desde un archivo al mapa. Cada capa se renderiza sobre la capa anterior, en el orden en que se agregaron al mapa. Consulte más detalles sobre cómo abrir capas vectoriales aquí.
Finalmente, llamamos a Map.Render para renderizar el mapa en un archivo. Especificamos una ruta donde guardar el archivo de resultados y un renderizador que se utilizará. La clase Renderers contiene referencias a todos los renderizadores incluidos con Aspose.GIS. Por ejemplo, puede especificar Renderers.Png en lugar de Renderers.Svg en el ejemplo anterior para renderizar el mapa en un archivo PNG
Estilo avanzado
Con la API Aspose.GIS, puede personalizar el renderizado y los estilos de las características para lograr el aspecto que desea.
// 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); | |
} |
Dibujar ráster en el mapa
Con Aspose.GIS para .NET, puede renderizar un mapa desde formatos ráster.
Renderizar con la configuración predeterminada
Aquí le mostramos cómo renderizar un mapa de GeoTIFF a SVG utilizando la configuración predeterminada:
// 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); | |
} |
Renderizar rásters sesgados
Con Aspose.GIS puede renderizar un ráster con celdas de ráster sesgadas.
// 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); | |
} |
Renderizar en referencia espacial polar
Aspose.GIS le permite utilizar referencias espaciales polares en un proceso de renderizado de mapas.
// 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); | |
} |