从 gfortran 读取 .dbf 文件的最简单方法是什么
我正在使用 gfortran,我需要编写一个函数来从与 ESRI Shapefile 关联的 .dbf 文件中读取记录。 我应该能够阅读的文件可以从互联网 http:// diss.rm.ingv.it/diss/DISS_3.0.4.shp.zip
file
命令对文件格式的意见是:
$ file GGSources_polyline.dbf
GGSources_polyline.dbf: \012- DBase 3 data file\012- (119 records)
感谢您的建议
I'm using gfortran, I need to write a function that reads records from a .dbf file associated with an ESRI Shapefile. The file I should be able to read is available from internet http://diss.rm.ingv.it/diss/DISS_3.0.4.shp.zip
The opinion of the file
command about the format of the file is:
$ file GGSources_polyline.dbf
GGSources_polyline.dbf: \012- DBase 3 data file\012- (119 records)
Thanks for your suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在此处找到了文件格式的粗略描述。 看起来整个变量类型和大小相当混合,这会让事情变得有些复杂。 我不知道使用 Fortran 尝试读取这些数据是否是最佳选择,但如果您必须这样做,这里有一些提示:
transfer()
函数将内存中的位置解释为特定类型。 这将允许您将文件中的二进制数据读取到整数类型的变量中,然后分配给实数,而不进行类型转换。我现在处于类似的情况,尝试读取结构与 dBase 文件非常相似的文件(即指向不同类型的文件区域的不同大小的标头),最终使用 Python 和 Numpy 来读取该文件。 读取包括
seek
到文件中的某个位置,读取一堆字节,然后使用numpy.fromstring
选项将其转换为real*4< /code>、
real*8
、integer*8
等。您可以做到这一点,但您可能希望保留您的选择。I found a rough description of the file format here. It looks like there is quite a mix of variable types and sizes throughout, which is going to complicate things somewhat. I don't know if using Fortran to try and read this data is the best option, but if you must here are some hints:
transfer()
function to interpret a location in memory as a particular type. This will allow you to read the binary data from the file into a variable of typeinteger
but then assign to areal
without doing a type cast.I'm in a similar situation now trying to read a file with a structure very similar to the dBase file (i.e. varying sizes of headers pointing to regions of the file with different types) and ended up using Python and Numpy to read the file. Reading consists of
seek
ing to a location in the file, reading a bunch of bytes, then using thenumpy.fromstring
option to convert that intoreal*4
,real*8
,integer*8
, etc. You can make this work, but you may want to keep your options open.最好的选择是将 dbf 文件转换为其他文件,例如使用大多数 linux 中提供的 OGR 工具分布。 您可以使用 ogr2ogr 将 dbf 文件的内容转换为 CSV 文件:(
请注意,您需要包含图层名称,对于 Shapefile,该图层名称与 shapefile 的名称相同)。 CSV 的前 3 行如下所示:
另一种方法是使用 OGR 访问 Shapefile(或 Shapelib) 并用 C 进行处理,将其返回到主 Fortran 程序。
Your best bet is to conver the dbf file into something else, using e.g. the OGR tools, available in most linux distributions. You can just convert the contents of the dbf file into a CSV file using ogr2ogr:
(note that you need to include the layername, which for Shapefiles, is identical to the shapefile's name). The first 3 lines of the CSV look like this:
An alternative would be to access the Shapefile using OGR (or Shapelib) and doing the processing in C, returning it to the main Fortran program.
您可能很难读取 Fortran 中的二进制未格式化文件,这些文件不是通过 Fortran 写入语句编写的,除非您的编译器具有某些扩展名。
Fortran 二进制无格式文件具有记录开始和记录结束标记。 这些标记通常是记录的长度(以字节为单位)。
因此运行时系统会尝试将文件中的字符解释为记录标记并感到困惑。
转换为 csv ascii 并从 Fortran 读取即可。 如果您打算尝试读取其他文件类型,那么编写一些 C 函数来连接 CI/O 库应该允许您直接读取文件。
You may struggle reading binary unformatted files in Fortran which were not written from a Fortran write statement unless your compiler has some extensions.
Fortran binary unformatted files have beginning of record and end of record marks. These marks are usually the length of the record in bytes.
So the runtime system will try to interpret characters in the file as record marks and get confused.
Converting to csv ascii and reading that from Fortran will work. If you were going to try reading other file types then writing some C functions to interface to the C I/O library should allow you to read the files directly.
FortranGIS 包具有与 shapelib 库的 Fortran 绑定,允许直接从 Fortran 程序对 shapefile 和关联的 dbf 文件进行编码/解码:
http://fortrangis.berlios.de/ 后来移至 https://github。 com/ARPA-SIMC/fortrangis
它适用于 gfortran 4.1.2 或更高版本(F2003 ISO_C_BINDING 模块)。
The FortranGIS package has Fortran bindings to the shapelib library, allowing to encode/decode shapefiles and the associated dbf files directly from a Fortran program:
http://fortrangis.berlios.de/ later moved to https://github.com/ARPA-SIMC/fortrangis
It works with gfortran 4.1.2 or later (F2003 ISO_C_BINDING module).