在 OpenLayers 中保存和恢复几何图形
上下文:我是 OpenLayers 刚入职几个小时的新手,请保持温柔。
基本上,我有一张地图,上面有一些绘制的对象。如果我理解正确的话,我有许多 OpenLayer.Feature.Vector (层?),上面有许多 OpenLayer.Geometry “东西”(如 LinearRing)。
目前,我似乎能够使用 .toString() 获得几何图形的良好表示。是的,我怀疑我做错了——请随意指出我正确的方向。
这会产生非常人类可读且可存储在数据库中的字符串,例如:
POINT(-104.74560546875 44.2841796875)
POLYGON((-96.52783203125 44.6 796875,-96.52783203125 45.734375,-92.22119140625 45.734375,-92.22119140625 44.6796875,-96.52783203125 44.6796875))
LINESTRING(-105.71240234375 44.6796875,-106.06396484375 42.658203125,-103.55908203125 42.7021484375,-103.47119140625 45.55859375,-104.65771484375 45.20703125)
Is there an inverse way of getting这些返回到它们从何而来的对象格式?
我很想使用 JSON,但似乎无法让 GeoJSON 接受我的 OpenLayer.Feature.Vector 对象(其中这是当我向内部查看时 CLASS_NAME 属性所说的内容)。
非常感谢。
Context: I'm a just-hours-old newbie at OpenLayers, please be gentle.
Fundamentally, I have a map with some drawn objects on it. If I understand things correctly, I have a number of OpenLayer.Feature.Vector (layers?) with a number of OpenLayer.Geometry "things" (like LinearRing) on it.
At the moment, I seem to be able to get a nice representation of the geometry, using .toString(). Yes, I suspect I'm doing it wrong -- feel free to point me in the right direction.
This yields a very human readable, and database storable, strings such as:
POINT(-104.74560546875 44.2841796875)
POLYGON((-96.52783203125 44.6796875,-96.52783203125 45.734375,-92.22119140625 45.734375,-92.22119140625 44.6796875,-96.52783203125 44.6796875))
LINESTRING(-105.71240234375 44.6796875,-106.06396484375 42.658203125,-103.55908203125 42.7021484375,-103.47119140625 45.55859375,-104.65771484375 45.20703125)
Is there an inverse way of getting these back into the object format from whence they came?
I'd love to be using JSON, but can't seem to get GeoJSON to accept my OpenLayer.Feature.Vector object (which is what the CLASS_NAME property says it is when I peer inside).
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Openlayers.Geometry 对象的
toString
方法将它们很好地转换为 WKT (众所周知的文本)。如果您在数据库之上使用 GIS 图层(例如用于 PostGres 的 PostGIS、用于 SQL Server 的 SQL Spatial、用于 SQLite 的 Spatialite 等),它们应该提供使您能够处理 WKT 的功能。但是,如果您想将该 WKT 转换为新的
Openlayers.Geometry
对象(在浏览器中),则可以使用fromWKT
函数:这里,变量
point
现在将包含一个新的Openlayers.Geometry
对象,该对象与您使用toString()
的原始对象具有相同的属性。如果将数组传递给
fromWKT
函数,它将返回一个包含所有生成的几何图形的 GeometryCollection。之后,
collection.toString()
应产生以下结果:GEOMETRYCOLLECTION(POINT(-104.74560546875 44.2841796875),POLYGON((-96.52783203125 44.6796875,-96.52783203125 45.734375 ,-92.22119140625 45.734375,-92.22119140625 44.6796875,-96.52783203125 44.6796875)),线串(-105.71240234375 44.6796875,-106.06396484375 42.658203125,-103.55908203125 42.7021484375,-103.47119140625 45.55859375,-104.65771484375 45.20703125))
The Openlayers.Geometry objects’
toString
method converts them nicely to WKT (Well-Known Text). If you use a GIS layer on top of your database (like PostGIS for PostGres, SQL Spatial for SQL Server, Spatialite for SQLite, etc.), they should offer functions that enable you to process WKT.But if you want to convert that WKT to a new
Openlayers.Geometry
object (in the browser), you can use thefromWKT
function:Here, the variable
point
will now contain a newOpenlayers.Geometry
object, which has the same properties as the original one you usedtoString()
on.If you pass an array to the
fromWKT
function, it will return a GeometryCollection containing all the generated geometries.After this,
collection.toString()
should yield the following:GEOMETRYCOLLECTION(POINT(-104.74560546875 44.2841796875),POLYGON((-96.52783203125 44.6796875,-96.52783203125 45.734375,-92.22119140625 45.734375,-92.22119140625 44.6796875,-96.52783203125 44.6796875)),LINESTRING(-105.71240234375 44.6796875,-106.06396484375 42.658203125,-103.55908203125 42.7021484375,-103.47119140625 45.55859375,-104.65771484375 45.20703125))
在我的另一个答案中,我选择了 WKT,因为你提到了它。我现在发现您似乎更喜欢 GeoJSON。
将矢量图层或 Openlayers.Geometry 对象转换为GeoJSON 字符串,您应该使用 OpenLayers.Format.GeoJSON.write 函数:
请注意,您应该能够将对象传递给此函数,因为(根据文档)它接受 OpenLayers.Feature.Vector 以及 OpenLayers.Geometry 或一系列功能。
相反,当您获得 GeoJSON 字符串时,您可以使用 OpenLayers.Format.GeoJSON.read 函数:
第二个参数可让您指示要返回的对象类型。阅读链接的文档以获取更多信息。
另外,请查看此演示以获取更广泛的示例。 (查看页面的源代码以了解他们是如何做到的)。
In my other answer, I went with WKT because you mentioned it. I now see that you seem to prefer GeoJSON.
To convert a vector layer or an Openlayers.Geometry object to a GeoJSON string, you should use the OpenLayers.Format.GeoJSON.write function:
Note that you should be able to pass your object to this function, since (according to documentation) it accepts an OpenLayers.Feature.Vector as well as a OpenLayers.Geometry or an array of features.
Conversely, when you’ve got a GeoJSON string, you can convert that back to an object using the OpenLayers.Format.GeoJSON.read function:
The second parameter lets you indicate which type of object you’d like returned. Read the docs linked to for more information.
Also, take a look at this demo for a more extensive example. (View the source of the page to see how they’re doing it).