การแสดงผลแผนที่ไปยังรูปภาพ SVG, PNG, JPG โดยใช้ไลบรารี GIS C#

ภาพรวมการแสดงผลแผนที่

ด้วย Aspose.GIS for .NET C# API คุณสามารถแสดงผลแผนที่จาก Shapefile, FileGDB, GeoJSON, KML หรือ รูปแบบไฟล์ที่รองรับอื่นๆ ไปยัง SVG, PNG, JPEG หรือ BMP

นี่คือโค้ด C# ที่แสดงวิธีการแสดงผลแผนที่จาก shapefile ไปยัง SVG โดยใช้การตั้งค่าเริ่มต้น:

// 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);
}

นี่คือผลลัพธ์:

map rendering

ลองดูโค้ดอย่างใกล้ชิดกัน

ขั้นแรก เราสร้างอินสแตนซ์ออบเจ็กต์ Map ออบเจ็กต์นี้แสดงถึงคอลเลกชันของเลเยอร์จากแหล่งต่างๆ ที่สามารถแสดงผลได้ แผนที่หนึ่งแผนที่มีขนาดที่ตั้งใจจะแสดงผล ที่นี่เรากำหนดให้แผนที่เป็นความกว้าง 800 พิกเซล และสูง 400 พิกเซล

โปรดสังเกตว่า Map ถูกปิดไว้ในคำสั่ง using นี่เป็นสิ่งจำเป็นเนื่องจาก map จะติดตามทรัพยากรทั้งหมดที่เพิ่มเข้าไป และกำจัดทิ้งเมื่อเราเสร็จสิ้นการแสดงผลและออบเจ็กต์ Map ถูกกำจัดทิ้ง

ถัดไป เราเพิ่มเลเยอร์จากไฟล์ไปยังแผนที่ แต่ละเลเยอร์จะถูกแสดงซ้อนทับบนเลเยอร์ก่อนหน้า ตามลำดับที่เพิ่มลงในแผนที่ ดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีการเปิดเลเยอร์เวกเตอร์ ได้ที่นี่

สุดท้าย เราเรียกใช้ Map.Render เพื่อแสดงผลแผนที่เป็นไฟล์ เรากำหนดเส้นทางไปยังตำแหน่งที่จะบันทึกไฟล์ผลลัพธ์ และตัวเรนเดอร์ที่จะใช้ คลาส Renderers มีการอ้างอิงถึงตัวเรนเดอร์ทั้งหมดที่รวมอยู่ใน Aspose.GIS ตัวอย่างเช่น คุณสามารถระบุ Renderers.Png แทน Renderers.Svg ในตัวอย่างข้างต้นเพื่อแสดงผลแผนที่เป็นไฟล์ PNG

รูปแบบขั้นสูง

ด้วย API ของ Aspose.GIS คุณสามารถปรับแต่งการแสดงผลและรูปแบบคุณสมบัติเพื่อให้ได้รูปลักษณ์ที่คุณต้องการ

advanced styling

// 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);
}

วาด raster ในแผนที่

ด้วย Aspose.GIS for .NET คุณสามารถแสดงผลแผนที่จากรูปแบบ raster ได้

แสดงผลด้วยการตั้งค่าเริ่มต้น

นี่คือวิธีการแสดงผลแผนที่จาก GeoTIFF ไปยัง SVG โดยใช้การตั้งค่าเริ่มต้น:

// 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);
}

default raster

แสดงผล rasters ที่เบ้

ด้วย Aspose.GIS คุณสามารถแสดงผล raster ที่มีเซลล์ raster เบ้ได้

// 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);
}

skew raster

แสดงผลในการอ้างอิงเชิงพื้นที่แบบ polar

Aspose.GIS ช่วยให้คุณใช้การอ้างอิงเชิงพื้นที่แบบ polar ในกระบวนการแสดงผลแผนที่ได้

// 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);
}

gnomonic countries