使用GDAL/OGR api读取矢量数据(shapefile)--如何?
我正在开发一个涉及一些地理信息系统内容的应用程序。 将会有一些 .shp 文件被读取并绘制到 opengl 屏幕上。 当前的 opengl 屏幕正在使用从 glOrtho()
设置的正交投影,并且已经使用简单文本文件中的坐标显示地图。
现在要绘制的地图将从 shapefile 中读取。
我有以下疑问:
如何将 .shp 文件的 WGS84 投影(从 shapefile 的 .prj 文件读取,WKT 格式)转换为我现有的 glOrtho 投影..是否需要进行任何转换完毕? 它与 glOrtho() 设置有何不同?基本上如何使用这些信息?
我的应用程序需要以这样的方式设置,以便我可以知道地图上某个点的确切纬度/经度。例如。 如果我悬停在 X 城市,可以获取其正确的纬度/经度。我知道这可以通过使用开源 utils/api(如 GDAL/OGR)来完成,但我很困惑,因为这些 api 的文档没有进入我的头。 我试图找到一些示例 C++ 程序,但找不到。
我已经编写了自己的逻辑来从包含点/折线/多边形的形状文件中读取坐标(使用C-shapelib)并在我的opengl屏幕上绘制。我在doc中找到了一个OGR示例代码来读取POINTS形状文件但问题是这个应用程序必须如此动态,在加载 shapefile 时,它应该根据正在读取的 .shp 文件的投影正确设置 opengl 屏幕的投影..例如 WGS84, LCC、珠穆朗玛峰改装...等 如何从 OGR api 实现这一点?
请就这个问题提供您的意见..我真的很想完成这项工作,但我没有得到正确的开始..
I am working on an application that involves some gis stuff. There would be some .shp files to be read and plotted onto an opengl screen. The current opengl screen is using the orthographic projection as set from glOrtho()
and is already displaying a map using coordinates from a simple text file..
Now the map to be plotted is to be read from a shapefile.
I have the following doubts:
How to use the WGS84 projection of the .shp file(as read from the .prj file of the shapefile,WKT format) into my existing glOrtho projection..is there any conversion that needs to be done? and how is it different from what the glOrtho() sets up?basically how to use this information?
My application needs to be setup in such a way that i can know the exact lat/long of a point on the map.for eg. if i am hovering on X city,its correct lat/long could be fetched.I know that this can be done by using opensource utils/apis like GDAL/OGR but i am messed up as the documentation of these apis are not getting into my head.
I tried to find some sample c++ progs but couldnt find one.I have already written my own logic to read the coordinates from a shapefile containing either points/polyline/polygon(using C-shapelib) and plotted over my opengl screen.I found a OGR sample code in doc to read a POINTS shapefile but none for POLYGON shapefile.And the problem is that this application has to be so dynamic that upon loading the shapefile,it should correctly setup the projection of the opengl screen depending upon the projection of the .shp file being read..eg WGS84,LCC,EVEREST MODIFIED...etc. how to achieve this from OGR api?
Kindly give your inputs on this problem.. I am really keen to make this work but im not getting the right start..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Shapefile 渲染在 OpenGL 中非常简单。 您可能需要“shapelib”,一个免费的 C 语言 shapefile 解析库(Google 一下)。 使用 GL_POINTS 作为点形状文件,
GL_LINES 用于线形状文件,GL_LINE_LOOP 用于多边形形状文件。 将边界框坐标设置为正交。
您从 .prj 文件中读取的是投影信息。 WGS84 为您提供纬度/经度坐标(球面)。
但你的显示系统是2D(矩形)的。 因此,您需要将 3D 球面坐标转换为 2D 矩形坐标(这就是投影的含义)。投影类型有很多种,具体取决于地球上感兴趣的区域(记住投影会扭曲特征的面积/形状/大小)。投影类型范围包括 Polyconic、Modified Everest、NAD、UTM 等。
如果您只需要 WGS84,则读取 .sh 文件的边界框坐标并将其分配给 glOrtho。 如果您有任何投影(例如:-UTM),则将边界框坐标转换为投影坐标,然后将新投影的坐标分配给 glOrtho。 要将纬度/经度转换为任何投影,您可能需要“Projlib”或“GeotransEngine”等投影库。
如需进一步说明,您可以通过 dgplinux@ yahoo 与我联系。 com
Shapefile rendering is quite straight forward in OpenGL. You may require "shapelib",a free shapefile parsing library in C(Google it). Use GL_POINTS for point shapefile,
GL_LINES for line shapefile and GL_LINE_LOOP for polygon shapefile. Set your bounding box coords to the Ortho.
What you read from the .prj file is projection info. WGS84 gives you lat/long coords(Spherical).
But your display system is 2D(Rectangular). So, you need to convert 3D Spherical coords to 2D Rectangular coords(This is the meaning of Projection).Projection types are numerous,depending on the area of interest on the globe(remember projection distorts area/shape/size of features).Projection types range from Polyconic, Modified Everest, NAD, UTM, etc.,
If you simply need WGS84 ,then read bounding box coords of your .sh file and assign them to glOrtho. If you have any projection(eg:-UTM), then you convert your bounding box coords into Projection coords and then assign the newly projected coords to glOrtho. For converting lat/long into any Projection, you may require projection libraries like "Projlib" or "GeotransEngine" and etc.
For further clarifications you may contact me on dgplinux@ y a h o o . c o m
请阅读 OGR API 教程,您可以在其中了解如何从 Shapefile 等来源读取矢量数据。 接下来,查看 OGR 投影教程,您可以在其中了解如何使用有关投影和空间的信息参考系统从 OGR 源读取。
Please, read the OGR API Tutorial where you can learn how to read vector data from sources like Shapefile. Next, check the OGR Projections Tutorial where you can learn about how to use information about projection and spatial reference system read from OGR sources.
GDAL/OGR 拥有加载矢量文件、然后转换任何坐标所需的一切。 我理解您对 GDAL 的失望,因为文档并不是最好的。 如果您想要了解如何使用 API,请查看 GDAL 颠覆树中的 gdalinfo.c 和 ogrinfo.cpp。 来源可以在https://svn.osgeo.org/gdal/trunk/gdal。
如果这没有帮助,我有两个基本示例,用于解析矢量信息并进行坐标转换。 他们确实很糟糕,但他们可能有助于理解这一点。
矢量加载
坐标转换
最后,如果您不熟悉GIS格式,我会考虑阅读 此处
下的 ArcGIS 介绍指南书籍
/地图投影
。 尽管没有接受过制图培训,但有了这些指南,我仍然可以与专家竞争。 另一个很好的来源是维基百科。如有疑问,只需选择您想要坚持使用的 UTM 网格并使用 UTM 作为坐标系。 它使用 X(东向)、Y(北向)和 Z(高度)。 唯一的关键是选择一个 UTM 网格并确保所有坐标都使用它作为参考。 UTM 很容易测试代码,因为网上有很多指南。 您还可以使用 OGR/GDAL 或其他资源找到转换代码。 其他投影坐标系是值得的并且可能更好,但我会首先考虑它。
最后,如果其他方法都失败,请看看 NGA GeoTrans。 这是一个很棒的测试工具。
GDAL/OGR has everything you need to load a vector file, then convert any coordinates. I understand your frustration with GDAL as the documentation is not the greatest. If you want a good intro to using the API, look at gdalinfo.c and ogrinfo.cpp in the GDAL subversion tree. Source can be seen at https://svn.osgeo.org/gdal/trunk/gdal.
If that does not help, I have two basic examples that I use to parse vector info and do coordinate conversion. They are really bad, but they may help get the point.
Vector Loading
Coordinate Conversion
Finally, if you are not familiar with GIS formats, I would consider reading the ArcGIS introduction here under
Guide Books
/Map Projections
. I can compete with experts despite no cartography training due to these guides. Another good source is Wikipedia.When in doubt, just pick a UTM grid you want to stick with and use UTM as your coordinate system. It uses X (Easting), Y(Northing), and Z(Altitude). The only key is picking a single UTM Grid and making sure all coordinates use that as a reference. UTM is easy to test code with as there are a lot of guides online. You also can find conversion code using OGR/GDAL or other resources. Other projected coordinate systems are worthwhile and may be better, but I would look at that to start with.
Finally, if all else fails, take a look at the NGA GeoTrans. That is a great testing tool.