解析 MSDN 几何数据类型
我有一个数据库,其中一个字段给出空间坐标。我了解到该字段是序列化的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如下面的评论所示(感谢 MarkJ!):
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!):
select geocolumn.STX, geocolumn.STY from myTable;
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_BindingsI had originally thought SQL Server stored CLR data types as serialized .NET objects directly in the table but this turned out to be wrong.
SQL Server 空间
GEOMETRY
和GEOGRAPHY
类型的二进制序列化格式为此处指定:这个规范写得很好,二进制格式很容易理解,因此您自己实现二进制格式的基本解析器应该不成问题。
通过 .NET 互操作使用
Microsoft.SqlServer.Types
来反序列化这些类型:如果您不想实现自己的反序列化器(这应该相当简单),但您可以找到一种与 Python 中的 .NET 程序集交互的方法 - 也许通过 pythonnet? —,那么以下提示可能会引起您的兴趣:
两种 T-SQL 类型
GEOMETRY
和GEOGRAPHY
是作为 .NET 程序集 (Microsoft. SqlServer.Types
)执行从/到上面提到的二进制格式的反/序列化,以及一个非托管 DLL(SqlServerSpatial….dll
),它包含几乎所有其他内容(即空间操作例程)。如果您只对反序列化 SQL Server 空间数据感兴趣,并且小心地不要调用
SqlGeometry
或SqlGeography
上的任何空间函数,那么您也许可以使用 Microsoft.SqlServer.Types 为您反序列化空间二进制数据,然后通过实现检查它您必须提供给例如SqlGeometry.Populate
方法的IGeometrySink110
。Microsoft.SqlServer.Types
和SqlServerSpatial….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 部分:空间标准中添加了对圆弧的支持。)
The binary serialization format for SQL Server's spatial
GEOMETRY
andGEOGRAPHY
types is specified here: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.
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
andGEOGRAPHY
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
orSqlGeography
, then you might be able to useMicrosoft.SqlServer.Types
to de-serialize spatial binary data for you, then inspecting it with an implementation ofIGeometrySink110
that you have to provide to e.g. theSqlGeometry.Populate
method.Microsoft.SqlServer.Types
andSqlServerSpatial….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) usingSELECT geometryColumn.STAsText()
orSELECT 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.)