gpt4 book ai didi

c# - 如何在 GMap.NET 输出之上或使用我的图形对象绘制 ESRI 形状

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:17 24 4
gpt4 key购买 nike

我有 C# 应用程序,它使用 GMap.NET 渲染世界地图和我的其他数据。我有要渲染到的 Graphics 对象。我还需要在我的 map 上将 ESRI 形状文件显示为半透明形状。我如何渲染它?可用的库看起来像是将绘制到自己的 Windows 控件中。另外,当我能够渲染它时,如何准确同步 map 和形状坐标?

谢谢,D.

最佳答案

GMap.Net 不支持直接读取 ESRI 形状文件,但您可以使用第 3 方库,如 MapTools它是 (ShapeLib) 库的 .Net 包装器。

您必须首先使用 (MapTools) 库读取形状文件并循环处理每个几何体(点、线、多边形),然后使用 Proj4Net Libarary 转换几何体投影并将每个几何图形添加到其在 GMapControl 上的适当叠加层。

您可以从this link 下载最新版本的(MapTools)。

下面是一个读取充满路线的形状文件的示例函数,它将返回 GMapRoutes 列表,然后您必须在 map 上绘制它们。

Public  Function ReadRoutesFromShapefile(ByVal filename As String) As List(Of GMapRoute)

Dim basePath As String = Path.GetDirectoryName(filename) & AscW(Path.DirectorySeparatorChar) & Path.GetFileNameWithoutExtension(filename)

If Not File.Exists(basePath & ".prj") Then
Throw New Exception("Could not find the projection file!")
End If

'Get info from WKT-File:
Dim wkt As String = File.ReadAllText(basePath & ".prj")

Dim gcs As ICoordinateSystem = TryCast(CoordinateSystemWktReader.Parse(wkt), ICoordinateSystem)
Dim doTransformation = Not gcs.AuthorityCode = 4326

Dim wgs84 As GeographicCoordinateSystem = GeographicCoordinateSystem.WGS84
Dim ctfac As New CoordinateTransformationFactory()
Dim trans As ICoordinateTransformation = ctfac.CreateFromCoordinateSystems(gcs, wgs84)

'Open shape-file
Dim shapeFile As IntPtr = ShapeLib.SHPOpen(basePath, "rb")

Dim numOfRecords As Integer = 0

'Check type and get number of entries
Dim shapeType As ShapeLib.ShapeType = 0
ShapeLib.SHPGetInfo(shapeFile, numOfRecords, shapeType, Nothing, Nothing)

If Not shapeType.Equals(ShapeLib.ShapeType.PolyLine) Then
Throw New Exception("The shape type is not polyline but " & shapeType)
End If

Dim lstRoutes As New List(Of GMapRoute)(numOfRecords)

'Get info from shapefile and save data:
For i As Integer = 0 To numOfRecords - 1
'Add all GPS-Points:
'Get pointer to object
Dim ptrPolyline As IntPtr = ShapeLib.SHPReadObject(shapeFile, i)

'Create actual object:
Dim polyline As New ShapeLib.SHPObject()
Marshal.PtrToStructure(ptrPolyline, polyline)

If polyline.nParts = 1 Then
'Get number of points and arrays of X and Y values:
Dim numPoints As Integer = polyline.nVertices

Dim lstpoints As New List(Of PointLatLng)(numPoints)

Dim xCoord(numPoints - 1) As Double
Dim yCoord(numPoints - 1) As Double

'Fill the arrays:
Marshal.Copy(polyline.padfX, xCoord, 0, numPoints)
Marshal.Copy(polyline.padfY, yCoord, 0, numPoints)

'Add all Points
For j As Integer = 0 To numPoints - 1
Dim latitude As Double = 0.0R
Dim longitude As Double = 0.0R

If doTransformation Then
'Convert from original coordinate system to wgs84
Dim fromPoint() As Double = {xCoord(j), yCoord(j)}
Dim toPoint() As Double = trans.MathTransform.Transform(fromPoint)
'Get point from polyline
longitude = toPoint(0)
latitude = toPoint(1)
Else
longitude = xCoord(j)
latitude = yCoord(j)
End If

lstpoints.Add(New PointLatLng(latitude, longitude))
Next j

lstRoutes.Add(New GMapRoute(lstpoints, "Route" & (i + 1).ToString()))
End If

polyline = Nothing
ShapeLib.SHPDestroyObject(ptrPolyline)
Next i

ShapeLib.SHPClose(shapeFile)

Return lstRoutes
End Function

关于c# - 如何在 GMap.NET 输出之上或使用我的图形对象绘制 ESRI 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com