Rendering di mappe in immagine SVG, PNG, JPG utilizzando la libreria GIS C#
Panoramica del rendering di mappe
Con l’API Aspose.GIS per .NET C# puoi renderizzare una mappa da un Shapefile, FileGDB, GeoJSON, KML o altri formati di file supportati in SVG, PNG, JPEG o BMP.
Ecco il codice C# che illustra come renderizzare una mappa da un shapefile a SVG utilizzando le impostazioni predefinite:
// 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); | |
} |
Ecco il risultato:
Esaminiamo più da vicino il codice.
Innanzitutto, istanziamo un oggetto Map . Rappresenta una raccolta di layer provenienti da varie fonti che possono essere renderizzati. Una mappa ha una dimensione in cui deve essere visualizzata. Qui impostiamo la mappa su 800 pixel di larghezza e 400 pixel di altezza.
Nota che la Mappa è racchiusa nell’istruzione using. Questo è necessario perché la mappa tiene traccia di tutte le risorse aggiunte ad essa e le elimina quando abbiamo finito di renderizzare e l’oggetto Map viene eliminato.
Successivamente, aggiungiamo un layer da un file alla mappa. Ogni layer viene renderizzato sopra il layer precedente, nell’ordine in cui sono stati aggiunti alla mappa. Vedi maggiori dettagli su come aprire i layer vettoriali qui.
Infine, chiamiamo Map.Render per renderizzare la mappa in un file. Specifichiamo un percorso dove salvare il file di risultato e un renderer da utilizzare. La classe Renderers contiene riferimenti a tutti i renderer inclusi con Aspose.GIS. Ad esempio, puoi specificare Renderers.Png invece di Renderers.Svg nell’esempio precedente per renderizzare la mappa in un file PNG
Styling avanzato
Con l’API Aspose.GIS, puoi personalizzare il rendering e gli stili delle feature per ottenere l’aspetto desiderato.
// 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); | |
} |
Disegna raster nella mappa
Con Aspose.GIS per .NET puoi renderizzare una mappa da formati raster.
Render con impostazioni predefinite
Ecco come renderizzare una mappa da un GeoTIFF a SVG utilizzando le impostazioni predefinite:
// 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 raster distorti
Con Aspose.GIS puoi renderizzare un raster con celle raster distorte.
// 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 riferimento spaziale polare
Aspose.GIS ti consente di utilizzare riferimenti spaziali polari su un processo di rendering della mappa.
// 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); | |
} |