Renderowanie mapy do obrazu SVG, PNG, JPG za pomocą biblioteki GIS C#
Przegląd renderowania map
Dzięki API Aspose.GIS dla .NET C# możesz renderować mapę z pliku Shapefile, FileGDB, GeoJSON, KML lub innych obsługiwanych formatów plików do SVG, PNG, JPEG lub BMP.
Oto kod C#, który ilustruje sposób renderowania mapy z pliku shapefile do SVG przy użyciu domyślnych ustawień:
// 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); | |
} |
Oto wynik:
Przyjrzyjmy się bliżej kodowi.
Najpierw tworzymy obiekt Map . Reprezentuje on kolekcję warstw z różnych źródeł, które mogą być renderowane. Mapa ma rozmiar, w którym ma być wyświetlana. Tutaj ustawiamy mapę na 800 pikseli szerokości i 400 pikseli wysokości.
Zauważ, że mapa jest zamknięta w instrukcji using. Jest to konieczne, ponieważ mapa śledzi wszystkie zasoby dodane do niej i usuwa je po zakończeniu renderowania i usunięciu obiektu Mapy.
Następnie dodajemy warstwę z pliku do mapy. Każda warstwa jest renderowana na wierzchu poprzedniej warstwy, w kolejności, w jakiej zostały dodane do mapy. Więcej szczegółów o tym, jak otwierać warstwy wektorowe, można znaleźć tutaj.
Na koniec wywołujemy metodę Map.Render, aby renderować mapę do pliku. Określamy ścieżkę, w której ma zostać zapisany wynikowy plik i renderer do użycia. Klasa Renderers zawiera odwołania do wszystkich rendererów zawartych w Aspose.GIS. Na przykład możesz określić Renderers.Png zamiast Renderers.Svg w powyższym przykładzie, aby renderować mapę do pliku PNG
Zaawansowane stylizowanie
Dzięki API Aspose.GIS możesz dostosowywać renderowanie i style cech, aby osiągnąć pożądany wygląd.
// 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); | |
} |
Rysowanie rastra na mapie
Dzięki Aspose.GIS dla .NET możesz renderować mapę z formatów rastrowych.
Renderowanie z domyślnymi ustawieniami
Oto jak renderować mapę z GeoTIFF do SVG przy użyciu domyślnych ustawień:
// 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); | |
} |
Renderowanie skośnych rastrów
Dzięki Aspose.GIS możesz renderować rastry ze skośnymi komórkami rastrowymi.
// 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); | |
} |
Renderowanie w biegunowym układzie współrzędnych
Aspose.GIS umożliwia używanie biegunowych układów współrzędnych w procesie renderowania mapy.
// 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); | |
} |