从 gfortran 读取 .dbf 文件的最简单方法是什么

发布于 2024-07-17 13:37:43 字数 400 浏览 12 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

澜川若宁 2024-07-24 13:37:43

我在此处找到了文件格式的粗略描述。 看起来整个变量类型和大小相当混合,这会让事情变得有些复杂。 我不知道使用 Fortran 尝试读取这些数据是否是最佳选择,但如果您必须这样做,这里有一些提示:

  • 打开文件以直接访问未格式化的 I/O。 无格式意味着您可以直接从文件中读取字节,并且直接访问不会向记录添加任何填充。
  • 将记录长度设置为字段之间的最小公共长度
  • 使用 transfer() 函数将内存中的位置解释为特定类型。 这将允许您将文件中的二进制数据读取到整数类型的变量中,然后分配给实数,而不进行类型转换。

我现在处于类似的情况,尝试读取结构与 dBase 文件非常相似的文件(即指向不同类型的文件区域的不同大小的标头),最终使用 Python 和 Numpy 来读取该文件。 读取包括 seek 到文件中的某个位置,读取一堆字节,然后使用 numpy.fromstring 选项将其转换为 real*4< /code>、real*8integer*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:

  • Open the file for direct access unformatted I/O. Unformatted means that you can just read the bytes straight out of the file, and direct access won't add any padding to records.
  • Set the record length as the lowest common length between fields
  • Use the 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 type integer but then assign to a real 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 seeking to a location in the file, reading a bunch of bytes, then using the numpy.fromstring option to convert that into real*4, real*8, integer*8, etc. You can make this work, but you may want to keep your options open.

终难愈 2024-07-24 13:37:43

最好的选择是将 dbf 文件转换为其他文件,例如使用大多数 linux 中提供的 OGR 工具分布。 您可以使用 ogr2ogr 将 dbf 文件的内容转换为 CSV 文件:(

ogr2ogr -f "CSV" output.csv FaultScarps_polyline.shp FaultScarps_polyline

请注意,您需要包含图层名称,对于 Shapefile,该图层名称与 shapefile 的名称相同)。 CSV 的前 3 行如下所示:

IDSOURCE,IDSCARP,SOURCENAME,FAULTSCARP,LENGHT,HEIGHT,AVGVOFFSET,MAXVOFFSET,VOFFSETTYP,AVGHOFFSET,MAXHOFFSET,HOFFSETTYP,AGE,NOEVENTS,LENGHTQ,HEIGHTQ,VOFFSETQ,HOFFSETQ,AGEQ,NOEVENTSQ,LENGHTN,HEIGHTN,VOFFSETN,HOFFSETN,AGEN,NOEVENTSN,REFERENCE
ITGG001,          1,Ovindoli-Pezza,Ovindoli-Pezza Fault Piano Pezza,  4.40, 18.00,   9.750,  16.000,          1,   0.000,   0.000,3,             10.000000000000000,3,1,0,1,1,1,1,Based on topographic observations.,Max height in late Pleistocene-Holocene fluvioglacial deposits.,Based on geological survey and refers to late Pleistocene-Holocene deposits.,Based on geological survey.,Based on geological observations.,Refers to Holocene and based on paleoseismology.,Pantosti et al. [1996].
ITGG001,          2,Ovindoli-Pezza,Ovindoli-Pezza  Fault Campo Porcaro,  8.60,  0.00,   8.700,  12.000,          1,   3.045,   4.025,1,             18.000000000000000,3,1,0,1,1,1,1,Based on topographic observations.,,Max offset observed  in the late Pleistocene-Holocene fluvioglacial and moraine deposits.,"Calculated as 35 % of the vertical component, on the basis of literature data.",Based on geological observations.,Refers to Holocene and based on paleoseismology.,Pantosti et al. [1996]

另一种方法是使用 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:

ogr2ogr -f "CSV" output.csv FaultScarps_polyline.shp FaultScarps_polyline

(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:

IDSOURCE,IDSCARP,SOURCENAME,FAULTSCARP,LENGHT,HEIGHT,AVGVOFFSET,MAXVOFFSET,VOFFSETTYP,AVGHOFFSET,MAXHOFFSET,HOFFSETTYP,AGE,NOEVENTS,LENGHTQ,HEIGHTQ,VOFFSETQ,HOFFSETQ,AGEQ,NOEVENTSQ,LENGHTN,HEIGHTN,VOFFSETN,HOFFSETN,AGEN,NOEVENTSN,REFERENCE
ITGG001,          1,Ovindoli-Pezza,Ovindoli-Pezza Fault Piano Pezza,  4.40, 18.00,   9.750,  16.000,          1,   0.000,   0.000,3,             10.000000000000000,3,1,0,1,1,1,1,Based on topographic observations.,Max height in late Pleistocene-Holocene fluvioglacial deposits.,Based on geological survey and refers to late Pleistocene-Holocene deposits.,Based on geological survey.,Based on geological observations.,Refers to Holocene and based on paleoseismology.,Pantosti et al. [1996].
ITGG001,          2,Ovindoli-Pezza,Ovindoli-Pezza  Fault Campo Porcaro,  8.60,  0.00,   8.700,  12.000,          1,   3.045,   4.025,1,             18.000000000000000,3,1,0,1,1,1,1,Based on topographic observations.,,Max offset observed  in the late Pleistocene-Holocene fluvioglacial and moraine deposits.,"Calculated as 35 % of the vertical component, on the basis of literature data.",Based on geological observations.,Refers to Holocene and based on paleoseismology.,Pantosti et al. [1996]

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.

夏雨凉 2024-07-24 13:37:43

您可能很难读取 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.

白衬杉格子梦 2024-07-24 13:37:43

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).

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