解析 MSDN 几何数据类型

发布于 2024-09-14 02:29:26 字数 380 浏览 5 评论 0原文

我有一个数据库,其中一个字段给出空间坐标。我了解到该字段是序列化的 MSDN 几何数据类型 (http://msdn. microsoft.com/en-us/library/bb933973.aspx)。

我想从 Python 访问这个数据库,并且想知道是否有人知道几何数据类型的格式,或者任何能够将其解析为 Python 中的一组地理坐标的库。

该链接指出,微软在设计此数据类型时使用了“开放地理空间联盟(OGC)标准”,这是否意味着空间坐标是由该标准定义的?

还有人有这方面的经验吗?

任何帮助将不胜感激!

I have a database where one field gives spatial coordinates. I have learned the field is a serialised MSDN geometry Data Type (http://msdn.microsoft.com/en-us/library/bb933973.aspx).

I want to access this database from Python and was wandering if anyone knew the format of the Geometry Data Type, or any libraries capable of parsing it out into a set of Geo Coordinates in Python.

The link states that Microsoft used the "Open Geospatial Consortium (OGC) standard" in designing this data type, does this mean the spatial coordinates are defined by this standard?

Does any one else have any experience with this?

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ偶尔ゞ 2024-09-21 02:29:26

正如下面的评论所示(感谢 MarkJ!):

  • 几何是 .NET 数据类型,但使用其自己的自定义序列化格式;您可以选择整个列,然后通过在 Reflector 中打开 Microsoft.SqlServer.Types.dll 并从以下位置开始重新实现它,
  • 或者您可以使用该类型的 SQL Server 支持从数据库中读出几何数据的属性,例如 < code>select geocolumn.STX, geocolumn.STY from myTable;
  • 或者您可以将整个值导出为 GML ,例如 select geocolumn.AsGml() from myTable; ,可以由 Python 几何库处理,例如 http://gispython.org/ http://mapnik.org/ http://www.qgis.org/wiki/Python_Bindings

我原本以为 SQL Server 存储CLR 数据类型直接在表中序列化 .NET 对象,但事实证明这是错误的。

As figured out in the comments below (thanks MarkJ!):

  • geometry is a .NET datatype but uses its own custom serialization format; you could select the column whole and then reimplement this by opening Microsoft.SqlServer.Types.dll in Reflector and starting from
  • or you can use the SQL server support for the type to read out the properties of the geometry data from the database, e.g. select geocolumn.STX, geocolumn.STY from myTable;
  • or you can export the whole value as GML with e.g. select geocolumn.AsGml() from myTable; which can be processed by Python geometry libraries such as http://gispython.org/ http://mapnik.org/ http://www.qgis.org/wiki/Python_Bindings

I had originally thought SQL Server stored CLR data types as serialized .NET objects directly in the table but this turned out to be wrong.

娇俏 2024-09-21 02:29:26

“……如果有人知道几何[数据类型]的格式……”

SQL Server 空间 GEOMETRYGEOGRAPHY 类型的二进制序列化格式为此处指定:

[MS-SSCLRT]:Microsoft SQL Server CLR 类型序列化格式

“指定 SQL Server 管理的 GEOGRAPHY、GEOMETRY、HIERARCHYID 和 CLR 用户定义类型 (UDT) 结构的二进制格式。”

这个规范写得很好,二进制格式很容易理解,因此您自己实现二进制格式的基本解析器应该不成问题。

“……或者任何能够在 Python 中将其解析为一组[地理坐标]的库……”

通过 .NET 互操作使用 Microsoft.SqlServer.Types 来反序列化这些类型:

如果您不想实现自己的反序列化器(这应该相当简单),但您可以找到一种与 Python 中的 .NET 程序集交互的方法 - 也许通过 pythonnet? —,那么以下提示可能会引起您的兴趣:

两种 T-SQL 类型 GEOMETRYGEOGRAPHY 是作为 .NET 程序集 (Microsoft. SqlServer.Types)执行从/到上面提到的二进制格式的反/序列化,以及一个非托管 DLL(SqlServerSpatial….dll),它包含几乎所有其他内容(即空间操作例程)。

如果您只对反序列化 SQL Server 空间数据感兴趣,并且小心地不要调用 SqlGeometrySqlGeography 上的任何空间函数,那么您也许可以使用 Microsoft.SqlServer.Types 为您反序列化空间二进制数据,然后通过实现检查它您必须提供给例如SqlGeometry.Populate方法IGeometrySink110

Microsoft.SqlServer.TypesSqlServerSpatial….dll 可以作为 .NET 项目范围的 NuGet 包,或作为系统范围的MSI 安装包 (SQLSysClrTypes.msi)。 AFAIK DLL 也会随 SQL Server 自动安装。

众所周知的文本 (WKT) 和众所周知的二进制 (WKB):

另一种选择是让 SQL Server 将空间值转换为众所周知的文本 (WKT) 或众所周知的二进制 (WKB) )使用 SELECT GeometryColumn.STAsText()SELECT GeometryColumn.STAsBinary(),然后查找可以解析这些标准交换格式的 Python 库。

(需要注意的是:如果您沿着这条路线走下去,请小心您的数据是否包含圆弧。WKT 和 WKB 数据格式有不同版本。它们首先被指定为 Open Open 的简单功能访问规范的一部分地理空间联盟;该版本不支持圆弧。在 SQL/MM 第 3 部分:空间标准中添加了对圆弧的支持。)

"…if anyone knew the format of the Geometry [data type]…"

The binary serialization format for SQL Server's spatial GEOMETRY and GEOGRAPHY types is specified here:

[MS-SSCLRT]: Microsoft SQL Server CLR Types Serialization Formats

"Specifies the binary format of the GEOGRAPHY, GEOMETRY, HIERARCHYID, and CLR user-defined type (UDT) structures that are managed by SQL Server."

This specification is well written and the binary format is easy to understand, so it shouldn't be much of a problem implementing a basic parser for the binary format yourself.

"…or any libraries capable of parsing it out into a set of [geo coordinates] in Python…"

Using Microsoft.SqlServer.Types via .NET interop to deserialize these types:

If you don't want to implement your own de-serializer (which should be fairly simple), but you can find a way to interact with a .NET assembly from Python — perhaps via pythonnet? —, then the following hints may be of interest:

The two T-SQL types GEOMETRY and GEOGRAPHY are implemented as a combination of a .NET assembly (Microsoft.SqlServer.Types) that performs the de-/serialization from/to the binary format just mentioned above, and an unmanaged DLL (SqlServerSpatial….dll) which contains almost everything else (i.e. the routines for spatial operations).

If you're only interested in de-serializing SQL Server spatial data, and you're careful not to invoke any spatial functions on SqlGeometry or SqlGeography, then you might be able to use Microsoft.SqlServer.Types to de-serialize spatial binary data for you, then inspecting it with an implementation of IGeometrySink110 that you have to provide to e.g. the SqlGeometry.Populate method.

Microsoft.SqlServer.Types and SqlServerSpatial….dll are available either as a .NET project-wide NuGet package, or as a system-wide MSI installation package (SQLSysClrTypes.msi). AFAIK the DLLs are also automatically installed with SQL Server.

Well-Known Text (WKT) and Well-Known Binary (WKB):

One more option would be to let SQL Server translate spatial values to Well-Known Text (WKT) or Well-Known Binary (WKB) using SELECT geometryColumn.STAsText() or SELECT geometryColumn.STAsBinary(), then look for a Python library that can parse these standard interchange formats.

(One word of caution: If you go down that route, just be careful if your data contains circular arcs. There are different versions of the WKT and WKB data format. They were first specified as part of the Simple Features Access specification of the Open Geospatial Consortium; that version doesn't understand about circular arcs. Support for circular curve segments was added in the SQL/MM Part 3: Spatial standard, which SQL Server implements.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文